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

Subversion Repositories openrisc

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

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
        "crypto/md5"
10
        "fmt"
11
        "io"
12
        "os"
13
        "testing"
14
        "time"
15
)
16
 
17
type untarTest struct {
18
        file    string
19
        headers []*Header
20
        cksums  []string
21
}
22
 
23
var gnuTarTest = &untarTest{
24
        file: "testdata/gnu.tar",
25
        headers: []*Header{
26
                {
27
                        Name:     "small.txt",
28
                        Mode:     0640,
29
                        Uid:      73025,
30
                        Gid:      5000,
31
                        Size:     5,
32
                        ModTime:  time.Unix(1244428340, 0),
33
                        Typeflag: '0',
34
                        Uname:    "dsymonds",
35
                        Gname:    "eng",
36
                },
37
                {
38
                        Name:     "small2.txt",
39
                        Mode:     0640,
40
                        Uid:      73025,
41
                        Gid:      5000,
42
                        Size:     11,
43
                        ModTime:  time.Unix(1244436044, 0),
44
                        Typeflag: '0',
45
                        Uname:    "dsymonds",
46
                        Gname:    "eng",
47
                },
48
        },
49
        cksums: []string{
50
                "e38b27eaccb4391bdec553a7f3ae6b2f",
51
                "c65bd2e50a56a2138bf1716f2fd56fe9",
52
        },
53
}
54
 
55
var untarTests = []*untarTest{
56
        gnuTarTest,
57
        {
58
                file: "testdata/star.tar",
59
                headers: []*Header{
60
                        {
61
                                Name:       "small.txt",
62
                                Mode:       0640,
63
                                Uid:        73025,
64
                                Gid:        5000,
65
                                Size:       5,
66
                                ModTime:    time.Unix(1244592783, 0),
67
                                Typeflag:   '0',
68
                                Uname:      "dsymonds",
69
                                Gname:      "eng",
70
                                AccessTime: time.Unix(1244592783, 0),
71
                                ChangeTime: time.Unix(1244592783, 0),
72
                        },
73
                        {
74
                                Name:       "small2.txt",
75
                                Mode:       0640,
76
                                Uid:        73025,
77
                                Gid:        5000,
78
                                Size:       11,
79
                                ModTime:    time.Unix(1244592783, 0),
80
                                Typeflag:   '0',
81
                                Uname:      "dsymonds",
82
                                Gname:      "eng",
83
                                AccessTime: time.Unix(1244592783, 0),
84
                                ChangeTime: time.Unix(1244592783, 0),
85
                        },
86
                },
87
        },
88
        {
89
                file: "testdata/v7.tar",
90
                headers: []*Header{
91
                        {
92
                                Name:     "small.txt",
93
                                Mode:     0444,
94
                                Uid:      73025,
95
                                Gid:      5000,
96
                                Size:     5,
97
                                ModTime:  time.Unix(1244593104, 0),
98
                                Typeflag: '\x00',
99
                        },
100
                        {
101
                                Name:     "small2.txt",
102
                                Mode:     0444,
103
                                Uid:      73025,
104
                                Gid:      5000,
105
                                Size:     11,
106
                                ModTime:  time.Unix(1244593104, 0),
107
                                Typeflag: '\x00',
108
                        },
109
                },
110
        },
111
}
112
 
113
func TestReader(t *testing.T) {
114
testLoop:
115
        for i, test := range untarTests {
116
                f, err := os.Open(test.file)
117
                if err != nil {
118
                        t.Errorf("test %d: Unexpected error: %v", i, err)
119
                        continue
120
                }
121
                tr := NewReader(f)
122
                for j, header := range test.headers {
123
                        hdr, err := tr.Next()
124
                        if err != nil || hdr == nil {
125
                                t.Errorf("test %d, entry %d: Didn't get entry: %v", i, j, err)
126
                                f.Close()
127
                                continue testLoop
128
                        }
129
                        if *hdr != *header {
130
                                t.Errorf("test %d, entry %d: Incorrect header:\nhave %+v\nwant %+v",
131
                                        i, j, *hdr, *header)
132
                        }
133
                }
134
                hdr, err := tr.Next()
135
                if err == io.EOF {
136
                        break
137
                }
138
                if hdr != nil || err != nil {
139
                        t.Errorf("test %d: Unexpected entry or error: hdr=%v err=%v", i, hdr, err)
140
                }
141
                f.Close()
142
        }
143
}
144
 
145
func TestPartialRead(t *testing.T) {
146
        f, err := os.Open("testdata/gnu.tar")
147
        if err != nil {
148
                t.Fatalf("Unexpected error: %v", err)
149
        }
150
        defer f.Close()
151
 
152
        tr := NewReader(f)
153
 
154
        // Read the first four bytes; Next() should skip the last byte.
155
        hdr, err := tr.Next()
156
        if err != nil || hdr == nil {
157
                t.Fatalf("Didn't get first file: %v", err)
158
        }
159
        buf := make([]byte, 4)
160
        if _, err := io.ReadFull(tr, buf); err != nil {
161
                t.Fatalf("Unexpected error: %v", err)
162
        }
163
        if expected := []byte("Kilt"); !bytes.Equal(buf, expected) {
164
                t.Errorf("Contents = %v, want %v", buf, expected)
165
        }
166
 
167
        // Second file
168
        hdr, err = tr.Next()
169
        if err != nil || hdr == nil {
170
                t.Fatalf("Didn't get second file: %v", err)
171
        }
172
        buf = make([]byte, 6)
173
        if _, err := io.ReadFull(tr, buf); err != nil {
174
                t.Fatalf("Unexpected error: %v", err)
175
        }
176
        if expected := []byte("Google"); !bytes.Equal(buf, expected) {
177
                t.Errorf("Contents = %v, want %v", buf, expected)
178
        }
179
}
180
 
181
func TestIncrementalRead(t *testing.T) {
182
        test := gnuTarTest
183
        f, err := os.Open(test.file)
184
        if err != nil {
185
                t.Fatalf("Unexpected error: %v", err)
186
        }
187
        defer f.Close()
188
 
189
        tr := NewReader(f)
190
 
191
        headers := test.headers
192
        cksums := test.cksums
193
        nread := 0
194
 
195
        // loop over all files
196
        for ; ; nread++ {
197
                hdr, err := tr.Next()
198
                if hdr == nil || err == io.EOF {
199
                        break
200
                }
201
 
202
                // check the header
203
                if *hdr != *headers[nread] {
204
                        t.Errorf("Incorrect header:\nhave %+v\nwant %+v",
205
                                *hdr, headers[nread])
206
                }
207
 
208
                // read file contents in little chunks EOF,
209
                // checksumming all the way
210
                h := md5.New()
211
                rdbuf := make([]uint8, 8)
212
                for {
213
                        nr, err := tr.Read(rdbuf)
214
                        if err == io.EOF {
215
                                break
216
                        }
217
                        if err != nil {
218
                                t.Errorf("Read: unexpected error %v\n", err)
219
                                break
220
                        }
221
                        h.Write(rdbuf[0:nr])
222
                }
223
                // verify checksum
224
                have := fmt.Sprintf("%x", h.Sum(nil))
225
                want := cksums[nread]
226
                if want != have {
227
                        t.Errorf("Bad checksum on file %s:\nhave %+v\nwant %+v", hdr.Name, have, want)
228
                }
229
        }
230
        if nread != len(headers) {
231
                t.Errorf("Didn't process all files\nexpected: %d\nprocessed %d\n", len(headers), nread)
232
        }
233
}
234
 
235
func TestNonSeekable(t *testing.T) {
236
        test := gnuTarTest
237
        f, err := os.Open(test.file)
238
        if err != nil {
239
                t.Fatalf("Unexpected error: %v", err)
240
        }
241
        defer f.Close()
242
 
243
        type readerOnly struct {
244
                io.Reader
245
        }
246
        tr := NewReader(readerOnly{f})
247
        nread := 0
248
 
249
        for ; ; nread++ {
250
                _, err := tr.Next()
251
                if err == io.EOF {
252
                        break
253
                }
254
                if err != nil {
255
                        t.Fatalf("Unexpected error: %v", err)
256
                }
257
        }
258
 
259
        if nread != len(test.headers) {
260
                t.Errorf("Didn't process all files\nexpected: %d\nprocessed %d\n", len(test.headers), nread)
261
        }
262
}

powered by: WebSVN 2.1.0

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