github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/container/externalbuilder/tar_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package externalbuilder_test 8 9 import ( 10 "io/ioutil" 11 "os" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 16 "github.com/hechain20/hechain/core/container/externalbuilder" 17 ) 18 19 var _ = Describe("Tar", func() { 20 Describe("Untar", func() { 21 var dst string 22 23 BeforeEach(func() { 24 var err error 25 dst, err = ioutil.TempDir("", "untar-test") 26 Expect(err).NotTo(HaveOccurred()) 27 }) 28 29 AfterEach(func() { 30 os.RemoveAll(dst) 31 }) 32 33 It("extracts a tar.gz to a destination", func() { 34 file, err := os.Open("testdata/normal_archive.tar.gz") 35 Expect(err).NotTo(HaveOccurred()) 36 defer file.Close() 37 err = externalbuilder.Untar(file, dst) 38 Expect(err).NotTo(HaveOccurred()) 39 }) 40 41 Context("when the archive conains an odd file type", func() { 42 It("returns an error", func() { 43 file, err := os.Open("testdata/archive_with_symlink.tar.gz") 44 Expect(err).NotTo(HaveOccurred()) 45 defer file.Close() 46 err = externalbuilder.Untar(file, dst) 47 Expect(err).To(MatchError("invalid file type '50' contained in archive for file 'c.file'")) 48 }) 49 }) 50 51 Context("when the archive contains absolute paths", func() { 52 It("returns an error", func() { 53 file, err := os.Open("testdata/archive_with_absolute.tar.gz") 54 Expect(err).NotTo(HaveOccurred()) 55 defer file.Close() 56 err = externalbuilder.Untar(file, dst) 57 Expect(err).To(MatchError("tar contains the absolute or escaping path '/home/yellickj/go/src/github.com/hechain20/hechain/core/chaincode/externalbuilders/testdata/a/test.file'")) 58 }) 59 }) 60 61 Context("when the file's directory cannot be created", func() { 62 BeforeEach(func() { 63 ioutil.WriteFile(dst+"/a", []byte("test"), 0o700) 64 }) 65 66 It("returns an error", func() { 67 file, err := os.Open("testdata/normal_archive.tar.gz") 68 Expect(err).NotTo(HaveOccurred()) 69 defer file.Close() 70 err = externalbuilder.Untar(file, dst) 71 Expect(err).To(MatchError(ContainSubstring("could not create directory 'a'"))) 72 }) 73 }) 74 75 Context("when the empty directory cannot be created", func() { 76 BeforeEach(func() { 77 ioutil.WriteFile(dst+"/d", []byte("test"), 0o700) 78 }) 79 80 It("returns an error", func() { 81 file, err := os.Open("testdata/normal_archive.tar.gz") 82 Expect(err).NotTo(HaveOccurred()) 83 defer file.Close() 84 err = externalbuilder.Untar(file, dst) 85 Expect(err).To(MatchError(ContainSubstring("could not create directory 'd/'"))) 86 }) 87 }) 88 }) 89 90 Describe("ValidPath()", func() { 91 It("validates that a path is relative and a child", func() { 92 Expect(externalbuilder.ValidPath("a/simple/path")).To(BeTrue()) 93 Expect(externalbuilder.ValidPath("../path/to/parent")).To(BeFalse()) 94 Expect(externalbuilder.ValidPath("a/path/../with/intermediates")).To(BeTrue()) 95 Expect(externalbuilder.ValidPath("a/path/../../../with/toomanyintermediates")).To(BeFalse()) 96 Expect(externalbuilder.ValidPath("a/path/with/trailing/../..")).To(BeTrue()) 97 Expect(externalbuilder.ValidPath("a/path/with/toomanytrailing/../../../../..")).To(BeFalse()) 98 Expect(externalbuilder.ValidPath("..")).To(BeFalse()) 99 Expect(externalbuilder.ValidPath("../other")).To(BeFalse()) 100 Expect(externalbuilder.ValidPath("...")).To(BeTrue()) 101 Expect(externalbuilder.ValidPath("..foo")).To(BeTrue()) 102 Expect(externalbuilder.ValidPath("/an/absolute/path")).To(BeFalse()) 103 }) 104 }) 105 })