test_ota_ops.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * SPDX-FileCopyrightText: 2021 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. #include "bootloader_common.h"
  16. /* These OTA tests currently don't assume an OTA partition exists
  17. on the device, so they're a bit limited
  18. */
  19. TEST_CASE("esp_ota_begin() verifies arguments", "[ota]")
  20. {
  21. const esp_partition_t *running = esp_ota_get_running_partition();
  22. esp_partition_t partition;
  23. static esp_ota_handle_t handle = 0;
  24. if (handle != 0) { /* clean up from any previous test */
  25. esp_ota_end(handle);
  26. handle = 0;
  27. }
  28. /* running partition & configured boot partition are same */
  29. TEST_ASSERT_NOT_NULL(running);
  30. /* trying to 'begin' on running partition fails */
  31. TEST_ASSERT_NOT_EQUAL(ESP_OK, esp_ota_begin(running, OTA_SIZE_UNKNOWN, &handle));
  32. TEST_ASSERT_EQUAL(0, handle);
  33. memcpy(&partition, running, sizeof(esp_partition_t));
  34. partition.address--;
  35. /* non existent partition fails */
  36. TEST_ASSERT_EQUAL_HEX(ESP_ERR_NOT_FOUND, esp_ota_begin(&partition, OTA_SIZE_UNKNOWN, &handle));
  37. TEST_ASSERT_EQUAL(0, handle);
  38. }
  39. TEST_CASE("esp_ota_get_next_update_partition logic", "[ota]")
  40. {
  41. const esp_partition_t *running = esp_ota_get_running_partition();
  42. const esp_partition_t *factory = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  43. ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
  44. const esp_partition_t *ota_0 = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  45. ESP_PARTITION_SUBTYPE_APP_OTA_0, NULL);
  46. const esp_partition_t *ota_1 = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  47. ESP_PARTITION_SUBTYPE_APP_OTA_1, NULL);
  48. const esp_partition_t *ota_2 = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
  49. ESP_PARTITION_SUBTYPE_APP_OTA_2, NULL);
  50. TEST_ASSERT_NOT_NULL(running);
  51. TEST_ASSERT_NOT_NULL(factory);
  52. TEST_ASSERT_NOT_NULL(ota_0);
  53. TEST_ASSERT_NOT_NULL(ota_1);
  54. TEST_ASSERT_NULL(ota_2); /* this partition shouldn't exist in test partition table */
  55. TEST_ASSERT_EQUAL_PTR(factory, running); /* this may not be true if/when we get OTA tests that do OTA updates */
  56. /* (The test steps verify subtypes before verifying pointer equality, because the failure messages are more readable
  57. this way.)
  58. */
  59. /* Factory app OTA updates OTA 0 slot */
  60. const esp_partition_t *p = esp_ota_get_next_update_partition(NULL);
  61. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_0, p->subtype);
  62. TEST_ASSERT_EQUAL_PTR(ota_0, p);
  63. p = esp_ota_get_next_update_partition(factory);
  64. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_0, p->subtype);
  65. TEST_ASSERT_EQUAL_PTR(ota_0, p);
  66. /* OTA slot 0 updates OTA slot 1 */
  67. p = esp_ota_get_next_update_partition(ota_0);
  68. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_1, p->subtype);
  69. TEST_ASSERT_EQUAL_PTR(ota_1, p);
  70. /* OTA slot 1 updates OTA slot 0 */
  71. p = esp_ota_get_next_update_partition(ota_1);
  72. TEST_ASSERT_EQUAL_HEX8(ESP_PARTITION_SUBTYPE_APP_OTA_0, p->subtype);;
  73. TEST_ASSERT_EQUAL_PTR(ota_0, p);
  74. }
  75. TEST_CASE("esp_ota_get_partition_description", "[ota]")
  76. {
  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. }