Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix game.RandomHex() to return a string of correct length #87

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/common/enum.go → server/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ const (
HostStatusStarting = 0
HostStatusRunning = 1
HostStatusClosing = 2

RoomIdLen = 32
RoomIdPattern = "^[0-9a-f]{32}$"
)
4 changes: 2 additions & 2 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func Load(conffile string) (*Config, error) {
ClientConf: ClientConf{
EventBufSize: 128,
WaitAfterClose: Duration(30 * time.Second),
AuthKeyLen: 32,
AuthKeyLen: 64,
},

LogConf: LogConf{
Expand Down Expand Up @@ -217,7 +217,7 @@ func Load(conffile string) (*Config, error) {
ClientConf: ClientConf{
EventBufSize: 128,
WaitAfterClose: Duration(30 * time.Second),
AuthKeyLen: 32,
AuthKeyLen: 64,
},

LogConf: LogConf{
Expand Down
2 changes: 1 addition & 1 deletion server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestLoad(t *testing.T) {
ClientConf: ClientConf{
EventBufSize: 512,
WaitAfterClose: Duration(time.Second * 60),
AuthKeyLen: 32,
AuthKeyLen: 64,
},

LogConf: LogConf{
Expand Down
18 changes: 6 additions & 12 deletions server/game/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@ import (
"golang.org/x/xerrors"
"google.golang.org/grpc/codes"

"wsnet2/common"
"wsnet2/config"
"wsnet2/log"
"wsnet2/pb"
)

const (
// RoomID文字列長
lenId = 16

idPattern = "^[0-9a-f]+$"
)

var (
roomInsertQuery string
roomUpdateQuery string
Expand All @@ -46,7 +40,7 @@ func init() {
seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
randsrc = rand.New(rand.NewSource(seed.Int64()))

rerid = regexp.MustCompile(idPattern)
rerid = regexp.MustCompile(common.RoomIdPattern)
}

func dbCols(t reflect.Type) []string {
Expand Down Expand Up @@ -84,13 +78,13 @@ func initQueries() {
}

func RandomHex(n int) string {
b := make([]byte, n)
b := make([]byte, (n+1)/2)
_, _ = randsrc.Read(b) // (*rand.Rand).Read always success.
return hex.EncodeToString(b)
return hex.EncodeToString(b)[:n]
}

func IsValidRoomId(id string) bool {
return rerid.Match([]byte(id))
return rerid.MatchString(id)
}

type Repository struct {
Expand Down Expand Up @@ -309,7 +303,7 @@ func (repo *Repository) newRoomInfo(ctx context.Context, tx *sqlx.Tx, op *pb.Roo
default:
}

ri.Id = RandomHex(lenId)
ri.Id = RandomHex(common.RoomIdLen)
if op.WithNumber {
ri.Number.Number = randsrc.Int31n(maxNumber) + 1 // [1..maxNumber]
}
Expand Down
24 changes: 21 additions & 3 deletions server/game/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/jmoiron/sqlx"
"golang.org/x/xerrors"

"wsnet2/common"
"wsnet2/config"
"wsnet2/pb"
)
Expand Down Expand Up @@ -39,9 +40,11 @@ func TestQueries(t *testing.T) {

func TestIsValidRoomId(t *testing.T) {
tests := map[string]bool{
"123456789abcdef": true,
"123456789ABCDEF": false,
"": false,
"0123456789abcdef0123456789abcdef": true,
"0123456789ABCDEF0123456789ABCDEF": false,
"0123456789abcdef0123456789abcde": false,
"0123456789abcdef0123456789abcdef0": false,
"": false,
}

for id, valid := range tests {
Expand All @@ -60,6 +63,7 @@ func newDbMock(t *testing.T) (*sqlx.DB, sqlmock.Sqlmock) {
}

func TestNewRoomInfo(t *testing.T) {
const lenId = common.RoomIdLen
ctx := context.Background()
db, mock := newDbMock(t)
retryCount := 3
Expand Down Expand Up @@ -130,3 +134,17 @@ func TestNewRoomInfo(t *testing.T) {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

func TestRandomHexRoomId(t *testing.T) {
const lenId = common.RoomIdLen
rid := RandomHex(lenId)

if len(rid) != lenId {
t.Errorf("room id len = %v wants %v (%q)", len(rid), lenId, rid)
}

ok, err := regexp.MatchString(common.RoomIdPattern, rid)
if err != nil || !ok {
t.Errorf("room id pattern missmatch: %v", rid)
}
}
3 changes: 2 additions & 1 deletion server/lobby/service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"golang.org/x/xerrors"

"wsnet2/auth"
"wsnet2/common"
"wsnet2/lobby"
"wsnet2/log"
"wsnet2/pb"
Expand Down Expand Up @@ -248,7 +249,7 @@ func (sv *LobbyService) handleCreateRoom(w http.ResponseWriter, r *http.Request)
}

var (
idRegexp = regexp.MustCompile("^[0-9a-f]+$")
idRegexp = regexp.MustCompile(common.RoomIdPattern)
)

type JoinVars struct {
Expand Down
Loading