-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_id_test.go
98 lines (93 loc) · 3.08 KB
/
client_id_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
package main
import (
"encoding/base64"
"strconv"
"testing"
"time"
)
func TestClientIdFromStd1(t *testing.T) {
initTime := time.Now().Add(time.Duration(-60) * time.Minute).Unix()
sessionTime := time.Now().Add(time.Duration(-30) * time.Minute).Unix()
cid := strconv.Itoa(int(initTime)) + ":" + strconv.Itoa(int(sessionTime)) + ":0000000000000000"
valid1 := base64.StdEncoding.EncodeToString([]byte(cid))
data1, err1 := clientIDStandardParser(valid1)
if err1 != nil {
t.Error(err1)
}
if data1.CidStdInitTime == 0 {
t.Errorf("invalid init time")
}
if data1.CidStdSessionTime == 0 {
t.Errorf("invalid session time")
}
}
func TestClientIdFromStd2(t *testing.T) {
initTime := time.Now().Add(time.Duration(-10) * time.Minute).Unix()
sessionTime := time.Now().Add(time.Duration(-30) * time.Minute).Unix()
cid := strconv.Itoa(int(initTime)) + ":" + strconv.Itoa(int(sessionTime)) + ":0000000000000000"
invalid1 := base64.StdEncoding.EncodeToString([]byte(cid))
_, err1 := clientIDStandardParser(invalid1)
if err1 == nil {
t.Errorf("init time must be before session time")
}
}
func TestClientIdFromStd3(t *testing.T) {
cid := "1640995140:1647253300:0000000000000000"
invalid1 := base64.StdEncoding.EncodeToString([]byte(cid))
_, err1 := clientIDStandardParser(invalid1)
if err1 == nil {
t.Errorf("init time must start with 2022")
}
}
func TestClientIdFromStd4(t *testing.T) {
cid := "1647253200:1647339601:0000000000000000"
invalid1 := base64.StdEncoding.EncodeToString([]byte(cid))
_, err1 := clientIDStandardParser(invalid1)
if err1 == nil {
t.Errorf("init time must start with 2022")
}
}
func TestClientIdFromStd5(t *testing.T) {
cid := "1647253200-1647339601-0000000000000000"
invalid1 := base64.StdEncoding.EncodeToString([]byte(cid))
_, err1 := clientIDStandardParser(invalid1)
if err1 == nil {
t.Errorf("invalid pattern must be error")
}
}
func TestClientIdFromStd6(t *testing.T) {
invalid1 := "./-()*"
_, err1 := clientIDStandardParser(invalid1)
if err1 == nil {
t.Errorf("invalid base64 must be error")
}
}
func TestClientIdFromStd7(t *testing.T) {
it := time.Now().Add(time.Hour).Add(time.Second)
st := time.Now()
cid := strconv.Itoa(int(it.Unix())) + ":" + strconv.Itoa(int(st.Unix())) + ":0000000000000000"
invalid1 := base64.StdEncoding.EncodeToString([]byte(cid))
_, err1 := clientIDStandardParser(invalid1)
if err1 == nil {
t.Errorf("invalid base64 must be error")
}
}
func TestClientIdFromNotStd1(t *testing.T) {
c1 := clientIDNoneSTD([]string{"amp-xyz"}, clientIDTypeAmp)
if len(c1.CidSessionChecksum) != 24 {
t.Errorf("invalid cid")
}
c2 := clientIDNoneSTD([]string{"1"}, clientIDTypeOther)
if len(c2.CidSessionChecksum) != 24 {
t.Errorf("invalid cid")
}
}
func BenchmarkClientIdFromStd(b *testing.B) {
initTime := time.Now().Add(time.Duration(-60) * time.Minute).Unix()
sessionTime := time.Now().Add(time.Duration(-30) * time.Minute).Unix()
cid := strconv.Itoa(int(initTime)) + ":" + strconv.Itoa(int(sessionTime)) + ":0000000000000000"
valid1 := base64.StdEncoding.EncodeToString([]byte(cid))
for n := 0; n < b.N; n++ {
clientIDStandardParser(valid1)
}
}