Commit 751f871b authored by Andrei Emeltchenko's avatar Andrei Emeltchenko Committed by Carles Cufi
Browse files

tests: acpi: Add unit test for ACPI table parsing



Initial unit test for ACPI table parsing.

Signed-off-by: default avatarAndrei Emeltchenko <andrei.emeltchenko@intel.com>
parent 24b58ecc
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

project(lib_acpi)
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})

# Assert library
add_library(mock_assert STATIC src/assert.c)
target_link_libraries(mock_assert PRIVATE test_interface)
target_compile_options(test_interface INTERFACE -include ztest.h)

target_include_directories(testbinary PRIVATE
  ${ZEPHYR_BASE}/../modules/lib
  ${ZEPHYR_BASE}/../modules/lib/acpica/source/include
  ${ZEPHYR_BASE}/../modules/lib/acpica/source/tools/acpiexec
  )

target_sources(testbinary PRIVATE
  src/main.c
  src/mock.c
  )

target_link_libraries(testbinary PRIVATE mock_assert)
+2 −0
Original line number Diff line number Diff line
CONFIG_ZTEST=y
CONFIG_ASSERT=y
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2022 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "assert.h"

DEFINE_FAKE_VALUE_FUNC(bool, mock_check_if_assert_expected);

void assert_print(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vprintk(fmt, ap);
	va_end(ap);
}

void assert_post_action(const char *file, unsigned int line)
{
	/* ztest_test_pass()/ztest_test_fail() are used to stop the execution
	 * If this is an unexpected assert (i.e. not following expect_assert())
	 * calling mock_check_if_assert_expected() will return 'false' as
	 * a default return value
	 */
	if (mock_check_if_assert_expected() == true) {
		printk("Assertion expected as part of a test case.\n");
		/* Mark the test as passed and stop execution:
		 * Needed in the passing scenario to prevent undefined behavior after hitting the
		 * assert. In real builds (non-UT), the system will be halted by the assert.
		 */
		ztest_test_pass();
	} else {
		/* Mark the test as failed and stop execution */
		ztest_test_fail();
	}
}
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2022 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/fff.h>
#include <stdint.h>
#include <stdbool.h>

#include <zephyr/kernel.h>

/* List of fakes used by this unit tester */
#define ASSERT_FFF_FAKES_LIST(FAKE)                                             \
	FAKE(mock_check_if_assert_expected)                                     \

DECLARE_FAKE_VALUE_FUNC(bool, mock_check_if_assert_expected);

#define expect_assert()     (mock_check_if_assert_expected_fake.return_val = 1)
+101 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2023 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/ztest.h>
#include <string.h>
#include <inttypes.h>

#include "mock.h"

#define CONFIG_ACPI_DEV_MAX 1
#define _AcpiModuleName ""

#include <zephyr/acpi/acpi.h>
#include <lib/acpi/acpi.c>
#include "assert.h"

#include <zephyr/fff.h>
DEFINE_FFF_GLOBALS;

struct DMAR {
	ACPI_TABLE_DMAR header;

	/* Hardware Unit 0 */
	struct {
		ACPI_DMAR_HARDWARE_UNIT header;

		ACPI_DMAR_DEVICE_SCOPE unit0_ds0;
		ACPI_DMAR_PCI_PATH unit0_ds0_path0;

		ACPI_DMAR_DEVICE_SCOPE unit0_ds1;
		ACPI_DMAR_PCI_PATH unit0_ds1_path0;
	} unit0;

	/* Hardware Unit 1 */
	struct {
		ACPI_DMAR_HARDWARE_UNIT header;

		ACPI_DMAR_DEVICE_SCOPE unit1_ds0;
		ACPI_DMAR_PCI_PATH unit1_ds0_path0;

		ACPI_DMAR_DEVICE_SCOPE unit1_ds1;
		ACPI_DMAR_PCI_PATH unit1_ds1_path0;
	} unit1;
};

static struct DMAR dmar0 = {
	.header.Header.Length = sizeof(struct DMAR),

	.unit0.header.Header.Length = sizeof(dmar0.unit0),
	.unit1.header.Header.Length = sizeof(dmar0.unit1),
};

static void dmar_initialize(struct DMAR *dmar)
{
	dmar->unit0.header.Header.Length = sizeof(dmar->unit0);
	dmar->unit1.header.Header.Length = sizeof(dmar->unit1);
}

static void dmar_clear(struct DMAR *dmar)
{
	dmar->unit0.header.Header.Length = 0;
	dmar->unit1.header.Header.Length = 0;
}

ZTEST(lib_acpi, test_nop)
{
}

static void count_subtables(ACPI_DMAR_HEADER *subtable, void *arg)
{
	uint8_t *count = arg;

	(*count)++;
}

ZTEST(lib_acpi, test_dmar_foreach)
{
	uint8_t count = 0;

	dmar_initialize(&dmar0);

	acpi_dmar_foreach_subtable((void *)&dmar0, count_subtables, &count);
	zassert_equal(count, 2);

	TC_PRINT("Counted %u hardware units\n", count);
}

ZTEST(lib_acpi, test_dmar_foreach_invalid_unit_size)
{
	uint8_t count = 0;

	dmar_clear(&dmar0);
	expect_assert();

	acpi_dmar_foreach_subtable((void *)&dmar0, count_subtables, &count);
}

ZTEST_SUITE(lib_acpi, NULL, NULL, NULL, NULL, NULL);
Loading