func TestIdentify(t *testing.T) { is := is.New(t) id, err := asset.Identify(iotest.DataErrReader(strings.NewReader("foo"))) is.NoErr(err) is.Equal(id.String(), "c45XyDwWmrPQwJPdULBhma6LGNaLghKtN7R9vLn2tFrepZJ9jJFSDzpCKei11EgA5r1veenBu3Q8qfvWeDuPc7fJK2") }
func (s *CollisionSuite) TestCollisionOrCorrupt(c *check.C) { fooMD5 := "acbd18db4cc2f85cedef654fccc4a4d8" c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, []byte{'o'}, bytes.NewBufferString("o")), check.Equals, CollisionError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, nil, bytes.NewBufferString("oo")), check.Equals, CollisionError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f'}, []byte{'o', 'o'}, nil), check.Equals, CollisionError) c.Check(collisionOrCorrupt(fooMD5, nil, []byte{}, bytes.NewBufferString("foo")), check.Equals, CollisionError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o', 'o'}, nil, bytes.NewBufferString("")), check.Equals, CollisionError) c.Check(collisionOrCorrupt(fooMD5, nil, nil, iotest.NewReadLogger("foo: ", iotest.DataErrReader(iotest.OneByteReader(bytes.NewBufferString("foo"))))), check.Equals, CollisionError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o', 'o'}, nil, bytes.NewBufferString("bar")), check.Equals, DiskHashError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, nil, nil), check.Equals, DiskHashError) c.Check(collisionOrCorrupt(fooMD5, []byte{}, nil, bytes.NewBufferString("")), check.Equals, DiskHashError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'O'}, nil, bytes.NewBufferString("o")), check.Equals, DiskHashError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'O', 'o'}, nil, nil), check.Equals, DiskHashError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, []byte{'O'}, nil), check.Equals, DiskHashError) c.Check(collisionOrCorrupt(fooMD5, []byte{'f', 'o'}, nil, bytes.NewBufferString("O")), check.Equals, DiskHashError) c.Check(collisionOrCorrupt(fooMD5, []byte{}, nil, iotest.TimeoutReader(iotest.OneByteReader(bytes.NewBufferString("foo")))), check.Equals, iotest.ErrTimeout) }
func TestReader_DataErr(t *testing.T) { const readSize = 3 testing: for _, test := range compressionTests { r := iotest.DataErrReader(bytes.NewReader(test.compressed)) d := NewReader(bufio.NewReader(r)) buf := make([]byte, readSize) var actual []byte for { n, err := d.Read(buf) actual = append(actual, buf[:n]...) if err != nil { if err == io.EOF { break } t.Errorf("%s: Read: %v", test.name, err) continue testing } } if len(test.original) != len(actual) { t.Errorf("%s: expected %d bytes, got %d", test.name, len(test.original), len(actual)) continue } if !bytes.Equal(test.original, actual) { t.Errorf("%s: bytes not equal", test.name) } } }
func TestCornerCase1(t *testing.T) { s := "k=1\\\r\n2\nl=1\n" for i := 0; i < 1025; i++ { s += "#" } p := make(Properties) p.Load(iotest.DataErrReader(bytes.NewReader([]byte(s)))) if !eqString(p.String("k", "-"), "12") { t.Fail() } }
func (s *ReaderSuite) TestMultipleReads(c *C) { text := "SEND\ndestination:xxx\n\nPayload\x00\n" + "SEND\ndestination:yyy\ncontent-length:12\n" + "dodgy\\c\\n\\cheader:dodgy\\c\\n\\r\\nvalue\\ \\\n\n" + "123456789AB\x00\x00" ioreaders := []io.Reader{ strings.NewReader(text), iotest.DataErrReader(strings.NewReader(text)), iotest.HalfReader(strings.NewReader(text)), iotest.OneByteReader(strings.NewReader(text)), } for _, ioreader := range ioreaders { // uncomment the following line to view the bytes being read //ioreader = iotest.NewReadLogger("RX", ioreader) reader := NewReader(ioreader) frame, err := reader.Read() c.Assert(err, IsNil) c.Assert(frame, NotNil) c.Assert(frame.Command, Equals, "SEND") c.Assert(frame.Header.Len(), Equals, 1) v := frame.Header.Get("destination") c.Assert(v, Equals, "xxx") c.Assert(string(frame.Body), Equals, "Payload") // now read a heart-beat from the input frame, err = reader.Read() c.Assert(err, IsNil) c.Assert(frame, IsNil) // this frame has content-length frame, err = reader.Read() c.Assert(err, IsNil) c.Assert(frame, NotNil) c.Assert(frame.Command, Equals, "SEND") c.Assert(frame.Header.Len(), Equals, 3) v = frame.Header.Get("destination") c.Assert(v, Equals, "yyy") n, ok, err := frame.Header.ContentLength() c.Assert(n, Equals, 12) c.Assert(ok, Equals, true) c.Assert(err, IsNil) k, v := frame.Header.GetAt(2) c.Assert(k, Equals, "dodgy:\n:header") c.Assert(v, Equals, "dodgy:\n\r\nvalue\\ \\") c.Assert(string(frame.Body), Equals, "123456789AB\x00") // ensure we are at the end of input frame, err = reader.Read() c.Assert(frame, IsNil) c.Assert(err, Equals, io.EOF) } }
func TestReadAll(t *testing.T) { orw := bytes.NewReader([]byte("tester")) rw := iotest.OneByteReader(orw) data := []byte("aaaaaa") if err := ReadAll(rw, data); err != nil { t.Fatal("unexpected error", err) } rw = iotest.DataErrReader(orw) if err := ReadAll(rw, data); err != io.EOF { t.Fatal("expected io.EOF") } }
func TestStdCopyDataErrReader(t *testing.T) { var input, stdout, stderr bytes.Buffer input.Write([]byte{2, 0, 0, 0, 0, 0, 0, 19}) input.Write([]byte("something happened!")) n, err := stdCopy(&stdout, &stderr, iotest.DataErrReader(&input)) if err != nil { t.Fatal(err) } if expected := int64(19); n != expected { t.Errorf("Wrong number of bytes. Want %d. Got %d.", expected, n) } if got := stderr.String(); got != "something happened!" { t.Errorf("stdCopy: wrong stderr. Want %q. Got %q.", "something happened!", got) } if got := stdout.String(); got != "" { t.Errorf("stdCopy: wrong stdout. Want %q. Got %q.", "", got) } }
func TestReadMessage(t *testing.T) { var r io.Reader a := DefaultAllocator() data := []byte{4, 0, 0, 116, 101, 115, 116} if ParsePrefix(data) != 4 { t.Fatal("wrong prefix") } max := 10 r = bytes.NewReader(data) /* Test Prefix Buffer Size */ p := []byte{0, 0} if data, err := ReadMessage(r, a, p, max); err != ErrPrefixBufferSize || data != nil { t.Fatal("expected ErrPrefixBufferSize") } p = []byte{0, 0, 0, 0} if data, err := ReadMessage(r, a, p, max); err != ErrPrefixBufferSize || data != nil { t.Fatal("expected ErrPrefixBufferSize") } p = []byte{0, 0, 0} /* Test Data Errors */ r = iotest.DataErrReader(bytes.NewReader([]byte{10, 0})) if data, err := ReadMessage(r, a, p, max); err != io.EOF || data != nil { t.Fatal("expected io.EOF", err, data) } r = bytes.NewReader(data) if data, err := ReadMessage(bytes.NewReader(data), a, p, 3); err != ErrMsgSize || data != nil { t.Fatal("expected ErrMsgSize") } if data, err := ReadMessage(bytes.NewReader(data), a, p, 4); err != nil || string(data) != "test" { t.Fatal("expected ErrMsgSize") } }
var readers = []struct { name string f func(string) io.Reader }{ {"StringReader", func(s string) io.Reader { return strings.NewReader(s) }}, {"ReaderOnly", func(s string) io.Reader { return struct{ io.Reader }{strings.NewReader(s)} }}, {"OneByteReader", func(s string) io.Reader { return iotest.OneByteReader(strings.NewReader(s)) }}, {"DataErrReader", func(s string) io.Reader { return iotest.DataErrReader(strings.NewReader(s)) }}, } func testScan(t *testing.T, f func(string) io.Reader, scan func(r io.Reader, a ...interface{}) (int, error)) { for _, test := range scanTests { r := f(test.text) n, err := scan(r, test.in) if err != nil { m := "" if n > 0 { m = Sprintf(" (%d fields ok)", n) } t.Errorf("got error scanning %q: %s%s", test.text, err, m) continue }