-
Notifications
You must be signed in to change notification settings - Fork 1
DNS Running and Testing
Once you've built the DNS validator, the next step is to prove it behaves like a strict grammar checker:
-
Good packets should be accepted (
Packet Passed) -
Bad packets should be rejected (
Packet Failed)
This project supports two workflows:
- Run one packet at a time (good for debugging)
-
Run the full test suite using
tests/runtests.py(good for regression testing)
Source files:
main.c,tests/runtests.py
Hammer concepts:h_parsefailure propagation · count-driven parsing · structural validation
.
├── Makefile
├── main.c
├── dns.c
├── dns.h
└── tests/
├── good_packets/
├── bad_packets/
└── runtests.py
The test suite is intentionally split into:
-
tests/good_packets/- packets expected to be accepted -
tests/bad_packets/- packets expected to be rejected
From the Hammer repository root, build the checkout-local library first:
scons examplesThen build the DNS example:
cd examples/dns
makeThis should produce the dns_parser executable in examples/dns. If ../../build/opt/src is not present, the Makefile falls back to pkg-config libhammer.
Manual testing is the fastest way to debug one specific failure.
From examples/dns:
./dns_parser tests/good_packets/1q_pass.binExpected output:
Packet Passed
./dns_parser tests/bad_packets/truncated_header.binExpected output:
Packet Failed
The repository includes a Python script that runs all packets in both directories and checks that they match expectations.
- Runs every file in
tests/good_packets/- expects
"Packet Passed"
- expects
- Runs every file in
tests/bad_packets/- expects rejection (anything that is not
"Packet Passed")
- expects rejection (anything that is not
The output is reported as:
-
[PASS] (packet accepted)for good packets that pass -
[PASS] (packet rejected)for bad packets that fail -
[FAIL]for mismatches
From inside the tests/ directory:
cd tests
python3 runtests.pyYou should see output like:
Testing good packets...
[PASS] (packet accepted) good_packets/1q_pass.bin
...
Testing bad packets...
[PASS] (packet rejected) bad_packets/truncated_header.bin
...
Good packets are designed to exercise valid grammar variations, such as:
- Standard query format
- Standard response format
- Multiple questions / multiple answers
- Pointer-only names (
C0 0Cform) - Label + pointer names (
03 'www' C0 0Cform) - Mixed sections (answers + authorities + additionals)
- AAAA queries and uppercase labels
Bad packets are designed to trigger specific invariants, such as:
-
Pointer flag bits not
11bad_pointer_pflag_not_11.binbad_label_plus_pointer_pflag_not_11.bin
-
Pointer offsets invalid
bad_pointer_offset_out_of_range.bininvalid_aname_compression.bin
-
Count mismatches
wrong_qdcount.binbad_qdcount_fail.bin
-
Truncation / out-of-bounds
truncated_header.bintruncated_packet_fail.bintruncated_question.bintruncated_rdata.bin
-
Invalid fields
invalid_qclass.bininvalid_rtype.bininvalid_rclass.bin
-
Label encoding violations
zero_length_label.binlabel_too_long.bin
-
Invalid flags
invalid_qr_flag.bin
Common causes:
- You didn't run
makefirst - You're running from the wrong working directory (the script assumes it's run inside
tests/and looks for../dns_parser)
| Task | Command |
|---|---|
| Build | make |
| Run one packet | ./dns_parser tests/good_packets/<file>.bin |
| Run all tests | cd tests && python3 runtests.py |
When everything is correct, all good packets are accepted and all bad packets are rejected - giving you a repeatable regression suite for future parser changes.
Previous: Assembling the Full Parser
Back to: Examples Index
Learn Hammer
Protocol Examples
NTP
- NTP Overview
- Parsing the Header
- Parsing Data Fields
- Extension Fields and MAC
- Assembling the Parser
- Hex Input Preprocessing
- Running and Testing
DNS
TFTP
- TFTP Overview
- RRQ/WRQ Packets
- DATA Packets
- ACK Packets
- ERROR Packets
- Assembling the Parser
- Running and Testing
References
- Hammer Quick Reference
- Parsing Backends
- Unit Testing
- Using RTEMS
- Extending Hammer
- Adding a New Example
- Adding a New Binding
Further Reading