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