Skip to content

Commit dc96c47

Browse files
committed
Fixed bug in bmdiff
F/222
1 parent 66badb2 commit dc96c47

3 files changed

Lines changed: 147 additions & 2 deletions

File tree

tools/delta/Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bmdiff.o:
3232
gcc -c -o bmdiff.o bmdiff.c -I../../include -ggdb $(CFLAGS)
3333

3434
clean:
35-
rm -f bmpatch bmdiff delta.o
35+
rm -f bmpatch bmdiff bmdiff-test delta.o test-bmdiff.o
3636

3737
delta-test: FORCE bmdiff bmpatch
3838
@./bmdiff delta-test/0.txt delta-test/1.txt 0-to-1.patch
@@ -45,4 +45,13 @@ delta-test: FORCE bmdiff bmpatch
4545
@diff 1p.txt delta-test/0.txt && echo "Test 1-to-0: OK"
4646
@rm -f 0-to-1.patch 1-to-0.patch 0p.txt 1p.txt
4747

48+
bmdiff-test: test-bmdiff.o
49+
gcc -o bmdiff-test test-bmdiff.o
50+
51+
test-bmdiff.o:
52+
gcc -c -o test-bmdiff.o test-bmdiff.c -I../../include -ggdb $(CFLAGS)
53+
54+
test: FORCE bmdiff-test
55+
@./bmdiff-test && echo "bmdiff mmap failure test: OK"
56+
4857
.PHONY: FORCE

tools/delta/bmdiff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ int main(int argc, char *argv[])
9898
}
9999
len2 = st.st_size;
100100
buffer = mmap(NULL, len2, PROT_READ, MAP_SHARED, fd2, 0);
101-
if (base == (void *)(-1)) {
101+
if (buffer == (void *)(-1)) {
102102
perror("mmap");
103103
exit(3);
104104
}

tools/delta/test-bmdiff.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <assert.h>
2+
#include <setjmp.h>
3+
#include <stdint.h>
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include <sys/mman.h>
7+
#include <sys/stat.h>
8+
9+
static jmp_buf exit_jmp;
10+
static int exit_code = -1;
11+
static int mmap_calls = 0;
12+
static int patch_init_called = 0;
13+
static int perror_called = 0;
14+
15+
static int mock_stat(const char *path, struct stat *st)
16+
{
17+
memset(st, 0, sizeof(*st));
18+
if (strcmp(path, "source.bin") == 0) {
19+
st->st_size = 16;
20+
return 0;
21+
}
22+
if (strcmp(path, "patch.bin") == 0) {
23+
st->st_size = 8;
24+
return 0;
25+
}
26+
return -1;
27+
}
28+
29+
static int mock_open(const char *path, int flags, ...)
30+
{
31+
(void)flags;
32+
if ((strcmp(path, "source.bin") == 0) || (strcmp(path, "patch.bin") == 0))
33+
return 3;
34+
return -1;
35+
}
36+
37+
static void *mock_mmap(void *addr, size_t len, int prot, int flags, int fd,
38+
off_t offset)
39+
{
40+
static uint8_t source_map[16];
41+
42+
(void)addr;
43+
(void)len;
44+
(void)prot;
45+
(void)flags;
46+
(void)fd;
47+
(void)offset;
48+
49+
mmap_calls++;
50+
if (mmap_calls == 1)
51+
return source_map;
52+
53+
return MAP_FAILED;
54+
}
55+
56+
static void mock_perror(const char *s)
57+
{
58+
(void)s;
59+
perror_called = 1;
60+
}
61+
62+
static void mock_exit(int code)
63+
{
64+
exit_code = code;
65+
longjmp(exit_jmp, 1);
66+
}
67+
68+
#define WOLFBOOT_SECTOR_SIZE 1024
69+
#define stat(path, st) mock_stat(path, st)
70+
#define open(path, flags, ...) mock_open(path, flags, ##__VA_ARGS__)
71+
#define mmap(addr, len, prot, flags, fd, offset) \
72+
mock_mmap(addr, len, prot, flags, fd, offset)
73+
#define perror(s) mock_perror(s)
74+
#define exit(code) mock_exit(code)
75+
#define main bmdiff_tool_main
76+
#include "bmdiff.c"
77+
#undef main
78+
#undef exit
79+
#undef perror
80+
#undef mmap
81+
#undef open
82+
#undef stat
83+
84+
int wb_patch_init(WB_PATCH_CTX *bm, uint8_t *src, uint32_t ssz, uint8_t *patch,
85+
uint32_t psz)
86+
{
87+
(void)bm;
88+
(void)src;
89+
(void)ssz;
90+
(void)patch;
91+
(void)psz;
92+
93+
patch_init_called = 1;
94+
mock_exit(99);
95+
return -1;
96+
}
97+
98+
int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
99+
{
100+
(void)ctx;
101+
(void)dst;
102+
(void)len;
103+
return 0;
104+
}
105+
106+
int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a,
107+
uint8_t *src_b, uint32_t len_b)
108+
{
109+
(void)ctx;
110+
(void)src_a;
111+
(void)len_a;
112+
(void)src_b;
113+
(void)len_b;
114+
return 0;
115+
}
116+
117+
int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
118+
{
119+
(void)ctx;
120+
(void)patch;
121+
(void)len;
122+
return 0;
123+
}
124+
125+
int main(void)
126+
{
127+
char *argv[] = { (char *)"bmpatch", (char *)"source.bin", (char *)"patch.bin" };
128+
129+
if (setjmp(exit_jmp) == 0)
130+
bmdiff_tool_main(3, argv);
131+
132+
assert(exit_code == 3);
133+
assert(perror_called == 1);
134+
assert(patch_init_called == 0);
135+
return 0;
136+
}

0 commit comments

Comments
 (0)