summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/host_cmd/src/get_panic_info.c
blob: bdb19d9a988e7dba4a0b5ae57e3666b83fbfba83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <zephyr/ztest.h>

#include "host_command.h"
#include "panic.h"
#include "test/drivers/test_state.h"

struct host_cmd_get_panic_info_fixture {
	struct panic_data saved_pdata;
};

static void *host_cmd_get_panic_info_setup(void)
{
	static struct host_cmd_get_panic_info_fixture fixture = { 0 };

	return &fixture;
}

static void host_cmd_get_panic_info_before(void *f)
{
	struct host_cmd_get_panic_info_fixture *fixture = f;
	struct panic_data *pdata = get_panic_data_write();

	fixture->saved_pdata = *pdata;
}

static void host_cmd_get_panic_info_after(void *f)
{
	struct host_cmd_get_panic_info_fixture *fixture = f;
	struct panic_data *pdata = get_panic_data_write();

	*pdata = fixture->saved_pdata;
}

ZTEST_SUITE(host_cmd_get_panic_info, drivers_predicate_post_main,
	    host_cmd_get_panic_info_setup, host_cmd_get_panic_info_before,
	    host_cmd_get_panic_info_after, NULL);

ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info)
{
	struct panic_data *pdata = get_panic_data_write();
	struct panic_data response = { 0 };
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE(
		EC_CMD_GET_PANIC_INFO, UINT8_C(0), response);

	pdata->arch = 0;
	pdata->struct_version = 1;
	pdata->flags = 2;
	pdata->reserved = 3;
	pdata->struct_size = sizeof(struct panic_data);
	pdata->magic = PANIC_DATA_MAGIC;

	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(sizeof(struct panic_data), args.response_size, NULL);
	zassert_equal(0, response.arch, NULL);
	zassert_equal(1, response.struct_version, NULL);
	zassert_equal(2, response.flags, NULL);
	zassert_equal(3, response.reserved, NULL);
	zassert_equal(sizeof(struct panic_data), response.struct_size, NULL);
	zassert_equal(PANIC_DATA_MAGIC, response.magic, NULL);
	zassert_equal(pdata->flags & PANIC_DATA_FLAG_OLD_HOSTCMD,
		      PANIC_DATA_FLAG_OLD_HOSTCMD, NULL);
}

ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info_bad_magic)
{
	struct panic_data *pdata = get_panic_data_write();
	struct panic_data expected = { 0 };
	struct panic_data response = { 0 };
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE(
		EC_CMD_GET_PANIC_INFO, UINT8_C(0), response);

	pdata->magic = PANIC_DATA_MAGIC + 1;
	zassert_ok(host_command_process(&args), NULL);
	/* Check that nothing was written to response */
	zassert_mem_equal(&response, &expected, sizeof(struct panic_data),
			  NULL);
}

ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info_size_is_zero)
{
	struct panic_data *pdata = get_panic_data_write();
	struct panic_data expected = { 0 };
	struct panic_data response = { 0 };
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE(
		EC_CMD_GET_PANIC_INFO, UINT8_C(0), response);

	pdata->magic = PANIC_DATA_MAGIC;
	pdata->struct_size = 0;
	zassert_ok(host_command_process(&args), NULL);
	/* Check that nothing was written to response */
	zassert_mem_equal(&response, &expected, sizeof(struct panic_data),
			  NULL);
}

ZTEST_USER(host_cmd_get_panic_info, test_get_panic_info_truncate)
{
	struct panic_data *pdata = get_panic_data_write();
	struct panic_data response = { 0 };
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_RESPONSE(
		EC_CMD_GET_PANIC_INFO, UINT8_C(0), response);

	/* Panic data is bigger than max response size, should be truncated */
	args.response_max = sizeof(struct panic_data) - 2;

	pdata->flags = 0;
	pdata->magic = PANIC_DATA_MAGIC;
	pdata->struct_size = sizeof(struct panic_data);

	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(sizeof(struct panic_data) - 2, args.response_size, NULL);
	/* Truncation flag must be set */
	zassert_equal(response.flags & PANIC_DATA_FLAG_TRUNCATED,
		      PANIC_DATA_FLAG_TRUNCATED, NULL);
	zassert_equal(sizeof(struct panic_data), response.struct_size, NULL);
}