diff options
Diffstat (limited to 'formats/objfile')
-rw-r--r-- | formats/objfile/reader.go | 13 | ||||
-rw-r--r-- | formats/objfile/reader_test.go | 32 |
2 files changed, 45 insertions, 0 deletions
diff --git a/formats/objfile/reader.go b/formats/objfile/reader.go index 5b17da0..99ed754 100644 --- a/formats/objfile/reader.go +++ b/formats/objfile/reader.go @@ -121,3 +121,16 @@ func (r *Reader) Close() (err error) { return } + +// FillObject fills the given object from an object entry +func (r *Reader) FillObject(obj core.Object) error { + obj.SetType(r.header.t) + obj.SetSize(r.header.size) + w, err := obj.Writer() + if err != nil { + return err + } + _, err = io.Copy(w, r.r) + + return err +} diff --git a/formats/objfile/reader_test.go b/formats/objfile/reader_test.go index ec191f6..964a071 100644 --- a/formats/objfile/reader_test.go +++ b/formats/objfile/reader_test.go @@ -69,3 +69,35 @@ func (s *SuiteReader) TestReadCorruptZLib(c *C) { _, err := NewReader(source) c.Assert(err, NotNil) } + +func (s *SuiteReader) TestFillObject(c *C) { + for k, fixture := range objfileFixtures { + com := fmt.Sprintf("test %d: ", k) + hash := core.NewHash(fixture.hash) + content, _ := base64.StdEncoding.DecodeString(fixture.content) + data, _ := base64.StdEncoding.DecodeString(fixture.data) + + testFillObject(c, bytes.NewReader(data), hash, fixture.t, content, com) + } +} + +func testFillObject(c *C, source io.Reader, hash core.Hash, typ core.ObjectType, content []byte, com string) { + var o core.Object = &core.MemoryObject{} + r, err := NewReader(source) + c.Assert(err, IsNil) + err = r.FillObject(o) + c.Assert(err, IsNil) + c.Assert(o.Type(), Equals, typ) + c.Assert(o.Size(), Equals, int64(len(content))) + c.Assert(o.Hash(), Equals, hash) + or, err := o.Reader() + c.Assert(err, IsNil) + rc, err := ioutil.ReadAll(or) + c.Assert(err, IsNil) + c.Assert(rc, DeepEquals, content, Commentf("%scontent=%s, expected=%s", base64.StdEncoding.EncodeToString(rc), base64.StdEncoding.EncodeToString(content))) + c.Assert(or.Close(), IsNil) + _, err = or.Read(make([]byte, 0, 1)) + c.Assert(err, Equals, nil) + ow, err := o.Writer() + c.Assert(ow, Equals, o) +} |