test_bail(cq,r);
}
-void test_superblock() {
+void test_superblock_main() {
coquet_t cq;
int i,r;
struct cq_super super;
cq_super_save(&cq,&super,1);
- r = (cq.vfs_funcs.close)(cq.vfs_data,COQUET_FILE_MAIN);
- test_bail(&cq,r);
-
- r = (cq.vfs_funcs.open)(cq.vfs_data,COQUET_FILE_MAIN,COQUET_CMODE_EITHER);
- test_bail(&cq,r);
+ test_close(&cq);
+ test_open(&cq);
r = cq_super_load(&cq,&super,1);
test_bail(&cq,r);
test_bail(&cq,r);
}
+static void make_superblock(coquet_t *cq, bool_t also_b,
+ int corrupt, int check) {
+ int r;
+ struct cq_super super;
+ uint8_t d;
+
+ test_unlink("tmp/test.coquet");
+
+ test_open(cq);
+ r = cq_super_load(cq,&super,1);
+ super.current.nursery_size = 12;
+ cq_super_save(cq,&super,1);
+ if(also_b) {
+ super.current.nursery_size = 11;
+ cq_super_save(cq,&super,1);
+ }
+ if(corrupt>-1) {
+ r = (cq->vfs_funcs.read)(cq->vfs_data,COQUET_FILE_MAIN,&d,corrupt,1);
+ test_bail(cq,r);
+ d += 1;
+ r = (cq->vfs_funcs.write)(cq->vfs_data,COQUET_FILE_MAIN,&d,corrupt,1);
+ test_bail(cq,r);
+ }
+ test_close(cq);
+
+ /* check */
+ test_open(cq);
+ r = cq_super_load(cq,&super,1);
+ test_bail(cq,r);
+ test_eq_int(super.current.nursery_size,check,"c1");
+ test_close(cq);
+}
+
+void test_superblock_corruption() {
+ coquet_t cq;
+ int i,r,pos;
+
+ r = coquet_init(&cq,"tmp/test");
+ test_bail(&cq,r);
+ testvfs_fakerandom(cq.vfs_data,0xA5);
+
+ test_unlink("tmp/test.coquet");
+
+ /* Not corrupting should lead to A, ie n.s. = 12 */
+ make_superblock(&cq,0,-1,12);
+
+ /* Corrupting A should lead us to default, ie n.s. = 10 */
+ for(i=0;i<200;i++) {
+ pos = i<100 ?i : 100+i*9;
+ make_superblock(&cq,0,pos,10);
+ }
+
+ /* Corrupting A should keep B, ie n.s. = 11 */
+ for(i=0;i<200;i++) {
+ pos = i<100 ?i : 100+i*9;
+ make_superblock(&cq,1,pos,11);
+ }
+
+ /* Corrupting B should give A, ie n.s. = 12 */
+ for(i=0;i<200;i++) {
+ pos = i<100 ?i : 100+i*9;
+ make_superblock(&cq,1,2048+pos,12);
+ }
+}
+
+void test_superblock() {
+ test_superblock_main();
+ test_superblock_corruption();
+}
+
/* To test:
+move tests to external drive to avoid wear
creation
-corruption
a/b choice
magic