github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/utils/checksum_test.go (about) 1 package utils_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 8 . "github.com/cloudfoundry/cli/utils" 9 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Sha1Checksum", func() { 15 16 var ( 17 checksum Sha1Checksum 18 ) 19 20 Describe("ComputeFileSha1", func() { 21 Context("If file does not exist", func() { 22 It("returns error", func() { 23 checksum = NewSha1Checksum("file/path/to/no/where") 24 25 sha1, err := checksum.ComputeFileSha1() 26 Expect(len(sha1)).To(Equal(0)) 27 Expect(err).To(HaveOccurred()) 28 }) 29 }) 30 31 Context("If file does exist", func() { 32 var ( 33 f *os.File 34 err error 35 ) 36 37 BeforeEach(func() { 38 f, err = ioutil.TempFile("", "sha1_test_") 39 Expect(err).NotTo(HaveOccurred()) 40 defer f.Close() 41 f.Write([]byte("abc")) 42 }) 43 44 AfterEach(func() { 45 os.RemoveAll(f.Name()) 46 }) 47 48 It("returns the sha1 of a file", func() { 49 checksum = NewSha1Checksum(f.Name()) 50 51 sha1, err := checksum.ComputeFileSha1() 52 Expect(err).NotTo(HaveOccurred()) 53 Expect(len(sha1)).To(Equal(20)) 54 Expect(fmt.Sprintf("%x", sha1)).To(Equal("a9993e364706816aba3e25717850c26c9cd0d89d")) 55 }) 56 }) 57 58 }) 59 60 Describe("CheckSha1", func() { 61 Context("file doesn't exist", func() { 62 It("returns false", func() { 63 checksum = NewSha1Checksum("file/path/to/no/where") 64 65 sha1, err := checksum.ComputeFileSha1() 66 Expect(len(sha1)).To(Equal(0)) 67 Expect(err).To(HaveOccurred()) 68 }) 69 }) 70 71 Context("If file does exist", func() { 72 var ( 73 f *os.File 74 err error 75 ) 76 77 BeforeEach(func() { 78 f, err = ioutil.TempFile("", "sha1_test_") 79 Expect(err).NotTo(HaveOccurred()) 80 defer f.Close() 81 f.Write([]byte("abc")) 82 }) 83 84 AfterEach(func() { 85 os.RemoveAll(f.Name()) 86 }) 87 88 It("returns false if sha1 doesn't match", func() { 89 checksum = NewSha1Checksum(f.Name()) 90 91 Expect(checksum.CheckSha1("skj33933dabs2292391223aa393fjs92")).To(BeFalse()) 92 }) 93 94 It("returns true if sha1 matches", func() { 95 checksum = NewSha1Checksum(f.Name()) 96 97 Expect(checksum.CheckSha1("a9993e364706816aba3e25717850c26c9cd0d89d")).To(BeTrue()) 98 }) 99 }) 100 101 }) 102 })