hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

+test.ha (8178B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use fmt;
      6 use io;
      7 use memio;
      8 use strings;
      9 
     10 
     11 @test fn read() void = {
     12 	const testcert_str = fmt::asprintf(
     13 		"garbage\ngarbage\ngarbage\n{}garbage\n", cert_str);
     14 	defer free(testcert_str);
     15 	const in = memio::fixed(strings::toutf8(testcert_str));
     16 	const dec = newdecoder(&in);
     17 	defer finish(&dec);
     18 
     19 	const stream = next(&dec)! as (str, pemdecoder);
     20 	assert(stream.0 == "CERTIFICATE");
     21 	static let buf: [1024]u8 = [0...];
     22 	assert(len(buf) >= len(testcert_bin));
     23 
     24 	const data = io::drain(&stream.1)!;
     25 	defer free(data);
     26 	assert(bytes::equal(data, testcert_bin));
     27 
     28 	assert(next(&dec) is io::EOF);
     29 };
     30 
     31 @test fn read_many() void = {
     32 	const testmany = fmt::asprintf("{}{}", cert_str, privkey_str);
     33 	defer free(testmany);
     34 	const in = memio::fixed(strings::toutf8(testmany));
     35 	const dec = newdecoder(&in);
     36 	defer finish(&dec);
     37 
     38 	static let buf: [1024]u8 = [0...];
     39 	const stream = next(&dec)! as (str, pemdecoder);
     40 	assert(stream.0 == "CERTIFICATE");
     41 	const data = io::drain(&stream.1)!;
     42 	defer free(data);
     43 	assert(bytes::equal(data, testcert_bin));
     44 
     45 	const stream = next(&dec)! as (str, pemdecoder);
     46 	assert(stream.0 == "PRIVATE KEY");
     47 	const data = io::drain(&stream.1)!;
     48 	defer free(data);
     49 	assert(bytes::equal(data, testprivkey_bin));
     50 
     51 	assert(next(&dec) is io::EOF);
     52 };
     53 
     54 @test fn write() void = {
     55 	let out = memio::dynamic();
     56 	const stream = newencoder("CERTIFICATE", &out)!;
     57 	io::writeall(&stream, testcert_bin)!;
     58 	io::close(&stream)!;
     59 	assert(memio::string(&out)! == cert_str);
     60 	io::close(&out)!;
     61 
     62 	let out = memio::dynamic();
     63 	const stream = newencoder("PRIVATE KEY", &out)!;
     64 	io::writeall(&stream, testprivkey_bin)!;
     65 	io::close(&stream)!;
     66 	assert(memio::string(&out)! == privkey_str);
     67 	io::close(&out)!;
     68 
     69 	// test short writes
     70 	let out = memio::dynamic();
     71 	const stream = newencoder("CERTIFICATE", &out)!;
     72 	for (let i = 0z; i < len(testcert_bin); i += 1) {
     73 		io::write(&stream, [testcert_bin[i]])!;
     74 	};
     75 	io::close(&stream)!;
     76 	assert(memio::string(&out)! == cert_str);
     77 	io::close(&out)!;
     78 };
     79 
     80 const cert_str: str =
     81 `-----BEGIN CERTIFICATE-----
     82 MIICLDCCAdKgAwIBAgIBADAKBggqhkjOPQQDAjB9MQswCQYDVQQGEwJCRTEPMA0G
     83 A1UEChMGR251VExTMSUwIwYDVQQLExxHbnVUTFMgY2VydGlmaWNhdGUgYXV0aG9y
     84 aXR5MQ8wDQYDVQQIEwZMZXV2ZW4xJTAjBgNVBAMTHEdudVRMUyBjZXJ0aWZpY2F0
     85 ZSBhdXRob3JpdHkwHhcNMTEwNTIzMjAzODIxWhcNMTIxMjIyMDc0MTUxWjB9MQsw
     86 CQYDVQQGEwJCRTEPMA0GA1UEChMGR251VExTMSUwIwYDVQQLExxHbnVUTFMgY2Vy
     87 dGlmaWNhdGUgYXV0aG9yaXR5MQ8wDQYDVQQIEwZMZXV2ZW4xJTAjBgNVBAMTHEdu
     88 dVRMUyBjZXJ0aWZpY2F0ZSBhdXRob3JpdHkwWTATBgcqhkjOPQIBBggqhkjOPQMB
     89 BwNCAARS2I0jiuNn14Y2sSALCX3IybqiIJUvxUpj+oNfzngvj/Niyv2394BWnW4X
     90 uQ4RTEiywK87WRcWMGgJB5kX/t2no0MwQTAPBgNVHRMBAf8EBTADAQH/MA8GA1Ud
     91 DwEB/wQFAwMHBgAwHQYDVR0OBBYEFPC0gf6YEr+1KLlkQAPLzB9mTigDMAoGCCqG
     92 SM49BAMCA0gAMEUCIDGuwD1KPyG+hRf88MeyMQcqOFZD0TbVleF+UsAGQ4enAiEA
     93 l4wOuDwKQa+upc8GftXE2C//4mKANBC6It01gUaTIpo=
     94 -----END CERTIFICATE-----
     95 `;
     96 
     97 const testcert_bin: [_]u8 = [
     98 	0x30, 0x82, 0x02, 0x2c, 0x30, 0x82, 0x01, 0xd2, 0xa0, 0x03, 0x02, 0x01,
     99 	0x02, 0x02, 0x01, 0x00, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
    100 	0x3d, 0x04, 0x03, 0x02, 0x30, 0x7d, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
    101 	0x55, 0x04, 0x06, 0x13, 0x02, 0x42, 0x45, 0x31, 0x0f, 0x30, 0x0d, 0x06,
    102 	0x03, 0x55, 0x04, 0x0a, 0x13, 0x06, 0x47, 0x6e, 0x75, 0x54, 0x4c, 0x53,
    103 	0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x1c, 0x47,
    104 	0x6e, 0x75, 0x54, 0x4c, 0x53, 0x20, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66,
    105 	0x69, 0x63, 0x61, 0x74, 0x65, 0x20, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72,
    106 	0x69, 0x74, 0x79, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x08,
    107 	0x13, 0x06, 0x4c, 0x65, 0x75, 0x76, 0x65, 0x6e, 0x31, 0x25, 0x30, 0x23,
    108 	0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x1c, 0x47, 0x6e, 0x75, 0x54, 0x4c,
    109 	0x53, 0x20, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74,
    110 	0x65, 0x20, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30,
    111 	0x1e, 0x17, 0x0d, 0x31, 0x31, 0x30, 0x35, 0x32, 0x33, 0x32, 0x30, 0x33,
    112 	0x38, 0x32, 0x31, 0x5a, 0x17, 0x0d, 0x31, 0x32, 0x31, 0x32, 0x32, 0x32,
    113 	0x30, 0x37, 0x34, 0x31, 0x35, 0x31, 0x5a, 0x30, 0x7d, 0x31, 0x0b, 0x30,
    114 	0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x42, 0x45, 0x31, 0x0f,
    115 	0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x06, 0x47, 0x6e, 0x75,
    116 	0x54, 0x4c, 0x53, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x0b,
    117 	0x13, 0x1c, 0x47, 0x6e, 0x75, 0x54, 0x4c, 0x53, 0x20, 0x63, 0x65, 0x72,
    118 	0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x20, 0x61, 0x75, 0x74,
    119 	0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03,
    120 	0x55, 0x04, 0x08, 0x13, 0x06, 0x4c, 0x65, 0x75, 0x76, 0x65, 0x6e, 0x31,
    121 	0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x1c, 0x47, 0x6e,
    122 	0x75, 0x54, 0x4c, 0x53, 0x20, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69,
    123 	0x63, 0x61, 0x74, 0x65, 0x20, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69,
    124 	0x74, 0x79, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
    125 	0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01,
    126 	0x07, 0x03, 0x42, 0x00, 0x04, 0x52, 0xd8, 0x8d, 0x23, 0x8a, 0xe3, 0x67,
    127 	0xd7, 0x86, 0x36, 0xb1, 0x20, 0x0b, 0x09, 0x7d, 0xc8, 0xc9, 0xba, 0xa2,
    128 	0x20, 0x95, 0x2f, 0xc5, 0x4a, 0x63, 0xfa, 0x83, 0x5f, 0xce, 0x78, 0x2f,
    129 	0x8f, 0xf3, 0x62, 0xca, 0xfd, 0xb7, 0xf7, 0x80, 0x56, 0x9d, 0x6e, 0x17,
    130 	0xb9, 0x0e, 0x11, 0x4c, 0x48, 0xb2, 0xc0, 0xaf, 0x3b, 0x59, 0x17, 0x16,
    131 	0x30, 0x68, 0x09, 0x07, 0x99, 0x17, 0xfe, 0xdd, 0xa7, 0xa3, 0x43, 0x30,
    132 	0x41, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04,
    133 	0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d,
    134 	0x0f, 0x01, 0x01, 0xff, 0x04, 0x05, 0x03, 0x03, 0x07, 0x06, 0x00, 0x30,
    135 	0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xf0, 0xb4,
    136 	0x81, 0xfe, 0x98, 0x12, 0xbf, 0xb5, 0x28, 0xb9, 0x64, 0x40, 0x03, 0xcb,
    137 	0xcc, 0x1f, 0x66, 0x4e, 0x28, 0x03, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86,
    138 	0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02,
    139 	0x20, 0x31, 0xae, 0xc0, 0x3d, 0x4a, 0x3f, 0x21, 0xbe, 0x85, 0x17, 0xfc,
    140 	0xf0, 0xc7, 0xb2, 0x31, 0x07, 0x2a, 0x38, 0x56, 0x43, 0xd1, 0x36, 0xd5,
    141 	0x95, 0xe1, 0x7e, 0x52, 0xc0, 0x06, 0x43, 0x87, 0xa7, 0x02, 0x21, 0x00,
    142 	0x97, 0x8c, 0x0e, 0xb8, 0x3c, 0x0a, 0x41, 0xaf, 0xae, 0xa5, 0xcf, 0x06,
    143 	0x7e, 0xd5, 0xc4, 0xd8, 0x2f, 0xff, 0xe2, 0x62, 0x80, 0x34, 0x10, 0xba,
    144 	0x22, 0xdd, 0x35, 0x81, 0x46, 0x93, 0x22, 0x9a,
    145 ];
    146 
    147 const privkey_str: str =
    148 `-----BEGIN PRIVATE KEY-----
    149 MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgVcB/UNPxalR9zDYAjQIf
    150 jojUDiQuGnSJrFEEzZPT/92hRANCAASc7UJtgnF/abqWM60T3XNJEzBv5ez9TdwK
    151 H0M6xpM2q+53wmsN/eYLdgtjgBd3DBmHtPilCkiFICXyaA8z9LkJ
    152 -----END PRIVATE KEY-----
    153 `;
    154 
    155 const testprivkey_bin: [_]u8 = [
    156 	0x30, 0x81, 0x84, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86,
    157 	0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x0a,
    158 	0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, 0x55, 0xc0, 0x7f,
    159 	0x50, 0xd3, 0xf1, 0x6a, 0x54, 0x7d, 0xcc, 0x36, 0x00, 0x8d, 0x02, 0x1f,
    160 	0x8e, 0x88, 0xd4, 0x0e, 0x24, 0x2e, 0x1a, 0x74, 0x89, 0xac, 0x51, 0x04,
    161 	0xcd, 0x93, 0xd3, 0xff, 0xdd, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0x9c,
    162 	0xed, 0x42, 0x6d, 0x82, 0x71, 0x7f, 0x69, 0xba, 0x96, 0x33, 0xad, 0x13,
    163 	0xdd, 0x73, 0x49, 0x13, 0x30, 0x6f, 0xe5, 0xec, 0xfd, 0x4d, 0xdc, 0x0a,
    164 	0x1f, 0x43, 0x3a, 0xc6, 0x93, 0x36, 0xab, 0xee, 0x77, 0xc2, 0x6b, 0x0d,
    165 	0xfd, 0xe6, 0x0b, 0x76, 0x0b, 0x63, 0x80, 0x17, 0x77, 0x0c, 0x19, 0x87,
    166 	0xb4, 0xf8, 0xa5, 0x0a, 0x48, 0x85, 0x20, 0x25, 0xf2, 0x68, 0x0f, 0x33,
    167 	0xf4, 0xb9, 0x09,
    168 ];
    169 
    170 @test fn readcrlf() void = {
    171 	const test_str = fmt::asprintf(
    172 		"garbage\r\ngarbage\r\ngarbage\r\n{}garbage\r\n", testcrlf_str);
    173 	defer free(test_str);
    174 	const in = memio::fixed(strings::toutf8(test_str));
    175 	const dec = newdecoder(&in);
    176 	defer finish(&dec);
    177 
    178 	const stream = next(&dec)! as (str, pemdecoder);
    179 	assert(stream.0 == "TEST");
    180 	static let buf: [1024]u8 = [0...];
    181 	assert(len(buf) >= len(testcert_bin));
    182 
    183 	const data = io::drain(&stream.1)!;
    184 	defer free(data);
    185 	assert(bytes::equal(data, testcrlf_bin));
    186 
    187 	assert(next(&dec) is io::EOF);
    188 };
    189 
    190 const testcrlf_str: str = "-----BEGIN TEST-----\r\ndGVzdA==\r\n-----END TEST-----\r\n";
    191 const testcrlf_bin: [_]u8 = [0x74, 0x65, 0x73, 0x74];