OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package tar | 5 package tar |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "io" | 10 "io" |
11 "io/ioutil" | 11 "io/ioutil" |
| 12 "os" |
12 "strings" | 13 "strings" |
13 "testing" | 14 "testing" |
14 "testing/iotest" | 15 "testing/iotest" |
15 "time" | 16 "time" |
16 ) | 17 ) |
17 | 18 |
18 type writerTestEntry struct { | 19 type writerTestEntry struct { |
19 header *Header | 20 header *Header |
20 contents string | 21 contents string |
21 } | 22 } |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 actual := buf.Bytes() | 174 actual := buf.Bytes() |
174 if !bytes.Equal(expected, actual) { | 175 if !bytes.Equal(expected, actual) { |
175 t.Errorf("test %d: Incorrect result: (-=expected, +=actu
al)\n%v", | 176 t.Errorf("test %d: Incorrect result: (-=expected, +=actu
al)\n%v", |
176 i, bytediff(expected, actual)) | 177 i, bytediff(expected, actual)) |
177 } | 178 } |
178 if testing.Short() { // The second test is expensive. | 179 if testing.Short() { // The second test is expensive. |
179 break | 180 break |
180 } | 181 } |
181 } | 182 } |
182 } | 183 } |
| 184 |
| 185 func TestPax(t *testing.T) { |
| 186 // Create an archive with a large name |
| 187 fileinfo, err := os.Stat("testdata/small.txt") |
| 188 if err != nil { |
| 189 t.Fatal(err) |
| 190 } |
| 191 hdr, err := FileInfoHeader(fileinfo, "") |
| 192 if err != nil { |
| 193 t.Fatalf("os.Stat: %v", err) |
| 194 } |
| 195 // Force a PAX long name to be written |
| 196 longName := strings.Repeat("ab", 100) |
| 197 contents := strings.Repeat(" ", int(hdr.Size)) |
| 198 hdr.Name = longName |
| 199 var buf bytes.Buffer |
| 200 writer := NewWriter(&buf) |
| 201 if err := writer.WriteHeader(hdr); err != nil { |
| 202 t.Fatal(err) |
| 203 } |
| 204 if _, err = writer.Write([]byte(contents)); err != nil { |
| 205 t.Fatal(err) |
| 206 } |
| 207 if err := writer.Close(); err != nil { |
| 208 t.Fatal(err) |
| 209 } |
| 210 // Simple test to make sure PAX extensions are in effect |
| 211 if !bytes.Contains(buf.Bytes(), []byte("PaxHeaders.")) { |
| 212 t.Fatal("Expected at least one PAX header to be written.") |
| 213 } |
| 214 // Test that we can get a long name back out of the archive. |
| 215 reader := NewReader(&buf) |
| 216 hdr, err = reader.Next() |
| 217 if err != nil { |
| 218 t.Fatal(err) |
| 219 } |
| 220 if hdr.Name != longName { |
| 221 t.Fatal("Couldn't recover long file name") |
| 222 } |
| 223 } |
| 224 |
| 225 func aTestPAXHeader(t *testing.T) { |
| 226 medName := strings.Repeat("CD", 50) |
| 227 longName := strings.Repeat("AB", 100) |
| 228 paxTests := [][2]string{ |
| 229 {"name=/etc/hosts", "19 name=/etc/hosts\n"}, |
| 230 {"a=b", "6 a=b\n"}, // Single digit length |
| 231 {"a=names", "11 a=names\n"}, // Test case involving carry |
| 232 {"a=name", "10 a=name\n"}, // Test case involving multiple acc
eptable lengths |
| 233 {"a=name", "9 a=name\n"}, // Test case involving multiple acc
eptable lengths |
| 234 {"name=" + longName, fmt.Sprintf("210 name=%s\n", longName)}, |
| 235 {"name=" + medName, fmt.Sprintf("110 name=%s\n", medName)}} |
| 236 |
| 237 for _, test := range paxTests { |
| 238 key, expected := test[0], test[1] |
| 239 if result := paxHeader(key); result != expected { |
| 240 t.Fatalf("paxHeader: got %s, expected %s", result, expec
ted) |
| 241 } |
| 242 } |
| 243 } |
OLD | NEW |