summaryrefslogtreecommitdiff
path: root/payloads/libpayload/tests/drivers/speaker-test.c
diff options
context:
space:
mode:
authorJakub Czapiga <jacz@semihalf.com>2021-10-08 09:57:14 +0000
committerFelix Held <felix-coreboot@felixheld.de>2021-10-18 12:33:49 +0000
commit12ae850dfc10709f8c3bcf92ab7ba1397eb4ae43 (patch)
treec31b1c942ba91c67a11103695b47366580fdb3e3 /payloads/libpayload/tests/drivers/speaker-test.c
parente8b6b07bfc4cca2e7554ac8fecb6ddf88f89bc0e (diff)
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 <jacz@semihalf.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/58242 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org> Reviewed-by: Yu-Ping Wu <yupingso@google.com>
Diffstat (limited to 'payloads/libpayload/tests/drivers/speaker-test.c')
-rw-r--r--payloads/libpayload/tests/drivers/speaker-test.c148
1 files changed, 148 insertions, 0 deletions
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 <libpayload.h>
+#include <mocks/x86_io.h>
+
+/* Include source to gain access to private defines */
+#include "../drivers/speaker.c"
+
+#include <tests/test.h>
+
+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);
+}