Skip to content

Commit 728d121

Browse files
add more test cases
1 parent 302e37a commit 728d121

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

code/tests/cases/test_myshell.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,108 @@ FOSSIL_TEST(c_test_myshell_empty_strings) {
206206
remove(file_name);
207207
}
208208

209+
FOSSIL_TEST(c_test_myshell_corrupted_key_hash) {
210+
fossil_bluecrab_myshell_error_t err;
211+
const char *file_name = "corrupt_key.myshell";
212+
fossil_bluecrab_myshell_t *db = fossil_myshell_create(file_name, &err);
213+
ASSUME_ITS_TRUE(db != NULL);
214+
215+
fossil_myshell_put(db, "corruptkey", "corruptval");
216+
fossil_myshell_commit(db, "commit");
217+
218+
fossil_myshell_close(db);
219+
220+
// Corrupt the key hash in the file
221+
FILE *file = fopen(file_name, "rb+");
222+
ASSUME_ITS_TRUE(file != NULL);
223+
char line[1024];
224+
long pos = 0;
225+
while (fgets(line, sizeof(line), file)) {
226+
char *hash_comment = strstr(line, "#hash=");
227+
if (hash_comment) {
228+
pos = ftell(file) - strlen(line) + (hash_comment - line) + 6;
229+
break;
230+
}
231+
}
232+
fseek(file, pos, SEEK_SET);
233+
fputs("deadbeefdeadbeef", file); // overwrite hash with invalid value
234+
fclose(file);
235+
236+
db = fossil_myshell_open(file_name, &err);
237+
ASSUME_ITS_TRUE(db != NULL);
238+
239+
err = fossil_myshell_check_integrity(db);
240+
ASSUME_ITS_TRUE(err == FOSSIL_MYSHELL_ERROR_INTEGRITY);
241+
242+
fossil_myshell_close(db);
243+
remove(file_name);
244+
}
245+
246+
FOSSIL_TEST(c_test_myshell_corrupted_file_size) {
247+
fossil_bluecrab_myshell_error_t err;
248+
const char *file_name = "corrupt_size.myshell";
249+
fossil_bluecrab_myshell_t *db = fossil_myshell_create(file_name, &err);
250+
ASSUME_ITS_TRUE(db != NULL);
251+
252+
fossil_myshell_put(db, "sizekey", "sizeval");
253+
fossil_myshell_commit(db, "commit");
254+
255+
// Artificially change db->file_size to simulate corruption
256+
db->file_size += 10;
257+
258+
err = fossil_myshell_check_integrity(db);
259+
ASSUME_ITS_TRUE(err == FOSSIL_MYSHELL_ERROR_CORRUPTED);
260+
261+
fossil_myshell_close(db);
262+
remove(file_name);
263+
}
264+
265+
FOSSIL_TEST(c_test_myshell_parse_failed_commit) {
266+
fossil_bluecrab_myshell_error_t err;
267+
const char *file_name = "parsefail.myshell";
268+
fossil_bluecrab_myshell_t *db = fossil_myshell_create(file_name, &err);
269+
ASSUME_ITS_TRUE(db != NULL);
270+
271+
fossil_myshell_put(db, "parsekey", "parseval");
272+
fossil_myshell_commit(db, "parse commit");
273+
274+
fossil_myshell_close(db);
275+
276+
// Remove timestamp from commit line to cause parse failure
277+
FILE *file = fopen(file_name, "rb+");
278+
ASSUME_ITS_TRUE(file != NULL);
279+
char lines[10][1024];
280+
int count = 0;
281+
while (fgets(lines[count], sizeof(lines[count]), file)) {
282+
count++;
283+
}
284+
fclose(file);
285+
286+
file = fopen(file_name, "wb");
287+
ASSUME_ITS_TRUE(file != NULL);
288+
for (int i = 0; i < count; ++i) {
289+
if (strncmp(lines[i], "#commit ", 8) == 0) {
290+
char hash_str[17], msg[512];
291+
int n = sscanf(lines[i], "#commit %16s %511[^\n]", hash_str, msg);
292+
if (n == 2) {
293+
fprintf(file, "#commit %s %s\n", hash_str, msg); // omit timestamp
294+
}
295+
} else {
296+
fputs(lines[i], file);
297+
}
298+
}
299+
fclose(file);
300+
301+
db = fossil_myshell_open(file_name, &err);
302+
ASSUME_ITS_TRUE(db != NULL);
303+
304+
err = fossil_myshell_check_integrity(db);
305+
ASSUME_ITS_TRUE(err == FOSSIL_MYSHELL_ERROR_PARSE_FAILED);
306+
307+
fossil_myshell_close(db);
308+
remove(file_name);
309+
}
310+
209311
// * * * * * * * * * * * * * * * * * * * * * * * *
210312
// * Fossil Logic Test Pool
211313
// * * * * * * * * * * * * * * * * * * * * * * * *
@@ -217,6 +319,9 @@ FOSSIL_TEST_GROUP(c_myshell_database_tests) {
217319
FOSSIL_TEST_ADD(c_myshell_fixture, c_test_myshell_backup_restore);
218320
FOSSIL_TEST_ADD(c_myshell_fixture, c_test_myshell_errstr);
219321
FOSSIL_TEST_ADD(c_myshell_fixture, c_test_myshell_empty_strings);
322+
FOSSIL_TEST_ADD(c_myshell_fixture, c_test_myshell_corrupted_key_hash);
323+
FOSSIL_TEST_ADD(c_myshell_fixture, c_test_myshell_corrupted_file_size);
324+
FOSSIL_TEST_ADD(c_myshell_fixture, c_test_myshell_parse_failed_commit);
220325

221326
FOSSIL_TEST_REGISTER(c_myshell_fixture);
222327
} // end of tests

0 commit comments

Comments
 (0)