23
"github.com/stretchr/testify/require"
25
"github.com/cubefs/cubefs/blobstore/common/proto"
26
"github.com/cubefs/cubefs/blobstore/common/uptoken"
29
func TestAccessServerTokenBase(t *testing.T) {
30
token := uptoken.NewUploadToken(1, 1, 1, 1, 1, 0, []byte{})
31
require.Equal(t, "d29f19731941e44a010100", uptoken.EncodeToken(token))
34
func TestAccessServerTokenValid(t *testing.T) {
35
for ii := 0; ii < 1000; ii++ {
36
cid := proto.ClusterID(mrand.Uint32())
37
vid := proto.Vid(mrand.Uint32())
38
bid := proto.BlobID(mrand.Uint64())
39
count := mrand.Uint32()
40
size := mrand.Uint32()
41
secretKey := make([]byte, mrand.Intn(40)+1)
43
secretKey[len(secretKey)-1] = 0xff
45
if cid == 0 || vid == 0 || bid == 0 || count == 0 {
49
token := uptoken.NewUploadToken(cid, vid, bid, count, size, time.Minute, secretKey)
50
require.True(t, token.IsValidBid(bid))
51
require.True(t, token.IsValid(cid, vid, bid, size, secretKey))
54
for i := 0; i < 100; i++ {
55
bidx := proto.BlobID(mrand.Int63n(int64(bid >> 1)))
56
require.False(t, token.IsValidBid(bidx))
57
require.False(t, token.IsValid(cid, vid, bidx, size, secretKey))
61
for i := 0; i < 100; i++ {
62
bidx := bid + proto.BlobID(mrand.Int63n(int64(count)))
63
require.True(t, token.IsValidBid(bidx))
64
require.True(t, token.IsValid(cid, vid, bidx, size, secretKey))
68
for i := uint32(1); i < 100; i++ {
69
bidx := bid + proto.BlobID(count+i)
70
require.False(t, token.IsValidBid(bidx))
71
require.False(t, token.IsValid(cid, vid, bidx, size, secretKey))
75
for i := 0; i < 100; i++ {
76
cidx := proto.ClusterID(mrand.Uint32())
78
require.False(t, token.IsValid(cidx, vid, bid, size, secretKey))
83
for i := 0; i < 100; i++ {
84
vidx := proto.Vid(mrand.Uint32())
86
require.False(t, token.IsValid(cid, vidx, bid, size, secretKey))
91
for i := 0; i < 100; i++ {
92
sizex := mrand.Uint32()
94
require.False(t, token.IsValid(cid, vid, bid, sizex, secretKey))
99
for i := 0; i < 100; i++ {
100
secretKeyx := secretKey[:len(secretKey)/2]
101
require.False(t, token.IsValid(cid, vid, bid, size, secretKeyx))
105
for i := 0; i < 9; i++ {
106
var tokenx uptoken.UploadToken
107
tokenx.Offset = uint8(copy(tokenx.Data[:], token.Data[:token.Offset]))
108
char := mrand.Int31n(0xff)
109
if char != int32(token.Data[i]) {
110
tokenx.Data[i] = byte(char)
111
require.False(t, tokenx.IsValid(cid, vid, bid, size, secretKey))
116
str := uptoken.EncodeToken(token)
117
tokenx := uptoken.DecodeToken(str)
118
require.Equal(t, token.Offset, tokenx.Offset)
119
require.Equal(t, token.Data[:token.Offset], tokenx.Data[:tokenx.Offset])
124
func TestAccessServerTokenExpired(t *testing.T) {
125
secretKey := []byte{0x1f, 0xff}
126
for ii := 0; ii < 1000; ii++ {
127
expired := mrand.Intn(40) - 20
128
token := uptoken.NewUploadToken(1, 1, 1, 1, 1, time.Duration(expired)*time.Second, secretKey)
130
require.True(t, token.IsValid(1, 1, 1, 1, secretKey))
132
require.False(t, token.IsValid(1, 1, 1, 1, secretKey))
137
func BenchmarkAccessServerTokenNew(b *testing.B) {
138
secretKey := []byte{}
139
for ii := 0; ii <= b.N; ii++ {
140
uptoken.NewUploadToken(1, 1, 1, 1, 1, 1, secretKey)
144
func BenchmarkAccessServerTokenValid(b *testing.B) {
145
secretKey := []byte{}
146
token := uptoken.NewUploadToken(1, 1, 1, 1, 1, 1, secretKey)
148
for ii := 0; ii <= b.N; ii++ {
149
token.IsValid(1, 1, 1, 1, secretKey)
153
func BenchmarkAccessServerTokenEncode(b *testing.B) {
154
secretKey := []byte{}
155
token := uptoken.NewUploadToken(1, 1, 1, 1, 1, 1, secretKey)
157
for ii := 0; ii <= b.N; ii++ {
158
uptoken.EncodeToken(token)
162
func BenchmarkAccessServerTokenDecode(b *testing.B) {
163
for ii := 0; ii <= b.N; ii++ {
164
uptoken.DecodeToken("d29f19731941e44a010100")