bool test_to_array(void) { struct bib_entry *first_bib, *second_bib; struct bib_entry **array; int array_size; // Create and insert BIBs. first_bib = create_bib_entry(0, 0); if (!first_bib) { log_warning("Could not allocate the first BIB entry."); return false; } if (!bib_add(first_bib, IPPROTO_UDP)) { log_warning("Could not add the first BIB entry."); return false; } second_bib = create_bib_entry(1, 1); if (!second_bib) { log_warning("Could not allocate the second BIB entry."); return false; } if (!bib_add(second_bib, IPPROTO_UDP)) { log_warning("Could not add the second BIB entry."); return false; } // Call the function. array_size = bib_to_array(IPPROTO_UDP, &array); // Return value validations. if (array_size == -1) { log_warning("bib_to_array could not allocate the array."); goto free; } if (array_size != 2) { log_warning("Inserted 2 bibs, but the resulting array length is %d.", array_size); goto free; } // Array content validations. if (!bib_entry_equals(first_bib, array[0]) && !bib_entry_equals(first_bib, array[1])) { log_warning("The result array does not contain the first BIB entry."); goto free; } if (!bib_entry_equals(second_bib, array[0]) && !bib_entry_equals(second_bib, array[1])) { log_warning("The result array does not contain the second BIB entry."); goto free; } kfree(array); return true; free: kfree(array); return false; }
bool test_for_each(void) { struct bib_entry *bib1, *bib2; struct loop_summary summary = { .bib1 = NULL, .bib2 = NULL }; bool success = true; bib1 = create_and_insert_bib(0, 0, IPPROTO_UDP); if (!bib1) return false; bib2 = create_and_insert_bib(1, 1, IPPROTO_UDP); if (!bib2) return false; success &= assert_equals_int(0, bib_for_each(IPPROTO_UDP, for_each_func, &summary), ""); success &= assert_true(bib_entry_equals(bib1, summary.bib1) || bib_entry_equals(bib1, summary.bib2), ""); success &= assert_true(bib_entry_equals(bib2, summary.bib2) || bib_entry_equals(bib2, summary.bib2), ""); return success; }
bool assert_bib_entry_equals(struct bib_entry* expected, struct bib_entry* actual, char* test_name) { if (expected == actual) return true; if (expected == NULL) { log_warning("Test '%s' failed: Expected null, got " BIB_PRINT_KEY ".", test_name, PRINT_BIB(actual)); return false; } if (actual == NULL) { log_warning("Test '%s' failed: Expected " BIB_PRINT_KEY ", got null.", test_name, PRINT_BIB(expected)); return false; } if (!bib_entry_equals(expected, actual)) { log_warning("Test '%s' failed: Expected " BIB_PRINT_KEY " got " BIB_PRINT_KEY ".", test_name, PRINT_BIB(expected), PRINT_BIB(actual)); return false; } return true; }