github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/tarutil/tar_test.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package tarutil 6 7 import ( 8 "io/ioutil" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "testing" 13 ) 14 15 func extractAndCompare(t *testing.T, files []struct{ name, body string }) { 16 f, err := os.Open("test.tar") 17 if err != nil { 18 t.Fatal(err) 19 } 20 defer f.Close() 21 22 tmpDir := "tartest" 23 if err := ExtractDir(f, tmpDir); err != nil { 24 t.Fatal(err) 25 } 26 27 for _, f := range files { 28 body, err := ioutil.ReadFile(filepath.Join(tmpDir, f.name)) 29 if err != nil { 30 t.Errorf("could not read %s: %v", f.name, err) 31 continue 32 } 33 if string(body) != f.body { 34 t.Errorf("for file %s, got %q, want %q", 35 f.name, string(body), f.body) 36 } 37 } 38 } 39 40 func TestExtractDir(t *testing.T) { 41 tmpDir, err := ioutil.TempDir("", "tartest") 42 if err != nil { 43 t.Fatal(err) 44 } 45 defer os.RemoveAll(tmpDir) 46 47 var files = []struct { 48 name, body string 49 }{ 50 {"a.txt", "hello\n"}, 51 {"dir/b.txt", "world\n"}, 52 } 53 extractAndCompare(t, files) 54 } 55 56 func TestExtractDirNotExist(t *testing.T) { 57 tmpDir := "tartest" 58 defer os.RemoveAll(tmpDir) // ExtractDir should have created dir 59 60 var files = []struct { 61 name, body string 62 }{ 63 {"a.txt", "hello\n"}, 64 {"dir/b.txt", "world\n"}, 65 } 66 extractAndCompare(t, files) 67 } 68 69 func TestCreateTarSingleFile(t *testing.T) { 70 tmpDir, err := ioutil.TempDir("", "tartest") 71 if err != nil { 72 t.Fatal(err) 73 } 74 defer os.RemoveAll(tmpDir) 75 76 filename := filepath.Join(tmpDir, "test.tar") 77 f, err := os.Create(filename) 78 if err != nil { 79 t.Fatal(err) 80 } 81 defer f.Close() 82 83 if err := CreateTar(f, []string{"test0"}); err != nil { 84 t.Fatal(err) 85 } 86 87 out, err := exec.Command("tar", "-tf", filename).CombinedOutput() 88 if err != nil { 89 t.Fatalf("system tar could not parse the file: %v", err) 90 } 91 expected := `test0 92 test0/a.txt 93 test0/dir 94 test0/dir/b.txt 95 ` 96 if string(out) != expected { 97 t.Fatalf("got %q, want %q", string(out), expected) 98 } 99 } 100 101 func TestCreateTarMultFiles(t *testing.T) { 102 tmpDir, err := ioutil.TempDir("", "tartest") 103 if err != nil { 104 t.Fatal(err) 105 } 106 defer os.RemoveAll(tmpDir) 107 108 filename := filepath.Join(tmpDir, "test.tar") 109 f, err := os.Create(filename) 110 if err != nil { 111 t.Fatal(err) 112 } 113 defer f.Close() 114 115 files := []string{"test0", "test1", "test2.txt"} 116 if err := CreateTar(f, files); err != nil { 117 t.Fatal(err) 118 } 119 120 out, err := exec.Command("tar", "-tf", filename).CombinedOutput() 121 if err != nil { 122 t.Fatalf("system tar could not parse the file: %v", err) 123 } 124 expected := `test0 125 test0/a.txt 126 test0/dir 127 test0/dir/b.txt 128 test1 129 test1/a1.txt 130 test2.txt 131 ` 132 if string(out) != expected { 133 t.Fatalf("got %q, want %q", string(out), expected) 134 } 135 } 136 137 func TestListArchive(t *testing.T) { 138 f, err := os.Open("test.tar") 139 if err != nil { 140 t.Fatal(err) 141 } 142 defer f.Close() 143 144 if err := ListArchive(f); err != nil { 145 t.Fatal(err) 146 } 147 }