github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/encrypt_test.go (about) 1 // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package utils_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/utils" 7 "github.com/iwind/TeaGo/assert" 8 "github.com/iwind/TeaGo/maps" 9 "sync" 10 "testing" 11 ) 12 13 func TestSimpleEncrypt(t *testing.T) { 14 var a = assert.NewAssertion(t) 15 16 var arr = []string{"Hello", "World", "People"} 17 for _, s := range arr { 18 var value = []byte(s) 19 var encoded = utils.SimpleEncrypt(value) 20 t.Log(encoded, string(encoded)) 21 var decoded = utils.SimpleDecrypt(encoded) 22 t.Log(decoded, string(decoded)) 23 a.IsTrue(s == string(decoded)) 24 } 25 } 26 27 func TestSimpleEncryptObject(t *testing.T) { 28 var a = assert.NewAssertion(t) 29 30 type Obj struct { 31 Name string `json:"name"` 32 Age int `json:"age"` 33 } 34 35 encoded, err := utils.SimpleEncryptObject(&Obj{Name: "lily", Age: 20}) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 var obj = &Obj{} 41 err = utils.SimpleDecryptObjet(encoded, obj) 42 if err != nil { 43 t.Fatal(err) 44 } 45 t.Logf("%#v", obj) 46 a.IsTrue(obj.Name == "lily") 47 a.IsTrue(obj.Age == 20) 48 } 49 50 func TestSimpleEncrypt_Concurrent(t *testing.T) { 51 var wg = sync.WaitGroup{} 52 var arr = []string{"Hello", "World", "People"} 53 wg.Add(len(arr)) 54 for _, s := range arr { 55 go func(s string) { 56 defer wg.Done() 57 t.Log(string(utils.SimpleDecrypt(utils.SimpleEncrypt([]byte(s))))) 58 }(s) 59 } 60 wg.Wait() 61 } 62 63 func TestSimpleEncryptMap(t *testing.T) { 64 var m = maps.Map{ 65 "s": "Hello", 66 "i": 20, 67 "b": true, 68 } 69 encodedResult, err := utils.SimpleEncryptMap(m) 70 if err != nil { 71 t.Fatal(err) 72 } 73 t.Log("result:", encodedResult) 74 75 decodedResult, err := utils.SimpleDecryptMap(encodedResult) 76 if err != nil { 77 t.Fatal(err) 78 } 79 t.Log(decodedResult) 80 }