summaryrefslogtreecommitdiff
path: root/encryption_test.go
blob: fcec058e810989da67313822e2a7d92f98152ab7 (plain)
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
package kate

import (
	"bytes"
	"crypto/rand"
	"fmt"
	"testing"
	"testing/quick"
)

func TestEncryptDecryptRoundtrip(t *testing.T) {
	property := func(data []byte) bool {
		var key [naclKeyLen]byte
		if _, err := rand.Read(key[:]); err != nil {
			return false
		}

		encryption := encryption{Key: key}
		token := encryption.Encrypt(data)
		decryptedData, success := encryption.Decrypt(token)

		if !bytes.Equal(decryptedData, data) {
			fmt.Printf("expected %v, got %v\n", data, decryptedData)
		}
		return success && bytes.Equal(decryptedData, data)
	}

	if err := quick.Check(property, nil); err != nil {
		t.Errorf("Encrypt/Decrypt roundtrip property failed: %v", err)
	}
}

func TestEncryptionFromHexKey(t *testing.T) {
	tests := []struct {
		name    string
		key     string
		wantErr bool
	}{
		{
			name:    "valid hex key",
			key:     "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
			wantErr: false,
		},
		{
			name:    "key too short",
			key:     "0123456789abcdef",
			wantErr: true,
		},
		{
			name:    "key too long",
			key:     "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef00",
			wantErr: true,
		},
		{
			name:    "invalid hex characters",
			key:     "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdef0123456789abcdef",
			wantErr: true,
		},
		{
			name:    "empty key",
			key:     "",
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if _, err := encryptionFromHexKey(tt.key); (err != nil) != tt.wantErr {
				t.Errorf("encryptionFromHexKey() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestDecryptInvalidTokens(t *testing.T) {
	var key [naclKeyLen]byte
	if _, err := rand.Read(key[:]); err != nil {
		t.Fatal(err)
	}
	enc := encryption{Key: key}

	tests := []struct {
		name  string
		token string
	}{
		{
			name:  "invalid hex token",
			token: "invalid_hex_string",
		},
		{
			name:  "token with wrong MAC",
			token: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			data, ok := enc.Decrypt(tt.token)
			if ok {
				t.Errorf("Decrypt() should have failed for %s, but returned data: %v", tt.name, data)
			}
		})
	}
}

func TestEncryptEmptyData(t *testing.T) {
	var key [naclKeyLen]byte
	if _, err := rand.Read(key[:]); err != nil {
		t.Fatal(err)
	}
	enc := encryption{Key: key}

	token := enc.Encrypt([]byte{})
	if token == "" {
		t.Error("Encrypt() should return non-empty token even for empty data")
	}

	data, ok := enc.Decrypt(token)
	if !ok {
		t.Error("Decrypt() should succeed for encrypted empty data")
	}
	if len(data) != 0 {
		t.Errorf("Decrypt() should return empty data, got %v", data)
	}
}

func TestEncryptLargeData(t *testing.T) {
	var key [naclKeyLen]byte
	if _, err := rand.Read(key[:]); err != nil {
		t.Fatal(err)
	}
	enc := encryption{Key: key}

	largeData := make([]byte, 10000)
	if _, err := rand.Read(largeData); err != nil {
		t.Fatal(err)
	}

	token := enc.Encrypt(largeData)
	if token == "" {
		t.Error("Encrypt() should return non-empty token for large data")
	}

	decryptedData, ok := enc.Decrypt(token)
	if !ok {
		t.Error("Decrypt() should succeed for encrypted large data")
	}
	if !bytes.Equal(decryptedData, largeData) {
		t.Error("Decrypt() should return original large data")
	}
}