forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunit-delta.c
More file actions
230 lines (188 loc) · 6.06 KB
/
unit-delta.c
File metadata and controls
230 lines (188 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* unit-delta.c
*
* unit tests for delta updates module
*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <check.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "delta.h"
#define WC_RSA_BLINDING
#include "delta.c"
#define SRC_SIZE 4096
#define PATCH_SIZE 8192
#define DST_SIZE 4096
#define DIFF_SIZE 8192
START_TEST(test_wb_patch_init_invalid)
{
WB_PATCH_CTX ctx;
uint8_t src[SRC_SIZE] = {0};
uint8_t patch[PATCH_SIZE] = {0};
ck_assert_int_eq(wb_patch_init(NULL, src, SRC_SIZE, patch, PATCH_SIZE), -1);
ck_assert_int_eq(wb_patch_init(&ctx, src, 0, patch, PATCH_SIZE), -1);
ck_assert_int_eq(wb_patch_init(&ctx, src, SRC_SIZE, patch, 0), -1);
}
END_TEST
START_TEST(test_wb_patch_src_bounds_invalid)
{
WB_PATCH_CTX patch_ctx;
uint8_t src[SRC_SIZE] = {0};
uint8_t patch[PATCH_SIZE] = {0};
uint8_t dst[DELTA_BLOCK_SIZE] = {0};
int ret;
/* ESC + header with src_off beyond src_size */
patch[0] = ESC;
patch[1] = 0x00; /* off[0] */
patch[2] = 0x10; /* off[1] -> 0x001000 */
patch[3] = 0x00; /* off[2] */
patch[4] = 0x00; /* sz[0] */
patch[5] = 0x10; /* sz[1] -> 16 */
ret = wb_patch_init(&patch_ctx, src, SRC_SIZE, patch, BLOCK_HDR_SIZE);
ck_assert_int_eq(ret, 0);
ret = wb_patch(&patch_ctx, dst, sizeof(dst));
ck_assert_int_eq(ret, -1);
}
END_TEST
START_TEST(test_wb_patch_resume_bounds_invalid)
{
WB_PATCH_CTX patch_ctx;
uint8_t src[SRC_SIZE] = {0};
uint8_t patch[PATCH_SIZE] = {0};
uint8_t dst[DELTA_BLOCK_SIZE] = {0};
int ret;
ret = wb_patch_init(&patch_ctx, src, SRC_SIZE, patch, BLOCK_HDR_SIZE);
ck_assert_int_eq(ret, 0);
patch_ctx.matching = 1;
patch_ctx.blk_off = SRC_SIZE + 1;
patch_ctx.blk_sz = 4;
ret = wb_patch(&patch_ctx, dst, sizeof(dst));
ck_assert_int_eq(ret, -1);
}
END_TEST
START_TEST(test_wb_diff_init_invalid)
{
WB_DIFF_CTX ctx;
uint8_t src_a[SRC_SIZE] = {0};
uint8_t src_b[SRC_SIZE] = {0};
ck_assert_int_eq(wb_diff_init(NULL, src_a, SRC_SIZE, src_b, SRC_SIZE), -1);
ck_assert_int_eq(wb_diff_init(&ctx, src_a, 0, src_b, SRC_SIZE), -1);
ck_assert_int_eq(wb_diff_init(&ctx, src_a, SRC_SIZE, src_b, 0), -1);
}
END_TEST
static void initialize_buffers(uint8_t *src_a, uint8_t *src_b)
{
uint32_t pseudo_rand = 0;
uint8_t tmp[128];
for (int i = 0; i < SRC_SIZE; ++i) {
src_a[i] = pseudo_rand % 256;
src_b[i] = pseudo_rand % 256;
if ((i % 100) == 42) {
src_b[i] -= 1;
}
pseudo_rand *= 1664525;
pseudo_rand += 1013904223;
pseudo_rand ^= ~(i);
}
/* Introduce differences */
src_b[100] = src_a[100] + 1;
src_b[200] = src_a[200] + 2;
/* 10-bytes difference across two blocks */
for (int i = 1020; i < 1040; ++i) {
src_b[i] = src_a[i] + 3;
}
/* Copy a sequence from A to B, behind */
src_a[510] = ESC;
memcpy(src_b + 4090, src_a + 500, 20);
/* Copy a sequence from B to itself, ahead */
src_b[1022] = ESC;
memcpy(tmp, src_b + 1020, 30);
memcpy(src_b + 7163, tmp, 30);
}
START_TEST(test_wb_patch_and_diff)
{
WB_DIFF_CTX diff_ctx;
WB_PATCH_CTX patch_ctx;
uint8_t src_a[SRC_SIZE];
uint8_t src_b[SRC_SIZE];
uint8_t patch[PATCH_SIZE];
uint8_t patched_dst[DST_SIZE];
int ret;
int i;
uint32_t p_written = 0;
initialize_buffers(src_a, src_b);
ret = wb_diff_init(&diff_ctx, src_a, SRC_SIZE, src_b, SRC_SIZE);
ck_assert_int_eq(ret, 0);
/* Create the patch */
for (i = 0; i < SRC_SIZE; i += DELTA_BLOCK_SIZE) {
ret = wb_diff(&diff_ctx, patch + p_written, DELTA_BLOCK_SIZE);
ck_assert_int_ge(ret, 0); /* Should not be 0 until patch is over*/
if (ret == 0)
break;
p_written += ret;
}
ck_assert_int_gt(p_written, 0); /* Should not be 0 */
printf("patch size: %u\n", p_written);
ret = wb_patch_init(&patch_ctx, src_a, SRC_SIZE, patch, p_written);
ck_assert_int_eq(ret, 0);
/* Apply the patch */
for (i = 0; i < SRC_SIZE;)
{
ret = wb_patch(&patch_ctx, patched_dst + i, DELTA_BLOCK_SIZE);
ck_assert_int_ge(ret, 0); /* Should not be 0 until patch is over*/
if (ret == 0)
break;
i += ret;
}
ck_assert_int_gt(i, 0); /* Should not be 0 */
ck_assert_int_eq(i, SRC_SIZE); // The patched length should match the buffer size
/* Verify that the patched destination matches src_b */
for (int i = 0; i < SRC_SIZE; ++i) {
ck_assert_uint_eq(patched_dst[i], src_b[i]);
}
}
END_TEST
Suite *patch_diff_suite(void)
{
Suite *s;
TCase *tc_wolfboot_delta;
s = suite_create("PatchDiff");
/* Core test case */
tc_wolfboot_delta = tcase_create("wolfboot-delta");
tcase_add_test(tc_wolfboot_delta, test_wb_patch_init_invalid);
tcase_add_test(tc_wolfboot_delta, test_wb_diff_init_invalid);
tcase_add_test(tc_wolfboot_delta, test_wb_patch_src_bounds_invalid);
tcase_add_test(tc_wolfboot_delta, test_wb_patch_resume_bounds_invalid);
tcase_add_test(tc_wolfboot_delta, test_wb_patch_and_diff);
suite_add_tcase(s, tc_wolfboot_delta);
return s;
}
int main(void)
{
int ret;
Suite *s;
SRunner *sr;
s = patch_diff_suite();
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
ret = srunner_ntests_failed(sr);
srunner_free(sr);
return ret;
}