20862: Add 'sdk/ruby-google-api-client/' from commit '2f4be67955e48bb65d008ecd9ff6da9...
[arvados.git] / sdk / go / keepclient / hashcheck_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package keepclient
6
7 import (
8         "bytes"
9         "crypto/md5"
10         "fmt"
11         "io"
12         "io/ioutil"
13
14         . "gopkg.in/check.v1"
15 )
16
17 type HashcheckSuiteSuite struct{}
18
19 // Gocheck boilerplate
20 var _ = Suite(&HashcheckSuiteSuite{})
21
22 func (h *HashcheckSuiteSuite) TestRead(c *C) {
23         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
24
25         {
26                 r, w := io.Pipe()
27                 hcr := HashCheckingReader{r, md5.New(), hash}
28                 go func() {
29                         w.Write([]byte("foo"))
30                         w.Close()
31                 }()
32                 p, err := ioutil.ReadAll(hcr)
33                 c.Check(len(p), Equals, 3)
34                 c.Check(err, Equals, nil)
35         }
36
37         {
38                 r, w := io.Pipe()
39                 hcr := HashCheckingReader{r, md5.New(), hash}
40                 go func() {
41                         w.Write([]byte("bar"))
42                         w.Close()
43                 }()
44                 p, err := ioutil.ReadAll(hcr)
45                 c.Check(len(p), Equals, 3)
46                 c.Check(err, Equals, BadChecksum)
47         }
48 }
49
50 func (h *HashcheckSuiteSuite) TestWriteTo(c *C) {
51         hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
52
53         {
54                 bb := bytes.NewBufferString("foo")
55                 hcr := HashCheckingReader{bb, md5.New(), hash}
56                 r, w := io.Pipe()
57                 done := make(chan bool)
58                 go func() {
59                         p, err := ioutil.ReadAll(r)
60                         c.Check(len(p), Equals, 3)
61                         c.Check(err, Equals, nil)
62                         done <- true
63                 }()
64
65                 n, err := hcr.WriteTo(w)
66                 w.Close()
67                 c.Check(n, Equals, int64(3))
68                 c.Check(err, Equals, nil)
69                 <-done
70         }
71
72         {
73                 bb := bytes.NewBufferString("bar")
74                 hcr := HashCheckingReader{bb, md5.New(), hash}
75                 r, w := io.Pipe()
76                 done := make(chan bool)
77                 go func() {
78                         p, err := ioutil.ReadAll(r)
79                         c.Check(len(p), Equals, 3)
80                         c.Check(err, Equals, nil)
81                         done <- true
82                 }()
83
84                 n, err := hcr.WriteTo(w)
85                 w.Close()
86                 c.Check(n, Equals, int64(3))
87                 c.Check(err, Equals, BadChecksum)
88                 <-done
89         }
90
91         // If WriteTo stops early due to a write error, return the
92         // write error (not "bad checksum").
93         {
94                 input := bytes.NewBuffer(make([]byte, 1<<26))
95                 hcr := HashCheckingReader{input, md5.New(), hash}
96                 r, w := io.Pipe()
97                 r.Close()
98                 n, err := hcr.WriteTo(w)
99                 c.Check(n, Equals, int64(0))
100                 c.Check(err, NotNil)
101                 c.Check(err, Not(Equals), BadChecksum)
102         }
103 }