bbram: Add tests

Add emulation testing for it8xxx2, npcx, and mcp7940n. The test is made
to be generic and uses the backend API in order to verify both read and
write functionality. Additional tests are made for the API's limits when
reading/writing out of bounds.

Fixes #65018

Signed-off-by: Yuval Peress <peress@google.com>
This commit is contained in:
Yuval Peress 2024-01-04 02:13:23 -07:00 committed by Carles Cufí
parent 2772843cf1
commit 4efa11ebf7
8 changed files with 220 additions and 0 deletions

View file

@ -0,0 +1,16 @@
# Copyright 2024 Google LLC
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(bbram)
target_sources(app
PRIVATE
include/fixture.h
src/fixture.c
src/read.c
src/write.c
)
target_include_directories(app PRIVATE include)

View file

@ -0,0 +1,28 @@
/*
* Copyright 2021 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
mcp7940n: mcp7940n@0 {
compatible = "microchip,mcp7940n";
reg = <0x0>;
};
};
/ {
ite8xxx2: ite8xxx2@0 {
#address-cells = <1>;
#size-cells = <1>;
compatible = "ite,it8xxx2-bbram";
status = "okay";
reg = <0x0 0x40>;
};
npcx: npcx@40 {
compatible = "nuvoton,npcx-bbram";
reg = <0x40 0x40
0x80 0x1>;
reg-names = "memory", "status";
};
};

View file

@ -0,0 +1,21 @@
/*
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef TESTS_DRIVERS_BBRAM_GENERIC_INCLUDE_FIXTURE_H_
#define TESTS_DRIVERS_BBRAM_GENERIC_INCLUDE_FIXTURE_H_
#define BBRAM_TEST_IMPL(name, inst) \
ZTEST(generic, test_##name##_##inst) \
{ \
const struct device *dev = DEVICE_DT_GET(inst); \
run_test_##name(dev, get_and_check_emul(dev)); \
}
#define BBRAM_FOR_EACH(fn) \
fn(DT_NODELABEL(mcp7940n)) fn(DT_NODELABEL(ite8xxx2)) fn(DT_NODELABEL(npcx))
const struct emul *get_and_check_emul(const struct device *dev);
#endif /* TESTS_DRIVERS_BBRAM_GENERIC_INCLUDE_FIXTURE_H_ */

View file

@ -0,0 +1,6 @@
# Copyright 2021 Google LLC
# SPDX-License-Identifier: Apache-2.0
CONFIG_ZTEST=y
CONFIG_EMUL=y
CONFIG_BBRAM=y

View file

@ -0,0 +1,25 @@
/*
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/ztest.h>
#include "fixture.h"
ZTEST_SUITE(generic, NULL, NULL, NULL, NULL, NULL);
const struct emul *get_and_check_emul(const struct device *dev)
{
zassert_not_null(dev, "Cannot get device pointer. Is this driver properly instantiated?");
const struct emul *emul = emul_get_binding(dev->name);
/* Skip this sensor if there is no emulator or backend loaded. */
if (emul == NULL || emul->backend_api == NULL) {
ztest_test_skip();
}
return emul;
}

View file

@ -0,0 +1,57 @@
/*
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/drivers/bbram.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/emul_bbram.h>
#include <zephyr/ztest.h>
#include "fixture.h"
static void run_test_read_invalid_size(const struct device *dev, const struct emul *emulator)
{
uint8_t data = 0;
size_t bbram_size = 0;
int rc;
ARG_UNUSED(emulator);
rc = bbram_read(dev, 0, 0, &data);
zassert_equal(-EINVAL, rc, "got %d", rc);
rc = bbram_get_size(dev, &bbram_size);
zassert_ok(rc, "got %d", rc);
rc = bbram_read(dev, 0, bbram_size + 1, &data);
zassert_equal(-EINVAL, rc, "got %d", rc);
}
static void run_test_read_bytes(const struct device *dev, const struct emul *emulator)
{
uint8_t data = 0;
uint8_t expected_data = 0;
size_t bbram_size = 0;
int rc;
rc = bbram_get_size(dev, &bbram_size);
zassert_ok(rc, "got %d", rc);
for (size_t i = 0; i < bbram_size; ++i) {
expected_data = i % BIT(8);
rc = emul_bbram_backend_set_data(emulator, i, 1, &expected_data);
zassert_ok(rc, "Failed to set expected data at offset %zu", i);
rc = bbram_read(dev, i, 1, &data);
zassert_ok(rc, "Failed to read byte at offset %zu", i);
zassert_equal(expected_data, data, "Expected %u, but got %u", expected_data, data);
}
}
#define DECLARE_ZTEST_PER_DEVICE(inst) \
BBRAM_TEST_IMPL(read_invalid_size, inst) \
BBRAM_TEST_IMPL(read_bytes, inst)
BBRAM_FOR_EACH(DECLARE_ZTEST_PER_DEVICE)

View file

@ -0,0 +1,57 @@
/*
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/drivers/bbram.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/emul_bbram.h>
#include <zephyr/ztest.h>
#include "fixture.h"
static void run_test_write_invalid_size(const struct device *dev, const struct emul *emulator)
{
uint8_t data = 0;
size_t bbram_size = 0;
int rc;
ARG_UNUSED(emulator);
rc = bbram_write(dev, 0, 0, &data);
zassert_equal(-EINVAL, rc, "got %d", rc);
rc = bbram_get_size(dev, &bbram_size);
zassert_ok(rc, "got %d", rc);
rc = bbram_write(dev, 0, bbram_size + 1, &data);
zassert_equal(-EINVAL, rc, "got %d", rc);
}
static void run_test_write_bytes(const struct device *dev, const struct emul *emulator)
{
uint8_t data = 0;
uint8_t expected_data = 0;
size_t bbram_size = 0;
int rc;
rc = bbram_get_size(dev, &bbram_size);
zassert_ok(rc, "got %d", rc);
for (size_t i = 0; i < bbram_size; ++i) {
expected_data = i % BIT(8);
rc = bbram_write(dev, i, 1, &expected_data);
zassert_ok(rc, "Failed to set expected data at offset %zu", i);
rc = emul_bbram_backend_get_data(emulator, i, 1, &data);
zassert_ok(rc, "Failed to get byte at offset %zu", i);
zassert_equal(expected_data, data, "Expected %u, but got %u", expected_data, data);
}
}
#define DECLARE_ZTEST_PER_DEVICE(inst) \
BBRAM_TEST_IMPL(write_invalid_size, inst) \
BBRAM_TEST_IMPL(write_bytes, inst)
BBRAM_FOR_EACH(DECLARE_ZTEST_PER_DEVICE)

View file

@ -0,0 +1,10 @@
# Copyright 2024 Google LLC
# SPDX-License-Identifier: Apache-2.0
tests:
drivers.bbram.generic:
tags:
- drivers
- bbram
platform_allow:
- native_sim