github.com/c9s/go@v0.0.0-20180120015821-984e81f64e0c/src/encoding/hex/hex_test.go (about)

     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  	"io"
    11  	"io/ioutil"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  type encDecTest struct {
    17  	enc string
    18  	dec []byte
    19  }
    20  
    21  var encDecTests = []encDecTest{
    22  	{"", []byte{}},
    23  	{"0001020304050607", []byte{0, 1, 2, 3, 4, 5, 6, 7}},
    24  	{"08090a0b0c0d0e0f", []byte{8, 9, 10, 11, 12, 13, 14, 15}},
    25  	{"f0f1f2f3f4f5f6f7", []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}},
    26  	{"f8f9fafbfcfdfeff", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}},
    27  	{"67", []byte{'g'}},
    28  	{"e3a1", []byte{0xe3, 0xa1}},
    29  }
    30  
    31  func TestEncode(t *testing.T) {
    32  	for i, test := range encDecTests {
    33  		dst := make([]byte, EncodedLen(len(test.dec)))
    34  		n := Encode(dst, test.dec)
    35  		if n != len(dst) {
    36  			t.Errorf("#%d: bad return value: got: %d want: %d", i, n, len(dst))
    37  		}
    38  		if string(dst) != test.enc {
    39  			t.Errorf("#%d: got: %#v want: %#v", i, dst, test.enc)
    40  		}
    41  	}
    42  }
    43  
    44  func TestDecode(t *testing.T) {
    45  	// Case for decoding uppercase hex characters, since
    46  	// Encode always uses lowercase.
    47  	decTests := append(encDecTests, encDecTest{"F8F9FAFBFCFDFEFF", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}})
    48  	for i, test := range decTests {
    49  		dst := make([]byte, DecodedLen(len(test.enc)))
    50  		n, err := Decode(dst, []byte(test.enc))
    51  		if err != nil {
    52  			t.Errorf("#%d: bad return value: got:%d want:%d", i, n, len(dst))
    53  		} else if !bytes.Equal(dst, test.dec) {
    54  			t.Errorf("#%d: got: %#v want: %#v", i, dst, test.dec)
    55  		}
    56  	}
    57  }
    58  
    59  func TestEncodeToString(t *testing.T) {
    60  	for i, test := range encDecTests {
    61  		s := EncodeToString(test.dec)
    62  		if s != test.enc {
    63  			t.Errorf("#%d got:%s want:%s", i, s, test.enc)
    64  		}
    65  	}
    66  }
    67  
    68  func TestDecodeString(t *testing.T) {
    69  	for i, test := range encDecTests {
    70  		dst, err := DecodeString(test.enc)
    71  		if err != nil {
    72  			t.Errorf("#%d: unexpected err value: %s", i, err)
    73  			continue
    74  		}
    75  		if !bytes.Equal(dst, test.dec) {
    76  			t.Errorf("#%d: got: %#v want: #%v", i, dst, test.dec)
    77  		}
    78  	}
    79  }
    80  
    81  var errTests = []struct {
    82  	in  string
    83  	out string
    84  	err error
    85  }{
    86  	{"", "", nil},
    87  	{"0", "", ErrLength},
    88  	{"zd4aa", "", InvalidByteError('z')},
    89  	{"d4aaz", "\xd4\xaa", InvalidByteError('z')},
    90  	{"30313", "01", ErrLength},
    91  	{"0g", "", InvalidByteError('g')},
    92  	{"00gg", "\x00", InvalidByteError('g')},
    93  	{"0\x01", "", InvalidByteError('\x01')},
    94  	{"ffeed", "\xff\xee", ErrLength},
    95  }
    96  
    97  func TestDecodeErr(t *testing.T) {
    98  	for _, tt := range errTests {
    99  		out := make([]byte, len(tt.in)+10)
   100  		n, err := Decode(out, []byte(tt.in))
   101  		if string(out[:n]) != tt.out || err != tt.err {
   102  			t.Errorf("Decode(%q) = %q, %v, want %q, %v", tt.in, string(out[:n]), err, tt.out, tt.err)
   103  		}
   104  	}
   105  }
   106  
   107  func TestDecodeStringErr(t *testing.T) {
   108  	for _, tt := range errTests {
   109  		out, err := DecodeString(tt.in)
   110  		if string(out) != tt.out || err != tt.err {
   111  			t.Errorf("DecodeString(%q) = %q, %v, want %q, %v", tt.in, out, err, tt.out, tt.err)
   112  		}
   113  	}
   114  }
   115  
   116  func TestEncoderDecoder(t *testing.T) {
   117  	for _, multiplier := range []int{1, 128, 192} {
   118  		for _, test := range encDecTests {
   119  			input := bytes.Repeat(test.dec, multiplier)
   120  			output := strings.Repeat(test.enc, multiplier)
   121  
   122  			var buf bytes.Buffer
   123  			enc := NewEncoder(&buf)
   124  			r := struct{ io.Reader }{bytes.NewReader(input)} // io.Reader only; not io.WriterTo
   125  			if n, err := io.CopyBuffer(enc, r, make([]byte, 7)); n != int64(len(input)) || err != nil {
   126  				t.Errorf("encoder.Write(%q*%d) = (%d, %v), want (%d, nil)", test.dec, multiplier, n, err, len(input))
   127  				continue
   128  			}
   129  
   130  			if encDst := buf.String(); encDst != output {
   131  				t.Errorf("buf(%q*%d) = %v, want %v", test.dec, multiplier, encDst, output)
   132  				continue
   133  			}
   134  
   135  			dec := NewDecoder(&buf)
   136  			var decBuf bytes.Buffer
   137  			w := struct{ io.Writer }{&decBuf} // io.Writer only; not io.ReaderFrom
   138  			if _, err := io.CopyBuffer(w, dec, make([]byte, 7)); err != nil || decBuf.Len() != len(input) {
   139  				t.Errorf("decoder.Read(%q*%d) = (%d, %v), want (%d, nil)", test.enc, multiplier, decBuf.Len(), err, len(input))
   140  			}
   141  
   142  			if !bytes.Equal(decBuf.Bytes(), input) {
   143  				t.Errorf("decBuf(%q*%d) = %v, want %v", test.dec, multiplier, decBuf.Bytes(), input)
   144  				continue
   145  			}
   146  		}
   147  	}
   148  }
   149  
   150  func TestDecoderErr(t *testing.T) {
   151  	for _, tt := range errTests {
   152  		dec := NewDecoder(strings.NewReader(tt.in))
   153  		out, err := ioutil.ReadAll(dec)
   154  		wantErr := tt.err
   155  		// Decoder is reading from stream, so it reports io.ErrUnexpectedEOF instead of ErrLength.
   156  		if wantErr == ErrLength {
   157  			wantErr = io.ErrUnexpectedEOF
   158  		}
   159  		if string(out) != tt.out || err != wantErr {
   160  			t.Errorf("NewDecoder(%q) = %q, %v, want %q, %v", tt.in, out, err, tt.out, wantErr)
   161  		}
   162  	}
   163  }
   164  
   165  func TestDumper(t *testing.T) {
   166  	var in [40]byte
   167  	for i := range in {
   168  		in[i] = byte(i + 30)
   169  	}
   170  
   171  	for stride := 1; stride < len(in); stride++ {
   172  		var out bytes.Buffer
   173  		dumper := Dumper(&out)
   174  		done := 0
   175  		for done < len(in) {
   176  			todo := done + stride
   177  			if todo > len(in) {
   178  				todo = len(in)
   179  			}
   180  			dumper.Write(in[done:todo])
   181  			done = todo
   182  		}
   183  
   184  		dumper.Close()
   185  		if !bytes.Equal(out.Bytes(), expectedHexDump) {
   186  			t.Errorf("stride: %d failed. got:\n%s\nwant:\n%s", stride, out.Bytes(), expectedHexDump)
   187  		}
   188  	}
   189  }
   190  
   191  func TestDump(t *testing.T) {
   192  	var in [40]byte
   193  	for i := range in {
   194  		in[i] = byte(i + 30)
   195  	}
   196  
   197  	out := []byte(Dump(in[:]))
   198  	if !bytes.Equal(out, expectedHexDump) {
   199  		t.Errorf("got:\n%s\nwant:\n%s", out, expectedHexDump)
   200  	}
   201  }
   202  
   203  var expectedHexDump = []byte(`00000000  1e 1f 20 21 22 23 24 25  26 27 28 29 2a 2b 2c 2d  |.. !"#$%&'()*+,-|
   204  00000010  2e 2f 30 31 32 33 34 35  36 37 38 39 3a 3b 3c 3d  |./0123456789:;<=|
   205  00000020  3e 3f 40 41 42 43 44 45                           |>?@ABCDE|
   206  `)
   207  
   208  var sink []byte
   209  
   210  func BenchmarkEncode(b *testing.B) {
   211  	for _, size := range []int{256, 1024, 4096, 16384} {
   212  		src := bytes.Repeat([]byte{2, 3, 5, 7, 9, 11, 13, 17}, size/8)
   213  		sink = make([]byte, 2*size)
   214  
   215  		b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
   216  			for i := 0; i < b.N; i++ {
   217  				Encode(sink, src)
   218  			}
   219  		})
   220  	}
   221  }