cubefs

Форк
0
/
token_test.go 
166 строк · 4.7 Кб
1
// Copyright 2022 The CubeFS Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
14

15
package uptoken_test
16

17
import (
18
	"crypto/rand"
19
	mrand "math/rand"
20
	"testing"
21
	"time"
22

23
	"github.com/stretchr/testify/require"
24

25
	"github.com/cubefs/cubefs/blobstore/common/proto"
26
	"github.com/cubefs/cubefs/blobstore/common/uptoken"
27
)
28

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))
32
}
33

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)
42
		rand.Read(secretKey)
43
		secretKey[len(secretKey)-1] = 0xff
44

45
		if cid == 0 || vid == 0 || bid == 0 || count == 0 {
46
			continue
47
		}
48

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))
52

53
		{ // invalid bid
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))
58
			}
59
		}
60
		{ // valid bid
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))
65
			}
66
		}
67
		{ // invalid bid
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))
72
			}
73
		}
74
		{ // invalid clusterID
75
			for i := 0; i < 100; i++ {
76
				cidx := proto.ClusterID(mrand.Uint32())
77
				if cid != cidx {
78
					require.False(t, token.IsValid(cidx, vid, bid, size, secretKey))
79
				}
80
			}
81
		}
82
		{ // invalid vid
83
			for i := 0; i < 100; i++ {
84
				vidx := proto.Vid(mrand.Uint32())
85
				if vid != vidx {
86
					require.False(t, token.IsValid(cid, vidx, bid, size, secretKey))
87
				}
88
			}
89
		}
90
		{ // valid size
91
			for i := 0; i < 100; i++ {
92
				sizex := mrand.Uint32()
93
				if size != sizex {
94
					require.False(t, token.IsValid(cid, vid, bid, sizex, secretKey))
95
				}
96
			}
97
		}
98
		{ // invalid 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))
102
			}
103
		}
104
		{ // invalid token
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))
112
				}
113
			}
114
		}
115
		{ // encode decode
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])
120
		}
121
	}
122
}
123

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)
129
		if expired >= 0 {
130
			require.True(t, token.IsValid(1, 1, 1, 1, secretKey))
131
		} else {
132
			require.False(t, token.IsValid(1, 1, 1, 1, secretKey))
133
		}
134
	}
135
}
136

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)
141
	}
142
}
143

144
func BenchmarkAccessServerTokenValid(b *testing.B) {
145
	secretKey := []byte{}
146
	token := uptoken.NewUploadToken(1, 1, 1, 1, 1, 1, secretKey)
147
	b.ResetTimer()
148
	for ii := 0; ii <= b.N; ii++ {
149
		token.IsValid(1, 1, 1, 1, secretKey)
150
	}
151
}
152

153
func BenchmarkAccessServerTokenEncode(b *testing.B) {
154
	secretKey := []byte{}
155
	token := uptoken.NewUploadToken(1, 1, 1, 1, 1, 1, secretKey)
156
	b.ResetTimer()
157
	for ii := 0; ii <= b.N; ii++ {
158
		uptoken.EncodeToken(token)
159
	}
160
}
161

162
func BenchmarkAccessServerTokenDecode(b *testing.B) {
163
	for ii := 0; ii <= b.N; ii++ {
164
		uptoken.DecodeToken("d29f19731941e44a010100")
165
	}
166
}
167

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.