1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
17 type HashcheckSuiteSuite struct{}
19 // Gocheck boilerplate
20 var _ = Suite(&HashcheckSuiteSuite{})
22 func (h *HashcheckSuiteSuite) TestRead(c *C) {
23 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
27 hcr := HashCheckingReader{r, md5.New(), hash}
29 w.Write([]byte("foo"))
32 p, err := ioutil.ReadAll(hcr)
33 c.Check(len(p), Equals, 3)
34 c.Check(err, Equals, nil)
39 hcr := HashCheckingReader{r, md5.New(), hash}
41 w.Write([]byte("bar"))
44 p, err := ioutil.ReadAll(hcr)
45 c.Check(len(p), Equals, 3)
46 c.Check(err, Equals, BadChecksum)
50 func (h *HashcheckSuiteSuite) TestWriteTo(c *C) {
51 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
54 bb := bytes.NewBufferString("foo")
55 hcr := HashCheckingReader{bb, md5.New(), hash}
57 done := make(chan bool)
59 p, err := ioutil.ReadAll(r)
60 c.Check(len(p), Equals, 3)
61 c.Check(err, Equals, nil)
65 n, err := hcr.WriteTo(w)
67 c.Check(n, Equals, int64(3))
68 c.Check(err, Equals, nil)
73 bb := bytes.NewBufferString("bar")
74 hcr := HashCheckingReader{bb, md5.New(), hash}
76 done := make(chan bool)
78 p, err := ioutil.ReadAll(r)
79 c.Check(len(p), Equals, 3)
80 c.Check(err, Equals, nil)
84 n, err := hcr.WriteTo(w)
86 c.Check(n, Equals, int64(3))
87 c.Check(err, Equals, BadChecksum)
91 // If WriteTo stops early due to a write error, return the
92 // write error (not "bad checksum").
94 input := bytes.NewBuffer(make([]byte, 1<<26))
95 hcr := HashCheckingReader{input, md5.New(), hash}
98 n, err := hcr.WriteTo(w)
99 c.Check(n, Equals, int64(0))
101 c.Check(err, Not(Equals), BadChecksum)