Loading...
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | // SPDX-License-Identifier: GPL-2.0-or-later /* * efi_selftest_http * * This unit test covers the IPv4 Config2 Protocol, Http Service Binding Protocol, * and Http Protocol. * * An Http HEAD and an Http GET request are sent to the same destination. The test * is successful if the HEAD request gets a response with a valid Content-Length header * and the subsequent GET request receives the amount of bytes informed by the previous * Content-Length header. * */ #include <efi_selftest.h> #include <charset.h> #include <net.h> static struct efi_boot_services *boottime; static struct efi_http_protocol *http; static struct efi_service_binding_protocol *http_service; static struct efi_ip4_config2_protocol *ip4_config2; static efi_handle_t http_protocol_handle; static const efi_guid_t efi_http_guid = EFI_HTTP_PROTOCOL_GUID; static const efi_guid_t efi_http_service_binding_guid = EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID; static const efi_guid_t efi_ip4_config2_guid = EFI_IP4_CONFIG2_PROTOCOL_GUID; static int callback_done; /* * Setup unit test. * * * @handle: handle of the loaded image * @systable: system table * Return: EFI_ST_SUCCESS for success */ static int setup(const efi_handle_t handle, const struct efi_system_table *systable) { efi_status_t ret; efi_handle_t *net_handle; efi_uintn_t num_handles; efi_handle_t *handles; struct efi_http_config_data http_config; struct efi_httpv4_access_point ipv4_node; boottime = systable->boottime; num_handles = 0; boottime->locate_handle_buffer(BY_PROTOCOL, &efi_ip4_config2_guid, NULL, &num_handles, &handles); if (!num_handles) { efi_st_error("Failed to locate ipv4 config2 protocol\n"); return EFI_ST_FAILURE; } for (net_handle = handles; num_handles--; net_handle++) { ret = boottime->open_protocol(*net_handle, &efi_ip4_config2_guid, (void **)&ip4_config2, 0, 0, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (ret != EFI_SUCCESS || !ip4_config2) continue; ret = boottime->open_protocol(*net_handle, &efi_http_service_binding_guid, (void **)&http_service, 0, 0, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (ret != EFI_SUCCESS || !http_service) continue; break; // Get first handle that supports both protocols } if (!ip4_config2 || !http_service) { efi_st_error("Failed to open ipv4 config2 or http service binding protocol\n"); return EFI_ST_FAILURE; } http_protocol_handle = NULL; ret = http_service->create_child(http_service, &http_protocol_handle); if (ret != EFI_SUCCESS || !http_protocol_handle) { efi_st_error("Failed to create an http service instance\n"); return EFI_ST_FAILURE; } ret = boottime->open_protocol(http_protocol_handle, &efi_http_guid, (void **)&http, 0, 0, EFI_OPEN_PROTOCOL_GET_PROTOCOL); if (ret != EFI_SUCCESS || !http) { efi_st_error("Failed to open http protocol\n"); return EFI_ST_FAILURE; } efi_st_printf("HTTP Service Binding: child created successfully\n"); http_config.http_version = HTTPVERSION11; http_config.is_ipv6 = false; http_config.access_point.ipv4_node = &ipv4_node; ipv4_node.use_default_address = true; ret = http->configure(http, &http_config); if (ret != EFI_SUCCESS) { efi_st_error("Failed to configure http instance\n"); return EFI_ST_FAILURE; } return EFI_ST_SUCCESS; } void EFIAPI efi_test_http_callback(struct efi_event *event, void *context) { callback_done = 1; } /* * Execute unit test. * * * Return: EFI_ST_SUCCESS for success */ static int execute(void) { efi_status_t ret; struct efi_http_request_data request_data; struct efi_http_message request_message; struct efi_http_token request_token; struct efi_http_response_data response_data; struct efi_http_message response_message; struct efi_http_token response_token; enum efi_http_status_code status_code; void *response_buffer; efi_uintn_t len, sum; char *url = "http://example.com/"; u16 url_16[64]; u16 *tmp; /* Setup may have failed */ if (!ip4_config2 || !http) { efi_st_error("Cannot proceed with test after setup failure\n"); return EFI_ST_FAILURE; } tmp = url_16; utf8_utf16_strcpy(&tmp, url); request_data.url = url_16; request_data.method = HTTP_METHOD_GET; request_message.data.request = &request_data; request_message.header_count = 3; request_message.body_length = 0; request_message.body = NULL; /* request token */ request_token.event = NULL; request_token.status = EFI_NOT_READY; request_token.message = &request_message; callback_done = 0; ret = boottime->create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, efi_test_http_callback, NULL, &request_token.event); if (ret != EFI_SUCCESS) { efi_st_error("Failed to create request event\n"); return EFI_ST_FAILURE; } ret = http->request(http, &request_token); if (ret != EFI_SUCCESS) { boottime->close_event(request_token.event); efi_st_printf("Failed to proceed with the http request\n"); return EFI_ST_SUCCESS; } while (!callback_done) http->poll(http); response_data.status_code = HTTP_STATUS_UNSUPPORTED_STATUS; response_message.data.response = &response_data; response_message.header_count = 0; response_message.headers = NULL; response_message.body_length = 0; response_message.body = NULL; response_token.event = NULL; ret = boottime->create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK, efi_test_http_callback, NULL, &response_token.event); if (ret != EFI_SUCCESS) { boottime->close_event(request_token.event); efi_st_error("Failed to create response event\n"); return EFI_ST_FAILURE; } response_token.status = EFI_SUCCESS; response_token.message = &response_message; callback_done = 0; ret = http->response(http, &response_token); if (ret != EFI_SUCCESS) { efi_st_error("Failed http first response\n"); goto fail; } while (!callback_done) http->poll(http); if (response_message.data.response->status_code != HTTP_STATUS_200_OK) { status_code = response_message.data.response->status_code; if (status_code == HTTP_STATUS_404_NOT_FOUND) { efi_st_error("File not found\n"); } else { efi_st_error("Bad http status %d\n", response_message.data.response->status_code); } goto fail_free_hdr; } ret = boottime->allocate_pool(EFI_LOADER_CODE, response_message.body_length, &response_buffer); if (ret != EFI_SUCCESS) { efi_st_error("Failed allocating response buffer\n"); goto fail_free_hdr; } len = response_message.body_length; sum = 0; while (len) { response_message.data.response = NULL; response_message.header_count = 0; response_message.headers = NULL; response_message.body_length = len; response_message.body = response_buffer + sum; response_token.message = &response_message; response_token.status = EFI_NOT_READY; callback_done = 0; ret = http->response(http, &response_token); if (ret != EFI_SUCCESS) { efi_st_error("Failed http second response\n"); goto fail_free_buf; } while (!callback_done) http->poll(http); if (!response_message.body_length) break; len -= response_message.body_length; sum += response_message.body_length; } if (len) goto fail_free_buf; boottime->free_pool(response_buffer); if (response_message.headers) boottime->free_pool(response_message.headers); boottime->close_event(request_token.event); boottime->close_event(response_token.event); efi_st_printf("Efi Http request executed successfully\n"); return EFI_ST_SUCCESS; fail_free_buf: boottime->free_pool(response_buffer); fail_free_hdr: if (response_message.headers) boottime->free_pool(response_message.headers); fail: boottime->close_event(request_token.event); boottime->close_event(response_token.event); return EFI_ST_FAILURE; } /* * Tear down unit test. * * Return: EFI_ST_SUCCESS for success */ static int teardown(void) { efi_status_t ret; int exit_status = EFI_ST_SUCCESS; if (!http_service || !http_protocol_handle) { efi_st_error("No handles to destroy http instance"); exit_status = EFI_ST_FAILURE; } else { ret = http_service->destroy_child(http_service, http_protocol_handle); if (ret != EFI_SUCCESS) { efi_st_error("Failed to destroy http instance"); exit_status = EFI_ST_FAILURE; } efi_st_printf("HTTP Service Binding: child destroyed successfully\n"); } return exit_status; } EFI_UNIT_TEST(http) = { .name = "http protocol", .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, .setup = setup, .execute = execute, .teardown = teardown, #ifdef CONFIG_SANDBOX /* * Running this test on the sandbox requires setting environment * variable ethact to a network interface connected to a DHCP server and * ethrotate to 'no'. */ .on_request = true, #endif }; |