#ifdef COQUET_TEST
#include <stdio.h>
+#include "test.h"
+
+static void test_open(coquet_t *cq) {
+ int r;
+
+ r = (cq->vfs_funcs.open)(cq->vfs_data,
+ COQUET_FILE_MAIN,COQUET_CMODE_EITHER);
+ test_bail(cq,r);
+}
+
+static void test_close(coquet_t *cq) {
+ int r;
+
+ r = (cq->vfs_funcs.close)(cq->vfs_data,COQUET_FILE_MAIN);
+ test_bail(cq,r);
+}
void test_superblock() {
coquet_t cq;
test_unlink("tmp/test.coquet");
- r = (cq.vfs_funcs.open)(cq.vfs_data,COQUET_FILE_MAIN,COQUET_CMODE_EITHER);
- test_bail(&cq,r);
+ test_open(&cq);
r = cq_super_load(&cq,&super,1);
test_bail(&cq,r);
cq_super_save(&cq,&super,1);
test_bail(&cq,r);
- r = (cq.vfs_funcs.close)(cq.vfs_data,COQUET_FILE_MAIN);
- test_bail(&cq,r);
+ test_close(&cq);
test_file_compare("tmp/test.coquet","testdata/sb1.coquet");
/* reopen to check the marshalling */
- r = (cq.vfs_funcs.open)(cq.vfs_data,COQUET_FILE_MAIN,COQUET_CMODE_EITHER);
- test_bail(&cq,r);
+ test_open(&cq);
r = cq_super_load(&cq,&super,1);
test_bail(&cq,r);
for(i=0;i<GLOBAL_IV_LEN;i++)
test_assert(super.global_iv[i] == 0xA5,"iv");
+ test_close(&cq);
+
+ /* update with very long description for truncation */
+ desc = malloc(DESC_LEN*2+1);
+ if(desc == NULL) {
+ fprintf(stderr,"malloc failed\n");
+ exit(1);
+ }
+ for(i=0;i<DESC_LEN*2;i++) {
+ desc[i] = 'a' + (i%26);
+ }
+ desc[DESC_LEN*2] = '\0';
+ cq_super_set_desc(&super,desc);
+ free(desc);
+ desc = cq_super_get_desc(&super);
+ test_eq_int(strlen(desc),DESC_LEN,"desc");
+ for(i=0;i<DESC_LEN;i++) {
+ test_eq_char(desc[i],'a'+(i%26),"desc c");
+ }
+ free(desc);
+
+ /* check updated in file successfully */
+ test_open(&cq);
+
+ 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);
+
+ r = cq_super_load(&cq,&super,1);
+ test_bail(&cq,r);
+
+ desc = cq_super_get_desc(&super);
+ test_eq_int(strlen(desc),DESC_LEN,"desc");
+ for(i=0;i<DESC_LEN;i++) {
+ test_eq_char(desc[i],'a'+(i%26),"desc c");
+ }
+ free(desc);
+
+ test_close(&cq);
+
testvfs_fakerandom(cq.vfs_data,-1);
r = coquet_finish(&cq);
test_bail(&cq,r);
corruption
a/b choice
magic
-desc truncation
*/
#include <fcntl.h>
#include "sha2.h"
#include "superblock.h"
+#include "test.h"
void test_assert(int is_true, char *msg) {
if(!is_true) {
}
}
+void test_eq_char(char got, char expect, char *msg) {
+ if(got!=expect) {
+ fprintf(stderr,"assertion '%s' failed got=%c expect=%c\n",
+ msg,got,expect);
+ exit(1);
+ }
+}
+
+void test_eq_int(int got, int expect, char *msg) {
+ if(got!=expect) {
+ fprintf(stderr,"assertion '%s' failed got=%d expect=%d\n",
+ msg,got,expect);
+ exit(1);
+ }
+}
+
void test_unlink(char *path) {
int r;
--- /dev/null
+#ifndef COQUET_TEST_H
+#define COQUET_TEST_H
+
+#ifdef COQUET_TEST
+void test_bail(coquet_t * cq, int error_code);
+void test_unlink(char *path);
+void test_file_compare(char *got, char *expected);
+void test_assert(int is_true, char *msg);
+
+void test_eq_int(int got, int expect, char *msg);
+void test_eq_char(char got, char expect, char *msg);
+#endif
+
+#endif
\ No newline at end of file