diff options
| author | Tim Cooper <tim.cooper@layeh.com> | 2017-10-11 22:05:03 -0300 |
|---|---|---|
| committer | Joe Tsai <thebrokentoaster@gmail.com> | 2017-10-20 23:47:07 +0000 |
| commit | 6db4950dc57deb899bf5550411c01ce32f16bdd0 (patch) | |
| tree | 11a16d2d68e4b82612cb7ffd6d78bf2d9a2c6718 /src/encoding/hex/hex_test.go | |
| parent | d05f82a11af68a65b118de14bb230d640722d55c (diff) | |
| download | go-6db4950dc57deb899bf5550411c01ce32f16bdd0.tar.xz | |
encoding/hex: add NewEncoder, NewDecoder
NewEncoder returns an io.Writer that writes all incoming bytes as
hexadecimal characters to the underlying io.Writer. NewDecoder returns an
io.Reader that does the inverse.
Fixes #21590
Change-Id: Iebe0813faf365b42598f19a9aa41768f571dc0a8
Reviewed-on: https://go-review.googlesource.com/70210
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/encoding/hex/hex_test.go')
| -rw-r--r-- | src/encoding/hex/hex_test.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/encoding/hex/hex_test.go b/src/encoding/hex/hex_test.go index e6dc765c95..d874b39e95 100644 --- a/src/encoding/hex/hex_test.go +++ b/src/encoding/hex/hex_test.go @@ -7,6 +7,9 @@ package hex import ( "bytes" "fmt" + "io" + "io/ioutil" + "strings" "testing" ) @@ -111,6 +114,63 @@ func TestInvalidStringErr(t *testing.T) { } } +func TestEncoderDecoder(t *testing.T) { + for _, multiplier := range []int{1, 128, 192} { + for _, test := range encDecTests { + input := bytes.Repeat(test.dec, multiplier) + output := strings.Repeat(test.enc, multiplier) + + var buf bytes.Buffer + enc := NewEncoder(&buf) + r := struct{ io.Reader }{bytes.NewReader(input)} // io.Reader only; not io.WriterTo + if n, err := io.CopyBuffer(enc, r, make([]byte, 7)); n != int64(len(input)) || err != nil { + t.Errorf("encoder.Write(%q*%d) = (%d, %v), want (%d, nil)", test.dec, multiplier, n, err, len(input)) + continue + } + + if encDst := buf.String(); encDst != output { + t.Errorf("buf(%q*%d) = %v, want %v", test.dec, multiplier, encDst, output) + continue + } + + dec := NewDecoder(&buf) + var decBuf bytes.Buffer + w := struct{ io.Writer }{&decBuf} // io.Writer only; not io.ReaderFrom + if _, err := io.CopyBuffer(w, dec, make([]byte, 7)); err != nil || decBuf.Len() != len(input) { + t.Errorf("decoder.Read(%q*%d) = (%d, %v), want (%d, nil)", test.enc, multiplier, decBuf.Len(), err, len(input)) + } + + if !bytes.Equal(decBuf.Bytes(), input) { + t.Errorf("decBuf(%q*%d) = %v, want %v", test.dec, multiplier, decBuf.Bytes(), input) + continue + } + } + } +} + +func TestDecodeErr(t *testing.T) { + tests := []struct { + in string + wantOut string + wantErr error + }{ + {"", "", nil}, + {"0", "", io.ErrUnexpectedEOF}, + {"0g", "", InvalidByteError('g')}, + {"00gg", "\x00", InvalidByteError('g')}, + {"0\x01", "", InvalidByteError('\x01')}, + {"ffeed", "\xff\xee", io.ErrUnexpectedEOF}, + } + + for _, tt := range tests { + dec := NewDecoder(strings.NewReader(tt.in)) + got, err := ioutil.ReadAll(dec) + if string(got) != tt.wantOut || err != tt.wantErr { + t.Errorf("NewDecoder(%q) = (%q, %v), want (%q, %v)", tt.in, got, err, tt.wantOut, tt.wantErr) + } + } +} + func TestDumper(t *testing.T) { var in [40]byte for i := range in { |
