github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/container/externalbuilder/copy_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package externalbuilder
     8  
     9  import (
    10  	"io/ioutil"
    11  	"net"
    12  	"os"
    13  	"path/filepath"
    14  	"runtime"
    15  
    16  	"github.com/hechain20/hechain/common/flogging"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	"go.uber.org/zap"
    20  )
    21  
    22  var _ = Describe("copy", func() {
    23  	var (
    24  		logger                  *flogging.FabricLogger
    25  		tempDir                 string
    26  		srcRootDir, destRootDir string
    27  		srcSubDir, destSubDir   string
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		var err error
    32  		tempDir, err = ioutil.TempDir("", "copy-test-")
    33  		Expect(err).NotTo(HaveOccurred())
    34  
    35  		srcRootDir = filepath.Join(tempDir, "src")
    36  		err = os.Mkdir(srcRootDir, 0o755)
    37  		Expect(err).NotTo(HaveOccurred())
    38  
    39  		err = ioutil.WriteFile(filepath.Join(srcRootDir, "file-in-root.txt"), []byte("root file contents"), 0o644)
    40  		Expect(err).NotTo(HaveOccurred())
    41  
    42  		err = os.Symlink("file-in-root.txt", filepath.Join(srcRootDir, "symlink-in-root.txt"))
    43  		Expect(err).NotTo(HaveOccurred())
    44  
    45  		srcSubDir = filepath.Join(srcRootDir, "subdir")
    46  		err = os.Mkdir(srcSubDir, 0o755)
    47  		Expect(err).NotTo(HaveOccurred())
    48  
    49  		err = ioutil.WriteFile(filepath.Join(srcSubDir, "file-in-subdir.txt"), []byte("subdir file contents"), 0o644)
    50  		Expect(err).NotTo(HaveOccurred())
    51  
    52  		err = os.Symlink("file-in-subdir.txt", filepath.Join(srcSubDir, "symlink-in-subdir.txt"))
    53  		Expect(err).NotTo(HaveOccurred())
    54  		err = os.Symlink(filepath.Join("..", "file-in-root.txt"), filepath.Join(srcSubDir, "symlink-to-root.txt"))
    55  		Expect(err).NotTo(HaveOccurred())
    56  
    57  		destRootDir = filepath.Join(tempDir, "dest")
    58  		destSubDir = filepath.Join(destRootDir, "subdir")
    59  
    60  		logger = flogging.NewFabricLogger(zap.NewNop())
    61  	})
    62  
    63  	AfterEach(func() {
    64  		os.RemoveAll(tempDir)
    65  	})
    66  
    67  	It("copies all files and subdirectories", func() {
    68  		err := CopyDir(logger, srcRootDir, destRootDir)
    69  		Expect(err).NotTo(HaveOccurred())
    70  
    71  		Expect(destRootDir).To(BeADirectory())
    72  
    73  		Expect(filepath.Join(destRootDir, "file-in-root.txt")).To(BeARegularFile())
    74  		contents, err := ioutil.ReadFile(filepath.Join(destRootDir, "file-in-root.txt"))
    75  		Expect(err).NotTo(HaveOccurred())
    76  		Expect(contents).To(Equal([]byte("root file contents")))
    77  
    78  		symlink, err := os.Readlink(filepath.Join(destRootDir, "symlink-in-root.txt"))
    79  		Expect(err).NotTo(HaveOccurred())
    80  		Expect(symlink).To(Equal("file-in-root.txt"))
    81  
    82  		Expect(destSubDir).To(BeADirectory())
    83  
    84  		Expect(filepath.Join(destSubDir, "file-in-subdir.txt")).To(BeARegularFile())
    85  		contents, err = ioutil.ReadFile(filepath.Join(destSubDir, "file-in-subdir.txt"))
    86  		Expect(err).NotTo(HaveOccurred())
    87  		Expect(contents).To(Equal([]byte("subdir file contents")))
    88  
    89  		symlink, err = os.Readlink(filepath.Join(destSubDir, "symlink-in-subdir.txt"))
    90  		Expect(err).NotTo(HaveOccurred())
    91  		Expect(symlink).To(Equal("file-in-subdir.txt"))
    92  
    93  		symlink, err = os.Readlink(filepath.Join(destSubDir, "symlink-to-root.txt"))
    94  		Expect(err).NotTo(HaveOccurred())
    95  		Expect(symlink).To(Equal(filepath.Join("..", "file-in-root.txt")))
    96  	})
    97  
    98  	When("source contains an absolute symlink", func() {
    99  		It("returns an error and removes the destination directory", func() {
   100  			err := os.Symlink("/somewhere/else", filepath.Join(srcRootDir, "absolute-symlink.txt"))
   101  			Expect(err).NotTo(HaveOccurred())
   102  
   103  			err = CopyDir(logger, srcRootDir, destRootDir)
   104  			Expect(err).To(MatchError(ContainSubstring("refusing to copy absolute symlink")))
   105  
   106  			Expect(destRootDir).NotTo(BeAnExistingFile())
   107  		})
   108  	})
   109  
   110  	When("source contains a relative symlink that points outside of the tree", func() {
   111  		It("returns an error and removes the destination directory", func() {
   112  			err := os.Symlink("../somewhere/else", filepath.Join(srcRootDir, "relative-symlink-outside.txt"))
   113  			Expect(err).NotTo(HaveOccurred())
   114  
   115  			err = CopyDir(logger, srcRootDir, destRootDir)
   116  			Expect(err).To(MatchError(ContainSubstring("refusing to copy symlink")))
   117  
   118  			Expect(destRootDir).NotTo(BeAnExistingFile())
   119  		})
   120  	})
   121  
   122  	When("source contains a relative symlink that goes out and back into the tree", func() {
   123  		It("returns an error and removes the destination directory", func() {
   124  			srcRootName := filepath.Dir(srcRootDir)
   125  			err := os.Symlink(filepath.Join("..", srcRootName, "file-in-root.txt"), filepath.Join(srcRootDir, "relative-symlink-outside-and-inside.txt"))
   126  			Expect(err).NotTo(HaveOccurred())
   127  
   128  			err = CopyDir(logger, srcRootDir, destRootDir)
   129  			Expect(err).To(MatchError(ContainSubstring("refusing to copy symlink")))
   130  
   131  			Expect(destRootDir).NotTo(BeAnExistingFile())
   132  		})
   133  	})
   134  
   135  	When("source contains a relative symlink that points outside of the tree", func() {
   136  		It("returns an error and removes the destination directory", func() {
   137  			err := os.Symlink("../somewhere/else", filepath.Join(srcRootDir, "relative-symlink-outside.txt"))
   138  			Expect(err).NotTo(HaveOccurred())
   139  
   140  			err = CopyDir(logger, srcRootDir, destRootDir)
   141  			Expect(err).To(MatchError(ContainSubstring("refusing to copy symlink")))
   142  
   143  			Expect(destRootDir).NotTo(BeAnExistingFile())
   144  		})
   145  	})
   146  
   147  	When("source contains a file other than a regular file, directory, or symlink", func() {
   148  		It("returns an error and removes the destination directory", func() {
   149  			if runtime.GOOS == "windows" {
   150  				Skip("test not supported on Windows")
   151  			}
   152  
   153  			socket, err := net.Listen("unix", filepath.Join(srcRootDir, "uds-in-root.txt"))
   154  			Expect(err).NotTo(HaveOccurred())
   155  			defer socket.Close()
   156  
   157  			err = CopyDir(logger, srcRootDir, destRootDir)
   158  			Expect(err).To(MatchError(ContainSubstring("refusing to copy unsupported file")))
   159  
   160  			Expect(destRootDir).NotTo(BeAnExistingFile())
   161  		})
   162  	})
   163  })