08-sig.c (2686B)
1 #include <string.h> 2 3 #include "_tap.c" 4 #include "src/sfinx.c" 5 6 int main(void) { 7 static const uint8_t raw[8] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}; 8 static const uint8_t badhex[] = "zz"; 9 uint8_t out[64]; 10 uint8_t back[64]; 11 size_t out_len = 0; 12 size_t back_len = 0; 13 size_t need = 0; 14 sfinx_status status; 15 16 tap_begin("sfinx signature formats"); 17 18 status = sfinx_sig_decode(raw, sizeof(raw), "raw", out, sizeof(out), &out_len); 19 tap(status == SFINX_OK, "raw decode", "%s", sfinx_strerror(status)); 20 tap(out_len == sizeof(raw) && memcmp(out, raw, sizeof(raw)) == 0, "raw is the identity", "mismatch"); 21 22 need = sfinx_sig_encode_len(raw, sizeof(raw), "hex"); 23 tap(need == sizeof(raw) * 2, "hex encode_len", "got %zu", need); 24 status = sfinx_sig_encode(raw, sizeof(raw), "hex", out, sizeof(out), &out_len); 25 tap(status == SFINX_OK, "hex encode", "%s", sfinx_strerror(status)); 26 tap(out_len == sizeof(raw) * 2 && memcmp(out, "0123456789abcdef", 16) == 0, "hex text", "got %.*s", (int)out_len, 27 out); 28 29 status = sfinx_sig_decode(out, out_len, "hex", back, sizeof(back), &back_len); 30 tap(status == SFINX_OK, "hex decode", "%s", sfinx_strerror(status)); 31 tap(back_len == sizeof(raw) && memcmp(back, raw, sizeof(raw)) == 0, "hex round trip", "mismatch"); 32 33 need = sfinx_sig_encode_len(raw, sizeof(raw), "hdr"); 34 status = sfinx_sig_encode(raw, sizeof(raw), "hdr", out, sizeof(out), &out_len); 35 tap(status == SFINX_OK, "hdr encode", "%s", sfinx_strerror(status)); 36 tap(need == out_len, "hdr encode_len", "got %zu want %zu", need, out_len); 37 status = sfinx_sig_decode(out, out_len, NULL, back, sizeof(back), &back_len); 38 tap(status == SFINX_OK, "hdr auto-detect", "%s", sfinx_strerror(status)); 39 tap(back_len == sizeof(raw) && memcmp(back, raw, sizeof(raw)) == 0, "hdr round trip", "mismatch"); 40 41 status = sfinx_sig_decode(raw, sizeof(raw), NULL, back, sizeof(back), &back_len); 42 tap(status == SFINX_OK && back_len == sizeof(raw), "auto falls back to raw", "%s", sfinx_strerror(status)); 43 44 status = sfinx_sig_decode(badhex, sizeof(badhex) - 1, "hex", back, sizeof(back), &back_len); 45 tap(status == SFINX_ERR_FORMAT, "hex rejects bad input", "got %s", sfinx_strerror(status)); 46 47 status = sfinx_sig_decode(raw, sizeof(raw), "nope", back, sizeof(back), &back_len); 48 tap(status == SFINX_ERR_FORMAT, "rejects an unknown format", "got %s", sfinx_strerror(status)); 49 50 status = sfinx_sig_encode(raw, sizeof(raw), "hex", out, 4, &out_len); 51 tap(status == SFINX_ERR_NOSPACE, "hex encode rejects a small cap", "got %s", sfinx_strerror(status)); 52 53 return tap_plan(); 54 }