github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/internal/util/sha256sum_test.go (about) 1 // Copyright (c) 2023, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package util 6 7 import ( 8 "fmt" 9 "os" 10 "runtime" 11 12 . "github.com/onsi/ginkgo/v2" 13 . "github.com/onsi/gomega" 14 "github.com/sirupsen/logrus" 15 ) 16 17 var _ = Describe("Internal/Util/Sha256sums", func() { 18 var ( 19 log *logrus.Entry 20 ) 21 22 BeforeEach(func() { 23 log = logrus.NewEntry(logrus.New()) 24 log.Logger.SetOutput(GinkgoWriter) 25 }) 26 27 Describe("Sha256ChecksumDir", func() { 28 It("Should create valid checksums", func() { 29 if runtime.GOOS == "windows" { 30 Skip("Tests not supported on windows") 31 } 32 33 expected, err := os.ReadFile("testdata/SHA256SUM.dir") 34 Expect(err).ToNot(HaveOccurred()) 35 36 res, err := Sha256ChecksumDir("testdata/dir") 37 Expect(err).ToNot(HaveOccurred()) 38 Expect(res).To(Equal(expected)) 39 }) 40 }) 41 42 Describe("Sha256VerifyDir", func() { 43 It("Should verify valid files", func() { 44 lines := 0 45 ok, err := Sha256VerifyDir("testdata/SHA256SUM.good", "", log, func(f string, ok bool) { 46 lines++ 47 if !ok { 48 Fail(fmt.Sprintf("File %v did not match", f)) 49 } 50 }) 51 Expect(err).ToNot(HaveOccurred()) 52 Expect(ok).To(BeTrue()) 53 Expect(lines).To(Equal(2)) 54 }) 55 56 It("Should detect bad files", func() { 57 lines := 0 58 ok, err := Sha256VerifyDir("testdata/SHA256SUM.bad", "testdata", log, func(f string, ok bool) { 59 lines++ 60 switch f { 61 case "9f28c.txt", "other/missing": 62 if ok { 63 Fail(fmt.Sprintf("File %s did not fail", f)) 64 } 65 default: 66 if !ok { 67 Fail(fmt.Sprintf("File %v did not match", f)) 68 } 69 } 70 }) 71 Expect(err).ToNot(HaveOccurred()) 72 Expect(ok).To(BeFalse()) 73 Expect(lines).To(Equal(3)) 74 }) 75 76 It("Should handle corrupt sum files", func() { 77 ok, err := Sha256VerifyDir("testdata/SHA256SUM.corrupt", "testdata", log, func(string, bool) {}) 78 Expect(err).To(MatchError("invalid sums file: malformed line 1")) 79 Expect(ok).To(BeFalse()) 80 }) 81 }) 82 })