-
Notifications
You must be signed in to change notification settings - Fork 1
/
cipher_test.go
47 lines (41 loc) · 1.39 KB
/
cipher_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
package feistel_test
import (
"testing"
"github.com/cyrildever/feistel"
"github.com/cyrildever/feistel/exception"
utls "github.com/cyrildever/go-utls/common/utils"
"gotest.tools/assert"
)
// TestEncrypt ...
func TestEncrypt(t *testing.T) {
expected := "3d7c0a0f51415a521054"
cipher := feistel.NewCipher("8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 10)
found, err := cipher.Encrypt("Edgewhere")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, utls.ToHex(found), expected)
}
// TestDecrypt ...
func TestDecrypt(t *testing.T) {
expected := "Edgewhere"
cipher := feistel.NewCipher("8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 10)
found, err := cipher.Decrypt(utls.Must(utls.FromHex("3d7c0a0f51415a521054")))
if err != nil {
t.Fatal(err)
}
assert.Equal(t, found, expected)
}
// TestEmptyParameters ...
func TestEmptyParameters(t *testing.T) {
// Empty key
cipher := feistel.NewCipher("", 10)
_, err := cipher.Encrypt("Edgewhere")
assert.Error(t, err, "wrong cipher parameters: keys and rounds can't be null")
_, ok := err.(*exception.WrongCipherParametersError)
assert.Assert(t, ok)
// No round
cipher = feistel.NewCipher("8ed9dcc1701c064f0fd7ae235f15143f989920e0ee9658bb7882c8d7d5f05692", 0)
_, err = cipher.Decrypt(utls.Must(utls.FromHex("3d7c0a0f51415a521054")))
assert.Error(t, err, "wrong cipher parameters: keys and rounds can't be null")
}