forked from RobinUS2/golang-encrypted-uuid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenc_uuid_test.go
121 lines (108 loc) · 3.3 KB
/
enc_uuid_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package enc_uuid_test
import (
"fmt"
"github.com/FlxOne/golang-encrypted-uuid"
"log"
"sync"
"testing"
)
func TestGenerate(t *testing.T) {
// Generator
generator := enc_uuid.New([]byte("mysecret90123456"), true)
// Encrypt
enc, err := generator.Encrypt([]byte("test"))
if err != nil {
panic(err)
}
log.Printf("%s", enc)
// Decrypt
dec, err := generator.Decrypt(enc)
if err != nil {
panic(err)
}
log.Printf("%v %s", dec, string(dec))
// Generate
u := generator.New()
log.Println(u)
log.Println(u.UuidStr(generator))
log.Println(u.ToString())
// Parse
parsed, e := generator.Parse("T5LvxuSpeC0g2VglOnOACOzuFP0wmH04l49fQmSWR5+kpIXvGXzO0g==")
if e != nil {
panic(e)
}
parsedStr, _ := parsed.UuidStr(generator)
if parsedStr != "0c53cb40-fe28-4051-7c8f-318a1a870ee4" {
panic(fmt.Sprintf("Failed read got %s", parsedStr))
}
parsedStrCached, _ := parsed.UuidStr(generator)
if parsedStrCached != "0c53cb40-fe28-4051-7c8f-318a1a870ee4" {
panic("Failed read cache")
}
// Additional data
ud := generator.NewWithAdditionalData("c=1")
log.Printf("Encrypted with data %s", ud)
log.Println(ud.UuidStr(generator))
data, _ := ud.AdditionalDataStr(generator)
if data != "c=1" {
panic("Failed additional data read")
}
// Test data modification
setE := ud.SetAdditionalData(generator, "c=2")
if setE != nil {
panic(fmt.Sprintf("Failed to set data: %s", setE))
}
log.Printf("Encrypted with data changed %s", ud)
log.Println(ud.UuidStr(generator))
data2, _ := ud.AdditionalDataStr(generator)
if data2 != "c=2" {
panic("Failed additional data read after change")
}
// Read something non-encrypted
unecryptedU, eUnecrypted := generator.Parse("0c53cb40-fe28-4051-7c8f-318a1a870ee5")
log.Println(eUnecrypted)
log.Println(unecryptedU)
parsedUnecryptedStr, _ := unecryptedU.UuidStr(generator)
if parsedUnecryptedStr != "0c53cb40-fe28-4051-7c8f-318a1a870ee5" {
panic("Failed read unencrypted")
}
// Read something bullshit non-encrypted
unecryptedUbs, eUnecryptedBs := generator.Parse("123")
if eUnecryptedBs == nil || unecryptedUbs != nil {
panic("Should not be able to read bullshit in grace mode")
}
// Read non-encrypted without grace mode
nonGracefulGenerator := enc_uuid.New([]byte("mysecret90123456"), false)
unecryptedUnonGrace, eUnecryptedNonGrace := nonGracefulGenerator.Parse("0c53cb40-fe28-4051-7c8f-318a1a870ee5")
if eUnecryptedNonGrace == nil || unecryptedUnonGrace != nil {
panic("Should not be able to read non-encrypted in non-grace mode")
}
// Read something bullshit non-encrypted non grace
unecryptedUbsNnonGrace, eUnecryptedBsNonGrace := generator.Parse("123")
if unecryptedUbsNnonGrace != nil || eUnecryptedBsNonGrace == nil {
panic("Should not be able to read bullshit in non-grace mode")
}
// Test parallel, use the same generator in multiple go routines many times concurrently
var wg sync.WaitGroup
// var j int // enable this line to validate detection of races
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
for n := 0; n < 10000; n++ {
//j++ // enable this line to validate detection of races
u := generator.New()
str := u.ToString()
parsed, e := generator.Parse(str)
if e != nil {
panic("Failed parse")
}
_, e2 := parsed.UuidStr(generator)
if e2 != nil {
panic("Failed to uuid")
}
}
wg.Done()
}()
}
wg.Wait()
}