-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
sessionid_test.go
159 lines (146 loc) · 4.61 KB
/
sessionid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package slstorage_test
import (
"testing"
"github.com/cockroachdb/cockroach/pkg/sql/enum"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness/slstorage"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/stretchr/testify/require"
)
func FuzzSessionIDEncoding(f *testing.F) {
defer leaktest.AfterTest(f)()
defer log.Scope(f).Close(f)
f.Add(string(""))
f.Add(string(uuid.FastMakeV4().GetBytes()))
session, err := slstorage.MakeSessionID(enum.One, uuid.FastMakeV4())
require.NoError(f, err)
f.Add(string(session))
f.Fuzz(func(t *testing.T, randomSession string) {
session := sqlliveness.SessionID(randomSession)
region, id, err := slstorage.UnsafeDecodeSessionID(session)
if err == nil {
if len([]byte(randomSession)) == 16 {
// A 16 bytes session is always valid, because it is the legacy uuid encoding.
require.Equal(t, []byte(randomSession), id)
} else {
// If the session is a valid encoding, then re-encoding the
// decoded pieces should produce an identical session.
require.Len(t, id, 16)
reEncoded, err := slstorage.MakeSessionID(region, uuid.FromBytesOrNil(id))
require.NoError(t, err)
require.Equal(t, session, reEncoded)
}
}
})
}
func TestMakeSessionIDValidation(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
_, err := slstorage.MakeSessionID(nil, uuid.MakeV4())
require.ErrorContains(t, err, "session id requires a non-empty region")
_, err = slstorage.MakeSessionID([]byte{}, uuid.MakeV4())
require.ErrorContains(t, err, "session id requires a non-empty region")
_, err = slstorage.MakeSessionID(make([]byte, 256), uuid.MakeV4())
require.ErrorContains(t, err, "region is too long")
}
func TestSessionIDEncoding(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
id1 := uuid.MakeV4()
must := func(session sqlliveness.SessionID, err error) sqlliveness.SessionID {
require.NoError(t, err)
return session
}
testCases := []struct {
name string
session sqlliveness.SessionID
region []byte
id uuid.UUID
err string
}{
{
name: "empty_session",
session: "",
err: "session id is too short",
},
{
name: "legacy_session",
session: sqlliveness.SessionID(id1.GetBytes()),
id: id1,
},
{
name: "session_v1",
session: must(slstorage.MakeSessionID(enum.One, id1)),
region: enum.One,
id: id1,
},
{
name: "region_len_too_large",
session: func() sqlliveness.SessionID {
session := []byte(must(slstorage.MakeSessionID([]byte{128}, id1)))
session[1] = 3
return sqlliveness.SessionID(session)
}(),
err: "session id with length 19 is the wrong size to include a region with length 3",
region: []byte{},
id: id1,
},
{
name: "region_len_too_small",
session: func() sqlliveness.SessionID {
session := []byte(must(slstorage.MakeSessionID([]byte{128}, id1)))
session[1] = 0
return sqlliveness.SessionID(session)
}(),
err: "session id with length 19 is the wrong size to include a region with length 0",
region: []byte{},
id: id1,
},
{
name: "session_id_too_short",
session: func() sqlliveness.SessionID {
smallestValidSession := must(slstorage.MakeSessionID([]byte{128}, id1))
return smallestValidSession[:len(smallestValidSession)-1]
}(),
err: "session id is too short",
},
{
name: "session_v1_large_region",
session: must(slstorage.MakeSessionID(make([]byte, 255), id1)),
region: make([]byte, 255),
id: id1,
},
{
name: "invalid_version",
session: func() sqlliveness.SessionID {
session := []byte(must(slstorage.MakeSessionID(make([]byte, 255), id1)))
session[0] = 2
return sqlliveness.SessionID(session)
}(),
err: "invalid session id version: 2",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
region, uuid, err := slstorage.UnsafeDecodeSessionID(tc.session)
if tc.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.err)
} else {
require.Equal(t, region, tc.region)
require.Equal(t, uuid, tc.id.GetBytes())
}
})
}
}