1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package hex 6 7 import ( 8 "bytes" 9 "fmt" 10 "testing" 11 ) 12 13 type encDecTest struct { 14 enc string 15 dec []byte 16 } 17 18 var encDecTests = []encDecTest{ 19 {"", []byte{}}, 20 {"0001020304050607", []byte{0, 1, 2, 3, 4, 5, 6, 7}}, 21 {"08090a0b0c0d0e0f", []byte{8, 9, 10, 11, 12, 13, 14, 15}}, 22 {"f0f1f2f3f4f5f6f7", []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}}, 23 {"f8f9fafbfcfdfeff", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}}, 24 {"67", []byte{'g'}}, 25 {"e3a1", []byte{0xe3, 0xa1}}, 26 } 27 28 func TestEncode(t *testing.T) { 29 for i, test := range encDecTests { 30 dst := make([]byte, EncodedLen(len(test.dec))) 31 n := Encode(dst, test.dec) 32 if n != len(dst) { 33 t.Errorf("#%d: bad return value: got: %d want: %d", i, n, len(dst)) 34 } 35 if string(dst) != test.enc { 36 t.Errorf("#%d: got: %#v want: %#v", i, dst, test.enc) 37 } 38 } 39 } 40 41 func TestDecode(t *testing.T) { 42 // Case for decoding uppercase hex characters, since 43 // Encode always uses lowercase. 44 decTests := append(encDecTests, encDecTest{"F8F9FAFBFCFDFEFF", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}}) 45 for i, test := range decTests { 46 dst := make([]byte, DecodedLen(len(test.enc))) 47 n, err := Decode(dst, []byte(test.enc)) 48 if err != nil { 49 t.Errorf("#%d: bad return value: got:%d want:%d", i, n, len(dst)) 50 } else if !bytes.Equal(dst, test.dec) { 51 t.Errorf("#%d: got: %#v want: %#v", i, dst, test.dec) 52 } 53 } 54 } 55 56 func TestEncodeToString(t *testing.T) { 57 for i, test := range encDecTests { 58 s := EncodeToString(test.dec) 59 if s != test.enc { 60 t.Errorf("#%d got:%s want:%s", i, s, test.enc) 61 } 62 } 63 } 64 65 func TestDecodeString(t *testing.T) { 66 for i, test := range encDecTests { 67 dst, err := DecodeString(test.enc) 68 if err != nil { 69 t.Errorf("#%d: unexpected err value: %s", i, err) 70 continue 71 } 72 if !bytes.Equal(dst, test.dec) { 73 t.Errorf("#%d: got: %#v want: #%v", i, dst, test.dec) 74 } 75 } 76 } 77 78 type errTest struct { 79 in string 80 err string 81 } 82 83 var errTests = []errTest{ 84 {"0", "encoding/hex: odd length hex string"}, 85 {"0g", "encoding/hex: invalid byte: U+0067 'g'"}, 86 {"00gg", "encoding/hex: invalid byte: U+0067 'g'"}, 87 {"0\x01", "encoding/hex: invalid byte: U+0001"}, 88 } 89 90 func TestInvalidErr(t *testing.T) { 91 for i, test := range errTests { 92 dst := make([]byte, DecodedLen(len(test.in))) 93 _, err := Decode(dst, []byte(test.in)) 94 if err == nil { 95 t.Errorf("#%d: expected error; got none", i) 96 } else if err.Error() != test.err { 97 t.Errorf("#%d: got: %v want: %v", i, err, test.err) 98 } 99 } 100 } 101 102 func TestInvalidStringErr(t *testing.T) { 103 for i, test := range errTests { 104 _, err := DecodeString(test.in) 105 if err == nil { 106 t.Errorf("#%d: expected error; got none", i) 107 } else if err.Error() != test.err { 108 t.Errorf("#%d: got: %v want: %v", i, err, test.err) 109 } 110 } 111 } 112 113 func TestDumper(t *testing.T) { 114 var in [40]byte 115 for i := range in { 116 in[i] = byte(i + 30) 117 } 118 119 for stride := 1; stride < len(in); stride++ { 120 var out bytes.Buffer 121 dumper := Dumper(&out) 122 done := 0 123 for done < len(in) { 124 todo := done + stride 125 if todo > len(in) { 126 todo = len(in) 127 } 128 dumper.Write(in[done:todo]) 129 done = todo 130 } 131 132 dumper.Close() 133 if !bytes.Equal(out.Bytes(), expectedHexDump) { 134 t.Errorf("stride: %d failed. got:\n%s\nwant:\n%s", stride, out.Bytes(), expectedHexDump) 135 } 136 } 137 } 138 139 func TestDump(t *testing.T) { 140 var in [40]byte 141 for i := range in { 142 in[i] = byte(i + 30) 143 } 144 145 out := []byte(Dump(in[:])) 146 if !bytes.Equal(out, expectedHexDump) { 147 t.Errorf("got:\n%s\nwant:\n%s", out, expectedHexDump) 148 } 149 } 150 151 var expectedHexDump = []byte(`00000000 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d |.. !"#$%&'()*+,-| 152 00000010 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d |./0123456789:;<=| 153 00000020 3e 3f 40 41 42 43 44 45 |>?@ABCDE| 154 `) 155 156 var sink []byte 157 158 func BenchmarkEncode(b *testing.B) { 159 for _, size := range []int{256, 1024, 4096, 16384} { 160 src := bytes.Repeat([]byte{2, 3, 5, 7, 9, 11, 13, 17}, size/8) 161 sink = make([]byte, 2*size) 162 163 b.Run(fmt.Sprintf("%v", size), func(b *testing.B) { 164 for i := 0; i < b.N; i++ { 165 Encode(sink, src) 166 } 167 }) 168 } 169 } 170