[RFC PATCH 0/5] tty: Add KUnit test framework for TTY drivers

Abhinav Saxena posted 5 patches 1 month, 1 week ago
drivers/tty/Kconfig                  |   9 +
drivers/tty/Makefile                 |   7 +
drivers/tty/tests/.kunitconfig       |  44 ++++
drivers/tty/tests/Kconfig            |  44 ++++
drivers/tty/tests/Makefile           |   2 +
drivers/tty/tests/test_tty_io_core.c | 249 ++++++++++++++++++++++
drivers/tty/tests/test_ttynull.c     | 163 +++++++++++++++
drivers/tty/tests/tty_mock.c         | 186 +++++++++++++++++
drivers/tty/tests/tty_mock.h         |  34 +++
drivers/tty/tests/tty_test_helpers.c | 387 +++++++++++++++++++++++++++++++++++
drivers/tty/tests/tty_test_helpers.h | 239 +++++++++++++++++++++
drivers/tty/tty_io.c                 |   4 +
drivers/tty/ttynull.c                |   5 +
13 files changed, 1373 insertions(+)
[RFC PATCH 0/5] tty: Add KUnit test framework for TTY drivers
Posted by Abhinav Saxena 1 month, 1 week ago
This patch series introduces a KUnit testing framework for the TTY
subsystem, enabling deterministic, automated testing of TTY drivers and
core functionality without requiring hardware or userspace interaction.

On an x86_64 build with CONFIG_GCOV enabled, these tests increased
TTY subsystem coverage to approximately 10.6% line coverage and
14.7% function coverage [1].

Problem Statement
-----------------
Testing TTY drivers today requires:
- User-space interaction through device nodes
- Complex setup with ptys or real hardware
- Limited ability to test error paths reliably and deterministically

This series solves these issues by providing in-kernel KUnit tests that
exercise real TTY core paths under controlled, deterministic conditions.

What This Series Provides
-------------------------
1. Reusable test helpers (`tty_test_helpers.h`):
   - Minimal (~150 LOC) infrastructure that any TTY driver should be
   able to use
   - Automatic resource management
   - Integrated into core files under KUnit guard, with
     `EXPORT_SYMBOL_IF_KUNIT()` to keep the production symbol table
     clean

2. Mock TTY driver:
   - Demonstrates how drivers can leverage the helpers
   - Enables deterministic scenarios without hardware

3. Core TTY tests:
   - Validate open/close/read/write/termios paths
   - Exercise hangup, resize, and error handling
   - Ensure real kernel paths are tested, not mocked stubs

4. ttynull driver tests:
   - Validate data sink behavior of the null driver
   - Provide a minimal driver contract baseline

5. Optional coverage support:
   - GCOV integration for test coverage analysis

Future Work
-----------
With this foundation merged, follow-up work can:
- Add more coverage of TTY core functions
- Enable each TTY driver to maintain its own KUnit suite
- Introduce stress tests and race detection
- Extend to include more tests for other tty drivers:
  - UART drivers: test interrupt handling without hardware
  - USB serial: validate disconnect and reconnect sequences
  - PTY drivers: test resize, flow control, and hangups
  - Virtual consoles: test Unicode and input handling
  
Testing
-------
- All patches pass `checkpatch.pl`
- Verified on x86_64 with:
    ./tools/testing/kunit/kunit.py run \
        --kunitconfig=.kunit/ \
        --kunitconfig=drivers/tty/tests/.kunitconfig \
        --arch=x86_64
- All tests pass (working around tty_read wrapper in progress)

Feedback welcome! :)

References
----------
[1] Coverage reports: ttytests.haunted2bwanted.me (alt: linux-9ik.pages.dev)
[2] kunit.dev/third_party/kernel/docs/usage.html#testing-static-functions
[3] KUnit: docs.kernel.org/dev-tools/kunit/
[4] TTY driver API: https://docs.kernel.org/driver-api/serial/
[5] Big thanks to LDD3! (Ch18 especially!)

Signed-off-by: Abhinav Saxena <xandfury@gmail.com>
---
Abhinav Saxena (5):
      tty: Add KUnit test infrastructure configuration
      tty: Add KUnit test helper functions
      tty: Add mock TTY driver for KUnit testing
      tty: Add KUnit tests for core TTY functionality
      tty: Add KUnit tests for ttynull driver

 drivers/tty/Kconfig                  |   9 +
 drivers/tty/Makefile                 |   7 +
 drivers/tty/tests/.kunitconfig       |  44 ++++
 drivers/tty/tests/Kconfig            |  44 ++++
 drivers/tty/tests/Makefile           |   2 +
 drivers/tty/tests/test_tty_io_core.c | 249 ++++++++++++++++++++++
 drivers/tty/tests/test_ttynull.c     | 163 +++++++++++++++
 drivers/tty/tests/tty_mock.c         | 186 +++++++++++++++++
 drivers/tty/tests/tty_mock.h         |  34 +++
 drivers/tty/tests/tty_test_helpers.c | 387 +++++++++++++++++++++++++++++++++++
 drivers/tty/tests/tty_test_helpers.h | 239 +++++++++++++++++++++
 drivers/tty/tty_io.c                 |   4 +
 drivers/tty/ttynull.c                |   5 +
 13 files changed, 1373 insertions(+)
---
base-commit: 8d245acc1e884e89f0808f64d6af3fc91d4903a0
change-id: 20250824-tty-tests-7fcd8b2b093e

Best regards,
-- 
Abhinav Saxena <xandfury@gmail.com>
Re: [RFC PATCH 0/5] tty: Add KUnit test framework for TTY drivers
Posted by Jiri Slaby 1 month ago
On 27. 08. 25, 0:51, Abhinav Saxena wrote:
> This patch series introduces a KUnit testing framework for the TTY
> subsystem, enabling deterministic, automated testing of TTY drivers and
> core functionality without requiring hardware or userspace interaction.
> 
> On an x86_64 build with CONFIG_GCOV enabled, these tests increased
> TTY subsystem coverage to approximately 10.6% line coverage and
> 14.7% function coverage [1].
> 
> Problem Statement
> -----------------
> Testing TTY drivers today requires:
> - User-space interaction through device nodes
> - Complex setup with ptys or real hardware
> - Limited ability to test error paths reliably and deterministically
> 
> This series solves these issues by providing in-kernel KUnit tests that
> exercise real TTY core paths under controlled, deterministic conditions.
> 
> What This Series Provides
> -------------------------
> 1. Reusable test helpers (`tty_test_helpers.h`):
>     - Minimal (~150 LOC) infrastructure that any TTY driver should be
>     able to use
>     - Automatic resource management
>     - Integrated into core files under KUnit guard, with
>       `EXPORT_SYMBOL_IF_KUNIT()` to keep the production symbol table
>       clean
> 
> 2. Mock TTY driver:
>     - Demonstrates how drivers can leverage the helpers
>     - Enables deterministic scenarios without hardware
> 
> 3. Core TTY tests:
>     - Validate open/close/read/write/termios paths
>     - Exercise hangup, resize, and error handling
>     - Ensure real kernel paths are tested, not mocked stubs
> 
> 4. ttynull driver tests:
>     - Validate data sink behavior of the null driver
>     - Provide a minimal driver contract baseline
> 
> 5. Optional coverage support:
>     - GCOV integration for test coverage analysis
> 
> Future Work
> -----------
> With this foundation merged, follow-up work can:
> - Add more coverage of TTY core functions
> - Enable each TTY driver to maintain its own KUnit suite
> - Introduce stress tests and race detection
> - Extend to include more tests for other tty drivers:
>    - UART drivers: test interrupt handling without hardware
>    - USB serial: validate disconnect and reconnect sequences
>    - PTY drivers: test resize, flow control, and hangups
>    - Virtual consoles: test Unicode and input handling
>    
> Testing
> -------
> - All patches pass `checkpatch.pl`
> - Verified on x86_64 with:
>      ./tools/testing/kunit/kunit.py run \
>          --kunitconfig=.kunit/ \
>          --kunitconfig=drivers/tty/tests/.kunitconfig \
>          --arch=x86_64
> - All tests pass (working around tty_read wrapper in progress)
> 
> Feedback welcome! :)

Wow, looks good. Has it found something yet :)?


FWIW
Reviewed-by: Jiri Slaby <jirislaby@kernel.org>

thanks,
-- 
js
suse labs
Re: [RFC PATCH 0/5] tty: Add KUnit test framework for TTY drivers
Posted by Abhinav Saxena 1 month ago
Hi!

Jiri Slaby <jirislaby@kernel.org> writes:

> On 27. 08. 25, 0:51, Abhinav Saxena wrote:
>> This patch series introduces a KUnit testing framework for the TTY
>> subsystem, enabling deterministic, automated testing of TTY drivers and
>> core functionality without requiring hardware or userspace interaction.
>> On an x86_64 build with CONFIG_GCOV enabled, these tests increased
>> TTY subsystem coverage to approximately 10.6% line coverage and
>> 14.7% function coverage [1].
>> Problem Statement
>> —————–
>> Testing TTY drivers today requires:
>> - User-space interaction through device nodes
>> - Complex setup with ptys or real hardware
>> - Limited ability to test error paths reliably and deterministically
>> This series solves these issues by providing in-kernel KUnit tests
>> that
>> exercise real TTY core paths under controlled, deterministic conditions.
>> What This Series Provides
>> ————————-
>> 1. Reusable test helpers (`tty_test_helpers.h`):
>>     - Minimal (~150 LOC) infrastructure that any TTY driver should be
>>     able to use
>>     - Automatic resource management
>>     - Integrated into core files under KUnit guard, with
>>       `EXPORT_SYMBOL_IF_KUNIT()` to keep the production symbol table
>>       clean
>> 2. Mock TTY driver:
>>     - Demonstrates how drivers can leverage the helpers
>>     - Enables deterministic scenarios without hardware
>> 3. Core TTY tests:
>>     - Validate open/close/read/write/termios paths
>>     - Exercise hangup, resize, and error handling
>>     - Ensure real kernel paths are tested, not mocked stubs
>> 4. ttynull driver tests:
>>     - Validate data sink behavior of the null driver
>>     - Provide a minimal driver contract baseline
>> 5. Optional coverage support:
>>     - GCOV integration for test coverage analysis
>> Future Work
>> ———–
>> With this foundation merged, follow-up work can:
>> - Add more coverage of TTY core functions
>> - Enable each TTY driver to maintain its own KUnit suite
>> - Introduce stress tests and race detection
>> - Extend to include more tests for other tty drivers:
>>    - UART drivers: test interrupt handling without hardware
>>    - USB serial: validate disconnect and reconnect sequences
>>    - PTY drivers: test resize, flow control, and hangups
>>    - Virtual consoles: test Unicode and input handling
>>    Testing
>> ——-
>> - All patches pass `checkpatch.pl`
>> - Verified on x86_64 with:
>>      ./tools/testing/kunit/kunit.py run \
>>          –kunitconfig=.kunit/ \
>>          –kunitconfig=drivers/tty/tests/.kunitconfig \
>>          –arch=x86_64
>> - All tests pass (working around tty_read wrapper in progress)
>> Feedback welcome! :)
>
> Wow, looks good. Has it found something yet :)?
>

Not yet. But I am kinda excited about having fixture based tests which
can be applied to different tty drivers and test things like
race conditions, CVE-class vulnerabilities, edge cases among other things.

One step at a time, I guess :)

>
> FWIW
> Reviewed-by: Jiri Slaby <jirislaby@kernel.org>
>
> thanks,

Thanks for the review!

-Abhinav