Skip to content

Commit

Permalink
Add unit test parts for new autodetection
Browse files Browse the repository at this point in the history
Use new enum to specify forced present or missing .local SOA record. Use
from production code auto value, but use forced values from unit test.
Add few different results to unit test.
  • Loading branch information
pemensik committed Dec 7, 2022
1 parent 0cbe3ff commit f42e943
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/nss.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ enum nss_status _nss_mdns_gethostbyname_impl(const char* name, int af,
#ifndef MDNS_MINIMAL
mdns_allow_file = fopen(MDNS_ALLOW_FILE, "r");
#endif
result = verify_name_allowed_with_soa(name, mdns_allow_file);
result = verify_name_allowed_with_soa(name, mdns_allow_file,
TEST_LOCAL_SOA_AUTO);
#ifndef MDNS_MINIMAL
if (mdns_allow_file)
fclose(mdns_allow_file);
Expand Down
7 changes: 5 additions & 2 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ int ends_with(const char* name, const char* suffix) {
return strcasecmp(name + ln - ls, suffix) == 0;
}

use_name_result_t verify_name_allowed_with_soa(const char* name, FILE* mdns_allow_file) {
use_name_result_t verify_name_allowed_with_soa(const char* name,
FILE* mdns_allow_file,
test_local_soa_t test) {
switch (verify_name_allowed(name, mdns_allow_file)) {
case VERIFY_NAME_RESULT_NOT_ALLOWED:
return USE_NAME_RESULT_SKIP;
case VERIFY_NAME_RESULT_ALLOWED:
return USE_NAME_RESULT_AUTHORITATIVE;
case VERIFY_NAME_RESULT_ALLOWED_IF_NO_LOCAL_SOA:
if (local_soa())
if (test == TEST_LOCAL_SOA_YES ||
(test == TEST_LOCAL_SOA_AUTO && local_soa()) )
/* Make multicast resolution not authoritative for .local zone.
* Allow continuing to unicast resolution after multicast had not worked. */
return USE_NAME_RESULT_OPTIONAL;
Expand Down
9 changes: 8 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ typedef enum {
USE_NAME_RESULT_OPTIONAL,
} use_name_result_t;

typedef enum {
TEST_LOCAL_SOA_NO,
TEST_LOCAL_SOA_YES,
TEST_LOCAL_SOA_AUTO,
} test_local_soa_t;

// Returns true if we should try to resolve the name with mDNS.
//
// If mdns_allow_file is NULL, then this implements the "local" SOA
Expand All @@ -78,7 +84,8 @@ typedef enum {
// The two heuristics described above are disabled if mdns_allow_file
// is not NULL.
use_name_result_t verify_name_allowed_with_soa(const char* name,
FILE* mdns_allow_file);
FILE* mdns_allow_file,
test_local_soa_t test);

typedef enum {
VERIFY_NAME_RESULT_NOT_ALLOWED,
Expand Down
13 changes: 13 additions & 0 deletions tests/check_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ START_TEST(test_verify_name_allowed_minimal) {
VERIFY_NAME_RESULT_NOT_ALLOWED);
ck_assert_int_eq(verify_name_allowed(".", NULL),
VERIFY_NAME_RESULT_NOT_ALLOWED);

ck_assert_int_eq(verify_name_allowed_with_soa(".", NULL, TEST_LOCAL_SOA_YES),
USE_NAME_RESULT_SKIP);
ck_assert_int_eq(verify_name_allowed_with_soa(".", NULL, TEST_LOCAL_SOA_NO),
USE_NAME_RESULT_SKIP);
ck_assert_int_eq(verify_name_allowed_with_soa("example3.sub.local",
NULL, TEST_LOCAL_SOA_YES), USE_NAME_RESULT_SKIP);
ck_assert_int_eq(verify_name_allowed_with_soa("example4.sub.local",
NULL, TEST_LOCAL_SOA_NO), USE_NAME_RESULT_SKIP);
ck_assert_int_eq(verify_name_allowed_with_soa("example1.local",
NULL, TEST_LOCAL_SOA_YES), USE_NAME_RESULT_OPTIONAL);
ck_assert_int_eq(verify_name_allowed_with_soa("example2.local",
NULL, TEST_LOCAL_SOA_NO), USE_NAME_RESULT_AUTHORITATIVE);
}
END_TEST

Expand Down

0 comments on commit f42e943

Please sign in to comment.