summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/boringssl_crypto.cc66
-rw-r--r--test/boringssl_crypto.tasklist9
-rw-r--r--test/build.mk3
-rwxr-xr-xtest/run_device_tests.py2
-rw-r--r--test/sbs_charging_v2.c16
-rw-r--r--test/test_config.h4
-rw-r--r--test/unaligned_access.cc157
-rw-r--r--test/unaligned_access.tasklist9
8 files changed, 258 insertions, 8 deletions
diff --git a/test/boringssl_crypto.cc b/test/boringssl_crypto.cc
new file mode 100644
index 0000000000..c04b391854
--- /dev/null
+++ b/test/boringssl_crypto.cc
@@ -0,0 +1,66 @@
+/* Copyright 2023 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "common.h"
+#include "crypto/elliptic_curve_key.h"
+#include "openssl/bn.h"
+#include "openssl/ec.h"
+#include "openssl/mem.h"
+#include "openssl/obj_mac.h"
+#include "openssl/rand.h"
+#include "test_util.h"
+#include "util.h"
+
+test_static enum ec_error_list test_rand(void)
+{
+ constexpr uint8_t zero[256] = { 0 };
+ uint8_t buf1[256];
+ uint8_t buf2[256];
+
+ RAND_bytes(buf1, sizeof(buf1));
+ RAND_bytes(buf2, sizeof(buf2));
+
+ TEST_ASSERT_ARRAY_NE(buf1, zero, sizeof(zero));
+ TEST_ASSERT_ARRAY_NE(buf2, zero, sizeof(zero));
+ TEST_ASSERT_ARRAY_NE(buf1, buf2, sizeof(buf1));
+
+ return EC_SUCCESS;
+}
+
+test_static enum ec_error_list test_ecc_keygen(void)
+{
+ bssl::UniquePtr<EC_KEY> key1 = generate_elliptic_curve_key();
+
+ TEST_NE(key1.get(), nullptr, "%p");
+
+ /* The generated key should be valid.*/
+ TEST_EQ(EC_KEY_check_key(key1.get()), 1, "%d");
+
+ bssl::UniquePtr<EC_KEY> key2 = generate_elliptic_curve_key();
+
+ TEST_NE(key2.get(), nullptr, "%p");
+
+ /* The generated key should be valid. */
+ TEST_EQ(EC_KEY_check_key(key2.get()), 1, "%d");
+
+ const BIGNUM *priv1 = EC_KEY_get0_private_key(key1.get());
+ const BIGNUM *priv2 = EC_KEY_get0_private_key(key2.get());
+
+ /* The generated keys should not be the same. */
+ TEST_NE(BN_cmp(priv1, priv2), 0, "%d");
+
+ /* The generated keys should not be zero. */
+ TEST_EQ(BN_is_zero(priv1), 0, "%d");
+ TEST_EQ(BN_is_zero(priv2), 0, "%d");
+
+ return EC_SUCCESS;
+}
+
+extern "C" void run_test(int argc, const char **argv)
+{
+ RUN_TEST(test_rand);
+ RUN_TEST(test_ecc_keygen);
+ test_print_result();
+}
diff --git a/test/boringssl_crypto.tasklist b/test/boringssl_crypto.tasklist
new file mode 100644
index 0000000000..d1920322a9
--- /dev/null
+++ b/test/boringssl_crypto.tasklist
@@ -0,0 +1,9 @@
+/* Copyright 2023 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * See CONFIG_TASK_LIST in config.h for details.
+ */
+#define CONFIG_TEST_TASK_LIST
diff --git a/test/build.mk b/test/build.mk
index 2d2f9341a3..d90e8daf26 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -28,6 +28,7 @@ test-list-host += benchmark
test-list-host += bklight_lid
test-list-host += bklight_passthru
test-list-host += body_detection
+test-list-host += boringssl_crypto
test-list-host += button
test-list-host += cbi
test-list-host += cbi_wp
@@ -181,6 +182,7 @@ benchmark-y=benchmark.o
bklight_lid-y=bklight_lid.o
bklight_passthru-y=bklight_passthru.o
body_detection-y=body_detection.o body_detection_data_literals.o motion_common.o
+boringssl_crypto-y=boringssl_crypto.o
button-y=button.o
cbi-y=cbi.o
cbi_wp-y=cbi_wp.o
@@ -277,6 +279,7 @@ timer_calib-y=timer_calib.o
timer_dos-y=timer_dos.o
timer-y=timer.o
tpm_seed_clear-y=tpm_seed_clear.o
+unaligned_access-y=unaligned_access.o
uptime-y=uptime.o
usb_common-y=usb_common_test.o fake_battery.o
usb_pd_int-y=usb_pd_int.o
diff --git a/test/run_device_tests.py b/test/run_device_tests.py
index f385441f6d..e874d08bb7 100755
--- a/test/run_device_tests.py
+++ b/test/run_device_tests.py
@@ -230,6 +230,7 @@ class AllTests:
TestConfig(test_name="aes"),
TestConfig(test_name="always_memset"),
TestConfig(test_name="benchmark"),
+ TestConfig(test_name="boringssl_crypto"),
TestConfig(test_name="cec"),
TestConfig(test_name="cortexm_fpu"),
TestConfig(test_name="crc"),
@@ -332,6 +333,7 @@ class AllTests:
TestConfig(test_name="timer"),
TestConfig(test_name="timer_dos"),
TestConfig(test_name="tpm_seed_clear"),
+ TestConfig(test_name="unaligned_access"),
TestConfig(test_name="utils", timeout_secs=20),
TestConfig(test_name="utils_str"),
]
diff --git a/test/sbs_charging_v2.c b/test/sbs_charging_v2.c
index 482770ff1c..7c20bbe4ac 100644
--- a/test/sbs_charging_v2.c
+++ b/test/sbs_charging_v2.c
@@ -2,7 +2,7 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
- * Test charge_state_v2 behavior
+ * Test charge_state behavior
*/
#include "battery_smart.h"
@@ -367,7 +367,7 @@ test_static int test_low_battery(void)
test_static int test_deep_charge_battery(void)
{
- enum charge_state_v2 state_v2;
+ enum charge_state state;
const struct battery_info *bat_info = battery_get_info();
test_setup(1);
@@ -375,22 +375,22 @@ test_static int test_deep_charge_battery(void)
/* battery pack voltage bellow voltage_min */
sb_write(SB_VOLTAGE, (bat_info->voltage_min - 200));
wait_charging_state();
- state_v2 = charge_get_state_v2();
- TEST_ASSERT(state_v2 == ST_PRECHARGE);
+ state = charge_get_state();
+ TEST_ASSERT(state == ST_PRECHARGE);
/*
* Battery voltage keep bellow voltage_min,
* precharge over time CONFIG_BATTERY_LOW_VOLTAGE_TIMEOUT
*/
usleep(CONFIG_BATTERY_LOW_VOLTAGE_TIMEOUT);
- state_v2 = charge_get_state_v2();
- TEST_ASSERT(state_v2 == ST_IDLE);
+ state = charge_get_state();
+ TEST_ASSERT(state == ST_IDLE);
/* recovery from a low voltage. */
sb_write(SB_VOLTAGE, (bat_info->voltage_normal));
wait_charging_state();
- state_v2 = charge_get_state_v2();
- TEST_ASSERT(state_v2 == ST_CHARGE);
+ state = charge_get_state();
+ TEST_ASSERT(state == ST_CHARGE);
return EC_SUCCESS;
}
diff --git a/test/test_config.h b/test/test_config.h
index 51c15f4c8c..033ee966b7 100644
--- a/test/test_config.h
+++ b/test/test_config.h
@@ -114,6 +114,10 @@
#define CONFIG_SHA256
#endif
+#if defined(TEST_BORINGSSL_CRYPTO)
+#define CONFIG_BORINGSSL_CRYPTO
+#endif
+
#ifdef TEST_ROLLBACK_SECRET
#define CONFIG_ROLLBACK
#define CONFIG_ROLLBACK_SECRET_SIZE 32
diff --git a/test/unaligned_access.cc b/test/unaligned_access.cc
new file mode 100644
index 0000000000..f3dc99d432
--- /dev/null
+++ b/test/unaligned_access.cc
@@ -0,0 +1,157 @@
+/* Copyright 2023 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * Test if unaligned access works properly
+ */
+
+#include "common.h"
+#include "console.h"
+#include "test_util.h"
+
+extern "C" {
+#include "shared_mem.h"
+#include "timer.h"
+}
+
+#include <array>
+#include <cstdio>
+#include <cstring>
+
+test_static int test_unaligned_access()
+{
+ /* This is equivalent to {0xff, 0x09, 0x04, 0x06, 0x04, 0x06, 0x07,
+ * 0xed, 0x0a, 0x0b, 0x0d, 0x38, 0xbd, 0x57, 0x59} */
+ alignas(int32_t) constexpr std::array<int8_t, 15> test_array = {
+ -1, 9, 4, 6, 4, 6, 7, -19, 10, 11, 13, 56, -67, 87, 89
+ };
+
+ constexpr std::array<int32_t, 12> expected_results = {
+ 0x060409ff,
+ 0x04060409,
+ 0x06040604,
+ 0x07060406,
+ static_cast<int32_t>(0xed070604),
+ 0x0aed0706,
+ 0x0b0aed07,
+ 0x0d0b0aed,
+ 0x380d0b0a,
+ static_cast<int32_t>(0xbd380d0b),
+ 0x57bd380d,
+ 0x5957bd38
+ };
+
+ /* If i % 4 = 0, we have an aligned access. Otherwise, it is
+ unaligned access. */
+ for (int i = 0; i < expected_results.size(); ++i) {
+ const int32_t *test_array_ptr =
+ reinterpret_cast<const int32_t *>(test_array.data() +
+ i);
+
+ TEST_EQ(*test_array_ptr, expected_results[i], "0x%08x");
+ }
+
+ return EC_SUCCESS;
+}
+
+test_static int benchmark_unaligned_access_memcpy()
+{
+ int i;
+ timestamp_t t0, t1, t2, t3;
+ char *buf;
+ const int buf_size = 1000;
+ const int len = 400;
+ const int dest_offset = 500;
+ const int iteration = 1000;
+
+ TEST_ASSERT(shared_mem_acquire(buf_size, &buf) == EC_SUCCESS);
+
+ for (i = 0; i < len; ++i)
+ buf[i] = i & 0x7f;
+ for (i = len; i < buf_size; ++i)
+ buf[i] = 0;
+
+ t0 = get_time();
+ for (i = 0; i < iteration; ++i) {
+ memcpy(buf + dest_offset + 1, buf, len); /* unaligned */
+ }
+ t1 = get_time();
+ TEST_ASSERT_ARRAY_EQ(buf + dest_offset + 1, buf, len);
+ ccprintf(" (speed gain: %" PRId64 " ->", t1.val - t0.val);
+
+ t2 = get_time();
+ for (i = 0; i < iteration; ++i) {
+ memcpy(buf + dest_offset, buf, len); /* aligned */
+ }
+ t3 = get_time();
+ ccprintf(" %" PRId64 " us) ", t3.val - t2.val);
+ TEST_ASSERT_ARRAY_EQ(buf + dest_offset, buf, len);
+ return EC_SUCCESS;
+}
+
+test_static int benchmark_unaligned_access_array()
+{
+ timestamp_t t0, t1, t2, t3;
+ const int iteration = 1000;
+
+ alignas(int32_t) std::array<int8_t, 100> test_array_1;
+ std::array<int32_t, 20> test_array_2;
+ constexpr std::array<int32_t, 20> test_array_3 = {
+ 67305985, 134678021, 202050057, 269422093, 336794129,
+ 404166165, 471538201, 538910237, 606282273, 673654309,
+ 741026345, 808398381, 875770417, 943142453, 1010514489,
+ 1077886525, 1145258561, 1212630597, 1280002633, 1347374669
+ };
+ constexpr std::array<int32_t, 20> test_array_4 = {
+ 50462976, 117835012, 185207048, 252579084, 319951120,
+ 387323156, 454695192, 522067228, 589439264, 656811300,
+ 724183336, 791555372, 858927408, 926299444, 993671480,
+ 1061043516, 1128415552, 1195787588, 1263159624, 1330531660
+ };
+
+ for (int i = 0; i < test_array_1.size(); ++i) {
+ test_array_1[i] = static_cast<int8_t>(i);
+ }
+
+ t0 = get_time();
+ for (int t = 0; t < iteration; ++t) {
+ const int32_t *test_array_1_ptr =
+ reinterpret_cast<const int32_t *>(test_array_1.data() +
+ 1);
+
+ for (int i = 0; i < test_array_2.size(); ++i) {
+ test_array_2[i] = (*test_array_1_ptr++); /* unaligned */
+ }
+ TEST_ASSERT_ARRAY_EQ(test_array_2.data(), test_array_3.data(),
+ test_array_2.size());
+ }
+ t1 = get_time();
+ ccprintf(" (speed gain: %" PRId64 " ->", t1.val - t0.val);
+
+ t2 = get_time();
+ for (int t = 0; t < iteration; ++t) {
+ const int32_t *test_array_1_ptr =
+ reinterpret_cast<const int32_t *>(test_array_1.data());
+
+ for (int i = 0; i < test_array_2.size(); ++i) {
+ test_array_2[i] = (*test_array_1_ptr++); /* aligned */
+ }
+ TEST_ASSERT_ARRAY_EQ(test_array_2.data(), test_array_4.data(),
+ test_array_2.size());
+ }
+ t3 = get_time();
+ ccprintf(" %" PRId64 " us) ", t3.val - t2.val);
+
+ return EC_SUCCESS;
+}
+
+extern "C" void run_test(int, const char **)
+{
+ test_reset();
+ RUN_TEST(test_unaligned_access);
+ RUN_TEST(benchmark_unaligned_access_memcpy);
+ RUN_TEST(benchmark_unaligned_access_array);
+ test_print_result();
+}
diff --git a/test/unaligned_access.tasklist b/test/unaligned_access.tasklist
new file mode 100644
index 0000000000..2d177cee59
--- /dev/null
+++ b/test/unaligned_access.tasklist
@@ -0,0 +1,9 @@
+/* Copyright 2023 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * See CONFIG_TASK_LIST in config.h for details.
+ */
+#define CONFIG_TEST_TASK_LIST \ No newline at end of file