From 12ae850dfc10709f8c3bcf92ab7ba1397eb4ae43 Mon Sep 17 00:00:00 2001 From: Jakub Czapiga Date: Fri, 8 Oct 2021 09:57:14 +0000 Subject: libpayload: Add unit-tests framework and first test case This commit adds a unit-tests framework ported from coreboot, and test for drivers/speaker. Usage of the unit-tests framework is same as for the coreboot one. Change-Id: Iaa94ee4dcdc3f74af830113813df0e8fb0b31e4f Signed-off-by: Jakub Czapiga Reviewed-on: https://review.coreboot.org/c/coreboot/+/58242 Tested-by: build bot (Jenkins) Reviewed-by: Paul Fagerburg Reviewed-by: Yu-Ping Wu --- payloads/libpayload/tests/drivers/speaker-test.c | 148 +++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 payloads/libpayload/tests/drivers/speaker-test.c (limited to 'payloads/libpayload/tests/drivers/speaker-test.c') diff --git a/payloads/libpayload/tests/drivers/speaker-test.c b/payloads/libpayload/tests/drivers/speaker-test.c new file mode 100644 index 0000000000..199fa58864 --- /dev/null +++ b/payloads/libpayload/tests/drivers/speaker-test.c @@ -0,0 +1,148 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include + +/* Include source to gain access to private defines */ +#include "../drivers/speaker.c" + +#include + +void outb(unsigned char val, int port) +{ + check_expected(val); + check_expected(port); +} + +unsigned char inb(int port) +{ + check_expected(port); + return mock_type(unsigned char); +} + +static void setup_speaker_enable_calls(u16 freq, u8 port_val) +{ + /* Minimal correct value should be at leaset 256. For values lowe than that, + counter will have an incorrect value. Regardless, there is */ + u16 reg16 = 1193180 / freq; + + /* Select counter 2 */ + expect_value(outb, val, 0xb6); + expect_value(outb, port, I82C54_CONTROL_WORD_REGISTER); + + /* Write freq. [LSB, MSB] */ + expect_value(outb, val, (u8)(reg16 & 0xff)); + expect_value(outb, port, I82C54_COUNTER2); + expect_value(outb, val, (u8)(reg16 >> 8)); + expect_value(outb, port, I82C54_COUNTER2); + + /* Enable PC speaker */ + expect_value(inb, port, PC_SPEAKER_PORT); + will_return(inb, port_val); + expect_value(outb, val, port_val | 0x3); + expect_value(outb, port, PC_SPEAKER_PORT); +} + +static void test_speaker_enable(void **state) +{ + setup_speaker_enable_calls(1, 0); + speaker_enable(1); + + setup_speaker_enable_calls(1, 0xff); + speaker_enable(1); + + setup_speaker_enable_calls(1, 123); + speaker_enable(1); + + setup_speaker_enable_calls(1, -1); + speaker_enable(1); + + setup_speaker_enable_calls(-1, 0); + speaker_enable(-1); + + setup_speaker_enable_calls(-1, 0xff); + speaker_enable(-1); + + setup_speaker_enable_calls(-1, 222); + speaker_enable(-1); + + setup_speaker_enable_calls(-1, -1); + speaker_enable(-1); + + setup_speaker_enable_calls(10000, 0); + speaker_enable(10000); + + setup_speaker_enable_calls(10000, 0xff); + speaker_enable(10000); + + setup_speaker_enable_calls(10000, 91); + speaker_enable(10000); + + setup_speaker_enable_calls(10000, -1); + speaker_enable(10000); +} + +static void setup_speaker_disable_calls(u8 value) +{ + expect_value(inb, port, PC_SPEAKER_PORT); + will_return(inb, value); + expect_value(outb, val, value & 0xfc); + expect_value(outb, port, PC_SPEAKER_PORT); +} + +static void test_speaker_disable(void **state) +{ + setup_speaker_disable_calls(0); + speaker_disable(); + + setup_speaker_disable_calls(0xfc); + speaker_disable(); + + setup_speaker_disable_calls(0xff); + speaker_disable(); + + setup_speaker_disable_calls(0xff - 0xfc); + speaker_disable(); +} + +void arch_ndelay(uint64_t ns) +{ + check_expected(ns); +} + +static void setup_speaker_tone_calls(u16 freq, unsigned int duration) +{ + setup_speaker_enable_calls(freq, ~freq & 0xff); + expect_value(arch_ndelay, ns, (uint64_t)duration * NSECS_PER_MSEC); + setup_speaker_disable_calls(0xff); + expect_any(arch_ndelay, ns); +} + +static void test_speaker_tone(void **state) +{ + setup_speaker_tone_calls(500, 100); + speaker_tone(500, 100); + + setup_speaker_tone_calls(4321, 0); + speaker_tone(4321, 0); + + setup_speaker_tone_calls(-1, -1); + speaker_tone(-1, -1); + + setup_speaker_tone_calls(10000, 1000); + speaker_tone(10000, 1000); + + setup_speaker_tone_calls(433, 890); + speaker_tone(433, 890); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_speaker_enable), + cmocka_unit_test(test_speaker_disable), + cmocka_unit_test(test_speaker_tone), + }; + + return lp_run_group_tests(tests, NULL, NULL); +} -- cgit v1.2.3