protobuf.js/tests/base64.js
2016-12-11 00:01:14 +01:00

41 lines
1.3 KiB
JavaScript

var tape = require("tape");
var protobuf = require("..");
tape.test("base64", function(test) {
var strings = {
"": "",
"a": "YQ==",
"ab": "YWI=",
"abcdefg": "YWJjZGVmZw==",
"abcdefgh": "YWJjZGVmZ2g=",
"abcdefghi": "YWJjZGVmZ2hp"
};
Object.keys(strings).forEach(function(str) {
var enc = strings[str];
var len = protobuf.util.base64.length(enc);
test.equal(len, str.length, "should calculate '" + enc + "' as " + str.length + " bytes");
var buf = protobuf.util.newBuffer(len);
var len2 = protobuf.util.base64.decode(enc, buf, 0);
test.equal(len2, len, "should decode '" + enc + "' to " + len + " bytes");
if (protobuf.util.isNode && protobuf.util.Buffer)
test.equal(buf.toString("utf8"), str, "should decode '" + enc + "' to '" + str + "'");
var enc2 = protobuf.util.base64.encode(buf, 0, buf.length);
test.equal(enc2, enc, "should encode '" + str + "' to '" + enc + "'");
var writer = protobuf.Writer.create();
var buf2 = writer.bytes(enc).finish();
for (var i = 0; i < buf.length; ++i)
test.equal(buf2[i + buf2.length - buf.length], buf[i], "should write byte " + buf[i] + " at " + (i + buf2.length - buf.length));
});
test.end();
});