test_verify_image.c 3.9 KB

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