github.com/lzy4123/fabric@v2.1.1+incompatible/common/ledger/testutil/test_util.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package testutil 8 9 import ( 10 "archive/tar" 11 "archive/zip" 12 "bytes" 13 "crypto/rand" 14 "io" 15 "os" 16 "path/filepath" 17 "strings" 18 "testing" 19 ) 20 21 // ConstructRandomBytes constructs random bytes of given size 22 func ConstructRandomBytes(t testing.TB, size int) []byte { 23 value := make([]byte, size) 24 _, err := rand.Read(value) 25 if err != nil { 26 t.Fatalf("Error while generating random bytes: %s", err) 27 } 28 return value 29 } 30 31 // TarFileEntry is a structure for adding test index files to an tar 32 type TarFileEntry struct { 33 Name, Body string 34 } 35 36 // CreateTarBytesForTest creates a tar byte array for unit testing 37 func CreateTarBytesForTest(testFiles []*TarFileEntry) []byte { 38 //Create a buffer for the tar file 39 buffer := new(bytes.Buffer) 40 tarWriter := tar.NewWriter(buffer) 41 42 for _, file := range testFiles { 43 tarHeader := &tar.Header{ 44 Name: file.Name, 45 Mode: 0600, 46 Size: int64(len(file.Body)), 47 } 48 err := tarWriter.WriteHeader(tarHeader) 49 if err != nil { 50 return nil 51 } 52 _, err = tarWriter.Write([]byte(file.Body)) 53 if err != nil { 54 return nil 55 } 56 } 57 // Make sure to check the error on Close. 58 tarWriter.Close() 59 return buffer.Bytes() 60 } 61 62 // CopyDir creates a copy of a dir 63 func CopyDir(srcroot, destroot string, copyOnlySubdirs bool) error { 64 if !copyOnlySubdirs { 65 _, lastSegment := filepath.Split(srcroot) 66 destroot = filepath.Join(destroot, lastSegment) 67 } 68 69 walkFunc := func(srcpath string, info os.FileInfo, errDummy error) error { 70 srcsubpath, err := filepath.Rel(srcroot, srcpath) 71 if err != nil { 72 return err 73 } 74 destpath := filepath.Join(destroot, srcsubpath) 75 76 if info.IsDir() { // its a dir, make corresponding dir in the dest 77 if err = os.MkdirAll(destpath, info.Mode()); err != nil { 78 return err 79 } 80 return nil 81 } 82 83 // its a file, copy to corresponding path in the dest 84 if err = copyFile(srcpath, destpath); err != nil { 85 return err 86 } 87 return nil 88 } 89 90 return filepath.Walk(srcroot, walkFunc) 91 } 92 93 func copyFile(srcpath, destpath string) error { 94 var srcFile, destFile *os.File 95 var err error 96 if srcFile, err = os.Open(srcpath); err != nil { 97 return err 98 } 99 if destFile, err = os.Create(destpath); err != nil { 100 return err 101 } 102 if _, err = io.Copy(destFile, srcFile); err != nil { 103 return err 104 } 105 if err = srcFile.Close(); err != nil { 106 return err 107 } 108 if err = destFile.Close(); err != nil { 109 return err 110 } 111 return nil 112 } 113 114 // Unzip will decompress the src zip file to the dest directory. 115 // If createTopLevelDirInZip is true, it creates the top level dir when unzipped. 116 // Otherwise, it trims off the top level dir when unzipped. For example, ledersData/historydb/abc will become historydb/abc. 117 func Unzip(src string, dest string, createTopLevelDirInZip bool) error { 118 r, err := zip.OpenReader(src) 119 if err != nil { 120 return err 121 } 122 defer r.Close() 123 124 // iterate all the dirs and files in the zip file 125 for _, file := range r.File { 126 filePath := file.Name 127 if !createTopLevelDirInZip { 128 // trim off the top level dir - for example, trim ledgersData/historydb/abc to historydb/abc 129 index := strings.Index(filePath, string(filepath.Separator)) 130 filePath = filePath[index+1:] 131 } 132 133 fullPath := filepath.Join(dest, filePath) 134 if file.FileInfo().IsDir() { 135 os.MkdirAll(fullPath, os.ModePerm) 136 continue 137 } 138 if err = os.MkdirAll(filepath.Dir(fullPath), os.ModePerm); err != nil { 139 return err 140 } 141 outFile, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) 142 if err != nil { 143 return err 144 } 145 rc, err := file.Open() 146 if err != nil { 147 return err 148 } 149 _, err = io.Copy(outFile, rc) 150 151 outFile.Close() 152 rc.Close() 153 154 if err != nil { 155 return err 156 } 157 } 158 return nil 159 }