Skip to content

TFTP examplesTesting

Elbasiouny, Mahmoud edited this page May 29, 2026 · 6 revisions

Once you've built the TFTP parser, the next step is to verify it correctly accepts valid packets and rejects invalid ones.

  • Good packets should be accepted (parse succeeds)
  • Bad packets should be rejected (parse fails)

Source files: main.c, tests.c, tests.h Hammer concepts: h_parse failure propagation · structural validation


Project Structure

examples/tftp/
├── Makefile       # Build system
├── main.c         # Entry point, calls test runners
├── tests.c        # Test cases for each packet type
├── tests.h        # Test runner function declarations
└── tftp.c         # TFTP protocol grammar

Test cases are defined in tests.c as byte arrays representing valid and invalid TFTP packets. Each packet type has its own test runner function (run_rrqwrq_tests, run_data_tests, run_ack_tests, run_error_tests) declared in tests.h and called from main.c.


Step 1: Build the Parser

From the Hammer repository root, build the checkout-local library first:

scons examples

Then build the TFTP example:

cd examples/tftp
make

This produces the tftp executable. If ../../build/opt/src is not present, the Makefile falls back to pkg-config libhammer.


Step 2: Run the Tests

./tftp

The executable runs each test case and reports the result.

Expected output for a passing test

RRQ/WRQ Test 1 (normal write request): PASSED ✓

Expected output for a failing test (correctly rejected invalid input)

RRQ/WRQ Test 3 (invalid op code): PASSED ✓ (correctly rejected)

Expected output when all tests pass

All tests passed!

What the Tests Cover

The test suite exercises each packet type with both valid and invalid inputs:

  • RRQ/WRQ - Valid filenames and modes, invalid opcodes, missing terminators
  • DATA - Valid block numbers and payloads, invalid opcodes
  • ACK - Valid block numbers, invalid opcodes
  • ERROR - Valid error codes and messages, invalid error codes, missing terminators

Summary

Task Command
Build make
Run tests ./tftp

When everything is correct, all valid packets are accepted and all invalid packets are rejected.


Previous: Assembling the Parser

Back to: Examples Index

Clone this wiki locally