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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [base64/] [base64_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 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 base64
6
 
7
import (
8
        "bytes"
9
        "io"
10
        "io/ioutil"
11
        "testing"
12
)
13
 
14
type testpair struct {
15
        decoded, encoded string
16
}
17
 
18
var pairs = []testpair{
19
        // RFC 3548 examples
20
        {"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
21
        {"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
22
        {"\x14\xfb\x9c\x03", "FPucAw=="},
23
 
24
        // RFC 4648 examples
25
        {"", ""},
26
        {"f", "Zg=="},
27
        {"fo", "Zm8="},
28
        {"foo", "Zm9v"},
29
        {"foob", "Zm9vYg=="},
30
        {"fooba", "Zm9vYmE="},
31
        {"foobar", "Zm9vYmFy"},
32
 
33
        // Wikipedia examples
34
        {"sure.", "c3VyZS4="},
35
        {"sure", "c3VyZQ=="},
36
        {"sur", "c3Vy"},
37
        {"su", "c3U="},
38
        {"leasure.", "bGVhc3VyZS4="},
39
        {"easure.", "ZWFzdXJlLg=="},
40
        {"asure.", "YXN1cmUu"},
41
        {"sure.", "c3VyZS4="},
42
}
43
 
44
var bigtest = testpair{
45
        "Twas brillig, and the slithy toves",
46
        "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
47
}
48
 
49
func testEqual(t *testing.T, msg string, args ...interface{}) bool {
50
        if args[len(args)-2] != args[len(args)-1] {
51
                t.Errorf(msg, args...)
52
                return false
53
        }
54
        return true
55
}
56
 
57
func TestEncode(t *testing.T) {
58
        for _, p := range pairs {
59
                got := StdEncoding.EncodeToString([]byte(p.decoded))
60
                testEqual(t, "Encode(%q) = %q, want %q", p.decoded, got, p.encoded)
61
        }
62
}
63
 
64
func TestEncoder(t *testing.T) {
65
        for _, p := range pairs {
66
                bb := &bytes.Buffer{}
67
                encoder := NewEncoder(StdEncoding, bb)
68
                encoder.Write([]byte(p.decoded))
69
                encoder.Close()
70
                testEqual(t, "Encode(%q) = %q, want %q", p.decoded, bb.String(), p.encoded)
71
        }
72
}
73
 
74
func TestEncoderBuffering(t *testing.T) {
75
        input := []byte(bigtest.decoded)
76
        for bs := 1; bs <= 12; bs++ {
77
                bb := &bytes.Buffer{}
78
                encoder := NewEncoder(StdEncoding, bb)
79
                for pos := 0; pos < len(input); pos += bs {
80
                        end := pos + bs
81
                        if end > len(input) {
82
                                end = len(input)
83
                        }
84
                        n, err := encoder.Write(input[pos:end])
85
                        testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, error(nil))
86
                        testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos)
87
                }
88
                err := encoder.Close()
89
                testEqual(t, "Close gave error %v, want %v", err, error(nil))
90
                testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, bb.String(), bigtest.encoded)
91
        }
92
}
93
 
94
func TestDecode(t *testing.T) {
95
        for _, p := range pairs {
96
                dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)))
97
                count, end, err := StdEncoding.decode(dbuf, []byte(p.encoded))
98
                testEqual(t, "Decode(%q) = error %v, want %v", p.encoded, err, error(nil))
99
                testEqual(t, "Decode(%q) = length %v, want %v", p.encoded, count, len(p.decoded))
100
                if len(p.encoded) > 0 {
101
                        testEqual(t, "Decode(%q) = end %v, want %v", p.encoded, end, (p.encoded[len(p.encoded)-1] == '='))
102
                }
103
                testEqual(t, "Decode(%q) = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded)
104
 
105
                dbuf, err = StdEncoding.DecodeString(p.encoded)
106
                testEqual(t, "DecodeString(%q) = error %v, want %v", p.encoded, err, error(nil))
107
                testEqual(t, "DecodeString(%q) = %q, want %q", string(dbuf), p.decoded)
108
        }
109
}
110
 
111
func TestDecoder(t *testing.T) {
112
        for _, p := range pairs {
113
                decoder := NewDecoder(StdEncoding, bytes.NewBufferString(p.encoded))
114
                dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)))
115
                count, err := decoder.Read(dbuf)
116
                if err != nil && err != io.EOF {
117
                        t.Fatal("Read failed", err)
118
                }
119
                testEqual(t, "Read from %q = length %v, want %v", p.encoded, count, len(p.decoded))
120
                testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded)
121
                if err != io.EOF {
122
                        count, err = decoder.Read(dbuf)
123
                }
124
                testEqual(t, "Read from %q = %v, want %v", p.encoded, err, io.EOF)
125
        }
126
}
127
 
128
func TestDecoderBuffering(t *testing.T) {
129
        for bs := 1; bs <= 12; bs++ {
130
                decoder := NewDecoder(StdEncoding, bytes.NewBufferString(bigtest.encoded))
131
                buf := make([]byte, len(bigtest.decoded)+12)
132
                var total int
133
                for total = 0; total < len(bigtest.decoded); {
134
                        n, err := decoder.Read(buf[total : total+bs])
135
                        testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, error(nil))
136
                        total += n
137
                }
138
                testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded)
139
        }
140
}
141
 
142
func TestDecodeCorrupt(t *testing.T) {
143
        type corrupt struct {
144
                e string
145
                p int
146
        }
147
        examples := []corrupt{
148
                {"!!!!", 0},
149
                {"x===", 1},
150
                {"AA=A", 2},
151
                {"AAA=AAAA", 3},
152
                {"AAAAA", 4},
153
                {"AAAAAA", 4},
154
        }
155
 
156
        for _, e := range examples {
157
                dbuf := make([]byte, StdEncoding.DecodedLen(len(e.e)))
158
                _, err := StdEncoding.Decode(dbuf, []byte(e.e))
159
                switch err := err.(type) {
160
                case CorruptInputError:
161
                        testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p)
162
                default:
163
                        t.Error("Decoder failed to detect corruption in", e)
164
                }
165
        }
166
}
167
 
168
func TestBig(t *testing.T) {
169
        n := 3*1000 + 1
170
        raw := make([]byte, n)
171
        const alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
172
        for i := 0; i < n; i++ {
173
                raw[i] = alpha[i%len(alpha)]
174
        }
175
        encoded := new(bytes.Buffer)
176
        w := NewEncoder(StdEncoding, encoded)
177
        nn, err := w.Write(raw)
178
        if nn != n || err != nil {
179
                t.Fatalf("Encoder.Write(raw) = %d, %v want %d, nil", nn, err, n)
180
        }
181
        err = w.Close()
182
        if err != nil {
183
                t.Fatalf("Encoder.Close() = %v want nil", err)
184
        }
185
        decoded, err := ioutil.ReadAll(NewDecoder(StdEncoding, encoded))
186
        if err != nil {
187
                t.Fatalf("ioutil.ReadAll(NewDecoder(...)): %v", err)
188
        }
189
 
190
        if !bytes.Equal(raw, decoded) {
191
                var i int
192
                for i = 0; i < len(decoded) && i < len(raw); i++ {
193
                        if decoded[i] != raw[i] {
194
                                break
195
                        }
196
                }
197
                t.Errorf("Decode(Encode(%d-byte string)) failed at offset %d", n, i)
198
        }
199
}
200
 
201
func TestNewLineCharacters(t *testing.T) {
202
        // Each of these should decode to the string "sure", without errors.
203
        const expected = "sure"
204
        examples := []string{
205
                "c3VyZQ==",
206
                "c3VyZQ==\r",
207
                "c3VyZQ==\n",
208
                "c3VyZQ==\r\n",
209
                "c3VyZ\r\nQ==",
210
                "c3V\ryZ\nQ==",
211
                "c3V\nyZ\rQ==",
212
                "c3VyZ\nQ==",
213
                "c3VyZQ\n==",
214
        }
215
        for _, e := range examples {
216
                buf, err := StdEncoding.DecodeString(e)
217
                if err != nil {
218
                        t.Errorf("Decode(%q) failed: %v", e, err)
219
                        continue
220
                }
221
                if s := string(buf); s != expected {
222
                        t.Errorf("Decode(%q) = %q, want %q", e, s, expected)
223
                }
224
        }
225
}

powered by: WebSVN 2.1.0

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