-
Notifications
You must be signed in to change notification settings - Fork 2
Add comprehensive DNS cache tests in new dns_cache.c file #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dns-cache
Are you sure you want to change the base?
Add comprehensive DNS cache tests in new dns_cache.c file #7
Conversation
* dns: gwproxy: Swap out the old DNS setup for the new library version Link: #3 Signed-off-by: Ammar Faizi <[email protected]>
If there are 1 entries and 5 workers, and nr_sleeping is 5. It will always batch because nr_entries + 16 is always greater than 5. That's just wrong. Fixes: 317dc79 ("dns: Handle DNS query in batch via `getaddrinfo_a()`") Signed-off-by: Ammar Faizi <[email protected]>
…n_handle_data()` The `gwp_socks5_conn_handle_data()` does not properly handle socks5 data if the CONNECT command comes together with the proxied data. Fixes: 97a3bc1 ("socks5: Initial socks5 modularization") Signed-off-by: Ammar Faizi <[email protected]>
`gwp_socks5_conn_handle_data()` may return `-EAGAIN` and have `in_len` or `out_len` greater than zero. Make sure to account those values. Fixes: 3fad620 ("gwproxy: socks5: Use the socks5 library") Signed-off-by: Ammar Faizi <[email protected]>
The call to `handle_socks5_data()` must only be done when the connection is in `CONN_STATE_SOCKS5_DATA` state. Fixes: 3fad620 ("gwproxy: socks5: Use the socks5 library") Signed-off-by: Ammar Faizi <[email protected]>
After stress-testing the SOCKS5 proxy server, I found various memory corruption bugs. Here is the stress-test program: https://gist.github.com/ammarfaizi2/53324bb7dd8ca7cb73a92b80b62f8f3b Only the first patch is not related to memory corruption bugs. * bug-fix: gwproxy: Fix an invalid `handle_socks5_data()` call gwproxy: Fix missing buf advance and `target.len` increment gwproxy/socks5: Handle `GWP_SOCKS5_ST_CMD_CONNECT` in `gwp_socks5_conn_handle_data()` gwproxy/dns: Fix the wrong batch condition decision Link: #4 Signed-off-by: Ammar Faizi <[email protected]>
For future reusability. Signed-off-by: Ammar Faizi <[email protected]>
* extract-syscall: gwproxy: Split syscall code into a separate header file Signed-off-by: Ammar Faizi <[email protected]>
The old DNS cache setup in dns.c was kinda clunky because it needed to
slog through everything in O(n) time. This patch series swaps it out
for something way better and tidier. Create a new C file, dns_cache.c,
just for the DNS cache stuff to keep things nice and separate.
Plus, the storage is now smarter about memory, using this compact
structure:
```
struct gwp_dns_cache_entry {
/**
* Super compact domain name and IP address list.
*
* - name_len: length of the domain name (including null terminator).
* - nr_i4: number of IPv4 addresses.
* - nr_i6: number of IPv6 addresses.
* - block: flexible array member that contains:
* - The first name_len octets are the domain name.
* - The next nr_i4 * 4 octets are IPv4 addresses.
* - The next nr_i6 * 16 octets are IPv6 addresses.
*/
uint8_t name_len;
uint8_t nr_i4;
uint8_t nr_i6;
uint8_t block[];
};
```
Also, the lookup now uses a hashmap, so it's super fast, O(1) on average.
* dns-cache:
gwproxy/dns: Replace the old DNS cache system with a new one.
gwproxy/dns_cache: Introduce entry block getter functions
gwproxy/dns_cache: Don't return expired cache entry
gwproxy/dns_cache: Add a forward declaration of 'struct addrinfo'
gwproxy/dns_cache: Add `nr_entries` for accounting purpose
gwproxy/dns_cache: Initial DNS cache rework
Link: #5
Signed-off-by: Ammar Faizi <[email protected]>
Co-authored-by: ammarfaizi2 <[email protected]>
src/gwproxy/tests/dns_cache.c
Outdated
|
|
||
| printf("All DNS cache tests passed!\n"); | ||
| return 0; | ||
| } No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
End with a blank line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit a84e16a - added trailing newline to the end of the file.
Co-authored-by: ammarfaizi2 <[email protected]>
src/gwproxy/tests/dns_cache.c
Outdated
| assert(ai != NULL); | ||
|
|
||
| /* Insert entry */ | ||
| time_t expire_time = time(NULL) + 300; /* 5 minutes from now */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put all variable declarations at the beginning of the scope (so move that time_t var to the top). Apply that to other places, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit 840f5cd - moved all variable declarations to the beginning of their respective function scopes across all test functions.
src/gwproxy/tests/dns_cache.c
Outdated
| test_dns_cache_init_free(); | ||
| test_dns_cache_basic_insert_lookup(); | ||
| test_dns_cache_ipv6_support(); | ||
| test_dns_cache_mixed_ipv4_ipv6(); | ||
| test_dns_cache_entry_replacement(); | ||
| test_dns_cache_expiration(); | ||
| test_dns_cache_housekeeping(); | ||
| test_dns_cache_hash_collisions(); | ||
| test_dns_cache_reference_counting(); | ||
| test_dns_cache_invalid_inputs(); | ||
| test_dns_cache_large_dataset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test these 5000 times.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified the main function to run all DNS cache tests 5000 times in a loop with progress indication every 500 iterations. Commit 8f3c942
Co-authored-by: ammarfaizi2 <[email protected]>
Co-authored-by: ammarfaizi2 <[email protected]>
ammarfaizi2
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebase on top of the dns-cache branch.
Rebased on top of the dns-cache branch (commit 82c86a7) as requested. All DNS cache tests continue to pass successfully across 5000 iterations. Commit 219e83f |
Summary
This PR adds comprehensive test coverage for the DNS cache functionality by creating a new dedicated test file
src/gwproxy/tests/dns_cache.c. The tests follow existing patterns in the codebase and provide thorough validation of all DNS cache operations.Changes Made
New Test File:
src/gwproxy/tests/dns_cache.ctest_dns_cache_init_free()- Basic initialization and cleanuptest_dns_cache_basic_insert_lookup()- Insert and lookup operationstest_dns_cache_ipv6_support()- IPv6 address handlingtest_dns_cache_mixed_ipv4_ipv6()- Mixed IPv4/IPv6 entriestest_dns_cache_entry_replacement()- Entry replacement behaviortest_dns_cache_expiration()- Cache expiration handlingtest_dns_cache_housekeeping()- Cleanup of expired entriestest_dns_cache_hash_collisions()- Hash collision handlingtest_dns_cache_reference_counting()- Reference counting validationtest_dns_cache_invalid_inputs()- Error handling and edge casestest_dns_cache_large_dataset()- Performance with large datasetsUpdated Build System
LIBGWDNS_CACHE_TEST_TARGETand related variablesdns_cache.ttest executabletesttarget to include DNS cache testsTest Coverage
The tests validate all public APIs from
gwproxy/dns_cache.h:gwp_dns_cache_init()andgwp_dns_cache_free()gwp_dns_cache_insert()andgwp_dns_cache_getent()gwp_dns_cache_putent()for reference countinggwp_dns_cache_housekeep()for cleanupgwp_dns_cache_entget_i4()andgwp_dns_cache_entget_i6()Usage
Test Results
All 11 DNS cache tests pass successfully:
Code Quality
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.