Implement SEQUENCE encoding and decoding in client
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using StudySystemClient;
|
||||
using StudySystemClient.Der;
|
||||
|
||||
static bool bytes_equal(uint8[] expected, uint8[] actual) {
|
||||
@@ -46,15 +47,31 @@ static void test_decode_boolean(uint8[] bytes, bool expected) {
|
||||
}
|
||||
|
||||
static void test_decode_integer(uint8[] bytes, int64 expected) {
|
||||
Integer integer;
|
||||
Datum datum;
|
||||
try {
|
||||
integer = decode(bytes) as Integer;
|
||||
datum = decode(bytes);
|
||||
} catch (DecodeError err) {
|
||||
Test.message("Decoding failed: %s", err.message);
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
test_integer_value(expected, datum);
|
||||
}
|
||||
|
||||
static void test_decode_utf8string(uint8[] bytes, string expected) {
|
||||
Datum datum;
|
||||
try {
|
||||
datum = decode(bytes);
|
||||
} catch (DecodeError err) {
|
||||
Test.message("Decoding failed: %s", err.message);
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
test_utf8string_value(expected, datum);
|
||||
}
|
||||
|
||||
static void test_integer_value(int64 expected, Datum datum) {
|
||||
var integer = datum as Integer;
|
||||
if (integer == null) {
|
||||
Test.message("Bytes were not decoded as a INTEGER");
|
||||
Test.fail();
|
||||
@@ -67,16 +84,8 @@ static void test_decode_integer(uint8[] bytes, int64 expected) {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static void test_utf8string_value(string expected, Datum datum) {
|
||||
var utf8string = datum as Utf8String;
|
||||
if (utf8string == null) {
|
||||
Test.message("Bytes were not decoded as a UTF8String");
|
||||
Test.fail();
|
||||
@@ -154,6 +163,15 @@ void main(string[] args) {
|
||||
test_encode(new Utf8String(string.nfill(128, 'x')), expected);
|
||||
});
|
||||
|
||||
Test.add_func("/encode/sequence/foo,42", () => {
|
||||
var sequence = new Der.Sequence(
|
||||
{new Utf8String("foo"), new Integer(42)});
|
||||
var expected = new uint8[] {
|
||||
0x30, 0x08, 0x0c, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x2a
|
||||
};
|
||||
test_encode(sequence, expected);
|
||||
});
|
||||
|
||||
/*
|
||||
* Decoding
|
||||
*/
|
||||
@@ -208,5 +226,34 @@ void main(string[] args) {
|
||||
test_decode_utf8string(bytes, string.nfill(128, 'x'));
|
||||
});
|
||||
|
||||
Test.add_func("/decode/sequence/foo,42", () => {
|
||||
var bytes = new uint8[] {
|
||||
0x30, 0x08, 0x0c, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x2a
|
||||
};
|
||||
var expected = 2;
|
||||
Der.Sequence sequence;
|
||||
try {
|
||||
sequence = decode(bytes) as Der.Sequence;
|
||||
} catch (DecodeError err) {
|
||||
Test.message("Decoding failed: %s", err.message);
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
if (sequence == null) {
|
||||
Test.message("Bytes were not decoded as a SEQUENCE");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
Datum[] elems = sequence.value;
|
||||
if (elems.length != expected) {
|
||||
Test.message(
|
||||
@"Expected $expected elements, got $(elems.length)");
|
||||
Test.fail();
|
||||
return;
|
||||
}
|
||||
test_utf8string_value("foo", elems[0]);
|
||||
test_integer_value(42, elems[1]);
|
||||
});
|
||||
|
||||
Test.run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user