forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmp_test.go
156 lines (134 loc) · 4.97 KB
/
cmp_test.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package osutil_test
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"strings"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/osutil"
)
type CmpTestSuite struct{}
var _ = Suite(&CmpTestSuite{})
func (ts *CmpTestSuite) TestCmp(c *C) {
tmpdir := c.MkDir()
foo := filepath.Join(tmpdir, "foo")
f, err := os.Create(foo)
c.Assert(err, IsNil)
defer f.Close()
// test FilesAreEqual for various sizes:
// - bufsz not exceeded
// - bufsz matches file size
// - bufsz exceeds file size
canary := "1234567890123456"
for _, n := range []int{1, 128 / len(canary), (128 / len(canary)) + 1} {
for i := 0; i < n; i++ {
// Pick a smaller buffer size so that the test can complete quicker
c.Assert(osutil.FilesAreEqualChunked(foo, foo, 128), Equals, true)
_, err := f.WriteString(canary)
c.Assert(err, IsNil)
f.Sync()
}
}
}
func (ts *CmpTestSuite) TestCmpEmptyNeqMissing(c *C) {
tmpdir := c.MkDir()
foo := filepath.Join(tmpdir, "foo")
bar := filepath.Join(tmpdir, "bar")
f, err := os.Create(foo)
c.Assert(err, IsNil)
defer f.Close()
c.Assert(osutil.FilesAreEqual(foo, bar), Equals, false)
c.Assert(osutil.FilesAreEqual(bar, foo), Equals, false)
}
func (ts *CmpTestSuite) TestCmpEmptyNeqNonEmpty(c *C) {
tmpdir := c.MkDir()
foo := filepath.Join(tmpdir, "foo")
bar := filepath.Join(tmpdir, "bar")
f, err := os.Create(foo)
c.Assert(err, IsNil)
defer f.Close()
c.Assert(ioutil.WriteFile(bar, []byte("x"), 0644), IsNil)
c.Assert(osutil.FilesAreEqual(foo, bar), Equals, false)
c.Assert(osutil.FilesAreEqual(bar, foo), Equals, false)
}
func (ts *CmpTestSuite) TestCmpStreams(c *C) {
for _, x := range []struct {
a string
b string
r bool
}{
{"hello", "hello", true},
{"hello", "world", false},
{"hello", "hell", false},
} {
c.Assert(osutil.StreamsEqual(strings.NewReader(x.a), strings.NewReader(x.b)), Equals, x.r)
}
}
func (s *CmpTestSuite) TestStreamsEqualChunked(c *C) {
text := "marry had a little lamb"
// Passing the same stream twice is not mishandled.
readerA := bytes.NewReader([]byte(text))
readerB := readerA
eq := osutil.StreamsEqualChunked(readerA, readerB, 0)
c.Check(eq, Equals, true)
// Passing two streams with the same content works as expected. Note that
// we are using different block sizes to check for additional edge cases.
for _, chunkSize := range []int{0, 1, len(text) / 2, len(text), len(text) + 1} {
readerA = bytes.NewReader([]byte(text))
readerB = bytes.NewReader([]byte(text))
eq := osutil.StreamsEqualChunked(readerA, readerB, chunkSize)
c.Check(eq, Equals, true, Commentf("chunk size %d", chunkSize))
}
// Passing two streams with unequal contents but equal length works as
// expected.
for _, chunkSize := range []int{0, 1, len(text) / 2, len(text), len(text) + 1} {
comment := Commentf("chunk size %d", chunkSize)
readerA = bytes.NewReader([]byte(strings.ToLower(text)))
readerB = bytes.NewReader([]byte(strings.ToUpper(text)))
eq = osutil.StreamsEqualChunked(readerA, readerB, chunkSize)
c.Check(eq, Equals, false, comment)
}
// Passing two streams which differer by tail only also works as expected.
for _, chunkSize := range []int{0, 1, len(text) / 2, len(text), len(text) + 1} {
textWithChangedTail := text[:len(text)-1] + strings.ToUpper(text[len(text)-1:])
c.Assert(textWithChangedTail, Not(Equals), text)
c.Assert(len(textWithChangedTail), Equals, len(text))
comment := Commentf("chunk size %d", chunkSize)
readerA = bytes.NewReader([]byte(text))
readerB = bytes.NewReader([]byte(textWithChangedTail))
eq = osutil.StreamsEqualChunked(readerA, readerB, chunkSize)
c.Check(eq, Equals, false, comment)
}
// Passing two streams with different length works as expected.
// Note that this is not used by EnsureDirState in practice.
for _, chunkSize := range []int{0, 1, len(text) / 2, len(text), len(text) + 1} {
comment := Commentf("A: %q, B: %q, chunk size %d", text, text[:len(text)/2], chunkSize)
readerA = bytes.NewReader([]byte(text))
readerB = bytes.NewReader([]byte(text[:len(text)/2]))
eq = osutil.StreamsEqualChunked(readerA, readerB, chunkSize)
c.Check(eq, Equals, false, comment)
// Readers passed the other way around.
readerA = bytes.NewReader([]byte(text))
readerB = bytes.NewReader([]byte(text[:len(text)/2]))
eq = osutil.StreamsEqualChunked(readerB, readerA, chunkSize)
c.Check(eq, Equals, false, comment)
}
}