-
Notifications
You must be signed in to change notification settings - Fork 0
/
encodings_test.go
70 lines (61 loc) · 1.6 KB
/
encodings_test.go
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
package encodings
import (
"encoding/base64"
"encoding/hex"
"fmt"
"reflect"
"testing"
)
// realistically {Base64,Hex}ToBytes should just return []byte, error if this
// were anything other than a learning exercise, but as is, it made for a good chance
// to get more comfortable with panic/defer/recover
func TestInvalidBase64ToBytes(test *testing.T) {
defer checkForPanic(base64.CorruptInputError(9), test)
Base64ToBytes("NotBase64@!")
}
func TestInvalidHexToBytes(test *testing.T) {
defer checkForPanic(hex.InvalidByteError(0x4E), test)
HexToBytes("NotHex")
}
func checkForPanic(v interface{}, test *testing.T) {
if r := recover(); r != nil {
fmt.Println(reflect.TypeOf(r))
if v != r {
test.Errorf("Expected to recover %v but instead got %v", v, r)
}
} else {
test.Error("expected a panic, but none occured")
}
}
func TestBase64ToBytes(test *testing.T) {
i := "SQ=="
o := Base64ToBytes(i)
expected := byte(0x49)
if o[0] != expected {
test.Errorf("expected %v to decode to %x but got % x", i, expected, o)
}
}
func TestBytesToBase64(test *testing.T) {
i := []byte{0x49}
o := BytesToBase64(i)
expected := "SQ=="
if o != expected {
test.Errorf("expected %v to decode to %v but got %v", i, expected, o)
}
}
func TestHexToBytes(test *testing.T) {
i := "AF"
o := HexToBytes(i)
expected := byte(175)
if o[0] != expected {
test.Errorf("expected %v to decode to %x but got % v", i, expected, o)
}
}
func TestBytesToHex(test *testing.T) {
i := []byte{175}
o := BytesToHex(i)
expected := "af"
if o != expected {
test.Errorf("expected %v to decode to %v but got %v", i, expected, o)
}
}