]> git.lizzy.rs Git - go-anidb.git/blob - udp/aes_test.go
Modernize
[go-anidb.git] / udp / aes_test.go
1 package udpapi
2
3 import (
4         "math/rand"
5         "reflect"
6         "testing"
7 )
8
9 func TestECB(T *testing.T) {
10         T.Parallel()
11
12         rnd := rand.New(rand.NewSource(31415))
13
14         salt := make([]byte, rnd.Intn(64))
15         for i := range salt {
16                 salt[i] = byte(rnd.Intn(255))
17         }
18         plain := make([]byte, 1200+rnd.Intn(200))
19         for i := range plain {
20                 plain[i] = byte(rnd.Intn(255))
21         }
22
23         ecbState := newECBState("agaa", salt)
24
25         T.Log("Length of plaintext:", len(plain))
26         cipher := ecbState.Encrypt(plain)
27         T.Log("Length of ciphertext:", len(cipher))
28         plain2 := ecbState.Decrypt(cipher)
29         T.Log("Length of roundtrip plaintext:", len(plain2))
30
31         if !reflect.DeepEqual(plain, plain2) {
32                 T.Error("Encoding roundtrip result doesn't match plaintext")
33         }
34 }
35
36 func TestPKCS7(T *testing.T) {
37         T.Parallel()
38
39         blockSize := byte(4)
40         vec := [][2]string{
41                 [2]string{"testing", "testing\x01"},
42                 [2]string{"byte", "byte\x04\x04\x04\x04"},
43                 [2]string{"stuff", "stuff\x03\x03\x03"},
44         }
45
46         for i, v := range vec {
47                 p := string(pkcs7Pad([]byte(v[0]), blockSize))
48                 if p != v[1] {
49                         T.Errorf("Vector #%d: expected %q, got %q", i, v[1], p)
50                 }
51                 u := string(pkcs7Unpad([]byte(p), blockSize))
52                 if u != v[0] {
53                         T.Errorf("Vector #%d: expected %q, got %q", i, v[0], u)
54                 }
55         }
56 }