OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [archive/] [tar/] [writer_test.go] - Blame information for rev 833

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go 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 tar
6
 
7
import (
8
        "bytes"
9
        "fmt"
10
        "io"
11
        "io/ioutil"
12
        "testing"
13
        "testing/iotest"
14
        "time"
15
)
16
 
17
type writerTestEntry struct {
18
        header   *Header
19
        contents string
20
}
21
 
22
type writerTest struct {
23
        file    string // filename of expected output
24
        entries []*writerTestEntry
25
}
26
 
27
var writerTests = []*writerTest{
28
        // The writer test file was produced with this command:
29
        // tar (GNU tar) 1.26
30
        //   ln -s small.txt link.txt
31
        //   tar -b 1 --format=ustar -c -f writer.tar small.txt small2.txt link.txt
32
        {
33
                file: "testdata/writer.tar",
34
                entries: []*writerTestEntry{
35
                        {
36
                                header: &Header{
37
                                        Name:     "small.txt",
38
                                        Mode:     0640,
39
                                        Uid:      73025,
40
                                        Gid:      5000,
41
                                        Size:     5,
42
                                        ModTime:  time.Unix(1246508266, 0),
43
                                        Typeflag: '0',
44
                                        Uname:    "dsymonds",
45
                                        Gname:    "eng",
46
                                },
47
                                contents: "Kilts",
48
                        },
49
                        {
50
                                header: &Header{
51
                                        Name:     "small2.txt",
52
                                        Mode:     0640,
53
                                        Uid:      73025,
54
                                        Gid:      5000,
55
                                        Size:     11,
56
                                        ModTime:  time.Unix(1245217492, 0),
57
                                        Typeflag: '0',
58
                                        Uname:    "dsymonds",
59
                                        Gname:    "eng",
60
                                },
61
                                contents: "Google.com\n",
62
                        },
63
                        {
64
                                header: &Header{
65
                                        Name:     "link.txt",
66
                                        Mode:     0777,
67
                                        Uid:      1000,
68
                                        Gid:      1000,
69
                                        Size:     0,
70
                                        ModTime:  time.Unix(1314603082, 0),
71
                                        Typeflag: '2',
72
                                        Linkname: "small.txt",
73
                                        Uname:    "strings",
74
                                        Gname:    "strings",
75
                                },
76
                                // no contents
77
                        },
78
                },
79
        },
80
        // The truncated test file was produced using these commands:
81
        //   dd if=/dev/zero bs=1048576 count=16384 > /tmp/16gig.txt
82
        //   tar -b 1 -c -f- /tmp/16gig.txt | dd bs=512 count=8 > writer-big.tar
83
        {
84
                file: "testdata/writer-big.tar",
85
                entries: []*writerTestEntry{
86
                        {
87
                                header: &Header{
88
                                        Name:     "tmp/16gig.txt",
89
                                        Mode:     0640,
90
                                        Uid:      73025,
91
                                        Gid:      5000,
92
                                        Size:     16 << 30,
93
                                        ModTime:  time.Unix(1254699560, 0),
94
                                        Typeflag: '0',
95
                                        Uname:    "dsymonds",
96
                                        Gname:    "eng",
97
                                },
98
                                // no contents
99
                        },
100
                },
101
        },
102
}
103
 
104
// Render byte array in a two-character hexadecimal string, spaced for easy visual inspection.
105
func bytestr(offset int, b []byte) string {
106
        const rowLen = 32
107
        s := fmt.Sprintf("%04x ", offset)
108
        for _, ch := range b {
109
                switch {
110
                case '0' <= ch && ch <= '9', 'A' <= ch && ch <= 'Z', 'a' <= ch && ch <= 'z':
111
                        s += fmt.Sprintf("  %c", ch)
112
                default:
113
                        s += fmt.Sprintf(" %02x", ch)
114
                }
115
        }
116
        return s
117
}
118
 
119
// Render a pseudo-diff between two blocks of bytes.
120
func bytediff(a []byte, b []byte) string {
121
        const rowLen = 32
122
        s := fmt.Sprintf("(%d bytes vs. %d bytes)\n", len(a), len(b))
123
        for offset := 0; len(a)+len(b) > 0; offset += rowLen {
124
                na, nb := rowLen, rowLen
125
                if na > len(a) {
126
                        na = len(a)
127
                }
128
                if nb > len(b) {
129
                        nb = len(b)
130
                }
131
                sa := bytestr(offset, a[0:na])
132
                sb := bytestr(offset, b[0:nb])
133
                if sa != sb {
134
                        s += fmt.Sprintf("-%v\n+%v\n", sa, sb)
135
                }
136
                a = a[na:]
137
                b = b[nb:]
138
        }
139
        return s
140
}
141
 
142
func TestWriter(t *testing.T) {
143
testLoop:
144
        for i, test := range writerTests {
145
                expected, err := ioutil.ReadFile(test.file)
146
                if err != nil {
147
                        t.Errorf("test %d: Unexpected error: %v", i, err)
148
                        continue
149
                }
150
 
151
                buf := new(bytes.Buffer)
152
                tw := NewWriter(iotest.TruncateWriter(buf, 4<<10)) // only catch the first 4 KB
153
                for j, entry := range test.entries {
154
                        if err := tw.WriteHeader(entry.header); err != nil {
155
                                t.Errorf("test %d, entry %d: Failed writing header: %v", i, j, err)
156
                                continue testLoop
157
                        }
158
                        if _, err := io.WriteString(tw, entry.contents); err != nil {
159
                                t.Errorf("test %d, entry %d: Failed writing contents: %v", i, j, err)
160
                                continue testLoop
161
                        }
162
                }
163
                if err := tw.Close(); err != nil {
164
                        t.Errorf("test %d: Failed closing archive: %v", i, err)
165
                        continue testLoop
166
                }
167
 
168
                actual := buf.Bytes()
169
                if !bytes.Equal(expected, actual) {
170
                        t.Errorf("test %d: Incorrect result: (-=expected, +=actual)\n%v",
171
                                i, bytediff(expected, actual))
172
                }
173
                if testing.Short() { // The second test is expensive.
174
                        break
175
                }
176
        }
177
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.