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