|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdio.h> |
| 8 | +#include <catch2/catch_test_macros.hpp> |
| 9 | + |
| 10 | +#include "usb/msc_host_vfs.h" |
| 11 | +#include "mock_add_usb_device.h" |
| 12 | + |
| 13 | +extern "C" { |
| 14 | +#include "Mockusb_host.h" |
| 15 | +} |
| 16 | + |
| 17 | +SCENARIO("MSC Host pre-uninstall") |
| 18 | +{ |
| 19 | + // No MSC Driver installed |
| 20 | + GIVEN("NO MSC Host previously installed") { |
| 21 | + |
| 22 | + // Uninstall not-installed MAC Host driver |
| 23 | + SECTION("Uninstalling not installed MSC Host returns error") { |
| 24 | + // Call the DUT function, expect ESP_ERR_INVALID_STATE |
| 25 | + REQUIRE(ESP_ERR_INVALID_STATE == msc_host_uninstall()); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +SCENARIO("MSC Host install") |
| 31 | +{ |
| 32 | + msc_host_driver_config_t msc_host_driver_config = { |
| 33 | + .create_backround_task = true, |
| 34 | + .task_priority = 5, |
| 35 | + .stack_size = 4096, |
| 36 | + .core_id = 0, |
| 37 | + .callback = (reinterpret_cast<msc_host_event_cb_t>(0xdeadbeef)), |
| 38 | + .callback_arg = nullptr, |
| 39 | + }; |
| 40 | + |
| 41 | + // MSC Host driver config set to nullptr |
| 42 | + GIVEN("NO MSC Host driver config, driver not already installed") { |
| 43 | + |
| 44 | + SECTION("Config is nullptr") { |
| 45 | + // Call the DUT function msc_host_install with msc_host_driver_config set to nullptr |
| 46 | + REQUIRE(ESP_ERR_INVALID_ARG == msc_host_install(nullptr)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // MSC Host driver config set to config from MSC Host example, |
| 51 | + // but with various various changes causing MSC Host driver installation to fail |
| 52 | + GIVEN("Minimal MSC Host driver config, driver not already installed") { |
| 53 | + |
| 54 | + SECTION("Config error: no callback") { |
| 55 | + msc_host_driver_config.callback = nullptr; |
| 56 | + // Call the DUT function, expect ESP_ERR_INVALID_ARG |
| 57 | + REQUIRE(ESP_ERR_INVALID_ARG == msc_host_install(&msc_host_driver_config)); |
| 58 | + } |
| 59 | + |
| 60 | + SECTION("Config error: stack size is 0") { |
| 61 | + msc_host_driver_config.stack_size = 0; |
| 62 | + // Call the DUT function, expect ESP_ERR_INVALID_ARG |
| 63 | + REQUIRE(ESP_ERR_INVALID_ARG == msc_host_install(&msc_host_driver_config)); |
| 64 | + } |
| 65 | + |
| 66 | + SECTION("Config error: task priority is 0") { |
| 67 | + msc_host_driver_config.task_priority = 0; |
| 68 | + // Call the DUT function, expect ESP_ERR_INVALID_ARG |
| 69 | + REQUIRE(ESP_ERR_INVALID_ARG == msc_host_install(&msc_host_driver_config)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // MSC Host driver config set to config from MSC Host example |
| 74 | + GIVEN("Full MSC Host config, driver not already installed") { |
| 75 | + |
| 76 | + // Unable to register client: Invalid state, goto fail |
| 77 | + SECTION("Client register not successful: goto fail") { |
| 78 | + // Register a client, return ESP_ERR_INVALID_STATE, so the client is not registered successfully |
| 79 | + usb_host_client_register_ExpectAnyArgsAndReturn(ESP_ERR_INVALID_STATE); |
| 80 | + |
| 81 | + // goto fail: delete the semaphore and deregister client |
| 82 | + usb_host_client_deregister_ExpectAnyArgsAndReturn(ESP_OK); |
| 83 | + |
| 84 | + // Call the DUT function, expect ESP_ERR_INVALID_STATE |
| 85 | + REQUIRE(ESP_ERR_INVALID_STATE == msc_host_install(&msc_host_driver_config)); |
| 86 | + } |
| 87 | + |
| 88 | + // Call msc_host_install and expect successful installation |
| 89 | + SECTION("Client register successful: msc_host_install successful") { |
| 90 | + |
| 91 | + // Register a client, return ESP_OK, so the client is registered successfully |
| 92 | + usb_host_client_register_ExpectAnyArgsAndReturn(ESP_OK); |
| 93 | + usb_host_client_register_AddCallback(usb_host_client_register_mock_callback); |
| 94 | + |
| 95 | + // create USB Host Library client processing function |
| 96 | + usb_host_client_handle_events_ExpectAnyArgsAndReturn(ESP_OK); |
| 97 | + usb_host_client_handle_events_AddCallback(usb_host_client_handle_events_mock_callback); |
| 98 | + |
| 99 | + // Call the DUT function, expect ESP_OK |
| 100 | + REQUIRE(ESP_OK == msc_host_install(&msc_host_driver_config)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // MSC Host driver config set to config from MSC Host example |
| 105 | + GIVEN("Full MSC Host config, driver already installed") { |
| 106 | + |
| 107 | + // Driver is already installed |
| 108 | + SECTION("Install error: driver already installed") { |
| 109 | + // Call the DUT function, expect ESP_ERR_INVALID_STATE |
| 110 | + REQUIRE(ESP_ERR_INVALID_STATE == msc_host_install(&msc_host_driver_config)); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +SCENARIO("MSC Host post-uninstall") |
| 116 | +{ |
| 117 | + // MSC Host driver successfully installed |
| 118 | + GIVEN("MSC Host previously installed") { |
| 119 | + |
| 120 | + // MSC Driver is installed, uninstall the driver |
| 121 | + SECTION("MSC Host driver uninstall successful") { |
| 122 | + |
| 123 | + // Unblock client, return ESP_OK, register callback for unblocking the USB Host Library client processing function |
| 124 | + usb_host_client_unblock_ExpectAnyArgsAndReturn(ESP_OK); |
| 125 | + usb_host_client_unblock_AddCallback(usb_host_client_unblock_mock_callback); |
| 126 | + |
| 127 | + // Deregister the client |
| 128 | + usb_host_client_deregister_ExpectAnyArgsAndReturn(ESP_OK); |
| 129 | + usb_host_client_deregister_AddCallback(usb_host_client_deregister_mock_callback); |
| 130 | + |
| 131 | + // Call the DUT function, expect ESP_OK |
| 132 | + REQUIRE(ESP_OK == msc_host_uninstall()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // MSC Host driver successfully uninstalled |
| 137 | + GIVEN("MSC Host successfully uninstalled") { |
| 138 | + |
| 139 | + // MSC Driver already successfully uninstalled, uninstall it again |
| 140 | + SECTION("Uninstall already uninstalled MSC Host driver") { |
| 141 | + // Call the DUT function, expect error |
| 142 | + REQUIRE(ESP_ERR_INVALID_STATE == msc_host_uninstall()); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments