Commit b99eb596 authored by Tzung-Bi Shih's avatar Tzung-Bi Shih
Browse files

platform/chrome: cros_ec_proto: add Kunit tests for cros_ec_query_all()



cros_ec_query_all() sends multiple host commands to EC for querying
supported protocols and settings.

Add required mock for interacting with cros_ec_query_all() and Kunit
tests.

Reviewed-by: default avatarGuenter Roeck <groeck@chromium.org>
Signed-off-by: default avatarTzung-Bi Shih <tzungbi@kernel.org>
Link: https://lore.kernel.org/r/20220609084957.3684698-3-tzungbi@kernel.org
parent ea7f0f77
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -268,11 +268,17 @@ config CHROMEOS_PRIVACY_SCREEN
source "drivers/platform/chrome/wilco_ec/Kconfig"

# Kunit test cases
config CROS_KUNIT
	tristate
	help
	  ChromeOS Kunit.

config CROS_EC_PROTO_KUNIT_TEST
	tristate "Kunit tests for ChromeOS EC protocol" if !KUNIT_ALL_TESTS
	depends on KUNIT && CROS_EC
	default KUNIT_ALL_TESTS
	select CROS_EC_PROTO
	select CROS_KUNIT
	help
	  Kunit tests for the ChromeOS Embedded Controller protocol.

+1 −0
Original line number Diff line number Diff line
@@ -32,4 +32,5 @@ obj-$(CONFIG_CROS_USBPD_NOTIFY) += cros_usbpd_notify.o
obj-$(CONFIG_WILCO_EC)			+= wilco_ec/

# Kunit test cases
obj-$(CONFIG_CROS_KUNIT)		+= cros_kunit_util.o
obj-$(CONFIG_CROS_EC_PROTO_KUNIT_TEST)	+= cros_ec_proto_test.o
+802 −0

File changed.

Preview size limit exceeded, changes collapsed.

+98 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/*
 * CrOS Kunit tests utilities.
 */

#include <kunit/test.h>

#include <linux/list.h>
#include <linux/minmax.h>
#include <linux/platform_data/cros_ec_commands.h>
#include <linux/platform_data/cros_ec_proto.h>

#include "cros_ec.h"
#include "cros_kunit_util.h"

int cros_kunit_ec_xfer_mock_default_ret;
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_default_ret);

static struct list_head cros_kunit_ec_xfer_mock_in;
static struct list_head cros_kunit_ec_xfer_mock_out;

int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
{
	struct ec_xfer_mock *mock;

	mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_in, struct ec_xfer_mock, list);
	if (!mock)
		return cros_kunit_ec_xfer_mock_default_ret;

	list_del(&mock->list);

	memcpy(&mock->msg, msg, sizeof(*msg));
	if (msg->outsize) {
		mock->i_data = kunit_kzalloc(mock->test, msg->outsize, GFP_KERNEL);
		if (mock->i_data)
			memcpy(mock->i_data, msg->data, msg->outsize);
	}

	msg->result = mock->result;
	if (msg->insize)
		memcpy(msg->data, mock->o_data, min(msg->insize, mock->o_data_len));

	list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_out);

	return mock->ret;
}
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock);

struct ec_xfer_mock *cros_kunit_ec_xfer_mock_add(struct kunit *test, size_t size)
{
	return cros_kunit_ec_xfer_mock_addx(test, size, EC_RES_SUCCESS, size);
}
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_add);

struct ec_xfer_mock *cros_kunit_ec_xfer_mock_addx(struct kunit *test,
						  int ret, int result, size_t size)
{
	struct ec_xfer_mock *mock;

	mock = kunit_kzalloc(test, sizeof(*mock), GFP_KERNEL);
	if (!mock)
		return NULL;

	list_add_tail(&mock->list, &cros_kunit_ec_xfer_mock_in);
	mock->test = test;

	mock->ret = ret;
	mock->result = result;
	mock->o_data = kunit_kzalloc(test, size, GFP_KERNEL);
	if (!mock->o_data)
		return NULL;
	mock->o_data_len = size;

	return mock;
}
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_addx);

struct ec_xfer_mock *cros_kunit_ec_xfer_mock_next(void)
{
	struct ec_xfer_mock *mock;

	mock = list_first_entry_or_null(&cros_kunit_ec_xfer_mock_out, struct ec_xfer_mock, list);
	if (mock)
		list_del(&mock->list);

	return mock;
}
EXPORT_SYMBOL_GPL(cros_kunit_ec_xfer_mock_next);

void cros_kunit_mock_reset(void)
{
	cros_kunit_ec_xfer_mock_default_ret = 0;
	INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_in);
	INIT_LIST_HEAD(&cros_kunit_ec_xfer_mock_out);
}
EXPORT_SYMBOL_GPL(cros_kunit_mock_reset);

MODULE_LICENSE("GPL");
+36 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * CrOS Kunit tests utilities.
 */

#ifndef _CROS_KUNIT_UTIL_H_
#define _CROS_KUNIT_UTIL_H_

#include <linux/platform_data/cros_ec_proto.h>

struct ec_xfer_mock {
	struct list_head list;
	struct kunit *test;

	/* input */
	struct cros_ec_command msg;
	void *i_data;

	/* output */
	int ret;
	int result;
	void *o_data;
	u32 o_data_len;
};

extern int cros_kunit_ec_xfer_mock_default_ret;

int cros_kunit_ec_xfer_mock(struct cros_ec_device *ec_dev, struct cros_ec_command *msg);
struct ec_xfer_mock *cros_kunit_ec_xfer_mock_add(struct kunit *test, size_t size);
struct ec_xfer_mock *cros_kunit_ec_xfer_mock_addx(struct kunit *test,
						  int ret, int result, size_t size);
struct ec_xfer_mock *cros_kunit_ec_xfer_mock_next(void);

void cros_kunit_mock_reset(void);

#endif