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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
package core
import (
"bytes"
"encoding/base64"
"testing"
)
func TestCopy(t *testing.T) {
a := make([]byte, 8)
Scramble(a)
b := make([]byte, 16)
Scramble(b)
c := make([]byte, 32)
Scramble(c)
// dst > src
Copy(b, a)
if !bytes.Equal(b[:8], a) {
t.Error("incorrect copying")
}
// dst < src
Copy(b, c)
if !bytes.Equal(b, c[:16]) {
t.Error("incorrect copying")
}
// dst = src
b2 := make([]byte, 16)
Scramble(b2)
Copy(b, b2)
if !bytes.Equal(b, b2) {
t.Error("incorrect copying")
}
}
func TestMove(t *testing.T) {
a := make([]byte, 32)
Scramble(a)
b := make([]byte, 32)
Scramble(b)
Move(a, b)
if !bytes.Equal(b, make([]byte, 32)) {
t.Error("src buffer was not wiped")
}
}
func TestCompare(t *testing.T) {
a := make([]byte, 8)
Scramble(a)
b := make([]byte, 16)
Scramble(b)
c := make([]byte, 16)
copy(c, b)
// not equal
if Equal(a, b) {
t.Error("expected not equal")
}
// equal
if !Equal(b, c) {
t.Error("expected equal")
}
c[8] = ^c[8]
// not equal
if Equal(b, c) {
t.Error("expected not equal")
}
}
func TestScramble(t *testing.T) {
b := make([]byte, 32)
Scramble(b)
if bytes.Equal(b, make([]byte, 32)) {
t.Error("buffer not scrambled")
}
c := make([]byte, 32)
Scramble(c)
if bytes.Equal(b, make([]byte, 32)) {
t.Error("buffer not scrambled")
}
if bytes.Equal(b, c) {
t.Error("random repeated")
}
}
func TestHash(t *testing.T) {
known := make(map[string]string)
known[""] = "DldRwCblQ7Loqy6wYJnaodHl30d3j3eH+qtFzfEv46g="
known["hash"] = "l+2qaVlkOBNtzRKFU+kEvAP1JkJvcn0nC2mEH7bPUNM="
known["test"] = "kosgNmlD4q/RHrwOri5TqTvxd6T881vMZNUDcE5l4gI="
for k, v := range known {
if base64.StdEncoding.EncodeToString(Hash([]byte(k))) != v {
t.Error("digest doesn't match known values")
}
}
}
func TestWipe(t *testing.T) {
b := make([]byte, 32)
Scramble(b)
Wipe(b)
for i := range b {
if b[i] != 0 {
t.Error("wipe unsuccessful")
}
}
}
func TestEncryptDecrypt(t *testing.T) {
// Declare the plaintext and the key.
m := make([]byte, 64)
Scramble(m)
k := make([]byte, 32)
Scramble(k)
// Encrypt the message.
x, err := Encrypt(m, k)
if err != nil {
t.Error("expected no errors; got", err)
}
// Decrypt the message.
dm := make([]byte, len(x)-Overhead)
length, err := Decrypt(x, k, dm)
if err != nil {
t.Error("expected no errors; got", err)
}
if length != len(x)-Overhead {
t.Error("unexpected plaintext length; got", length)
}
// Verify that the plaintexts match.
if !bytes.Equal(m, dm) {
t.Error("decrypted plaintext does not match original")
}
// Attempt decryption /w buffer that is too small to hold the output.
out := make([]byte, len(x)-Overhead-1)
length, err = Decrypt(x, k, out)
if err != ErrBufferTooSmall {
t.Error("expected error; got", err)
}
if length != 0 {
t.Error("expected zero length; got", length)
}
// Construct a buffer that has the correct capacity but a smaller length.
out = make([]byte, len(x)-Overhead)
smallOut := out[:2]
if len(smallOut) != 2 || cap(smallOut) != len(x)-Overhead {
t.Error("invalid construction for test")
}
length, err = Decrypt(x, k, smallOut)
if err != nil {
t.Error("unexpected error:", err)
}
if length != len(x)-Overhead {
t.Error("unexpected length; got", length)
}
if !bytes.Equal(m, smallOut[:len(x)-Overhead]) {
t.Error("decrypted plaintext does not match original")
}
// Generate an incorrect key.
ik := make([]byte, 32)
Scramble(ik)
// Attempt decryption with the incorrect key.
length, err = Decrypt(x, ik, dm)
if length != 0 {
t.Error("expected length = 0; got", length)
}
if err != ErrDecryptionFailed {
t.Error("expected error with incorrect key; got", err)
}
// Modify the ciphertext somewhat.
for i := range x {
if i%32 == 0 {
x[i] = 0xdb
}
}
// Attempt decryption of the invalid ciphertext with the correct key.
length, err = Decrypt(x, k, dm)
if length != 0 {
t.Error("expected length = 0; got", length)
}
if err != ErrDecryptionFailed {
t.Error("expected error with modified ciphertext; got", err)
}
// Generate a key of an invalid length.
ik = make([]byte, 16)
Scramble(ik)
// Attempt encryption with the invalid key.
ix, err := Encrypt(m, ik)
if err != ErrInvalidKeyLength {
t.Error("expected error with invalid key; got", err)
}
if ix != nil {
t.Error("expected nil ciphertext; got", dm)
}
// Attempt decryption with the invalid key.
length, err = Decrypt(x, ik, dm)
if length != 0 {
t.Error("expected length = 0; got", length)
}
if err != ErrInvalidKeyLength {
t.Error("expected error with invalid key; got", err)
}
}
|