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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mock_test
import (
"bytes"
"errors"
"testing"
"github.com/ethersphere/bee/pkg/encryption/mock"
)
var errTest = errors.New("test error")
func TestEncryptor_Encrypt(t *testing.T) {
for _, tc := range []struct {
name string
options []mock.Option
data []byte
want []byte
wantErr error
}{
{
name: "empty",
wantErr: mock.ErrNotImplemented,
},
{
name: "func constant",
options: []mock.Option{mock.WithEncryptFunc(func([]byte) ([]byte, error) {
return []byte("some random data"), nil
})},
want: []byte("some random data"),
},
{
name: "func identity",
data: []byte("input data"),
options: []mock.Option{mock.WithEncryptFunc(func(data []byte) ([]byte, error) {
return data, nil
})},
want: []byte("input data"),
},
{
name: "func err",
options: []mock.Option{mock.WithEncryptFunc(func([]byte) ([]byte, error) {
return nil, errTest
})},
wantErr: errTest,
},
{
name: "xor",
data: []byte("input data"),
options: []mock.Option{mock.WithXOREncryption([]byte("the key"))},
want: []byte{0x1d, 0x6, 0x15, 0x55, 0x1f, 0x45, 0x1d, 0x15, 0x1c, 0x4},
},
{
name: "xor error",
options: []mock.Option{mock.WithXOREncryption(nil)},
wantErr: mock.ErrInvalidXORKey,
},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := mock.New(tc.options...).Encrypt(tc.data)
if err != tc.wantErr {
t.Fatalf("got error %v, want %v", err, tc.wantErr)
}
if !bytes.Equal(got, tc.want) {
t.Errorf("got data %#v, want %#v", got, tc.want)
}
})
}
}
func TestEncryptor_Decrypt(t *testing.T) {
for _, tc := range []struct {
name string
options []mock.Option
data []byte
want []byte
wantErr error
}{
{
name: "empty",
wantErr: mock.ErrNotImplemented,
},
{
name: "func constant",
options: []mock.Option{mock.WithDecryptFunc(func([]byte) ([]byte, error) {
return []byte("some random data"), nil
})},
want: []byte("some random data"),
},
{
name: "func identity",
data: []byte("input data"),
options: []mock.Option{mock.WithDecryptFunc(func(data []byte) ([]byte, error) {
return data, nil
})},
want: []byte("input data"),
},
{
name: "func err",
options: []mock.Option{mock.WithDecryptFunc(func([]byte) ([]byte, error) {
return nil, errTest
})},
wantErr: errTest,
},
{
name: "xor",
data: []byte("input data"),
options: []mock.Option{mock.WithXOREncryption([]byte("the key"))},
want: []byte{0x1d, 0x6, 0x15, 0x55, 0x1f, 0x45, 0x1d, 0x15, 0x1c, 0x4},
},
{
name: "xor error",
options: []mock.Option{mock.WithXOREncryption(nil)},
wantErr: mock.ErrInvalidXORKey,
},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := mock.New(tc.options...).Decrypt(tc.data)
if err != tc.wantErr {
t.Fatalf("got error %v, want %v", err, tc.wantErr)
}
if !bytes.Equal(got, tc.want) {
t.Errorf("got data %#v, want %#v", got, tc.want)
}
})
}
}
func TestEncryptor_Reset(t *testing.T) {
t.Run("empty", func(t *testing.T) {
// should not panic
mock.New().Reset()
})
t.Run("func", func(t *testing.T) {
var called bool
mock.New(mock.WithResetFunc(func() {
called = true
})).Reset()
if !called {
t.Error("reset func not called")
}
})
}
func TestEncryptor_XOREncryption(t *testing.T) {
key := []byte("some strong key")
e := mock.New(mock.WithXOREncryption(key))
data := []byte("very secret data")
enc, err := e.Encrypt(data)
if err != nil {
t.Fatal(err)
}
if bytes.Equal(enc, data) {
t.Fatal("encrypted and input data must not be the same")
}
dec, err := e.Decrypt(enc)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(dec, data) {
t.Errorf("got decrypted data %#v, want %#v", dec, data)
}
}