Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(794)

Delta Between Two Patch Sets: src/pkg/archive/tar/writer_test.go

Issue 6700047: code review 6700047: archive/tar: read/write extended pax/gnu tar archives (Closed)
Left Patch Set: diff -r 93dc7f0e302b https://code.google.com/p/go Created 11 years, 5 months ago
Right Patch Set: diff -r 439cb8bad388 https://code.google.com/p/go Created 11 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/archive/tar/writer.go ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 Uid: 73025, 92 Uid: 73025,
93 Gid: 5000, 93 Gid: 5000,
94 Size: 16 << 30, 94 Size: 16 << 30,
95 ModTime: time.Unix(1254699560, 0), 95 ModTime: time.Unix(1254699560, 0),
96 Typeflag: '0', 96 Typeflag: '0',
97 Uname: "dsymonds", 97 Uname: "dsymonds",
98 Gname: "eng", 98 Gname: "eng",
99 }, 99 },
100 // fake contents 100 // fake contents
101 contents: strings.Repeat("\x00", 4<<10), 101 contents: strings.Repeat("\x00", 4<<10),
102 },
103 },
104 },
105 // This file was produced using gnu tar 1.17
106 // gnutar -b 4 --format=ustar (longname/)*15 + file.txt
107 {
108 file: "testdata/ustar.tar",
109 entries: []*writerTestEntry{
110 {
111 header: &Header{
112 Name: strings.Repeat("longname/", 15 ) + "file.txt",
113 Mode: 0644,
114 Uid: 0765,
115 Gid: 024,
116 Size: 06,
117 ModTime: time.Unix(1360135598, 0),
118 Typeflag: '0',
119 Uname: "shane",
120 Gname: "staff",
121 },
122 contents: "hello\n",
102 }, 123 },
103 }, 124 },
104 }, 125 },
105 } 126 }
106 127
107 // Render byte array in a two-character hexadecimal string, spaced for easy visu al inspection. 128 // Render byte array in a two-character hexadecimal string, spaced for easy visu al inspection.
108 func bytestr(offset int, b []byte) string { 129 func bytestr(offset int, b []byte) string {
109 const rowLen = 32 130 const rowLen = 32
110 s := fmt.Sprintf("%04x ", offset) 131 s := fmt.Sprintf("%04x ", offset)
111 for _, ch := range b { 132 for _, ch := range b {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 } 204 }
184 205
185 func TestPax(t *testing.T) { 206 func TestPax(t *testing.T) {
186 // Create an archive with a large name 207 // Create an archive with a large name
187 fileinfo, err := os.Stat("testdata/small.txt") 208 fileinfo, err := os.Stat("testdata/small.txt")
188 if err != nil { 209 if err != nil {
189 t.Fatal(err) 210 t.Fatal(err)
190 } 211 }
191 hdr, err := FileInfoHeader(fileinfo, "") 212 hdr, err := FileInfoHeader(fileinfo, "")
192 if err != nil { 213 if err != nil {
193 » » t.Fatal("os.Stat: " + err.Error()) 214 » » t.Fatalf("os.Stat: %v", err)
dsymonds 2012/10/31 12:16:25 t.Fatalf("os.Stat: %v", err)
shanemhansen 2012/11/01 02:40:57 Done.
194 } 215 }
195 // Force a PAX long name to be written 216 // Force a PAX long name to be written
196 » long_name := strings.Repeat("ab", 100) 217 » longName := strings.Repeat("ab", 100)
dsymonds 2012/10/31 12:16:25 long_name -> longName
shanemhansen 2012/11/01 02:40:57 Done.
197 contents := strings.Repeat(" ", int(hdr.Size)) 218 contents := strings.Repeat(" ", int(hdr.Size))
198 » hdr.Name = long_name 219 » hdr.Name = longName
199 var buf bytes.Buffer 220 var buf bytes.Buffer
200 writer := NewWriter(&buf) 221 writer := NewWriter(&buf)
201 if err := writer.WriteHeader(hdr); err != nil { 222 if err := writer.WriteHeader(hdr); err != nil {
202 t.Fatal(err) 223 t.Fatal(err)
203 } 224 }
204 if _, err = writer.Write([]byte(contents)); err != nil { 225 if _, err = writer.Write([]byte(contents)); err != nil {
205 t.Fatal(err) 226 t.Fatal(err)
206 } 227 }
207 if err := writer.Close(); err != nil { 228 if err := writer.Close(); err != nil {
208 t.Fatal(err) 229 t.Fatal(err)
209 } 230 }
210 // Simple test to make sure PAX extensions are in effect 231 // Simple test to make sure PAX extensions are in effect
211 if !bytes.Contains(buf.Bytes(), []byte("PaxHeaders.")) { 232 if !bytes.Contains(buf.Bytes(), []byte("PaxHeaders.")) {
212 t.Fatal("Expected at least one PAX header to be written.") 233 t.Fatal("Expected at least one PAX header to be written.")
213 } 234 }
214 // Test that we can get a long name back out of the archive. 235 // Test that we can get a long name back out of the archive.
215 reader := NewReader(&buf) 236 reader := NewReader(&buf)
216 hdr, err = reader.Next() 237 hdr, err = reader.Next()
217 if err != nil { 238 if err != nil {
218 t.Fatal(err) 239 t.Fatal(err)
219 } 240 }
220 » if !(hdr.Name == long_name) { 241 » if hdr.Name != longName {
dsymonds 2012/10/31 12:16:25 if hdr.Name != longName
shanemhansen 2012/11/01 02:40:57 Done.
221 t.Fatal("Couldn't recover long file name") 242 t.Fatal("Couldn't recover long file name")
222 } 243 }
223 } 244 }
245
246 func TestPAXHeader(t *testing.T) {
247 medName := strings.Repeat("CD", 50)
248 longName := strings.Repeat("AB", 100)
249 paxTests := [][2]string{
250 {"name=/etc/hosts", "19 name=/etc/hosts\n"},
251 {"a=b", "6 a=b\n"}, // Single digit length
252 {"a=names", "11 a=names\n"}, // Test case involving carries
253 {"name=" + longName, fmt.Sprintf("210 name=%s\n", longName)},
254 {"name=" + medName, fmt.Sprintf("110 name=%s\n", medName)}}
255
256 for _, test := range paxTests {
257 key, expected := test[0], test[1]
258 if result := paxHeader(key); result != expected {
259 t.Fatalf("paxHeader: got %s, expected %s", result, expec ted)
260 }
261 }
262 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b