struct cq_super super;
uint8_t d;
- test_unlink("tmp/test.coquet");
+ r = (cq->vfs_funcs.delete)(cq->vfs_data,COQUET_FILE_MAIN);
+ test_bail(cq,r);
test_open(cq);
r = cq_super_load(cq,&super,1);
r = coquet_init(&cq,"tmp/test");
test_bail(&cq,r);
+ testvfs_virtual(cq.vfs_data,1);
testvfs_fakerandom(cq.vfs_data,0xA5);
test_unlink("tmp/test.coquet");
make_superblock(&cq,1,2048+pos,12);
}
+ testvfs_virtual(cq.vfs_data,0);
testvfs_fakerandom(cq.vfs_data,-1);
r = coquet_finish(&cq);
test_bail(&cq,r);
/* To test:
-move tests to external drive to avoid wear
creation
a/b choice
magic
#include "coquet.h"
struct test_data {
- int fake_random;
+ int fake_random, virtual;
+ uint8_t * vfiles[COQUET_FILE_NUM];
+ uint64_t vfile_len[COQUET_FILE_NUM];
vfs_t vfs_funcs;
void *vfs_data;
};
void * testvfs_make(vfs_t *vfs_funcs, void *vfs_data) {
struct test_data *td;
+ int i;
td = malloc(sizeof(struct test_data));
if(td == NULL)
return NULL;
td->fake_random = -1;
+ td->virtual = 0;
td->vfs_funcs = *vfs_funcs;
td->vfs_data = vfs_data;
+ for(i=0;i<COQUET_FILE_NUM;i++) {
+ td->vfiles[i] = NULL;
+ td->vfile_len[i] = 0;
+ }
return td;
}
static int test_finish(void *vfs_data) {
struct test_data * td = (struct test_data *)vfs_data;
- int r;
+ int i, r;
r = (td->vfs_funcs.finish)(td->vfs_data);
if(r != COQUET_RET_OK)
return r;
+ for(i=0;i<COQUET_FILE_NUM;i++) {
+ free(td->vfiles[i]);
+ }
free(td);
return COQUET_RET_OK;
}
int mode) {
struct test_data * td = (struct test_data *)vfs_data;
+ if(td->virtual)
+ return COQUET_RET_OK;
return (td->vfs_funcs.open)(td->vfs_data,which_file,mode);
}
static int test_close(void * vfs_data, int which_file) {
struct test_data * td = (struct test_data *)vfs_data;
+ if(td->virtual)
+ return COQUET_RET_OK;
return (td->vfs_funcs.close)(td->vfs_data,which_file);
}
+static uint8_t * vf_ensure(struct test_data *td, int which_file, int size) {
+ uint8_t **vf;
+ uint64_t *vfl;
+ uint8_t *r;
+
+ vf = &(td->vfiles[which_file]);
+ vfl = &(td->vfile_len[which_file]);
+ if(*vfl < size) {
+ r = realloc(*vf,size);
+ if(r == NULL)
+ return NULL;
+ *vf = r;
+ memset((*vf)+(*vfl),0,size-*vfl);
+ *vfl = size;
+ }
+ return *vf;
+}
+
static int test_write(void * vfs_data, int which_file, uint8_t * data,
off_t offset, uint64_t length) {
struct test_data * td = (struct test_data *)vfs_data;
+ uint8_t *vf;
+ if(td->virtual) {
+ vf = vf_ensure(td,which_file,offset+length);
+ if(vf == NULL)
+ return COQUET_RET_HEAPERR;
+ memcpy(vf+offset,data,length);
+ return COQUET_RET_OK;
+ }
return (td->vfs_funcs.write)(td->vfs_data,
which_file, data, offset, length);
}
static int test_read(void * vfs_data, int which_file, uint8_t * data,
uint64_t offset, uint64_t length) {
struct test_data * td = (struct test_data *)vfs_data;
+ uint8_t *vf;
+ if(td->virtual) {
+ vf = vf_ensure(td,which_file,offset+length);
+ if(vf == NULL)
+ return COQUET_RET_HEAPERR;
+ memcpy(data,vf+offset,length);
+ return COQUET_RET_OK;
+ }
return (td->vfs_funcs.read)(td->vfs_data,
which_file, data, offset, length);
}
bool wait) {
struct test_data * td = (struct test_data *)vfs_data;
+ if(td->virtual)
+ return COQUET_RET_OK;
return (td->vfs_funcs.lock)(td->vfs_data,
which_lock, lock_mode, wait);
}
static int test_delete(void *vfs_data, int which_file) {
struct test_data * td = (struct test_data *)vfs_data;
+ if(td->virtual) {
+ free(td->vfiles[which_file]);
+ td->vfiles[which_file] = NULL;
+ td->vfile_len[which_file] = 0;
+ return COQUET_RET_OK;
+ }
return (td->vfs_funcs.delete)(td->vfs_data, which_file);
}
static int test_sync(void * vfs_data, int which_file, bool data_only) {
struct test_data * td = (struct test_data *)vfs_data;
+ if(td->virtual)
+ return COQUET_RET_OK;
return (td->vfs_funcs.sync)(td->vfs_data, which_file, data_only);
}
+static int test_size(void * vfs_data, int which_file, off_t * size) {
+ struct test_data * td = (struct test_data *)vfs_data;
+
+ if(td->virtual) {
+ *size = td->vfile_len[which_file];
+ return COQUET_RET_OK;
+ }
+ return (td->vfs_funcs.size)(td->vfs_data, which_file, size);
+}
+
static int test_random(void * vfs_data, uint8_t *out, int len) {
struct test_data * td = (struct test_data *)vfs_data;
td->fake_random = setting;
}
+void testvfs_virtual(void * vfs_data, bool yn) {
+ struct test_data * td = (struct test_data *)vfs_data;
+
+ td->virtual = yn;
+}
+
vfs_t vfs_test = {
.start = test_start,
.get_error_text = test_get_error_text,
.lock = test_lock,
.open = test_open,
+ .size = test_size,
.close = test_close,
.write = test_write,
.read = test_read,