test_verify_image.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Tests for bootloader_support esp_load(ESP_IMAGE_VERIFY, ...)
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include "string.h"
  7. #include "rom/ets_sys.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/semphr.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/xtensa_api.h"
  13. #include "unity.h"
  14. #include "bootloader_common.h"
  15. #include "bootloader_util.h"
  16. #include "esp_partition.h"
  17. #include "esp_ota_ops.h"
  18. #include "esp_image_format.h"
  19. TEST_CASE("Verify bootloader image in flash", "[bootloader_support]")
  20. {
  21. const esp_partition_pos_t fake_bootloader_partition = {
  22. .offset = ESP_BOOTLOADER_OFFSET,
  23. .size = ESP_PARTITION_TABLE_OFFSET - ESP_BOOTLOADER_OFFSET,
  24. };
  25. esp_image_metadata_t data = { 0 };
  26. TEST_ASSERT_EQUAL_HEX(ESP_OK, esp_image_verify(ESP_IMAGE_VERIFY, &fake_bootloader_partition, &data));
  27. TEST_ASSERT_NOT_EQUAL(0, data.image_len);
  28. uint32_t bootloader_length = 0;
  29. TEST_ASSERT_EQUAL_HEX(ESP_OK, esp_image_verify_bootloader(&bootloader_length));
  30. TEST_ASSERT_EQUAL(data.image_len, bootloader_length);
  31. }
  32. TEST_CASE("Verify unit test app image", "[bootloader_support]")
  33. {
  34. esp_image_metadata_t data = { 0 };
  35. const esp_partition_t *running = esp_ota_get_running_partition();
  36. TEST_ASSERT_NOT_EQUAL(NULL, running);
  37. const esp_partition_pos_t running_pos = {
  38. .offset = running->address,
  39. .size = running->size,
  40. };
  41. TEST_ASSERT_EQUAL_HEX(ESP_OK, esp_image_verify(ESP_IMAGE_VERIFY, &running_pos, &data));
  42. TEST_ASSERT_NOT_EQUAL(0, data.image_len);
  43. TEST_ASSERT_TRUE(data.image_len <= running->size);
  44. }
  45. void check_label_search (int num_test, const char *list, const char *t_label, bool result)
  46. {
  47. // gen_esp32part.py trims up to 16 characters
  48. // and the string may not have a null terminal symbol.
  49. // below is cutting as it does the generator.
  50. char label[16 + 1] = {0};
  51. strncpy(label, t_label, sizeof(label) - 1);
  52. bool ret = bootloader_common_label_search(list, label);
  53. if (ret != result) {
  54. printf("%d) %s | %s \n", num_test, list, label);
  55. }
  56. TEST_ASSERT_MESSAGE(ret == result, "Test failed");
  57. }
  58. TEST_CASE("Test label_search", "[bootloader_support]")
  59. {
  60. TEST_ASSERT_FALSE(bootloader_common_label_search(NULL, NULL));
  61. TEST_ASSERT_FALSE(bootloader_common_label_search("nvs", NULL));
  62. check_label_search(1, "nvs", "nvs", true);
  63. check_label_search(2, "nvs, ", "nvs", true);
  64. check_label_search(3, "nvs1", "nvs", false);
  65. check_label_search(3, "nvs1, ", "nvs", false);
  66. check_label_search(4, "nvs1nvs1, phy", "nvs1", false);
  67. check_label_search(5, "nvs1, nvs1, phy", "nvs1", true);
  68. check_label_search(6, "nvs12, nvs12, phy", "nvs1", false);
  69. check_label_search(7, "nvs12, nvs1, phy", "nvs1", true);
  70. check_label_search(8, "nvs12, nvs3, phy, nvs1","nvs1", true);
  71. check_label_search(9, "nvs1nvs1, phy, nvs", "nvs", true);
  72. check_label_search(10, "nvs1nvs1, phy, nvs1", "nvs", false);
  73. check_label_search(11, "nvs1, nvs, phy, nvs1", "nvs", true);
  74. check_label_search(12, "nvs1, nvs2, phy, nvs","nvs", true);
  75. check_label_search(13, "ota_data, backup_nvs", "nvs", false);
  76. check_label_search(14, "nvs1, nvs2, ota, nvs", "vs1", false);
  77. check_label_search(20, "12345678901234, phy, nvs1", "12345678901234", true);
  78. check_label_search(21, "123456789012345, phy, nvs1", "123456789012345", true);
  79. check_label_search(22, "1234567890123456, phy, nvs1", "1234567890123456", true);
  80. check_label_search(23, "12345678901234567, phy, nvs1", "12345678901234567", false);
  81. check_label_search(24, "1234567890123456, phy, nvs1", "12345678901234567", true);
  82. check_label_search(25, "phy, 1234567890123456, nvs1", "12345678901234567", true);
  83. }
  84. TEST_CASE("Test regions_overlap", "[bootloader_support]")
  85. {
  86. TEST_ASSERT( bootloader_util_regions_overlap(1, 2, 1, 2) );
  87. TEST_ASSERT( bootloader_util_regions_overlap(1, 2, 0, 2) );
  88. TEST_ASSERT( bootloader_util_regions_overlap(1, 2, 1, 3) );
  89. TEST_ASSERT( bootloader_util_regions_overlap(1, 2, 0, 3) );
  90. TEST_ASSERT( bootloader_util_regions_overlap(0, 2, 1, 2) );
  91. TEST_ASSERT( bootloader_util_regions_overlap(1, 3, 1, 2) );
  92. TEST_ASSERT( bootloader_util_regions_overlap(0, 3, 1, 2) );
  93. TEST_ASSERT( !bootloader_util_regions_overlap(2, 3, 1, 2) );
  94. TEST_ASSERT( !bootloader_util_regions_overlap(1, 2, 2, 3) );
  95. TEST_ASSERT( !bootloader_util_regions_overlap(3, 4, 1, 2) );
  96. TEST_ASSERT( !bootloader_util_regions_overlap(1, 2, 3, 4) );
  97. }