github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/core/container/util/writer_test.go (about) 1 /* 2 Copyright London Stock Exchange 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "archive/tar" 21 "bytes" 22 "compress/gzip" 23 "fmt" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 "testing" 28 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func Test_WriteFileToPackage(t *testing.T) { 33 buf := bytes.NewBuffer(nil) 34 gw := gzip.NewWriter(buf) 35 tw := tar.NewWriter(gw) 36 err := WriteFileToPackage("blah", "", tw) 37 assert.Error(t, err, "Expected error writing non existent file to package") 38 39 // Create a file and write it to tar writer 40 filename := "test.txt" 41 filecontent := "hello" 42 filepath := os.TempDir() + filename 43 err = ioutil.WriteFile(filepath, bytes.NewBufferString(filecontent).Bytes(), 0600) 44 assert.NoError(t, err, "Error creating file %s", filepath) 45 defer os.Remove(filepath) 46 47 err = WriteFileToPackage(filepath, filename, tw) 48 assert.NoError(t, err, "Error returned by WriteFileToPackage while writing existing file") 49 tw.Close() 50 gw.Close() 51 52 // Read the file from the archive and check the name and file content 53 r := bytes.NewReader(buf.Bytes()) 54 gr, err1 := gzip.NewReader(r) 55 assert.NoError(t, err1, "Error creating a gzip reader") 56 tr := tar.NewReader(gr) 57 header, err2 := tr.Next() 58 assert.NoError(t, err2, "Error getting the file from the tar") 59 assert.Equal(t, filename, header.Name, 60 "Name of the file read from the archive is not same as the file added to the archive") 61 62 b := make([]byte, 5) 63 _, err3 := tr.Read(b) 64 assert.NoError(t, err3, "Error reading file from the archive") 65 assert.Equal(t, filecontent, bytes.NewBuffer(b).String(), 66 "file content from the archive is not same as original file content") 67 68 // tar writer is closed. Call WriteFileToPackage again, this should 69 // return an error 70 err = WriteFileToPackage(filepath, "", tw) 71 fmt.Println(err) 72 assert.Error(t, err, "Expected error writing using a closed writer") 73 } 74 75 func Test_WriteStreamToPackage(t *testing.T) { 76 tarw := tar.NewWriter(bytes.NewBuffer(nil)) 77 input := bytes.NewReader([]byte("hello")) 78 79 // Error case 1 80 err := WriteStreamToPackage(nil, "/nonexistentpath", "", tarw) 81 assert.Error(t, err, "Expected error getting info of non existent file") 82 83 // Error case 2 84 err = WriteStreamToPackage(input, os.TempDir(), "", tarw) 85 assert.Error(t, err, "Expected error copying to the tar writer (tarw)") 86 87 tarw.Close() 88 89 // Error case 3 90 err = WriteStreamToPackage(input, os.TempDir(), "", tarw) 91 assert.Error(t, err, "Expected error copying to closed tar writer (tarw)") 92 93 // Success case 94 buf := bytes.NewBuffer(nil) 95 gw := gzip.NewWriter(buf) 96 tw := tar.NewWriter(gw) 97 98 // Create a file and write it to tar writer 99 filename := "test.txt" 100 filecontent := "hello" 101 filepath := os.TempDir() + filename 102 err = ioutil.WriteFile(filepath, bytes.NewBufferString(filecontent).Bytes(), 0600) 103 assert.NoError(t, err, "Error creating file %s", filepath) 104 defer os.Remove(filepath) 105 106 // Read the file into a stream 107 var b []byte 108 b, err = ioutil.ReadFile(filepath) 109 assert.NoError(t, err, "Error reading file %s", filepath) 110 is := bytes.NewReader(b) 111 112 // Write contents of the file stream to tar writer 113 err = WriteStreamToPackage(is, filepath, filename, tw) 114 assert.NoError(t, err, "Error copying file to the tar writer (tw)") 115 116 // Close the writers 117 tw.Close() 118 gw.Close() 119 120 // Read the file from the archive and check the name and file content 121 br := bytes.NewReader(buf.Bytes()) 122 gr, err1 := gzip.NewReader(br) 123 assert.NoError(t, err1, "Error creating a gzip reader") 124 tr := tar.NewReader(gr) 125 header, err2 := tr.Next() 126 assert.NoError(t, err2, "Error getting the file from the tar") 127 assert.Equal(t, filename, header.Name, 128 "Name of the file read from the archive is not same as the file added to the archive") 129 130 b1 := make([]byte, 5) 131 _, err3 := tr.Read(b1) 132 assert.NoError(t, err3, "Error reading file from the archive") 133 assert.Equal(t, filecontent, bytes.NewBuffer(b1).String(), 134 "file content from the archive is not same as original file content") 135 } 136 137 func Test_WriteFolderToPackage(t *testing.T) { 138 buf := bytes.NewBuffer(nil) 139 gw := gzip.NewWriter(buf) 140 tw := tar.NewWriter(gw) 141 142 // Success case 1: with include and exclude file types and without exclude dir 143 gopath := os.Getenv("GOPATH") 144 gopath = filepath.SplitList(gopath)[0] 145 srcPath := filepath.Join(gopath, "src", 146 "github.com/hyperledger/fabric/examples/chaincode/java/SimpleSample") 147 filePath := "src/src/main/java/example/SimpleSample.java" 148 includeFileTypes := map[string]bool{ 149 ".java": true, 150 } 151 excludeFileTypes := map[string]bool{ 152 ".xml": true, 153 } 154 155 err := WriteFolderToTarPackage(tw, srcPath, "", 156 includeFileTypes, excludeFileTypes) 157 assert.NoError(t, err, "Error writing folder to package") 158 159 tw.Close() 160 gw.Close() 161 162 // Read the file from the archive and check the name 163 br := bytes.NewReader(buf.Bytes()) 164 gr, err1 := gzip.NewReader(br) 165 assert.NoError(t, err1, "Error creating a gzip reader") 166 tr := tar.NewReader(gr) 167 header, err2 := tr.Next() 168 assert.NoError(t, err2, "Error getting the file from the tar") 169 assert.Equal(t, filePath, header.Name, 170 "Name of the file read from the archive is not same as the file added to the archive") 171 172 // Success case 2: with exclude dir and no include file types 173 srcPath = filepath.Join(gopath, "src", 174 "github.com/hyperledger/fabric/examples/chaincode/java") 175 tarw := tar.NewWriter(bytes.NewBuffer(nil)) 176 defer tarw.Close() 177 err = WriteFolderToTarPackage(tarw, srcPath, "SimpleSample", 178 nil, excludeFileTypes) 179 assert.NoError(t, err, "Error writing folder to package") 180 } 181 182 func Test_WriteJavaProjectToPackage(t *testing.T) { 183 inputbuf := bytes.NewBuffer(nil) 184 gw := gzip.NewWriter(inputbuf) 185 tw := tar.NewWriter(gw) 186 187 gopath := os.Getenv("GOPATH") 188 gopath = filepath.SplitList(gopath)[0] 189 pkgDir := filepath.Join(gopath, "src", 190 "github.com/hyperledger/fabric/examples/chaincode/java") 191 err := WriteJavaProjectToPackage(tw, pkgDir) 192 assert.NoError(t, err, "Error writing java project to package") 193 194 // Close the tar writer and call WriteFileToPackage again, this should 195 // return an error 196 tw.Close() 197 gw.Close() 198 err = WriteJavaProjectToPackage(tw, pkgDir) 199 assert.Error(t, err, "WriteJavaProjectToPackage was called with closed writer, should have failed") 200 } 201 202 func Test_WriteBytesToPackage(t *testing.T) { 203 inputbuf := bytes.NewBuffer(nil) 204 tw := tar.NewWriter(inputbuf) 205 defer tw.Close() 206 err := WriteBytesToPackage("foo", []byte("blah"), tw) 207 assert.NoError(t, err, "Error writing bytes to package") 208 }