chiark / gitweb /
Test desc truncation.
authorDan Sheppard <dan.sheppard.circle@gmail.com>
Tue, 22 Apr 2025 22:29:39 +0000 (23:29 +0100)
committerDan Sheppard <dan.sheppard.circle@gmail.com>
Tue, 22 Apr 2025 22:29:39 +0000 (23:29 +0100)
src/coquet.h
src/superblock.c
src/test.c
src/test.h [new file with mode: 0644]

index 304f1ad68f448752f08b795b110082b3e358c265..4cd6547ec2424051f82d0fe3c123127f2e00139f 100644 (file)
@@ -29,11 +29,4 @@ int coquet_finish(coquet_t *cq);
  */
 char * coquet_error_string(coquet_t *cq, int error);
 
-#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);
-#endif
-
 #endif
index 4f2b690ce495f34711ecaf44ed8f2f13fb1df537..c92489d42d3c5218fa7aeaf7e25f10e4ec3d17b3 100644 (file)
@@ -243,6 +243,22 @@ char * cq_super_get_desc(struct cq_super *super) {
 #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;
@@ -260,8 +276,7 @@ void test_superblock() {
 
     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);
@@ -274,14 +289,12 @@ void test_superblock() {
     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);
@@ -301,9 +314,50 @@ void test_superblock() {
     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);
@@ -315,7 +369,6 @@ creation
 corruption
 a/b choice
 magic
-desc truncation
 
 */
 
index 63c46a5ef7d1c5b980877f3f56f391bee8a147fb..2aacc4b01727c8e2d880cc1f866fd52e1d29fcba 100644 (file)
@@ -5,6 +5,7 @@
 #include <fcntl.h>
 #include "sha2.h"
 #include "superblock.h"
+#include "test.h"
 
 void test_assert(int is_true, char *msg) {
     if(!is_true) {
@@ -13,6 +14,22 @@ void test_assert(int is_true, char *msg) {
     }
 }
 
+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;
 
diff --git a/src/test.h b/src/test.h
new file mode 100644 (file)
index 0000000..8454167
--- /dev/null
@@ -0,0 +1,14 @@
+#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