Skip to content

Commit

Permalink
optimize unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miniLCT committed May 21, 2024
1 parent 4d8c246 commit b453342
Showing 1 changed file with 169 additions and 54 deletions.
223 changes: 169 additions & 54 deletions library/cryptox/cryptox_test.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,199 @@
package cryptox

import (
"testing"

"bytes"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMd5(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, md5: %s", b, Md5(b))
testCases := []struct {
name string
input []byte
expected string
}{
{"non-empty string", _cipherkey, "996ce17f6abc9fe126b57aa5f1d8c92c"},
{"empty string", []byte(""), "d41d8cd98f00b204e9800998ecf8427e"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Md5(tc.input)
if result != tc.expected {
t.Errorf("Md5(%q) = %v; expected %v", tc.input, result, tc.expected)
}
})
}
}

func TestSha1(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, sha1: %s", b, Sha1(b))
testCases := []struct {
name string
input []byte
expected string
}{
{"non-empty string", _cipherkey, "ff998abc1ce6d8f01a675fa197368e44c8916e9c"},
{"empty string", []byte(""), "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Sha1(tc.input)
if result != tc.expected {
t.Errorf("Sha1(%q) = %v; expected %v", tc.input, result, tc.expected)
}
})
}
}

func TestSha256(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, Sha256: %s", b, Sha256(b))
testCases := []struct {
name string
input []byte
expected string
}{
{"non-empty string", _cipherkey, "8e9916c5340c43fa003fe2dd54cd4e3027affbfc0d631e4cd858f64ec09fa9ed"},
{"empty string", []byte(""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Sha256(tc.input)
if result != tc.expected {
t.Errorf("Sha256(%q) = %v; expected %v", tc.input, result, tc.expected)
}
})
}
}

func TestSha512(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, Sha512: %s", b, Sha512(b))
testCases := []struct {
name string
input []byte
expected string
}{
{"non-empty string", _cipherkey, "6df7de339b39ae1125f181c9229cdb904fe31eac219aa2335059240101939083495221f7fbe8c32d73f8ee81dc68539c98c143f15700d944c8c0eafb27567a7a"},
{"empty string", []byte(""), "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Sha512(tc.input)
if result != tc.expected {
t.Errorf("Sha512(%q) = %v; expected %v", tc.input, result, tc.expected)
}
})
}
}

func TestFnv1aToUint(t *testing.T) {
b := []byte("1234567890abcdef")
t.Logf("text: %s, Fnv1aToUint64: %d", b, Fnv1aToUint64(b))
t.Logf("text: %s, Fnv1aToUint32: %d", b, Fnv1aToUint32(b))
func TestFnv1aToUint64(t *testing.T) {
testCases := []struct {
name string
input []byte
expected uint64
}{
{"non-empty string", _cipherkey, 0x30f1a9bc9e896233},
{"empty string", []byte(""), 0xcbf29ce484222325},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Fnv1aToUint64(tc.input)
if result != tc.expected {
t.Errorf("Fnv1aToUint64(%q) = %v; expected %v", tc.input, result, tc.expected)
}
})
}
}

var (
_cipherkey = []byte("1234567890abcdef")
_plaintext = []byte("text1234")
)

func TestAESEncrypt(t *testing.T) {
ciphertext := AESEncrypt(_cipherkey, _plaintext)
t.Logf("ciphertext hex: %s", ciphertext)
r, err := AESDecrypt(_cipherkey, ciphertext)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)

ciphertext = AESEncrypt(_cipherkey, _plaintext, true)
t.Logf("ciphertext base64: %s", ciphertext)
r, err = AESDecrypt(_cipherkey, ciphertext, true)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
func TestFnv1aToUint32(t *testing.T) {
testCases := []struct {
name string
input []byte
expected uint32
}{
{"non-empty string", _cipherkey, 0x7f19f353},
{"empty string", []byte(""), 0x811c9dc5},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Fnv1aToUint32(tc.input)
if result != tc.expected {
t.Errorf("Fnv1aToUint32(%q) = %v; expected %v", tc.input, result, tc.expected)
}
})
}
}

func TestAESCBCEncrypt(t *testing.T) {
ciphertext := AESCBCEncrypt(_cipherkey, _plaintext)
t.Logf("ciphertext hex: %s", ciphertext)
r, err := AESCBCDecrypt(_cipherkey, ciphertext)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)

ciphertext = AESCBCEncrypt(_cipherkey, _plaintext, true)
t.Logf("ciphertext base64: %s", ciphertext)
r, err = AESCBCDecrypt(_cipherkey, ciphertext, true)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
func TestAESEncryptDecrypt(t *testing.T) {
testCases := []struct {
name string
key []byte
plaintext []byte
}{
{"non-empty string", []byte("thisis32bitlongpassphraseimusing"), _cipherkey},
{"empty string", []byte("thisis32bitlongpassphraseimusing"), []byte("")},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ciphertext := AESEncrypt(tc.key, tc.plaintext)
decrypted, err := AESDecrypt(tc.key, ciphertext)
if err != nil {
t.Fatalf("AESDecrypt error: %v", err)
}
if !bytes.Equal(tc.plaintext, decrypted) {
t.Errorf("AESDecrypt = %v; expected %v", decrypted, tc.plaintext)
}
})
}
}

func TestAESCTREncrypt(t *testing.T) {
ciphertext := AESCTREncrypt(_cipherkey, _plaintext)
t.Logf("ciphertext hex: %s", ciphertext)
r, err := AESCTRDecrypt(_cipherkey, ciphertext)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
func TestAESCBCEncryptDecrypt(t *testing.T) {
testCases := []struct {
name string
key []byte
plaintext []byte
}{
{"non-empty string", []byte("thisis32bitlongpassphraseimusing"), _cipherkey},
{"empty string", []byte("thisis32bitlongpassphraseimusing"), []byte("")},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ciphertext := AESCBCEncrypt(tc.key, tc.plaintext)
decrypted, err := AESCBCDecrypt(tc.key, ciphertext)
if err != nil {
t.Fatalf("AESCBCDecrypt error: %v", err)
}
if !bytes.Equal(tc.plaintext, decrypted) {
t.Errorf("AESCBCDecrypt = %v; expected %v", decrypted, tc.plaintext)
}
})
}
}

ciphertext = AESCTREncrypt(_cipherkey, _plaintext, true)
t.Logf("ciphertext base64: %s", ciphertext)
r, err = AESCTRDecrypt(_cipherkey, ciphertext, true)
assert.NoError(t, err)
assert.Equal(t, _plaintext, r)
func TestAESCTREncryptDecrypt(t *testing.T) {
testCases := []struct {
name string
key []byte
plaintext []byte
}{
{"non-empty string", []byte("thisis32bitlongpassphraseimusing"), _cipherkey},
{"empty string", []byte("thisis32bitlongpassphraseimusing"), []byte("")},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ciphertext := AESCTREncrypt(tc.key, tc.plaintext)
decrypted, err := AESCTRDecrypt(tc.key, ciphertext)
if err != nil {
t.Fatalf("AESCTRDecrypt error: %v", err)
}
if !bytes.Equal(tc.plaintext, decrypted) {
t.Errorf("AESCTRDecrypt = %v; expected %v", decrypted, tc.plaintext)
}
})
}
}

var (
_cipherkey = []byte("1234567890abcdef")
_plaintext = []byte("text1234")
)

func TestPading(t *testing.T) {
blockSize := 16
padded := hexEncode(pkcs5Padding(_plaintext, blockSize))
Expand Down

0 comments on commit b453342

Please sign in to comment.