forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecksum_test.go
104 lines (81 loc) · 2.16 KB
/
checksum_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
package util_test
import (
"fmt"
"io/ioutil"
"os"
. "code.cloudfoundry.org/cli/util"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Sha1Checksum", func() {
var (
checksum Sha1Checksum
)
Describe("CheckSha1", func() {
Context("file doesn't exist", func() {
It("returns false", func() {
checksum = NewSha1Checksum("file/path/to/no/where")
sha1, err := checksum.ComputeFileSha1()
Expect(len(sha1)).To(Equal(0))
Expect(err).To(HaveOccurred())
})
})
Context("If file does exist", func() {
var (
f *os.File
err error
)
BeforeEach(func() {
f, err = ioutil.TempFile("", "sha1_test_")
Expect(err).NotTo(HaveOccurred())
defer f.Close()
_, err = f.Write([]byte("abc"))
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
os.RemoveAll(f.Name())
})
It("returns false if sha1 doesn't match", func() {
checksum = NewSha1Checksum(f.Name())
Expect(checksum.CheckSha1("skj33933dabs2292391223aa393fjs92")).To(BeFalse())
})
It("returns true if sha1 matches", func() {
checksum = NewSha1Checksum(f.Name())
Expect(checksum.CheckSha1("a9993e364706816aba3e25717850c26c9cd0d89d")).To(BeTrue())
})
})
})
Describe("ComputeFileSha1", func() {
Context("If file does not exist", func() {
It("returns error", func() {
checksum = NewSha1Checksum("file/path/to/no/where")
sha1, err := checksum.ComputeFileSha1()
Expect(len(sha1)).To(Equal(0))
Expect(err).To(HaveOccurred())
})
})
Context("If file does exist", func() {
var (
f *os.File
err error
)
BeforeEach(func() {
f, err = ioutil.TempFile("", "sha1_test_")
Expect(err).NotTo(HaveOccurred())
defer f.Close()
_, err = f.Write([]byte("abc"))
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
os.RemoveAll(f.Name())
})
It("returns the sha1 of a file", func() {
checksum = NewSha1Checksum(f.Name())
sha1, err := checksum.ComputeFileSha1()
Expect(err).NotTo(HaveOccurred())
Expect(len(sha1)).To(Equal(20))
Expect(fmt.Sprintf("%x", sha1)).To(Equal("a9993e364706816aba3e25717850c26c9cd0d89d"))
})
})
})
})