test_ota_ops.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <freertos/FreeRTOS.h>
  10. #include <freertos/task.h>
  11. #include <freertos/semphr.h>
  12. #include <unity.h>
  13. #include <test_utils.h>
  14. #include <esp_ota_ops.h>
  15. /* These OTA tests currently don't assume an OTA partition exists
  16. on the device, so they're a bit limited
  17. */
  18. TEST_CASE("esp_ota_begin() verifies arguments", "[ota]")
  19. {
  20. const esp_partition_t *running = esp_ota_get_running_partition();
  21. esp_partition_t partition;
  22. static esp_ota_handle_t handle = 0;
  23. if (handle != 0) { /* clean up from any previous test */
  24. esp_ota_end(handle);
  25. handle = 0;
  26. }
  27. /* running partition & configured boot partition are same */
  28. TEST_ASSERT_NOT_NULL(running);
  29. /* trying to 'begin' on running partition fails */
  30. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_ota_begin(running, OTA_SIZE_UNKNOWN, &handle));
  31. TEST_ASSERT_EQUAL(0, handle);
  32. memcpy(&partition, running, sizeof(esp_partition_t));
  33. partition.address--;
  34. /* non existent partition fails */
  35. TEST_ASSERT_EQUAL_HEX(ESP_ERR_NOT_FOUND, esp_ota_begin(&partition, OTA_SIZE_UNKNOWN, &handle));
  36. TEST_ASSERT_EQUAL(0, handle);
  37. }
  38. TEST_CASE("esp_ota_get_next_update_partition logic", "[ota]")
  39. {
  40. const esp_partition_t *running = esp_ota_get_running_partition();
  41. const esp_partition_t *factory = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  42. ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
  43. const esp_partition_t *ota_0 = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  44. ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL);
  45. const esp_partition_t *ota_1 = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  46. ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
  47. const esp_partition_t *ota_2 = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  48. ESP_PARTITION_SUBTYPE_APP_OTA_2, NULL);
  49. TEST_ASSERT_NOT_NULL(running);
  50. TEST_ASSERT_NOT_NULL(factory);
  51. TEST_ASSERT_NOT_NULL(ota_0);
  52. TEST_ASSERT_NOT_NULL(ota_1);
  53. TEST_ASSERT_NULL(ota_2); /* this partition shouldn't exist in test partition table */
  54. TEST_ASSERT_EQUAL_PTR(factory, running); /* this may not be true if/when we get OTA tests that do OTA updates */
  55. /* (The test steps verify subtypes before verifying pointer equality, because the failure messages are more readable
  56. this way.)
  57. */
  58. /* Factory app OTA updates OTA 0 slot */
  59. const esp_partition_t *p = esp_ota_get_next_update_partition(NULL);
  60. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_0, p->subtype);
  61. TEST_ASSERT_EQUAL_PTR(ota_0, p);
  62. p = esp_ota_get_next_update_partition(factory);
  63. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_0, p->subtype);
  64. TEST_ASSERT_EQUAL_PTR(ota_0, p);
  65. /* OTA slot 0 updates OTA slot 1 */
  66. p = esp_ota_get_next_update_partition(ota_0);
  67. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_1, p->subtype);
  68. TEST_ASSERT_EQUAL_PTR(ota_1, p);
  69. /* OTA slot 1 updates OTA slot 0 */
  70. p = esp_ota_get_next_update_partition(ota_1);
  71. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_0, p->subtype);;
  72. TEST_ASSERT_EQUAL_PTR(ota_0, p);
  73. }
  74. TEST_CASE("esp_ota_get_partition_description", "[ota]")
  75. {
  76. extern esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc);
  77. const esp_partition_t *running = esp_ota_get_running_partition();
  78. TEST_ASSERT_NOT_NULL(running);
  79. esp_app_desc_t app_desc1, app_desc2;
  80. TEST_ESP_OK(esp_ota_get_partition_description(running, &app_desc1));
  81. const esp_partition_pos_t running_pos = {
  82. .offset = running->address,
  83. .size = running->size
  84. };
  85. TEST_ESP_OK(bootloader_common_get_partition_description(&running_pos, &app_desc2));
  86. TEST_ASSERT_EQUAL_MEMORY_MESSAGE((uint8_t *)&app_desc1, (uint8_t *)&app_desc2, sizeof(app_desc1), "must be the same");
  87. const esp_partition_t *not_app = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL);
  88. TEST_ASSERT_NOT_NULL(not_app);
  89. TEST_ESP_ERR(ESP_ERR_NOT_SUPPORTED, esp_ota_get_partition_description(not_app, &app_desc1));
  90. const esp_partition_pos_t not_app_pos = {
  91. .offset = not_app->address,
  92. .size = not_app->size
  93. };
  94. TEST_ESP_ERR(ESP_ERR_NOT_FOUND, bootloader_common_get_partition_description(&not_app_pos, &app_desc1));
  95. }