summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAlexander Goncharov <chat@joursoir.net>2022-09-09 09:53:29 +0300
committerAnastasia Klimchuk <aklm@chromium.org>2022-11-29 21:05:00 +0000
commitd0fc4e76e16b6c413519fcfaba9f382afd080407 (patch)
tree0005e4e5e95e29cc9b0450464cc13ff564a77735 /tests
parent224ce8168791c5914e4289d700ae15f3f9926c8d (diff)
downloadflashrom-d0fc4e76e16b6c413519fcfaba9f382afd080407.tar.gz
flashrom-d0fc4e76e16b6c413519fcfaba9f382afd080407.tar.bz2
flashrom-d0fc4e76e16b6c413519fcfaba9f382afd080407.zip
tests: add basic lifecycle test for ch341a_spi
TEST=the following scenarios run tests successfully 1) ch341a_spi is enabled result: all tests run and pass, including ch341a 2) ch341a_spi is disabled result: ch341a_spi test is skipped, the rest of tests run and pass 3) libusb isn't presented in the system result: tests for usb programmers are skipped, the rest of tests run normally Change-Id: If28fbe09ad685082152aa3a7e8d5a150169aee9e Signed-off-by: Alexander Goncharov <chat@joursoir.net> Reviewed-on: https://review.coreboot.org/c/flashrom/+/67664 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/ch341a_spi.c94
-rw-r--r--tests/tests.c1
-rw-r--r--tests/tests.h1
-rw-r--r--tests/usb_unittests.h2
4 files changed, 97 insertions, 1 deletions
diff --git a/tests/ch341a_spi.c b/tests/ch341a_spi.c
new file mode 100644
index 000000000..c7061719a
--- /dev/null
+++ b/tests/ch341a_spi.c
@@ -0,0 +1,94 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright 2022 Alexander Goncharov <chat@joursoir.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include "lifecycle.h"
+
+#if CONFIG_CH341A_SPI == 1
+
+/* Same macro as in ch341a_spi.c programmer. */
+#define WRITE_EP 0x02
+
+struct ch341a_spi_io_state {
+ struct libusb_transfer *transfer_out;
+};
+
+static struct libusb_transfer *ch341a_libusb_alloc_transfer(void *state, int iso_packets)
+{
+ return calloc(1, sizeof(struct libusb_transfer));
+}
+
+/*
+ * The libusb code stores submitted transfers in their own context. But this
+ * function doesn't require a context pointer because libusb stores context
+ * pointers in libusb_transfer instances. Since our ch341 driver is using
+ * the default context, we store the transfer in our own.
+ */
+static int ch341a_libusb_submit_transfer(void *state, struct libusb_transfer *transfer)
+{
+ struct ch341a_spi_io_state *io_state = state;
+
+ assert_true(transfer->endpoint == WRITE_EP);
+
+ assert_null(io_state->transfer_out);
+ io_state->transfer_out = transfer;
+
+ return 0;
+}
+
+static void ch341a_libusb_free_transfer(void *state, struct libusb_transfer *transfer)
+{
+ free(transfer);
+}
+
+/*
+ * Handle submitted transfers by pretending that a transfer is completed and
+ * invoking its callback (that is the flashrom code).
+ */
+static int ch341a_libusb_handle_events_timeout(void *state, libusb_context *ctx, struct timeval *tv)
+{
+ struct ch341a_spi_io_state *io_state = state;
+
+ if (io_state->transfer_out) {
+ io_state->transfer_out->status = LIBUSB_TRANSFER_COMPLETED;
+ io_state->transfer_out->actual_length = io_state->transfer_out->length;
+ io_state->transfer_out->callback(io_state->transfer_out);
+ io_state->transfer_out = NULL;
+ }
+
+ return 0;
+}
+
+void ch341a_spi_basic_lifecycle_test_success(void **state)
+{
+ struct ch341a_spi_io_state ch341a_spi_io_state = { 0 };
+ struct io_mock_fallback_open_state ch341a_spi_fallback_open_state = {
+ .noc = 0,
+ .paths = { NULL },
+ };
+ const struct io_mock ch341a_spi_io = {
+ .state = &ch341a_spi_io_state,
+ .libusb_alloc_transfer = &ch341a_libusb_alloc_transfer,
+ .libusb_submit_transfer = &ch341a_libusb_submit_transfer,
+ .libusb_free_transfer = &ch341a_libusb_free_transfer,
+ .libusb_handle_events_timeout = &ch341a_libusb_handle_events_timeout,
+ .fallback_open_state = &ch341a_spi_fallback_open_state,
+ };
+
+ run_basic_lifecycle(state, &ch341a_spi_io, &programmer_ch341a_spi, "");
+}
+
+#else
+ SKIP_TEST(ch341a_spi_basic_lifecycle_test_success)
+#endif /* CONFIG_CH341A_SPI */
diff --git a/tests/tests.c b/tests/tests.c
index ea416b835..2461ff6e7 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -420,6 +420,7 @@ int main(int argc, char *argv[])
cmocka_unit_test(mediatek_i2c_no_allow_brick_test_success),
cmocka_unit_test(realtek_mst_basic_lifecycle_test_success),
cmocka_unit_test(realtek_mst_no_allow_brick_test_success),
+ cmocka_unit_test(ch341a_spi_basic_lifecycle_test_success),
};
ret |= cmocka_run_group_tests_name("lifecycle.c tests", lifecycle_tests, NULL, NULL);
diff --git a/tests/tests.h b/tests/tests.h
index 7d84101e7..4d91e4dfe 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -60,6 +60,7 @@ void mediatek_i2c_spi_basic_lifecycle_test_success(void **state);
void mediatek_i2c_no_allow_brick_test_success(void **state);
void realtek_mst_basic_lifecycle_test_success(void **state);
void realtek_mst_no_allow_brick_test_success(void **state);
+void ch341a_spi_basic_lifecycle_test_success(void **state);
/* layout.c */
void included_regions_dont_overlap_test_success(void **state);
diff --git a/tests/usb_unittests.h b/tests/usb_unittests.h
index f4d83f126..c81e18147 100644
--- a/tests/usb_unittests.h
+++ b/tests/usb_unittests.h
@@ -25,7 +25,7 @@
#ifndef _USB_UNITTESTS_H_
#define _USB_UNITTESTS_H_
-#if CONFIG_RAIDEN_DEBUG_SPI == 1 || CONFIG_DEDIPROG == 1
+#if CONFIG_RAIDEN_DEBUG_SPI == 1 || CONFIG_DEDIPROG == 1 || CONFIG_CH341A_SPI == 1
#include <libusb.h>