github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/checksum_test.go (about)

     1  package util_test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	. "code.cloudfoundry.org/cli/util"
     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("CheckSha1", func() {
    21  		Context("file doesn't exist", func() {
    22  			It("returns false", 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  				_, err = f.Write([]byte("abc"))
    42  				Expect(err).ToNot(HaveOccurred())
    43  			})
    44  
    45  			AfterEach(func() {
    46  				os.RemoveAll(f.Name())
    47  			})
    48  
    49  			It("returns false if sha1 doesn't match", func() {
    50  				checksum = NewSha1Checksum(f.Name())
    51  
    52  				Expect(checksum.CheckSha1("skj33933dabs2292391223aa393fjs92")).To(BeFalse())
    53  			})
    54  
    55  			It("returns true if sha1 matches", func() {
    56  				checksum = NewSha1Checksum(f.Name())
    57  
    58  				Expect(checksum.CheckSha1("a9993e364706816aba3e25717850c26c9cd0d89d")).To(BeTrue())
    59  			})
    60  		})
    61  
    62  	})
    63  
    64  	Describe("ComputeFileSha1", func() {
    65  		Context("If file does not exist", func() {
    66  			It("returns error", func() {
    67  				checksum = NewSha1Checksum("file/path/to/no/where")
    68  
    69  				sha1, err := checksum.ComputeFileSha1()
    70  				Expect(len(sha1)).To(Equal(0))
    71  				Expect(err).To(HaveOccurred())
    72  			})
    73  		})
    74  
    75  		Context("If file does exist", func() {
    76  			var (
    77  				f   *os.File
    78  				err error
    79  			)
    80  
    81  			BeforeEach(func() {
    82  				f, err = ioutil.TempFile("", "sha1_test_")
    83  				Expect(err).NotTo(HaveOccurred())
    84  				defer f.Close()
    85  				_, err = f.Write([]byte("abc"))
    86  				Expect(err).ToNot(HaveOccurred())
    87  			})
    88  
    89  			AfterEach(func() {
    90  				os.RemoveAll(f.Name())
    91  			})
    92  
    93  			It("returns the sha1 of a file", func() {
    94  				checksum = NewSha1Checksum(f.Name())
    95  
    96  				sha1, err := checksum.ComputeFileSha1()
    97  				Expect(err).NotTo(HaveOccurred())
    98  				Expect(len(sha1)).To(Equal(20))
    99  				Expect(fmt.Sprintf("%x", sha1)).To(Equal("a9993e364706816aba3e25717850c26c9cd0d89d"))
   100  			})
   101  		})
   102  
   103  	})
   104  })