Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: common/archive/ar/ar_test.go

Issue 2043623002: luci-go: Tools for working with BSD style ar archives. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Fixing for maruel's review. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | common/archive/ar/doc.go » ('j') | common/archive/ar/reader.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package ar
6
7 import (
8 "bytes"
9 "io/ioutil"
10 "os"
11 "os/exec"
12 "path"
13 "strings"
14 "testing"
15 "time"
16
17 "github.com/maruel/ut"
18 )
19
20 var (
21 TestFile1 = ("" +
22 // ar file header
23 "!<arch>\n" +
24 // filename len - 16 bytes
25 "#1/9 " +
26 // modtime - 12 bytes
27 "1447140471 " +
28 // owner id - 6 bytes
29 "1000 " +
30 // group id - 6 bytes
31 "1000 " +
32 // file mode - 8 bytes
33 "100640 " +
34 // Data size - 10 bytes
35 "15 " +
36 // File magic - 2 bytes
37 "\x60\n" +
38 // File name - 9 bytes
39 "filename1" +
40 // File data - 6 bytes
41 "abc123" +
42 // Padding - 1 byte
43 "\n" +
44 "")
45 )
46
47 func TestWriterCreatesTestFile1(t *testing.T) {
48 b := &bytes.Buffer{}
49 data := []byte("abc123")
50
51 ar := NewWriter(b)
52 if err := ar.Add("filename1", data); err != nil {
53 t.Fatalf("Add: %v", err)
54 }
55 if err := ar.Close(); err != nil {
56 t.Fatalf("Close: %v", err)
57 }
58
59 ut.AssertEqual(t, []byte(TestFile1), b.Bytes())
60 }
61
62 func TestReaderOnTestFile1(t *testing.T) {
63 r := strings.NewReader(TestFile1)
64
65 ar, err := NewReader(r)
66 if err != nil {
67 t.Fatalf("NewReader: %v", err)
68 }
69
70 h, err := ar.Next()
71 if err != nil {
72 t.Fatalf("Header: %v", err)
73 }
74
75 ut.AssertEqual(t, time.Unix(1447140471, 0), h.ModTime())
76 ut.AssertEqual(t, 1000, h.UserId())
77 ut.AssertEqual(t, 1000, h.GroupId())
78 ut.AssertEqual(t, "filename1", h.Name())
79 ut.AssertEqual(t, int64(6), h.Size())
80
81 data := make([]byte, 6)
82 n, err := ar.Read(data)
83 if err != nil {
84 t.Fatalf("Data: %v", err)
85 }
86 ut.AssertEqual(t, 6, n)
87 ut.AssertEqual(t, []byte("abc123"), data)
88
89 if err := ar.Close(); err != nil {
90 t.Fatalf("Close: %v", err)
91 }
92 }
93
94 func TestWithSystemArCommandList(t *testing.T) {
95 if _, err := exec.LookPath("ar"); err != nil {
96 t.Skipf("ar command not found: %v", err)
97 }
98
99 // Write out to an archive file
100 tmpfile, err := ioutil.TempFile("", "go-ar-test.")
101 if err != nil {
102 t.Fatalf("unable to create temp file: %v", err)
103 }
104 defer os.Remove(tmpfile.Name()) // clean up
105 ar := NewWriter(tmpfile)
106 ar.Add("file1.txt", []byte("file1 contents"))
107 ar.Add("file2.txt", []byte("file2 contents"))
108 ar.Add("dir1/file3.txt", []byte("file3 contents"))
109 ar.Close()
110
111 // Use the ar command to list the file
112 cmd_list := exec.Command("ar", "t", tmpfile.Name())
113 var cmd_list_out_buf bytes.Buffer
114 cmd_list.Stdout = &cmd_list_out_buf
115 if err := cmd_list.Run(); err != nil {
116 t.Fatalf("ar command failed: %v\n%s", err, cmd_list_out_buf.Stri ng())
117 }
118
119 cmd_list_actual_out := cmd_list_out_buf.String()
120 cmd_list_expect_out := `file1.txt
121 file2.txt
122 dir1/file3.txt
123 `
124 ut.AssertEqual(t, cmd_list_expect_out, cmd_list_actual_out)
125 }
126
127 func TestWithSystemArCommandExtract(t *testing.T) {
128 arpath, err := exec.LookPath("ar")
129 if err != nil {
130 t.Skipf("ar command not found: %v", err)
131 }
132
133 // Write out to an archive file
134 tmpfile, err := ioutil.TempFile("", "go-ar-test.")
135 if err != nil {
136 t.Fatalf("unable to create temp file: %v", err)
137 }
138 defer os.Remove(tmpfile.Name()) // clean up
139 ar := NewWriter(tmpfile)
140 ar.Add("file1.txt", []byte("file1 contents"))
141 ar.Add("file2.txt", []byte("file2 contents"))
142 ar.Close()
143
144 // Extract the ar
145 tmpdir, err := ioutil.TempDir("", "go-ar-test.")
146 defer os.RemoveAll(tmpdir)
147 cmd_extract := exec.Cmd{
148 Path: arpath,
149 Args: []string{"ar", "x", tmpfile.Name()},
150 Dir: tmpdir,
151 }
152 var cmd_extract_out_buf bytes.Buffer
153 cmd_extract.Stdout = &cmd_extract_out_buf
154 if err := cmd_extract.Run(); err != nil {
155 t.Fatalf("ar command failed: %v\n%s", err, cmd_extract_out_buf.S tring())
156 }
157
158 // Compare the directory output
159 dir_contents, err := ioutil.ReadDir(tmpdir)
160 if err != nil {
161 t.Fatalf("Unable to read the output directory: %v", err)
162 }
163 for _, fi := range dir_contents {
164 if fi.Name() != "file1.txt" && fi.Name() != "file2.txt" {
165 t.Errorf("Found unexpected file '%s'", fi.Name())
166 }
167 }
168
169 file1_contents, err := ioutil.ReadFile(path.Join(tmpdir, "file1.txt"))
170 file1_expected := []byte("file1 contents")
171 if err != nil {
172 t.Errorf("%v", err)
173 } else {
174 if bytes.Compare(file1_contents, file1_expected) != 0 {
175 t.Errorf("file1.txt content incorrect. Got:\n%v\n%v\n", file1_contents, file1_expected)
176 }
177 }
178
179 file2_contents, err := ioutil.ReadFile(path.Join(tmpdir, "file2.txt"))
180 file2_expected := []byte("file2 contents")
181 if err != nil {
182 t.Errorf("%v", err)
183 } else {
184 if bytes.Compare(file2_contents, file2_expected) != 0 {
185 t.Errorf("file2.txt content incorrect. Got:\n%v\n%v\n", file2_contents, file2_expected)
186 }
187 }
188 }
OLDNEW
« no previous file with comments | « no previous file | common/archive/ar/doc.go » ('j') | common/archive/ar/reader.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698