github.com/ahlemtn/fabric@v2.1.1+incompatible/core/container/externalbuilder/tar_test.go (about) 1 /* 2 Copyright IBM Corp. 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/hyperledger/fabric/core/container/externalbuilder" 17 ) 18 19 var _ = Describe("Tar", func() { 20 Describe("Untar", func() { 21 var ( 22 dst string 23 ) 24 25 BeforeEach(func() { 26 var err error 27 dst, err = ioutil.TempDir("", "untar-test") 28 Expect(err).NotTo(HaveOccurred()) 29 }) 30 31 AfterEach(func() { 32 os.RemoveAll(dst) 33 }) 34 35 It("extracts a tar.gz to a destination", func() { 36 file, err := os.Open("testdata/normal_archive.tar.gz") 37 Expect(err).NotTo(HaveOccurred()) 38 defer file.Close() 39 err = externalbuilder.Untar(file, dst) 40 Expect(err).NotTo(HaveOccurred()) 41 }) 42 43 Context("when the archive conains an odd file type", func() { 44 It("returns an error", func() { 45 file, err := os.Open("testdata/archive_with_symlink.tar.gz") 46 defer file.Close() 47 Expect(err).NotTo(HaveOccurred()) 48 err = externalbuilder.Untar(file, dst) 49 Expect(err).To(MatchError("invalid file type '50' contained in archive for file 'c.file'")) 50 }) 51 }) 52 53 Context("when the archive contains absolute paths", func() { 54 It("returns an error", func() { 55 file, err := os.Open("testdata/archive_with_absolute.tar.gz") 56 Expect(err).NotTo(HaveOccurred()) 57 defer file.Close() 58 err = externalbuilder.Untar(file, dst) 59 Expect(err).To(MatchError("tar contains the absolute or escaping path '/home/yellickj/go/src/github.com/hyperledger/fabric/core/chaincode/externalbuilders/testdata/a/test.file'")) 60 }) 61 }) 62 63 Context("when the file's directory cannot be created", func() { 64 BeforeEach(func() { 65 ioutil.WriteFile(dst+"/a", []byte("test"), 0700) 66 }) 67 68 It("returns an error", func() { 69 file, err := os.Open("testdata/normal_archive.tar.gz") 70 Expect(err).NotTo(HaveOccurred()) 71 defer file.Close() 72 err = externalbuilder.Untar(file, dst) 73 Expect(err).To(MatchError(ContainSubstring("could not create directory 'a'"))) 74 }) 75 }) 76 77 Context("when the empty directory cannot be created", func() { 78 BeforeEach(func() { 79 ioutil.WriteFile(dst+"/d", []byte("test"), 0700) 80 }) 81 82 It("returns an error", func() { 83 file, err := os.Open("testdata/normal_archive.tar.gz") 84 Expect(err).NotTo(HaveOccurred()) 85 defer file.Close() 86 err = externalbuilder.Untar(file, dst) 87 Expect(err).To(MatchError(ContainSubstring("could not create directory 'd/'"))) 88 }) 89 }) 90 }) 91 92 Describe("ValidPath()", func() { 93 It("validates that a path is relative and a child", func() { 94 Expect(externalbuilder.ValidPath("a/simple/path")).To(BeTrue()) 95 Expect(externalbuilder.ValidPath("../path/to/parent")).To(BeFalse()) 96 Expect(externalbuilder.ValidPath("a/path/../with/intermediates")).To(BeTrue()) 97 Expect(externalbuilder.ValidPath("a/path/../../../with/toomanyintermediates")).To(BeFalse()) 98 Expect(externalbuilder.ValidPath("a/path/with/trailing/../..")).To(BeTrue()) 99 Expect(externalbuilder.ValidPath("a/path/with/toomanytrailing/../../../../..")).To(BeFalse()) 100 Expect(externalbuilder.ValidPath("..")).To(BeFalse()) 101 Expect(externalbuilder.ValidPath("../other")).To(BeFalse()) 102 Expect(externalbuilder.ValidPath("...")).To(BeTrue()) 103 Expect(externalbuilder.ValidPath("..foo")).To(BeTrue()) 104 Expect(externalbuilder.ValidPath("/an/absolute/path")).To(BeFalse()) 105 }) 106 }) 107 108 })