~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/encoding/base64/base64_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
        "errors"
10
10
        "io"
11
11
        "io/ioutil"
 
12
        "strings"
12
13
        "testing"
13
14
        "time"
14
15
)
142
143
}
143
144
 
144
145
func TestDecodeCorrupt(t *testing.T) {
145
 
        type corrupt struct {
146
 
                e string
147
 
                p int
148
 
        }
149
 
        examples := []corrupt{
 
146
        testCases := []struct {
 
147
                input  string
 
148
                offset int // -1 means no corruption.
 
149
        }{
 
150
                {"", -1},
150
151
                {"!!!!", 0},
151
152
                {"x===", 1},
152
153
                {"AA=A", 2},
154
155
                {"AAAAA", 4},
155
156
                {"AAAAAA", 4},
156
157
                {"A=", 1},
 
158
                {"A==", 1},
157
159
                {"AA=", 3},
 
160
                {"AA==", -1},
 
161
                {"AAA=", -1},
 
162
                {"AAAA", -1},
158
163
                {"AAAAAA=", 7},
159
164
        }
160
 
 
161
 
        for _, e := range examples {
162
 
                dbuf := make([]byte, StdEncoding.DecodedLen(len(e.e)))
163
 
                _, err := StdEncoding.Decode(dbuf, []byte(e.e))
 
165
        for _, tc := range testCases {
 
166
                dbuf := make([]byte, StdEncoding.DecodedLen(len(tc.input)))
 
167
                _, err := StdEncoding.Decode(dbuf, []byte(tc.input))
 
168
                if tc.offset == -1 {
 
169
                        if err != nil {
 
170
                                t.Error("Decoder wrongly detected coruption in", tc.input)
 
171
                        }
 
172
                        continue
 
173
                }
164
174
                switch err := err.(type) {
165
175
                case CorruptInputError:
166
 
                        testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p)
 
176
                        testEqual(t, "Corruption in %q at offset %v, want %v", tc.input, int(err), tc.offset)
167
177
                default:
168
 
                        t.Error("Decoder failed to detect corruption in", e)
 
178
                        t.Error("Decoder failed to detect corruption in", tc)
169
179
                }
170
180
        }
171
181
}
216
226
                "c3V\nyZ\rQ==",
217
227
                "c3VyZ\nQ==",
218
228
                "c3VyZQ\n==",
 
229
                "c3VyZQ=\n=",
 
230
                "c3VyZQ=\r\n\r\n=",
219
231
        }
220
232
        for _, e := range examples {
221
233
                buf, err := StdEncoding.DecodeString(e)
257
269
        wantErr := errors.New("my error")
258
270
        next <- nextRead{5, nil}
259
271
        next <- nextRead{10, wantErr}
 
272
        next <- nextRead{0, wantErr}
260
273
        d := NewDecoder(StdEncoding, &faultInjectReader{
261
274
                source: "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==", // twas brillig...
262
275
                nextc:  next,
275
288
                t.Errorf("timeout; Decoder blocked without returning an error")
276
289
        }
277
290
}
 
291
 
 
292
func TestDecoderIssue4779(t *testing.T) {
 
293
        encoded := `CP/EAT8AAAEF
 
294
AQEBAQEBAAAAAAAAAAMAAQIEBQYHCAkKCwEAAQUBAQEBAQEAAAAAAAAAAQACAwQFBgcICQoLEAAB
 
295
BAEDAgQCBQcGCAUDDDMBAAIRAwQhEjEFQVFhEyJxgTIGFJGhsUIjJBVSwWIzNHKC0UMHJZJT8OHx
 
296
Y3M1FqKygyZEk1RkRcKjdDYX0lXiZfKzhMPTdePzRieUpIW0lcTU5PSltcXV5fVWZnaGlqa2xtbm
 
297
9jdHV2d3h5ent8fX5/cRAAICAQIEBAMEBQYHBwYFNQEAAhEDITESBEFRYXEiEwUygZEUobFCI8FS
 
298
0fAzJGLhcoKSQ1MVY3M08SUGFqKygwcmNcLSRJNUoxdkRVU2dGXi8rOEw9N14/NGlKSFtJXE1OT0
 
299
pbXF1eX1VmZ2hpamtsbW5vYnN0dXZ3eHl6e3x//aAAwDAQACEQMRAD8A9VSSSSUpJJJJSkkkJ+Tj
 
300
1kiy1jCJJDnAcCTykpKkuQ6p/jN6FgmxlNduXawwAzaGH+V6jn/R/wCt71zdn+N/qL3kVYFNYB4N
 
301
ji6PDVjWpKp9TSXnvTf8bFNjg3qOEa2n6VlLpj/rT/pf567DpX1i6L1hs9Py67X8mqdtg/rUWbbf
 
302
+gkp0kkkklKSSSSUpJJJJT//0PVUkkklKVLq3WMDpGI7KzrNjADtYNXvI/Mqr/Pd/q9W3vaxjnvM
 
303
NaCXE9gNSvGPrf8AWS3qmba5jjsJhoB0DAf0NDf6sevf+/lf8Hj0JJATfWT6/dV6oXU1uOLQeKKn
 
304
EQP+Hubtfe/+R7Mf/g7f5xcocp++Z11JMCJPgFBxOg7/AOuqDx8I/ikpkXkmSdU8mJIJA/O8EMAy
 
305
j+mSARB/17pKVXYWHXjsj7yIex0PadzXMO1zT5KHoNA3HT8ietoGhgjsfA+CSnvvqh/jJtqsrwOv
 
306
2b6NGNzXfTYexzJ+nU7/ALkf4P8Awv6P9KvTQQ4AgyDqCF85Pho3CTB7eHwXoH+LT65uZbX9X+o2
 
307
bqbPb06551Y4
 
308
`
 
309
        encodedShort := strings.Replace(encoded, "\n", "", -1)
 
310
 
 
311
        dec := NewDecoder(StdEncoding, bytes.NewBufferString(encoded))
 
312
        res1, err := ioutil.ReadAll(dec)
 
313
        if err != nil {
 
314
                t.Errorf("ReadAll failed: %v", err)
 
315
        }
 
316
 
 
317
        dec = NewDecoder(StdEncoding, bytes.NewBufferString(encodedShort))
 
318
        var res2 []byte
 
319
        res2, err = ioutil.ReadAll(dec)
 
320
        if err != nil {
 
321
                t.Errorf("ReadAll failed: %v", err)
 
322
        }
 
323
 
 
324
        if !bytes.Equal(res1, res2) {
 
325
                t.Error("Decoded results not equal")
 
326
        }
 
327
}