-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathlicense_test.go
More file actions
105 lines (77 loc) · 2.51 KB
/
Copy pathlicense_test.go
File metadata and controls
105 lines (77 loc) · 2.51 KB
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
package lk_test
import (
"bytes"
"github.com/hyperboloide/lk"
)
func (s *Suite) TestExamples() {
s.Run("Example complete", Example_complete)
s.Run("Example license generation", Example_licenseGeneration)
s.Run("Example license verification", Example_licenseVerification)
}
func (s *Suite) TestLicense() {
var privateKey *lk.PrivateKey // private key for license generation
var wrongKey *lk.PrivateKey // wrong key for verification
var license *lk.License // license to be tested
var theData []byte // data to be signed
s.Run("Generate test data", func() {
var err error
privateKey, err = lk.NewPrivateKey()
s.Require().NoError(err)
s.Require().NotNil(privateKey)
wrongKey, err = lk.NewPrivateKey()
s.Require().NoError(err)
s.Require().NotNil(wrongKey)
theData = s.RandomBytes(100)
license, err = lk.NewLicense(privateKey, theData)
s.Require().NoError(err)
s.Require().NotNil(license)
ok, err := license.Verify(privateKey.GetPublicKey())
s.Require().NoError(err)
s.Require().True(ok)
})
s.Run("Should not validate with wrong key", func() {
ok, err := license.Verify(wrongKey.GetPublicKey())
s.Require().NoError(err)
s.Require().False(ok)
})
s.Run("Test license with bytes", func() {
b2, err := license.ToBytes()
s.Require().NoError(err)
l2, err := lk.LicenseFromBytes(b2)
s.Require().NoError(err)
ok, err := l2.Verify(privateKey.GetPublicKey())
s.Require().NoError(err)
s.Require().True(ok)
s.Require().True(bytes.Equal(license.Data, l2.Data))
})
s.Run("Test license with b64", func() {
b2, err := license.ToB64String()
s.Require().NoError(err)
l2, err := lk.LicenseFromB64String(b2)
s.Require().NoError(err)
ok, err := l2.Verify(privateKey.GetPublicKey())
s.Require().NoError(err)
s.Require().True(ok)
s.Require().True(bytes.Equal(license.Data, l2.Data))
})
s.Run("should test a license with b32", func() {
b2, err := license.ToB32String()
s.Require().NoError(err)
l2, err := lk.LicenseFromB32String(b2)
s.Require().NoError(err)
ok, err := l2.Verify(privateKey.GetPublicKey())
s.Require().NoError(err)
s.Require().True(ok)
s.Require().True(bytes.Equal(license.Data, l2.Data))
})
s.Run("should test a license with hex", func() {
b2, err := license.ToHexString()
s.Require().NoError(err)
l2, err := lk.LicenseFromHexString(b2)
s.Require().NoError(err)
ok, err := l2.Verify(privateKey.GetPublicKey())
s.Require().NoError(err)
s.Require().True(ok)
s.Require().True(bytes.Equal(license.Data, l2.Data))
})
}