-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.c
299 lines (239 loc) · 8.09 KB
/
test.c
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdint.h>
#include "md4_global.h"
#include "md4.h"
int allTestsPassed=1;
void printAndFail(char *line) {
printf("%s: %s\n", line, strerror(errno));
exit(1);
}
void createZeroFile(char *filename, size_t size) {
FILE *f = fopen(filename, "w");
if (!f) {
printAndFail("Cannot create file");
}
char *block = (char*) malloc(size);
bzero(block, size);
if (fwrite(block, 1, size, f) != size) {
printAndFail("Cannot write data 1");
}
fclose(f);
}
void changeByte(char *filename, off_t position, char byte) {
FILE *f = fopen(filename, "r+");
if (!f) {
printAndFail("Cannot create file");
}
if (fseeko(f, position, SEEK_SET)<0) {
printAndFail("Cannot seek");
}
char bytes[1];
bytes[0]=byte;
if (fwrite(bytes, 1, 1, f)!=1) {
printAndFail("Cannot write data 2");
}
fclose(f);
}
void addBytes(char *filename, int countOfBytes, char byte) {
FILE *f = fopen(filename, "a+");
if (!f) {
printAndFail("Cannot create file");
}
char *bytes= (char*) malloc(countOfBytes+1);
int i;
for(i=0;i<countOfBytes;i++) {
bytes[i]=byte;
}
if (fwrite(bytes, 1, countOfBytes, f)!=countOfBytes) {
printAndFail("Cannot write data 3");
}
fclose(f);
free(bytes);
}
uint32_t blocksCount(char *filename) {
struct stat fileStat;
if (stat(filename,&fileStat)==-1) {
return -1;
}
return (uint32_t) fileStat.st_blocks;
}
off_t fileSize(char *filename) {
struct stat fileStat;
if (stat(filename,&fileStat)==-1) {
return -1;
}
return fileStat.st_size;
}
int checkFileSize(char *testName, char *filename, size_t referenceSize) {
int64_t sourceFileSize = (int64_t) fileSize(filename);
if (sourceFileSize == (int64_t) referenceSize) {
printf("%s (size): Pass\n", testName);
return 1;
} else {
allTestsPassed=0;
printf("%s (size): FAIL. Source file size %llu; Dest file size %llu\n", testName, sourceFileSize, (int64_t) referenceSize);
return 0;
}
}
void calcMD4(char *filename, char *md4Result) {
off_t size = fileSize(filename);
char *block = (char*) malloc(size);
bzero(block, size);
FILE *f = fopen(filename, "r");
fread(block, 1, size, f);
fclose(f);
unsigned char digest[16];
MD4_CTX mdContext;
MD4Init (&mdContext);
MD4Update (&mdContext, block, size);
MD4Final (digest, &mdContext);
char readingMD4[33];
bzero(readingMD4, 33);
int i;
for (i = 0; i < 16; i++) {
sprintf(readingMD4, "%s%02x", readingMD4, digest[i]);
}
strncpy(md4Result, readingMD4, 33);
}
int syncAndCheckMd4(char *testName, char *sourceFilename, char *destFilename, int isSparse, int isSourceZero) {
char md4Source[33];
calcMD4(sourceFilename, md4Source);
char command[100];
sprintf(command, "./bigsync --source %s --dest %s --blocksize _ --quiet %s %s",
sourceFilename, destFilename,
isSparse ? "--sparse" : "",
isSourceZero ? "--zero" : ""
);
system(command);
char md4Dest[33];
calcMD4(destFilename, md4Dest);
if (strcmp(md4Source, md4Dest)==0) {
printf("%s (sync): Pass\n", testName);
return 1;
} else {
allTestsPassed=0;
printf("%s (sync): FAIL. Source MD4 %s; Dest MD4 %s\n", testName, md4Source, md4Dest);
return 0;
}
}
void cleanup() {
remove("testSource.bin");
remove("testDest.bin");
remove("testDest.bin.bigsync");
}
void testSparse() {
cleanup();
uint32_t sparseFailCount=0;
createZeroFile("testSource.bin", 1024*1024*5);
uint32_t sourceBlocksCount = blocksCount("testSource.bin");
syncAndCheckMd4("recreated source to zero", "testSource.bin", "testDest.bin", 1, 0);
checkFileSize("recreated source to zero size", "testDest.bin", 1024*1024*5);
uint32_t destBlocksCount = blocksCount("testDest.bin");
if (destBlocksCount >= sourceBlocksCount) {
sparseFailCount++;
}
changeByte("testSource.bin", 5, 'c');
syncAndCheckMd4("changed byte (SPARSE) ", "testSource.bin", "testDest.bin", 1, 0);
checkFileSize("changed byte (SPARSE) (size)", "testDest.bin", 1024*1024*5);
destBlocksCount = blocksCount("testDest.bin");
if (destBlocksCount >= sourceBlocksCount) {
sparseFailCount++;
}
changeByte("testSource.bin", 5, 0);
syncAndCheckMd4("changed byte to zero (SPARSE) ", "testSource.bin", "testDest.bin", 1, 0);
checkFileSize("changed byte to zero (SPARSE) (size)", "testDest.bin", 1024*1024*5);
destBlocksCount = blocksCount("testDest.bin");
if (destBlocksCount >= sourceBlocksCount) {
sparseFailCount++;
}
addBytes("testSource.bin", 5*1024*1024, 0);
sourceBlocksCount = blocksCount("testSource.bin");
syncAndCheckMd4("added 5 md (SPARSE) ", "testSource.bin", "testDest.bin", 1, 0);
checkFileSize("added 5 md (SPARSE) (size)", "testDest.bin", 1024*1024*10);
destBlocksCount = blocksCount("testDest.bin");
if (destBlocksCount >= sourceBlocksCount) {
sparseFailCount++;
}
changeByte("testSource.bin", 7*1024*1024, 'c');
syncAndCheckMd4("changed one byte (SPARSE) ", "testSource.bin", "testDest.bin", 1, 0);
checkFileSize("changed one byte (SPARSE) (size)", "testDest.bin", 1024*1024*10);
destBlocksCount = blocksCount("testDest.bin");
if (destBlocksCount >= sourceBlocksCount) {
sparseFailCount++;
}
if (sparseFailCount>0) {
printf("\n Warning: sparse mode was enabled, but the destination file doesn't seem\n"
" to occupy less disk space then the original file. For certain file systems it's\n"
" okay as they don't support sparse files.\n\n"
" This warning is safe, sparse files support is NOT required for data integrity.\n\n"
);
}
}
void testZeroSizedSource(int isSparse) {
cleanup();
createZeroFile("testSource.bin", 1024*1024*5);
syncAndCheckMd4("zero sized source", "testSource.bin", "testDest.bin", isSparse, 1);
checkFileSize("zero sized source", "testDest.bin", 1024*1024*5);
changeByte("testSource.bin", 5, 'c');
syncAndCheckMd4("changed byte (zero sized source) ", "testSource.bin", "testDest.bin", isSparse, 1);
checkFileSize("changed byte (zero sized source) (size)", "testDest.bin", 1024*1024*5);
addBytes("testSource.bin", 1*1024*1024, 0);
syncAndCheckMd4("added 1 md (zero sized source) ", "testSource.bin", "testDest.bin", isSparse, 1);
checkFileSize("added 1 md (zero sized source) (size)", "testDest.bin", 1024*1024*6);
truncate("testSource.bin", 2*1024*1024);
syncAndCheckMd4("truncated (zero sized source) ", "testSource.bin", "testDest.bin", isSparse, 1);
checkFileSize("truncated (zero sized source) (size)", "testDest.bin", 1024*1024*2);
}
void testCycle(int sparseEnabled) {
cleanup();
createZeroFile("testSource.bin", 110000);
syncAndCheckMd4("sync zero 1", "testSource.bin", "testDest.bin", sparseEnabled, 0);
checkFileSize("sync zero 1", "testSource.bin", 110000);
changeByte("testSource.bin", 5, 'c');
syncAndCheckMd4("one byte changed 1", "testSource.bin", "testDest.bin", sparseEnabled, 0);
checkFileSize("one byte changed 2", "testDest.bin", 110000);
changeByte("testSource.bin", 80000, 'c');
syncAndCheckMd4("one byte changed 1", "testSource.bin", "testDest.bin", sparseEnabled, 0);
checkFileSize("one byte changed 2", "testDest.bin", 110000);
addBytes("testSource.bin", 3, 'c');
syncAndCheckMd4("added 3 bytes 1", "testSource.bin", "testDest.bin", sparseEnabled, 0);
checkFileSize("added 3 bytes 2", "testDest.bin", 110003);
addBytes("testSource.bin", 100001, 'c');
syncAndCheckMd4("added 8 bytes 1", "testSource.bin", "testDest.bin", sparseEnabled, 0);
checkFileSize("added 8 bytes 2", "testDest.bin", 210004);
truncate("testSource.bin", 123000);
syncAndCheckMd4("truncated", "testSource.bin", "testDest.bin", sparseEnabled, 0);
checkFileSize("truncated", "testDest.bin", 123000);
}
void testBasic() {
cleanup();
createZeroFile("testSource.bin", 4000);
changeByte("testSource.bin", 5, 'r');
syncAndCheckMd4("sync single block", "testSource.bin", "testDest.bin", 0, 0);
checkFileSize("sync single block size", "testSource.bin", 4000);
}
int main(void) {
testBasic();
testCycle(0);
testCycle(1);
testSparse();
testZeroSizedSource(0);
testZeroSizedSource(1);
cleanup();
if (allTestsPassed) {
printf("\nAll tests passed.\n");
} else {
printf("\nSome tests failed to pass.\n");
}
return 0;
}