Skip to content

Commit c9027d3

Browse files
authoredApr 4, 2024
Fix game.RandomHex() to return a string of correct length (#87)
`game.RandomHex(n int)` の返す文字列が2nになってしまっていたのを修正しました RandomHexで生成していたのは次の2箇所です - RoomID - これまで通り32文字になるようconstを修正 - クライアントのAuthKey - configでの指定値の倍の長さになっていたのが指定値通りに - 無指定の場合これまでと同じ長さになるよう初期値を修正 ついでにRoomIDの長さや正規表現のconstをcommonパッケージに移して共通化しました
2 parents 1cbc278 + 1ba50d7 commit c9027d3

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed
 

Diff for: ‎server/common/enum.go renamed to ‎server/common/const.go

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ const (
44
HostStatusStarting = 0
55
HostStatusRunning = 1
66
HostStatusClosing = 2
7+
8+
RoomIdLen = 32
9+
RoomIdPattern = "^[0-9a-f]{32}$"
710
)

Diff for: ‎server/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func Load(conffile string) (*Config, error) {
187187
ClientConf: ClientConf{
188188
EventBufSize: 128,
189189
WaitAfterClose: Duration(30 * time.Second),
190-
AuthKeyLen: 32,
190+
AuthKeyLen: 64,
191191
},
192192

193193
LogConf: LogConf{
@@ -217,7 +217,7 @@ func Load(conffile string) (*Config, error) {
217217
ClientConf: ClientConf{
218218
EventBufSize: 128,
219219
WaitAfterClose: Duration(30 * time.Second),
220-
AuthKeyLen: 32,
220+
AuthKeyLen: 64,
221221
},
222222

223223
LogConf: LogConf{

Diff for: ‎server/config/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestLoad(t *testing.T) {
4848
ClientConf: ClientConf{
4949
EventBufSize: 512,
5050
WaitAfterClose: Duration(time.Second * 60),
51-
AuthKeyLen: 32,
51+
AuthKeyLen: 64,
5252
},
5353

5454
LogConf: LogConf{

Diff for: ‎server/game/repository.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,12 @@ import (
1919
"golang.org/x/xerrors"
2020
"google.golang.org/grpc/codes"
2121

22+
"wsnet2/common"
2223
"wsnet2/config"
2324
"wsnet2/log"
2425
"wsnet2/pb"
2526
)
2627

27-
const (
28-
// RoomID文字列長
29-
lenId = 16
30-
31-
idPattern = "^[0-9a-f]+$"
32-
)
33-
3428
var (
3529
roomInsertQuery string
3630
roomUpdateQuery string
@@ -46,7 +40,7 @@ func init() {
4640
seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
4741
randsrc = rand.New(rand.NewSource(seed.Int64()))
4842

49-
rerid = regexp.MustCompile(idPattern)
43+
rerid = regexp.MustCompile(common.RoomIdPattern)
5044
}
5145

5246
func dbCols(t reflect.Type) []string {
@@ -84,13 +78,13 @@ func initQueries() {
8478
}
8579

8680
func RandomHex(n int) string {
87-
b := make([]byte, n)
81+
b := make([]byte, (n+1)/2)
8882
_, _ = randsrc.Read(b) // (*rand.Rand).Read always success.
89-
return hex.EncodeToString(b)
83+
return hex.EncodeToString(b)[:n]
9084
}
9185

9286
func IsValidRoomId(id string) bool {
93-
return rerid.Match([]byte(id))
87+
return rerid.MatchString(id)
9488
}
9589

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

312-
ri.Id = RandomHex(lenId)
306+
ri.Id = RandomHex(common.RoomIdLen)
313307
if op.WithNumber {
314308
ri.Number.Number = randsrc.Int31n(maxNumber) + 1 // [1..maxNumber]
315309
}

Diff for: ‎server/game/repository_test.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/jmoiron/sqlx"
1212
"golang.org/x/xerrors"
1313

14+
"wsnet2/common"
1415
"wsnet2/config"
1516
"wsnet2/pb"
1617
)
@@ -39,9 +40,11 @@ func TestQueries(t *testing.T) {
3940

4041
func TestIsValidRoomId(t *testing.T) {
4142
tests := map[string]bool{
42-
"123456789abcdef": true,
43-
"123456789ABCDEF": false,
44-
"": false,
43+
"0123456789abcdef0123456789abcdef": true,
44+
"0123456789ABCDEF0123456789ABCDEF": false,
45+
"0123456789abcdef0123456789abcde": false,
46+
"0123456789abcdef0123456789abcdef0": false,
47+
"": false,
4548
}
4649

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

6265
func TestNewRoomInfo(t *testing.T) {
66+
const lenId = common.RoomIdLen
6367
ctx := context.Background()
6468
db, mock := newDbMock(t)
6569
retryCount := 3
@@ -130,3 +134,17 @@ func TestNewRoomInfo(t *testing.T) {
130134
t.Errorf("there were unfulfilled expectations: %s", err)
131135
}
132136
}
137+
138+
func TestRandomHexRoomId(t *testing.T) {
139+
const lenId = common.RoomIdLen
140+
rid := RandomHex(lenId)
141+
142+
if len(rid) != lenId {
143+
t.Errorf("room id len = %v wants %v (%q)", len(rid), lenId, rid)
144+
}
145+
146+
ok, err := regexp.MatchString(common.RoomIdPattern, rid)
147+
if err != nil || !ok {
148+
t.Errorf("room id pattern missmatch: %v", rid)
149+
}
150+
}

Diff for: ‎server/lobby/service/api.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"golang.org/x/xerrors"
1919

2020
"wsnet2/auth"
21+
"wsnet2/common"
2122
"wsnet2/lobby"
2223
"wsnet2/log"
2324
"wsnet2/pb"
@@ -248,7 +249,7 @@ func (sv *LobbyService) handleCreateRoom(w http.ResponseWriter, r *http.Request)
248249
}
249250

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

254255
type JoinVars struct {

0 commit comments

Comments
 (0)