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
|
package utils_test
import (
. "launchpad.net/gocheck"
"launchpad.net/juju-core/utils"
"strings"
"testing"
"time"
)
func Test(t *testing.T) {
TestingT(t)
}
type utilsSuite struct{}
var _ = Suite(utilsSuite{})
func (utilsSuite) TestAttemptTiming(c *C) {
const delta = 0.01e9
testAttempt := utils.AttemptStrategy{
Total: 0.25e9,
Delay: 0.1e9,
}
want := []time.Duration{0, 0.1e9, 0.2e9, 0.2e9}
got := make([]time.Duration, 0, len(want)) // avoid allocation when testing timing
t0 := time.Now()
for a := testAttempt.Start(); a.Next(); {
got = append(got, time.Now().Sub(t0))
}
got = append(got, time.Now().Sub(t0))
c.Assert(got, HasLen, len(want))
for i, got := range want {
lo := want[i] - delta
hi := want[i] + delta
if got < lo || got > hi {
c.Errorf("attempt %d want %g got %g", i, want[i].Seconds(), got.Seconds())
}
}
}
func (utilsSuite) TestRandomBytes(c *C) {
b, err := utils.RandomBytes(16)
c.Assert(err, IsNil)
c.Assert(b, HasLen, 16)
x0 := b[0]
for _, x := range b {
if x != x0 {
return
}
}
c.Errorf("all same bytes in result of RandomBytes")
}
func (utilsSuite) TestRandomPassword(c *C) {
p, err := utils.RandomPassword()
c.Assert(err, IsNil)
if len(p) < 18 {
c.Errorf("password too short: %q", p)
}
// check we're not adding base64 padding.
c.Assert(p[len(p)-1], Not(Equals), '=')
}
func (utilsSuite) TestPasswordHash(c *C) {
tests := []string{"", "a", "a longer password than i would usually bother with"}
hs := make(map[string]bool)
for i, t := range tests {
c.Logf("test %d", i)
h := utils.PasswordHash(t)
c.Logf("hash %q", h)
c.Assert(len(h), Equals, 24)
c.Assert(hs[h], Equals, false)
// check we're not adding base64 padding.
c.Assert(h[len(h)-1], Not(Equals), '=')
hs[h] = true
// check it's deterministic
h1 := utils.PasswordHash(t)
c.Assert(h1, Equals, h)
}
}
var (
data = []byte(strings.Repeat("some data to be compressed\n", 100))
// compressedData was produced by the gzip command.
compressedData = []byte{
0x1f, 0x8b, 0x08, 0x00, 0x33, 0xb5, 0xf6, 0x50,
0x00, 0x03, 0xed, 0xc9, 0xb1, 0x0d, 0x00, 0x20,
0x08, 0x45, 0xc1, 0xde, 0x29, 0x58, 0x0d, 0xe5,
0x97, 0x04, 0x23, 0xee, 0x1f, 0xa7, 0xb0, 0x7b,
0xd7, 0x5e, 0x57, 0xca, 0xc2, 0xaf, 0xdb, 0x2d,
0x9b, 0xb2, 0x55, 0xb9, 0x8f, 0xba, 0x15, 0xa3,
0x29, 0x8a, 0xa2, 0x28, 0x8a, 0xa2, 0x28, 0xea,
0x67, 0x3d, 0x71, 0x71, 0x6e, 0xbf, 0x8c, 0x0a,
0x00, 0x00,
}
)
func (utilsSuite) TestCompression(c *C) {
cdata := utils.Gzip(data)
c.Assert(len(cdata) < len(data), Equals, true)
data1, err := utils.Gunzip(cdata)
c.Assert(err, IsNil)
c.Assert(data1, DeepEquals, data)
data1, err = utils.Gunzip(compressedData)
c.Assert(err, IsNil)
c.Assert(data1, DeepEquals, data)
}
|