static void test_parse_nsec(void) { nsec_t u; log_info("/* %s */", __func__); assert_se(parse_nsec("5s", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC); assert_se(parse_nsec("5s500ms", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); assert_se(parse_nsec(" 5s 500ms ", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); assert_se(parse_nsec(" 5.5s ", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC); assert_se(parse_nsec(" .22s ", &u) >= 0); assert_se(u == 220 * NSEC_PER_MSEC); assert_se(parse_nsec(" .50y ", &u) >= 0); assert_se(u == NSEC_PER_YEAR / 2); assert_se(parse_nsec("2.5", &u) >= 0); assert_se(u == 2); assert_se(parse_nsec(".7", &u) >= 0); assert_se(u == 0); assert_se(parse_nsec("infinity", &u) >= 0); assert_se(u == NSEC_INFINITY); assert_se(parse_nsec(" infinity ", &u) >= 0); assert_se(u == NSEC_INFINITY); assert_se(parse_nsec(" xyz ", &u) < 0); assert_se(parse_nsec("", &u) < 0); assert_se(parse_nsec(" . ", &u) < 0); assert_se(parse_nsec(" 5. ", &u) < 0); assert_se(parse_nsec(".s ", &u) < 0); assert_se(parse_nsec(" infinity .7", &u) < 0); assert_se(parse_nsec(".3 infinity", &u) < 0); }
dns_result ReadName(unsigned char *reader, size_t data_len, unsigned short type, unsigned char *dns, char *output_buf, size_t output_buf_size) { if (type < 1 || type > 256) { return DNS_RESULT_ERR; } switch (type) { case T_A: return parse_ip(reader, output_buf, output_buf_size); break; case T_NS: // printf("datalen: %d", data_len); parse_ns(reader, (unsigned short)data_len, dns, output_buf, output_buf_size); break; case T_CNAME: parse_cname(reader, dns, output_buf, output_buf_size, data_len); break; case T_SOA: parse_soa(reader, (unsigned short)data_len, dns, output_buf, output_buf_size); break; case T_PTR: parse_ptr(reader, (unsigned short)data_len, dns, output_buf, output_buf_size); break; case T_HINFO: parse_hinfo(reader, (unsigned short)data_len, output_buf, output_buf_size); break; case T_MX: parse_mx(reader, (unsigned short)data_len, dns, output_buf, output_buf_size); break; case T_TXT: return parse_txt(reader, data_len, output_buf, output_buf_size); break; case T_RP: parse_rp(reader, dns, output_buf, output_buf_size, data_len); break; case T_AFSDB: parse_afsdb(reader, dns, output_buf, output_buf_size, data_len); break; case T_AAAA: parse_aaaa(reader, output_buf, output_buf_size); break; case T_LOC: parse_loc(reader, output_buf, output_buf_size); break; case T_SRV: parse_srv(reader, dns, output_buf, output_buf_size, data_len); break; case T_NAPTR: return parse_naptr(reader, (unsigned short)data_len, output_buf, output_buf_size); break; case T_RRSIG: parse_rrsig(reader, dns, (unsigned short)data_len, output_buf, output_buf_size); break; case T_NSEC: parse_nsec(reader, dns, output_buf, output_buf_size, data_len); break; case T_DNSKEY: parse_dnskey(reader, (unsigned short)data_len, output_buf, output_buf_size); break; default: parse_default(reader, (unsigned short)data_len, output_buf, output_buf_size); break; } return DNS_RESULT_OK; }
static void test_parse_nsec(void) { nsec_t u; log_info("/* %s */", __func__); assert_se(parse_nsec("5s", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC); assert_se(parse_nsec("5s500ms", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); assert_se(parse_nsec(" 5s 500ms ", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); assert_se(parse_nsec(" 5.5s ", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC); assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0); assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC); assert_se(parse_nsec(" .22s ", &u) >= 0); assert_se(u == 220 * NSEC_PER_MSEC); assert_se(parse_nsec(" .50y ", &u) >= 0); assert_se(u == NSEC_PER_YEAR / 2); assert_se(parse_nsec("2.5", &u) >= 0); assert_se(u == 2); assert_se(parse_nsec(".7", &u) >= 0); assert_se(u == 0); assert_se(parse_nsec("infinity", &u) >= 0); assert_se(u == NSEC_INFINITY); assert_se(parse_nsec(" infinity ", &u) >= 0); assert_se(u == NSEC_INFINITY); assert_se(parse_nsec("+3.1s", &u) >= 0); assert_se(u == 3100 * NSEC_PER_MSEC); assert_se(parse_nsec(" xyz ", &u) < 0); assert_se(parse_nsec("", &u) < 0); assert_se(parse_nsec(" . ", &u) < 0); assert_se(parse_nsec(" 5. ", &u) < 0); assert_se(parse_nsec(".s ", &u) < 0); assert_se(parse_nsec(" infinity .7", &u) < 0); assert_se(parse_nsec(".3 infinity", &u) < 0); assert_se(parse_nsec("-5s ", &u) < 0); assert_se(parse_nsec("-0.3s ", &u) < 0); assert_se(parse_nsec("-0.0s ", &u) < 0); assert_se(parse_nsec("-0.-0s ", &u) < 0); assert_se(parse_nsec("0.-0s ", &u) < 0); assert_se(parse_nsec("3.-0s ", &u) < 0); assert_se(parse_nsec(" infinity .7", &u) < 0); assert_se(parse_nsec(".3 infinity", &u) < 0); assert_se(parse_nsec("3.+1s", &u) < 0); assert_se(parse_nsec("3. 1s", &u) < 0); assert_se(parse_nsec("3.s", &u) < 0); assert_se(parse_nsec("1111111111111y", &u) == -ERANGE); assert_se(parse_nsec("1.111111111111y", &u) == -ERANGE); }