Commit c3a087a0 authored by Austin Clements's avatar Austin Clements

Base64 encoder/decoder package.

R=rsc
APPROVED=rsc
DELTA=722  (722 added, 0 deleted, 0 changed)
OCL=30660
CL=30691
parent e6ff6c8e
archive/tar.install: bufio.install bytes.install io.install os.install strconv.install
base64.install: io.install os.install strconv.install
bignum.install: fmt.install
bufio.install: io.install os.install utf8.install
bytes.install: utf8.install
......
......@@ -13,6 +13,7 @@ all: install
DIRS=\
archive/tar\
base64\
bignum\
bufio\
bytes\
......@@ -68,6 +69,7 @@ DIRS=\
TEST=\
archive/tar\
base64\
bignum\
bufio\
bytes\
......
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# DO NOT EDIT. Automatically generated by gobuild.
# gobuild -m >Makefile
D=
include $(GOROOT)/src/Make.$(GOARCH)
AR=gopack
default: packages
clean:
rm -rf *.[$(OS)] *.a [$(OS)].out _obj
test: packages
gotest
coverage: packages
gotest
6cov -g $$(pwd) | grep -v '_test\.go:'
%.$O: %.go
$(GC) -I_obj $*.go
%.$O: %.c
$(CC) $*.c
%.$O: %.s
$(AS) $*.s
O1=\
base64.$O\
phases: a1
_obj$D/base64.a: phases
a1: $(O1)
$(AR) grc _obj$D/base64.a base64.$O
rm -f $(O1)
newpkg: clean
mkdir -p _obj$D
$(AR) grc _obj$D/base64.a
$(O1): newpkg
$(O2): a1
nuke: clean
rm -f $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D/base64.a
packages: _obj$D/base64.a
install: packages
test -d $(GOROOT)/pkg && mkdir -p $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D
cp _obj$D/base64.a $(GOROOT)/pkg/$(GOOS)_$(GOARCH)$D/base64.a
This diff is collapsed.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package base64
import (
"base64";
"io";
"os";
"reflect";
"testing";
)
type testpair struct {
decoded, encoded string;
}
var pairs = []testpair {
// RFC 3548 examples
testpair{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
testpair{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
testpair{"\x14\xfb\x9c\x03", "FPucAw=="},
// RFC 4648 examples
testpair{"", ""},
testpair{"f", "Zg=="},
testpair{"fo", "Zm8="},
testpair{"foo", "Zm9v"},
testpair{"foob", "Zm9vYg=="},
testpair{"fooba", "Zm9vYmE="},
testpair{"foobar", "Zm9vYmFy"},
// Wikipedia examples
testpair{"sure.", "c3VyZS4="},
testpair{"sure", "c3VyZQ=="},
testpair{"sur", "c3Vy"},
testpair{"su", "c3U="},
testpair{"leasure.", "bGVhc3VyZS4="},
testpair{"easure.", "ZWFzdXJlLg=="},
testpair{"asure.", "YXN1cmUu"},
testpair{"sure.", "c3VyZS4="},
}
var bigtest = testpair {
"Twas brillig, and the slithy toves",
"VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw=="
}
func testEqual(t *testing.T, msg string, args ...) bool {
v := reflect.NewValue(args).(reflect.StructValue);
v1 := v.Field(v.Len() - 2);
v2 := v.Field(v.Len() - 1);
if v1.Interface() != v2.Interface() {
t.Errorf(msg, args);
return false;
}
return true;
}
func TestEncode(t *testing.T) {
for _, p := range pairs {
buf := make([]byte, StdEncoding.EncodedLen(len(p.decoded)));
StdEncoding.Encode(io.StringBytes(p.decoded), buf);
testEqual(t, "Encode(%q) = %q, want %q", p.decoded, string(buf), p.encoded);
}
}
func TestEncoder(t *testing.T) {
for _, p := range pairs {
bb := &io.ByteBuffer{};
encoder := NewEncoder(StdEncoding, bb);
encoder.Write(io.StringBytes(p.decoded));
encoder.Close();
testEqual(t, "Encode(%q) = %q, want %q", p.decoded, string(bb.Data()), p.encoded);
}
}
func TestEncoderBuffering(t *testing.T) {
input := io.StringBytes(bigtest.decoded);
for bs := 1; bs <= 12; bs++ {
buf := make([]byte, bs);
bb := &io.ByteBuffer{};
encoder := NewEncoder(StdEncoding, bb);
for pos := 0; pos < len(input); pos += bs {
end := pos+bs;
if end > len(input) {
end = len(input);
}
n, err := encoder.Write(input[pos:end]);
testEqual(t, "Write(%q) gave error %v, want %v", input[pos:end], err, os.Error(nil));
testEqual(t, "Write(%q) gave length %v, want %v", input[pos:end], n, end-pos);
}
err := encoder.Close();
testEqual(t, "Close gave error %v, want %v", err, os.Error(nil));
testEqual(t, "Encoding/%d of %q = %q, want %q", bs, bigtest.decoded, string(bb.Data()), bigtest.encoded);
}
}
func TestDecode(t *testing.T) {
for _, p := range pairs {
dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)));
count, end, err := StdEncoding.decode(io.StringBytes(p.encoded), dbuf);
testEqual(t, "Decode(%q) = error %v, want %v", p.encoded, err, os.Error(nil));
testEqual(t, "Decode(%q) = length %v, want %v", p.encoded, count, len(p.decoded));
if len(p.encoded) > 0 {
testEqual(t, "Decode(%q) = end %v, want %v", p.encoded, end, (p.encoded[len(p.encoded)-1] == '='));
}
testEqual(t, "Decode(%q) = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded);
}
}
func TestDecoder(t *testing.T) {
for _, p := range pairs {
decoder := NewDecoder(StdEncoding, io.NewByteReader(io.StringBytes(p.encoded)));
dbuf := make([]byte, StdEncoding.DecodedLen(len(p.encoded)));
count, err := decoder.Read(dbuf);
if err != nil && err != os.EOF {
t.Fatal("Read failed", err);
}
testEqual(t, "Read from %q = length %v, want %v", p.encoded, count, len(p.decoded));
testEqual(t, "Decoding of %q = %q, want %q", p.encoded, string(dbuf[0:count]), p.decoded);
if err != os.EOF {
count, err = decoder.Read(dbuf);
}
testEqual(t, "Read from %q = %v, want %v", p.encoded, err, os.EOF);
}
}
func TestDecoderBuffering(t *testing.T) {
input := io.StringBytes(bigtest.encoded);
for bs := 1; bs <= 12; bs++ {
decoder := NewDecoder(StdEncoding, io.NewByteReader(input));
buf := make([]byte, len(bigtest.decoded) + 12);
var total int;
for total = 0; total < len(bigtest.decoded); {
n, err := decoder.Read(buf[total:total+bs]);
testEqual(t, "Read from %q at pos %d = %d, %v, want _, %v", bigtest.encoded, total, n, err, os.Error(nil));
total += n;
}
testEqual(t, "Decoding/%d of %q = %q, want %q", bs, bigtest.encoded, string(buf[0:total]), bigtest.decoded);
}
}
func TestDecodeCorrupt(t *testing.T) {
type corrupt struct {
e string;
p int;
};
examples := []corrupt {
corrupt{"!!!!", 0},
corrupt{"x===", 1},
corrupt{"AA=A", 2},
corrupt{"AAA=AAAA", 3},
corrupt{"AAAAA", 4},
corrupt{"AAAAAA", 4},
};
for _, e := range examples {
dbuf := make([]byte, StdEncoding.DecodedLen(len(e.e)));
count, err := StdEncoding.Decode(io.StringBytes(e.e), dbuf);
switch err := err.(type) {
case CorruptInputError:
testEqual(t, "Corruption in %q at offset %v, want %v", e.e, int(err), e.p);
default:
t.Error("Decoder failed to detect corruption in", e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment