summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-09-01 12:12:24 +0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2013-09-04 20:32:52 +0000
commit844f4127e8de9c061b980f458d0d0f80b8083e6a (patch)
tree7bfafe1bde67634b29856517d7da1b619748bb03
parent04d8465b7550f9752454985f5bcafd3c61da8ee4 (diff)
downloadchrome-ec-844f4127e8de9c061b980f458d0d0f80b8083e6a.tar.gz
Add host command test
This test checks host command interface handles errors correctly. BUG=chrome-os-partner:19236 TEST=Pass host_command test BRANCH=None Change-Id: I62764977d2063edf5abd37e78e074384eec5f0d4 Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/167762 Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r--test/build.mk3
-rw-r--r--test/host_command.c160
-rw-r--r--test/host_command.tasklist17
3 files changed, 179 insertions, 1 deletions
diff --git a/test/build.mk b/test/build.mk
index 72071a478c..72c2982505 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -30,13 +30,14 @@ test-list-$(BOARD_bolt)=
# Emulator tests
test-list-host=mutex pingpong utils kb_scan kb_mkbp lid_sw power_button hooks
test-list-host+=thermal flash queue kb_8042 extpwr_gpio console_edit system
-test-list-host+=sbs_charging adapter
+test-list-host+=sbs_charging adapter host_command
adapter-y=adapter.o
console_edit-y=console_edit.o
extpwr_gpio-y=extpwr_gpio.o
flash-y=flash.o
hooks-y=hooks.o
+host_command-y=host_command.o
kb_8042-y=kb_8042.o
kb_8042-scale=3
kb_mkbp-y=kb_mkbp.o
diff --git a/test/host_command.c b/test/host_command.c
new file mode 100644
index 0000000000..639b32ed8a
--- /dev/null
+++ b/test/host_command.c
@@ -0,0 +1,160 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Test host command.
+ */
+
+#include "common.h"
+#include "console.h"
+#include "host_command.h"
+#include "task.h"
+#include "test_util.h"
+#include "timer.h"
+#include "util.h"
+
+struct host_packet pkt;
+static char resp_buf[128];
+static char req_buf[128];
+struct ec_host_request *req = (struct ec_host_request *)req_buf;
+struct ec_params_hello *p = (struct ec_params_hello *)(req_buf + sizeof(*req));
+struct ec_host_response *resp = (struct ec_host_response *)resp_buf;
+struct ec_response_hello *r =
+ (struct ec_response_hello *)(resp_buf + sizeof(*resp));
+
+static void hostcmd_respond(struct host_packet *pkt)
+{
+ task_wake(TASK_ID_TEST_RUNNER);
+}
+
+static char calculate_checksum(const char *buf, int size)
+{
+ int c = 0;
+ int i;
+
+ for (i = 0; i < size; ++i)
+ c += buf[i];
+
+ return -c;
+}
+
+static void hostcmd_send(void)
+{
+ req->checksum = calculate_checksum(req_buf, pkt.request_size);
+ host_packet_receive(&pkt);
+ task_wait_event(-1);
+}
+
+static void hostcmd_fill_in_default(void)
+{
+ req->struct_version = 3;
+ req->checksum = 0;
+ req->command = EC_CMD_HELLO;
+ req->command_version = 0;
+ req->reserved = 0;
+ req->data_len = 4;
+ p->in_data = 0x11223344;
+
+ pkt.request_size = 0;
+ pkt.send_response = hostcmd_respond;
+ pkt.request = (const void *)req_buf;
+ pkt.request_max = 128;
+ pkt.request_size = sizeof(*req) + sizeof(*p);
+ pkt.response = (void *)resp_buf;
+ pkt.response_max = 128;
+ pkt.driver_result = 0;
+}
+
+static int test_hostcmd_ok(void)
+{
+ hostcmd_fill_in_default();
+
+ hostcmd_send();
+
+ TEST_ASSERT(calculate_checksum(resp_buf,
+ sizeof(*resp) + resp->data_len) == 0);
+ TEST_ASSERT(resp->result == EC_RES_SUCCESS);
+ TEST_ASSERT(r->out_data == 0x12243648);
+
+ return EC_SUCCESS;
+}
+
+static int test_hostcmd_too_short(void)
+{
+ hostcmd_fill_in_default();
+
+ /* Smaller than header */
+ pkt.request_size = sizeof(*req) - 4;
+ hostcmd_send();
+ TEST_ASSERT(resp->result == EC_RES_REQUEST_TRUNCATED);
+
+ /* Smaller than expected data size */
+ pkt.request_size = sizeof(*req);
+ hostcmd_send();
+ TEST_ASSERT(resp->result == EC_RES_REQUEST_TRUNCATED);
+
+ return EC_SUCCESS;
+}
+
+static int test_hostcmd_too_long(void)
+{
+ hostcmd_fill_in_default();
+
+ /* Larger than request buffer */
+ pkt.request_size = sizeof(req_buf) + 4;
+ hostcmd_send();
+ TEST_ASSERT(resp->result == EC_RES_REQUEST_TRUNCATED);
+
+ return EC_SUCCESS;
+}
+
+static int test_hostcmd_driver_error(void)
+{
+ hostcmd_fill_in_default();
+
+ pkt.driver_result = EC_RES_ERROR;
+ hostcmd_send();
+ TEST_ASSERT(resp->result == EC_RES_ERROR);
+
+ return EC_SUCCESS;
+}
+
+static int test_hostcmd_wrong_version(void)
+{
+ hostcmd_fill_in_default();
+
+ req->struct_version = 4;
+ hostcmd_send();
+ TEST_ASSERT(resp->result == EC_RES_INVALID_HEADER);
+
+ req->struct_version = 2;
+ hostcmd_send();
+ TEST_ASSERT(resp->result == EC_RES_INVALID_HEADER);
+
+ return EC_SUCCESS;
+}
+
+static int test_hostcmd_invalid_checksum(void)
+{
+ hostcmd_fill_in_default();
+
+ req->checksum++;
+ hostcmd_send();
+ TEST_ASSERT(resp->result == EC_RES_INVALID_CHECKSUM);
+
+ return EC_SUCCESS;
+}
+
+void run_test(void)
+{
+ test_reset();
+
+ RUN_TEST(test_hostcmd_ok);
+ RUN_TEST(test_hostcmd_too_short);
+ RUN_TEST(test_hostcmd_too_long);
+ RUN_TEST(test_hostcmd_driver_error);
+ RUN_TEST(test_hostcmd_wrong_version);
+ RUN_TEST(test_hostcmd_invalid_checksum);
+
+ test_print_result();
+}
diff --git a/test/host_command.tasklist b/test/host_command.tasklist
new file mode 100644
index 0000000000..26cfc53453
--- /dev/null
+++ b/test/host_command.tasklist
@@ -0,0 +1,17 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * List of enabled tasks in the priority order
+ *
+ * The first one has the lowest priority.
+ *
+ * For each task, use the macro TASK_TEST(n, r, d, s) where :
+ * 'n' in the name of the task
+ * 'r' in the main routine of the task
+ * 'd' in an opaque parameter passed to the routine at startup
+ * 's' is the stack size in bytes; must be a multiple of 8
+ */
+#define CONFIG_TEST_TASK_LIST /* No test task */