Implement DER decoding for BOOLEAN, INTEGER and UTF8String in Client
This commit is contained in:
@@ -1,22 +1,19 @@
|
||||
using StudySystemClient.Der;
|
||||
|
||||
static bool bytes_equal(uint8[] expected, uint8[] actual)
|
||||
{
|
||||
static bool bytes_equal(uint8[] expected, uint8[] actual) {
|
||||
if (expected.length != actual.length)
|
||||
return false;
|
||||
return Memory.cmp(expected, actual, expected.length) == 0;
|
||||
}
|
||||
|
||||
static string bytes_to_string(uint8[] bytes)
|
||||
{
|
||||
static string bytes_to_string(uint8[] bytes) {
|
||||
var s = "";
|
||||
foreach (var byte in bytes)
|
||||
s += "%02x".printf(byte);
|
||||
return s;
|
||||
}
|
||||
|
||||
static void test_encode(Datum datum, uint8[] expected)
|
||||
{
|
||||
static void test_encode(Datum datum, uint8[] expected) {
|
||||
var bytes = datum.encode();
|
||||
if (!bytes_equal(expected, bytes)) {
|
||||
Test.message("Encoding is incorrect: expected %s got %s",
|
||||
@@ -26,6 +23,72 @@ static void test_encode(Datum datum, uint8[] expected)
|
||||
}
|
||||
}
|
||||
|
||||
static void test_decode_boolean(uint8[] bytes, bool expected) {
|
||||
Boolean boolean;
|
||||
try {
|
||||
boolean = decode(bytes) as Boolean;
|
||||
} catch (DecodeError err) {
|
||||
Test.message("Decoding failed: %s", err.message);
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
|
||||
if (boolean == null) {
|
||||
Test.message("Bytes were not decoded as a BOOLEAN");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
if (boolean.value != expected) {
|
||||
Test.message(@"Expected $expected got $(boolean.value)");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_decode_integer(uint8[] bytes, int64 expected) {
|
||||
Integer integer;
|
||||
try {
|
||||
integer = decode(bytes) as Integer;
|
||||
} catch (DecodeError err) {
|
||||
Test.message("Decoding failed: %s", err.message);
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
|
||||
if (integer == null) {
|
||||
Test.message("Bytes were not decoded as a INTEGER");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
if (integer.value != expected) {
|
||||
Test.message(@"Expected $expected got $(integer.value)");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void test_decode_utf8string(uint8[] bytes, string expected) {
|
||||
Utf8String utf8string;
|
||||
try {
|
||||
utf8string = decode(bytes) as Utf8String;
|
||||
} catch (DecodeError err) {
|
||||
Test.message("Decoding failed: %s", err.message);
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
|
||||
if (utf8string == null) {
|
||||
Test.message("Bytes were not decoded as a UTF8String");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
if (utf8string.value != expected) {
|
||||
Test.message(@"Expected $expected got $(utf8string.value)");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void main(string[] args) {
|
||||
Test.init(ref args);
|
||||
|
||||
@@ -90,6 +153,60 @@ void main(string[] args) {
|
||||
Memory.set(expected[3:], 0x78, 128);
|
||||
test_encode(new Utf8String(string.nfill(128, 'x')), expected);
|
||||
});
|
||||
|
||||
/*
|
||||
* Decoding
|
||||
*/
|
||||
|
||||
Test.add_func("/decode/boolean/true", () => {
|
||||
test_decode_boolean({0x01, 0x01, 0xff}, true);
|
||||
});
|
||||
Test.add_func("/decode/boolean/false", () => {
|
||||
test_decode_boolean({0x01, 0x01, 0x00}, false);
|
||||
});
|
||||
|
||||
Test.add_func("/decode/integer/small/42", () => {
|
||||
test_decode_integer({0x02, 0x01, 0x2a}, 42);
|
||||
});
|
||||
Test.add_func("/decode/integer/large/1337", () => {
|
||||
test_decode_integer({0x02, 0x02, 0x05, 0x39}, 1337);
|
||||
});
|
||||
Test.add_func("/decode/integer/sign/128", () => {
|
||||
test_decode_integer({0x02, 0x02, 0x00, 0x80}, 128);
|
||||
});
|
||||
Test.add_func("/decode/integer/sign/0xbeef", () => {
|
||||
test_decode_integer({0x02, 0x03, 0x00, 0xbe, 0xef}, 0xbeef);
|
||||
});
|
||||
Test.add_func("/decode/integer/sign/-128", () => {
|
||||
test_decode_integer({0x02, 0x01, 0x80}, -128);
|
||||
});
|
||||
Test.add_func("/decode/integer/sign/-1337", () => {
|
||||
test_decode_integer({0x02, 0x02, 0xfa, 0xc7}, -1337);
|
||||
});
|
||||
|
||||
Test.add_func("/decode/utf8string/short/foo", () => {
|
||||
test_decode_utf8string({0x0c, 0x03, 0x66, 0x6f, 0x6f}, "foo");
|
||||
});
|
||||
Test.add_func("/decode/utf8string/short/bar", () => {
|
||||
test_decode_utf8string({0x0c, 0x03, 0x62, 0x61, 0x72}, "bar");
|
||||
});
|
||||
Test.add_func("/decode/utf8string/long/x300", () => {
|
||||
var bytes = new uint8[304];
|
||||
bytes[0] = 0x0c;
|
||||
bytes[1] = 0x82;
|
||||
bytes[2] = 0x01;
|
||||
bytes[3] = 0x2c;
|
||||
Memory.set(bytes[4:], 0x78, 300);
|
||||
test_decode_utf8string(bytes, string.nfill(300, 'x'));
|
||||
});
|
||||
Test.add_func("/decode/utf8string/long/x128", () => {
|
||||
var bytes = new uint8[131];
|
||||
bytes[0] = 0x0c;
|
||||
bytes[1] = 0x81;
|
||||
bytes[2] = 0x80;
|
||||
Memory.set(bytes[3:], 0x78, 128);
|
||||
test_decode_utf8string(bytes, string.nfill(128, 'x'));
|
||||
});
|
||||
|
||||
Test.run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user