-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlenc_test.go
100 lines (82 loc) · 2.45 KB
/
xmlenc_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
package xmlenc
import (
"io/ioutil"
"math/rand"
"testing"
"github.com/beevik/etree"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func TestDataAES128(t *testing.T) {
t.Run("CBC", func(t *testing.T) {
RandReader = rand.New(rand.NewSource(0)) //nolint:gosec // deterministic random numbers for tests
plaintext, err := ioutil.ReadFile("testdata/encrypt-data-aes128-cbc.data")
assert.Check(t, err)
var ciphertext string
{
encrypter := AES128CBC
cipherEl, encErr := encrypter.Encrypt([]byte("abcdefghijklmnop"), plaintext, nil)
assert.Check(t, encErr)
doc := etree.NewDocument()
doc.SetRoot(cipherEl)
doc.IndentTabs()
ciphertext, err = doc.WriteToString()
assert.Check(t, err)
}
{
decrypter := AES128CBC
doc := etree.NewDocument()
err = doc.ReadFromString(ciphertext)
assert.Check(t, err)
actualPlaintext, err := decrypter.Decrypt(
[]byte("abcdefghijklmnop"), doc.Root())
assert.Check(t, err)
assert.Check(t, is.DeepEqual(plaintext, actualPlaintext))
}
{
decrypter := AES128CBC
doc := etree.NewDocument()
err := doc.ReadFromFile("testdata/encrypt-data-aes128-cbc.xml")
assert.Check(t, err)
actualPlaintext, err := decrypter.Decrypt([]byte("abcdefghijklmnop"), doc.Root())
assert.Check(t, err)
assert.Check(t, is.DeepEqual(plaintext, actualPlaintext))
}
})
t.Run("GCM", func(t *testing.T) {
RandReader = rand.New(rand.NewSource(0)) //nolint:gosec // deterministic random numbers for tests
plaintext := "top secret message to use with gcm"
{
encrypter := AES128GCM
cipherEl, encErr := encrypter.Encrypt([]byte("abcdefghijklmnop"), []byte(plaintext), []byte("1234567890AZ"))
assert.Check(t, encErr)
doc := etree.NewDocument()
doc.SetRoot(cipherEl)
doc.IndentTabs()
_, err := doc.WriteToString()
assert.Check(t, err)
}
})
}
/*
func TestAES256CBC(t *testing.T) {
RandReader = rand.New(rand.NewSource(0)) // deterministic random numbers for tests
doc := etree.NewDocument()
err := doc.ReadFromFile("testdata/plaintext.xml")
assert.NoError(t, err)
el := doc.FindElement("//PaymentInfo")
assert.NotNil(t, el)
tmpDoc := etree.NewDocument()
tmpDoc.SetRoot(el.Copy())
tmpBuf, _ := tmpDoc.WriteToString()
encrypter := AES256CBC
cipherEl, err := encrypter.Encrypt(
[]byte("abcdefghijklmnopqrstuvwxyz012345"), []byte(tmpBuf))
assert.NoError(t, err)
el.Child = nil
el.AddChild(cipherEl)
doc.IndentTabs()
s, _ := doc.WriteToString()
fmt.Println(s)
}
*/