// authenticate_test.go var testAESes = []testS{ {"An Apple A Day", "\xca\x35\x19\xc8\x90\x0a\xed\x4f\x69\x09\x3e\xb2\x56\x41"}, {"Keep Doctor Away .", "\xc0\x3e\x5c\xf9\xc0\x3e\xee\x49\x3d\x27\x6c\xd6\x76\x4f\xed\x74\xeb\x1a\xe4"}, {"+w-s*a/d%4", "\xa0\x2c\x14\xfa\xca\x1b\xae\x4e\x6c\x7c"}, }
func TestAesDecoding(t *testing.T) { c := NewAesBlock([]byte("AABCDEFGHIJKLMNOPBCDEFGHIJKLMNOP")) for i, item := range testAESes { s, err := AesDecode([]byte(item.out), int64(len(item.in)), c) verify(t, i, "AesDecoding 256bits", []byte(item.out), []byte(item.in), s, err) } }
同样,将输入、输出参数位置调换即可写出 TestAesEncoding。
1 2 3 4 5 6 7 8
// authenticate_test.go func TestAesEncoding(t *testing.T) { c := NewAesBlock([]byte("AABCDEFGHIJKLMNOPBCDEFGHIJKLMNOP")) for i, item := range testAESes { s := AesEncode([]byte(item.in), c) verify(t, i, "AesEncoding 256bits", []byte(item.in), []byte(item.out), s, nil) } }
// authenticate_test.go package authenticate import"testing" type testS struct { in string out string }
var testBase64s = []testS{ {"An Apple A Day", "QW4gQXBwbGUgQSBEYXk="}, {"Keep Doctor Away .", "S2VlcCBEb2N0b3IgQXdheSAgLg=="}, {"+w-s*a/d%4", "K3ctcyphL2QlNA=="}, }
func TestBase64Encoding(t *testing.T) { for i, item := range testBase64s { s := Base64Encode([]byte(item.in)) verify(t, i, "Base64Encoding", []byte(item.in), []byte(item.out), s, nil) } }
func TestBase64Decoding(t *testing.T) { for i, item := range testBase64s { s, err := Base64Decode([]byte(item.out)) verify(t, i, "Base64Decoding", []byte(item.out), []byte(item.in), s, err) } }
func TestNewAes(t *testing.T) { testkey1 := "abcdefghijklmnop" c := NewAesBlock([]byte(testkey1)) if c == nil { t.Errorf("NewAesBlock returns nil with 128bits key %s", testkey1) } testkey2 := "abcdefghijklmnopabcdefgh" c = NewAesBlock([]byte(testkey2)) if c == nil { t.Errorf("NewAesBlock returns nil with 192bits key %s", testkey2) } testkey3 := "abcdefghijklmnopabcdefghijklmnop" c = NewAesBlock([]byte(testkey3)) if c == nil { t.Errorf("NewAesBlock returns nil with 256bits key %s", testkey3) } }