Testing microcontroller firmware with Python

Last year’s talk showed you how to use CFFI to write unit tests for C code in Python. This year we will take the concept one step further and create integration tests covering (almost) the whole firmware of a microcontroller, again leveraging the power of CFFI.

But instead of running the firmware on the controller, it will be executed on the development machine (that is, a standard x86 architecture), allowing for much faster test execution, without requiring the target hardware. For this to work, all the hardware-dependent parts of the firmware code need to be replaced by Python code simulating the hardware functionality, so that all the firmware above this hardware abstraction layer can be executed unmodified.

In addition, this allows to use advanced security testing tools like AddressSanitizer and american fuzzy lop that would not be able to run directly on the microcontroller.