LCOV - code coverage report
Current view: top level - libsystemd-network - test-sd-dhcp-lease.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 41 41 100.0 %
Date: 2019-08-22 15:41:25 Functions: 7 7 100.0 %

          Line data    Source code
       1             : #include <errno.h>
       2             : 
       3             : #include "dhcp-lease-internal.h"
       4             : #include "macro.h"
       5             : #include "string-util.h"
       6             : #include "strv.h"
       7             : 
       8             : /* According to RFC1035 section 4.1.4, a domain name in a message can be either:
       9             :  *      - a sequence of labels ending in a zero octet
      10             :  *      - a pointer
      11             :  *      - a sequence of labels ending with a pointer
      12             :  */
      13           1 : static void test_dhcp_lease_parse_search_domains_basic(void) {
      14             :         int r;
      15           1 :         _cleanup_strv_free_ char **domains = NULL;
      16             :         static const uint8_t optionbuf[] = {
      17             :                 0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
      18             :                 0x04, 'A', 'B', 'C', 'D', 0x03, 'E', 'F', 'G', 0x00,
      19             :         };
      20             : 
      21           1 :         r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
      22           1 :         assert_se(r == 2);
      23           1 :         assert_se(streq(domains[0], "FOO.BAR"));
      24           1 :         assert_se(streq(domains[1], "ABCD.EFG"));
      25           1 : }
      26             : 
      27           1 : static void test_dhcp_lease_parse_search_domains_ptr(void) {
      28             :         int r;
      29           1 :         _cleanup_strv_free_ char **domains = NULL;
      30             :         static const uint8_t optionbuf[] = {
      31             :                 0x03, 'F', 'O', 'O', 0x00, 0xC0, 0x00,
      32             :         };
      33             : 
      34           1 :         r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
      35           1 :         assert_se(r == 2);
      36           1 :         assert_se(streq(domains[0], "FOO"));
      37           1 :         assert_se(streq(domains[1], "FOO"));
      38           1 : }
      39             : 
      40           1 : static void test_dhcp_lease_parse_search_domains_labels_and_ptr(void) {
      41             :         int r;
      42           1 :         _cleanup_strv_free_ char **domains = NULL;
      43             :         static const uint8_t optionbuf[] = {
      44             :                 0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
      45             :                 0x03, 'A', 'B', 'C', 0xC0, 0x04,
      46             :         };
      47             : 
      48           1 :         r = dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains);
      49           1 :         assert_se(r == 2);
      50           1 :         assert_se(streq(domains[0], "FOO.BAR"));
      51           1 :         assert_se(streq(domains[1], "ABC.BAR"));
      52           1 : }
      53             : 
      54             : /* Tests for exceptions. */
      55             : 
      56           1 : static void test_dhcp_lease_parse_search_domains_no_data(void) {
      57           1 :         _cleanup_strv_free_ char **domains = NULL;
      58             :         static const uint8_t optionbuf[3] = {0, 0, 0};
      59             : 
      60           1 :         assert_se(dhcp_lease_parse_search_domains(NULL, 0, &domains) == -ENODATA);
      61           1 :         assert_se(dhcp_lease_parse_search_domains(optionbuf, 0, &domains) == -ENODATA);
      62           1 : }
      63             : 
      64           1 : static void test_dhcp_lease_parse_search_domains_loops(void) {
      65           1 :         _cleanup_strv_free_ char **domains = NULL;
      66             :         static const uint8_t optionbuf[] = {
      67             :                 0x03, 'F', 'O', 'O', 0x00, 0x03, 'B', 'A', 'R', 0xC0, 0x06,
      68             :         };
      69             : 
      70           1 :         assert_se(dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf), &domains) == -EBADMSG);
      71           1 : }
      72             : 
      73           1 : static void test_dhcp_lease_parse_search_domains_wrong_len(void) {
      74           1 :         _cleanup_strv_free_ char **domains = NULL;
      75             :         static const uint8_t optionbuf[] = {
      76             :                 0x03, 'F', 'O', 'O', 0x03, 'B', 'A', 'R', 0x00,
      77             :                 0x04, 'A', 'B', 'C', 'D', 0x03, 'E', 'F', 'G', 0x00,
      78             :         };
      79             : 
      80           1 :         assert_se(dhcp_lease_parse_search_domains(optionbuf, sizeof(optionbuf) - 5, &domains) == -EBADMSG);
      81           1 : }
      82             : 
      83           1 : int main(int argc, char *argv[]) {
      84           1 :         test_dhcp_lease_parse_search_domains_basic();
      85           1 :         test_dhcp_lease_parse_search_domains_ptr();
      86           1 :         test_dhcp_lease_parse_search_domains_labels_and_ptr();
      87           1 :         test_dhcp_lease_parse_search_domains_no_data();
      88           1 :         test_dhcp_lease_parse_search_domains_loops();
      89           1 :         test_dhcp_lease_parse_search_domains_wrong_len();
      90             : }

Generated by: LCOV version 1.14