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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 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 zip
6
 
7
import (
8
        "bytes"
9
        "io/ioutil"
10
        "math/rand"
11
        "os"
12
        "testing"
13
)
14
 
15
// TODO(adg): a more sophisticated test suite
16
 
17
type WriteTest struct {
18
        Name   string
19
        Data   []byte
20
        Method uint16
21
        Mode   os.FileMode
22
}
23
 
24
var writeTests = []WriteTest{
25
        {
26
                Name:   "foo",
27
                Data:   []byte("Rabbits, guinea pigs, gophers, marsupial rats, and quolls."),
28
                Method: Store,
29
                Mode:   0666,
30
        },
31
        {
32
                Name:   "bar",
33
                Data:   nil, // large data set in the test
34
                Method: Deflate,
35
                Mode:   0644,
36
        },
37
        {
38
                Name:   "setuid",
39
                Data:   []byte("setuid file"),
40
                Method: Deflate,
41
                Mode:   0755 | os.ModeSetuid,
42
        },
43
        {
44
                Name:   "setgid",
45
                Data:   []byte("setgid file"),
46
                Method: Deflate,
47
                Mode:   0755 | os.ModeSetgid,
48
        },
49
        {
50
                Name:   "symlink",
51
                Data:   []byte("../link/target"),
52
                Method: Deflate,
53
                Mode:   0755 | os.ModeSymlink,
54
        },
55
}
56
 
57
func TestWriter(t *testing.T) {
58
        largeData := make([]byte, 1<<17)
59
        for i := range largeData {
60
                largeData[i] = byte(rand.Int())
61
        }
62
        writeTests[1].Data = largeData
63
        defer func() {
64
                writeTests[1].Data = nil
65
        }()
66
 
67
        // write a zip file
68
        buf := new(bytes.Buffer)
69
        w := NewWriter(buf)
70
 
71
        for _, wt := range writeTests {
72
                testCreate(t, w, &wt)
73
        }
74
 
75
        if err := w.Close(); err != nil {
76
                t.Fatal(err)
77
        }
78
 
79
        // read it back
80
        r, err := NewReader(sliceReaderAt(buf.Bytes()), int64(buf.Len()))
81
        if err != nil {
82
                t.Fatal(err)
83
        }
84
        for i, wt := range writeTests {
85
                testReadFile(t, r.File[i], &wt)
86
        }
87
}
88
 
89
func testCreate(t *testing.T, w *Writer, wt *WriteTest) {
90
        header := &FileHeader{
91
                Name:   wt.Name,
92
                Method: wt.Method,
93
        }
94
        if wt.Mode != 0 {
95
                header.SetMode(wt.Mode)
96
        }
97
        f, err := w.CreateHeader(header)
98
        if err != nil {
99
                t.Fatal(err)
100
        }
101
        _, err = f.Write(wt.Data)
102
        if err != nil {
103
                t.Fatal(err)
104
        }
105
}
106
 
107
func testReadFile(t *testing.T, f *File, wt *WriteTest) {
108
        if f.Name != wt.Name {
109
                t.Fatalf("File name: got %q, want %q", f.Name, wt.Name)
110
        }
111
        testFileMode(t, f, wt.Mode)
112
        rc, err := f.Open()
113
        if err != nil {
114
                t.Fatal("opening:", err)
115
        }
116
        b, err := ioutil.ReadAll(rc)
117
        if err != nil {
118
                t.Fatal("reading:", err)
119
        }
120
        err = rc.Close()
121
        if err != nil {
122
                t.Fatal("closing:", err)
123
        }
124
        if !bytes.Equal(b, wt.Data) {
125
                t.Errorf("File contents %q, want %q", b, wt.Data)
126
        }
127
}

powered by: WebSVN 2.1.0

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