|
| 1 | +From 6ff47454ff413e3033a77d4d9c09b914c78ab3a0 Mon Sep 17 00:00:00 2001 |
| 2 | +From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com> |
| 3 | +Date: Wed, 7 Dec 2022 22:56:47 +0100 |
| 4 | +Subject: [PATCH] Add unit test parts for new autodetection |
| 5 | + |
| 6 | +Use new enum to specify forced present or missing .local SOA record. Use |
| 7 | +from production code auto value, but use forced values from unit test. |
| 8 | +Add few different results to unit test. |
| 9 | +--- |
| 10 | + src/nss.c | 3 ++- |
| 11 | + src/util.c | 7 +++++-- |
| 12 | + src/util.h | 9 ++++++++- |
| 13 | + tests/check_util.c | 18 ++++++++++++++++++ |
| 14 | + 4 files changed, 33 insertions(+), 4 deletions(-) |
| 15 | + |
| 16 | +diff --git a/src/nss.c b/src/nss.c |
| 17 | +index 7f9230e..2e1a90b 100644 |
| 18 | +--- a/src/nss.c |
| 19 | ++++ b/src/nss.c |
| 20 | +@@ -118,7 +118,8 @@ enum nss_status _nss_mdns_gethostbyname_impl(const char* name, int af, |
| 21 | + #ifndef MDNS_MINIMAL |
| 22 | + mdns_allow_file = fopen(MDNS_ALLOW_FILE, "r"); |
| 23 | + #endif |
| 24 | +- result = verify_name_allowed_with_soa(name, mdns_allow_file); |
| 25 | ++ result = verify_name_allowed_with_soa(name, mdns_allow_file, |
| 26 | ++ TEST_LOCAL_SOA_AUTO); |
| 27 | + #ifndef MDNS_MINIMAL |
| 28 | + if (mdns_allow_file) |
| 29 | + fclose(mdns_allow_file); |
| 30 | +diff --git a/src/util.c b/src/util.c |
| 31 | +index 4eacf07..0a1c28a 100644 |
| 32 | +--- a/src/util.c |
| 33 | ++++ b/src/util.c |
| 34 | +@@ -55,14 +55,17 @@ int ends_with(const char* name, const char* suffix) { |
| 35 | + return strcasecmp(name + ln - ls, suffix) == 0; |
| 36 | + } |
| 37 | + |
| 38 | +-use_name_result_t verify_name_allowed_with_soa(const char* name, FILE* mdns_allow_file) { |
| 39 | ++use_name_result_t verify_name_allowed_with_soa(const char* name, |
| 40 | ++ FILE* mdns_allow_file, |
| 41 | ++ test_local_soa_t test) { |
| 42 | + switch (verify_name_allowed(name, mdns_allow_file)) { |
| 43 | + case VERIFY_NAME_RESULT_NOT_ALLOWED: |
| 44 | + return USE_NAME_RESULT_SKIP; |
| 45 | + case VERIFY_NAME_RESULT_ALLOWED: |
| 46 | + return USE_NAME_RESULT_AUTHORITATIVE; |
| 47 | + case VERIFY_NAME_RESULT_ALLOWED_IF_NO_LOCAL_SOA: |
| 48 | +- if (local_soa()) |
| 49 | ++ if (test == TEST_LOCAL_SOA_YES || |
| 50 | ++ (test == TEST_LOCAL_SOA_AUTO && local_soa()) ) |
| 51 | + /* Make multicast resolution not authoritative for .local zone. |
| 52 | + * Allow continuing to unicast resolution after multicast had not worked. */ |
| 53 | + return USE_NAME_RESULT_OPTIONAL; |
| 54 | +diff --git a/src/util.h b/src/util.h |
| 55 | +index 76809d4..80527e3 100644 |
| 56 | +--- a/src/util.h |
| 57 | ++++ b/src/util.h |
| 58 | +@@ -67,6 +67,12 @@ typedef enum { |
| 59 | + USE_NAME_RESULT_OPTIONAL, |
| 60 | + } use_name_result_t; |
| 61 | + |
| 62 | ++typedef enum { |
| 63 | ++ TEST_LOCAL_SOA_NO, |
| 64 | ++ TEST_LOCAL_SOA_YES, |
| 65 | ++ TEST_LOCAL_SOA_AUTO, |
| 66 | ++} test_local_soa_t; |
| 67 | ++ |
| 68 | + // Returns true if we should try to resolve the name with mDNS. |
| 69 | + // |
| 70 | + // If mdns_allow_file is NULL, then this implements the "local" SOA |
| 71 | +@@ -78,7 +84,8 @@ typedef enum { |
| 72 | + // The two heuristics described above are disabled if mdns_allow_file |
| 73 | + // is not NULL. |
| 74 | + use_name_result_t verify_name_allowed_with_soa(const char* name, |
| 75 | +- FILE* mdns_allow_file); |
| 76 | ++ FILE* mdns_allow_file, |
| 77 | ++ test_local_soa_t test); |
| 78 | + |
| 79 | + typedef enum { |
| 80 | + VERIFY_NAME_RESULT_NOT_ALLOWED, |
| 81 | +diff --git a/tests/check_util.c b/tests/check_util.c |
| 82 | +index d600a2e..36f1008 100644 |
| 83 | +--- a/tests/check_util.c |
| 84 | ++++ b/tests/check_util.c |
| 85 | +@@ -50,6 +50,24 @@ START_TEST(test_verify_name_allowed_minimal) { |
| 86 | + VERIFY_NAME_RESULT_NOT_ALLOWED); |
| 87 | + ck_assert_int_eq(verify_name_allowed(".", NULL), |
| 88 | + VERIFY_NAME_RESULT_NOT_ALLOWED); |
| 89 | ++ |
| 90 | ++ ck_assert_int_eq(verify_name_allowed_with_soa(".", NULL, TEST_LOCAL_SOA_YES), |
| 91 | ++ USE_NAME_RESULT_SKIP); |
| 92 | ++ ck_assert_int_eq(verify_name_allowed_with_soa(".", NULL, TEST_LOCAL_SOA_NO), |
| 93 | ++ USE_NAME_RESULT_SKIP); |
| 94 | ++ ck_assert_int_eq(verify_name_allowed_with_soa(".", NULL, TEST_LOCAL_SOA_AUTO), |
| 95 | ++ USE_NAME_RESULT_SKIP); |
| 96 | ++ ck_assert_int_eq(verify_name_allowed_with_soa("example3.sub.local", |
| 97 | ++ NULL, TEST_LOCAL_SOA_YES), USE_NAME_RESULT_SKIP); |
| 98 | ++ ck_assert_int_eq(verify_name_allowed_with_soa("example4.sub.local", |
| 99 | ++ NULL, TEST_LOCAL_SOA_NO), USE_NAME_RESULT_SKIP); |
| 100 | ++ ck_assert_int_eq(verify_name_allowed_with_soa("example4.sub.local", |
| 101 | ++ NULL, TEST_LOCAL_SOA_AUTO), USE_NAME_RESULT_SKIP); |
| 102 | ++ ck_assert_int_eq(verify_name_allowed_with_soa("example1.local", |
| 103 | ++ NULL, TEST_LOCAL_SOA_YES), USE_NAME_RESULT_OPTIONAL); |
| 104 | ++ ck_assert_int_eq(verify_name_allowed_with_soa("example2.local", |
| 105 | ++ NULL, TEST_LOCAL_SOA_NO), USE_NAME_RESULT_AUTHORITATIVE); |
| 106 | ++ /* TEST_LOCAL_SOA_AUTO would test actual DNS on host, skip that. */ |
| 107 | + } |
| 108 | + END_TEST |
| 109 | + |
| 110 | +-- |
| 111 | +2.38.1 |
| 112 | + |
0 commit comments