...

Source file src/encoding/hex/hex_test.go

Documentation: encoding/hex

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

View as plain text