Skip to content

Commit

Permalink
Fix game.RandomHex() to return a string of correct length (#87)
Browse files Browse the repository at this point in the history
`game.RandomHex(n int)` の返す文字列が2nになってしまっていたのを修正しました

RandomHexで生成していたのは次の2箇所です
- RoomID
  - これまで通り32文字になるようconstを修正
- クライアントのAuthKey
  - configでの指定値の倍の長さになっていたのが指定値通りに
  - 無指定の場合これまでと同じ長さになるよう初期値を修正

ついでにRoomIDの長さや正規表現のconstをcommonパッケージに移して共通化しました
  • Loading branch information
makiuchi-d authored Apr 4, 2024
2 parents 1cbc278 + 1ba50d7 commit c9027d3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
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

0 comments on commit c9027d3

Please sign in to comment.