From 218c1e36445e66afa04ab33787827388fed39e68 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Tue, 25 Feb 2025 21:58:59 +0000 Subject: [PATCH] Add NULL support to Client DER library --- client/src/der.vala | 20 ++++++++++++++++++++ client/tests/der_tests.vala | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/client/src/der.vala b/client/src/der.vala index 82332a8..63753e6 100644 --- a/client/src/der.vala +++ b/client/src/der.vala @@ -59,6 +59,8 @@ namespace StudySystemClient.Der { return new Utf8String.from_content(content); case Sequence.TYPE: return new Sequence.from_content(content); + case Null.TYPE: + return new Null.from_content(content); default: throw new DecodeError.UNKNOWN_TYPE("Unsupported type: %02x", type); @@ -287,4 +289,22 @@ namespace StudySystemClient.Der { value = decode(bytes); } } + + public class Null : Datum { + internal const uint8 TYPE = 0x05; + + public Null() { + type = TYPE; + content = new uint8[] {}; + } + + internal Null.from_content(uint8[] bytes) throws DecodeError { + if (bytes.length != 0) { + throw new DecodeError.INVALID_CONTENT( + "Non-empty content for NULL"); + } + type = TYPE; + content = bytes; + } + } } diff --git a/client/tests/der_tests.vala b/client/tests/der_tests.vala index 22ea1ae..f3f2388 100644 --- a/client/tests/der_tests.vala +++ b/client/tests/der_tests.vala @@ -180,6 +180,10 @@ void main(string[] args) { test_encode(choice, expected); }); + Test.add_func("/encode/null", () => { + test_encode(new Der.Null(), { 0x05, 0x00 }); + }); + /* * Decoding */ @@ -289,5 +293,23 @@ void main(string[] args) { test_utf8string_value("foo", choice.value); }); + Test.add_func("/decode/null", () => { + var bytes = new uint8[] { 0x05, 0x00 }; + Der.Null @null; + try { + @null = decode(bytes) as Der.Null; + } catch (DecodeError err) { + Test.message("Decoding failed: %s", err.message); + Test.fail(); + return; + } + if (@null == null) { + Test.message("Bytes were not decoded as a NULL"); + Test.fail(); + return; + } + }); + + Test.run(); }