|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+package mtglib_test
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "encoding/hex"
|
|
|
5
|
+ "encoding/json"
|
|
|
6
|
+ "testing"
|
|
|
7
|
+
|
|
|
8
|
+ "github.com/9seconds/mtg/v2/mtglib"
|
|
|
9
|
+ "github.com/stretchr/testify/suite"
|
|
|
10
|
+)
|
|
|
11
|
+
|
|
|
12
|
+type SecretTestSuite struct {
|
|
|
13
|
+ suite.Suite
|
|
|
14
|
+}
|
|
|
15
|
+
|
|
|
16
|
+func (suite *SecretTestSuite) TestParseSecret() {
|
|
|
17
|
+ secretData, _ := hex.DecodeString("d11c6cbbd9efe7fed5bc0db220b09665")
|
|
|
18
|
+ s := mtglib.Secret{
|
|
|
19
|
+ Host: "google.com",
|
|
|
20
|
+ }
|
|
|
21
|
+
|
|
|
22
|
+ copy(s.Key[:], secretData)
|
|
|
23
|
+
|
|
|
24
|
+ testData := map[string]string{
|
|
|
25
|
+ "hex": "eed11c6cbbd9efe7fed5bc0db220b09665676f6f676c652e636f6d",
|
|
|
26
|
+ "base64": "7tEcbLvZ7-f-1bwNsiCwlmVnb29nbGUuY29t",
|
|
|
27
|
+ }
|
|
|
28
|
+
|
|
|
29
|
+ for name, value := range testData {
|
|
|
30
|
+ param := value
|
|
|
31
|
+
|
|
|
32
|
+ suite.T().Run(name, func(t *testing.T) {
|
|
|
33
|
+ parsed, err := mtglib.ParseSecret(param)
|
|
|
34
|
+
|
|
|
35
|
+ suite.NoError(err)
|
|
|
36
|
+ suite.Equal(s.Key, parsed.Key)
|
|
|
37
|
+ suite.Equal(s.Host, parsed.Host)
|
|
|
38
|
+
|
|
|
39
|
+ newSecret := mtglib.Secret{}
|
|
|
40
|
+
|
|
|
41
|
+ suite.NoError(newSecret.UnmarshalText([]byte(param)))
|
|
|
42
|
+
|
|
|
43
|
+ suite.Equal(s.Key, newSecret.Key)
|
|
|
44
|
+ suite.Equal(s.Host, newSecret.Host)
|
|
|
45
|
+ })
|
|
|
46
|
+ }
|
|
|
47
|
+}
|
|
|
48
|
+
|
|
|
49
|
+func (suite *SecretTestSuite) TestSerialize() {
|
|
|
50
|
+ secretData, _ := hex.DecodeString("d11c6cbbd9efe7fed5bc0db220b09665")
|
|
|
51
|
+ s := mtglib.Secret{
|
|
|
52
|
+ Host: "google.com",
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
|
55
|
+ copy(s.Key[:], secretData)
|
|
|
56
|
+
|
|
|
57
|
+ suite.Equal("eed11c6cbbd9efe7fed5bc0db220b09665676f6f676c652e636f6d", s.Hex())
|
|
|
58
|
+ suite.Equal("7tEcbLvZ7-f-1bwNsiCwlmVnb29nbGUuY29t", s.Base64())
|
|
|
59
|
+}
|
|
|
60
|
+
|
|
|
61
|
+func (suite *SecretTestSuite) TestMarshalData() {
|
|
|
62
|
+ secretData, _ := hex.DecodeString("d11c6cbbd9efe7fed5bc0db220b09665")
|
|
|
63
|
+ s := mtglib.Secret{
|
|
|
64
|
+ Host: "google.com",
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ copy(s.Key[:], secretData)
|
|
|
68
|
+
|
|
|
69
|
+ data, err := json.Marshal(&s)
|
|
|
70
|
+
|
|
|
71
|
+ suite.NoError(err)
|
|
|
72
|
+ suite.Equal(string(data), `"7tEcbLvZ7-f-1bwNsiCwlmVnb29nbGUuY29t"`)
|
|
|
73
|
+}
|
|
|
74
|
+
|
|
|
75
|
+func (suite *SecretTestSuite) TestIncorrectSecret() {
|
|
|
76
|
+ testData := []string{
|
|
|
77
|
+ "aaa",
|
|
|
78
|
+ "d11c6cbbd9efe7fed5bc0db220b09665",
|
|
|
79
|
+ "ddd11c6cbbd9efe7fed5bc0db220b09665",
|
|
|
80
|
+ "+ueJ0q91t5XOnFYP8Xac3A",
|
|
|
81
|
+ "eed11c6cbbd9efe7fed5bc0db220b09665",
|
|
|
82
|
+ "ed11c6cbbd9efe7fed5bc0db220b09665",
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ for _, v := range testData {
|
|
|
86
|
+ param := v
|
|
|
87
|
+
|
|
|
88
|
+ suite.T().Run(param, func(t *testing.T) {
|
|
|
89
|
+ _, err := mtglib.ParseSecret(param)
|
|
|
90
|
+
|
|
|
91
|
+ suite.Error(err)
|
|
|
92
|
+ })
|
|
|
93
|
+ }
|
|
|
94
|
+}
|
|
|
95
|
+
|
|
|
96
|
+func (suite *SecretTestSuite) TestInvariant() {
|
|
|
97
|
+ generated := mtglib.GenerateSecret("google.com")
|
|
|
98
|
+
|
|
|
99
|
+ parsed, err := mtglib.ParseSecret(generated.Hex())
|
|
|
100
|
+
|
|
|
101
|
+ suite.NoError(err)
|
|
|
102
|
+ suite.Equal(generated.Key, parsed.Key)
|
|
|
103
|
+ suite.Equal(generated.Host, parsed.Host)
|
|
|
104
|
+ suite.Equal("google.com", parsed.Host)
|
|
|
105
|
+}
|
|
|
106
|
+
|
|
|
107
|
+func TestSecret(t *testing.T) {
|
|
|
108
|
+ t.Parallel()
|
|
|
109
|
+ suite.Run(t, &SecretTestSuite{})
|
|
|
110
|
+}
|