1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package objfile
import (
"bytes"
"encoding/base64"
"fmt"
"io"
. "gopkg.in/check.v1"
"gopkg.in/src-d/go-git.v4/core"
)
type SuiteWriter struct{}
var _ = Suite(&SuiteWriter{})
func (s *SuiteWriter) TestWriteObjfile(c *C) {
for k, fixture := range objfileFixtures {
buffer := bytes.NewBuffer(nil)
com := fmt.Sprintf("test %d: ", k)
hash := core.NewHash(fixture.hash)
content, _ := base64.StdEncoding.DecodeString(fixture.content)
// Write the data out to the buffer
testWriter(c, buffer, hash, fixture.t, content)
// Read the data back in from the buffer to be sure it matches
testReader(c, buffer, hash, fixture.t, content, com)
}
}
func testWriter(c *C, dest io.Writer, hash core.Hash, t core.ObjectType, content []byte) {
size := int64(len(content))
w := NewWriter(dest)
err := w.WriteHeader(t, size)
c.Assert(err, IsNil)
written, err := io.Copy(w, bytes.NewReader(content))
c.Assert(err, IsNil)
c.Assert(written, Equals, size)
c.Assert(w.Hash(), Equals, hash)
c.Assert(w.Close(), IsNil)
}
func (s *SuiteWriter) TestWriteOverflow(c *C) {
buf := bytes.NewBuffer(nil)
w := NewWriter(buf)
err := w.WriteHeader(core.BlobObject, 8)
c.Assert(err, IsNil)
n, err := w.Write([]byte("1234"))
c.Assert(err, IsNil)
c.Assert(n, Equals, 4)
n, err = w.Write([]byte("56789"))
c.Assert(err, Equals, ErrOverflow)
c.Assert(n, Equals, 4)
}
func (s *SuiteWriter) TestNewWriterInvalidType(c *C) {
buf := bytes.NewBuffer(nil)
w := NewWriter(buf)
err := w.WriteHeader(core.InvalidObject, 8)
c.Assert(err, Equals, core.ErrInvalidType)
}
func (s *SuiteWriter) TestNewWriterInvalidSize(c *C) {
buf := bytes.NewBuffer(nil)
w := NewWriter(buf)
err := w.WriteHeader(core.BlobObject, -1)
c.Assert(err, Equals, ErrNegativeSize)
err = w.WriteHeader(core.BlobObject, -1651860)
c.Assert(err, Equals, ErrNegativeSize)
}
|