...
authoralex <[email protected]>
Sat, 6 Nov 2021 04:10:07 +0000 (21:10 -0700)
committeralex <[email protected]>
Sat, 6 Nov 2021 04:10:07 +0000 (21:10 -0700)
test/unit/file.tests.c

index 5888d3225c184fe37eba28dc85e5c7e65c50c6f3..1b36f88782c3ce3f5194973535b7e7b6aad3b4a7 100644 (file)
@@ -3,6 +3,7 @@
 #include<file.h>
 
 int main();
+static void extend_file(const char*);
 static void file_hash_basic_test();
 static void file_hash_large_file_test();
 static void file_hash_large_piece_test();
@@ -23,6 +24,21 @@ int main() {
        return EXIT_SUCCESS;
 }
 
+static void extend_file(const char *file) {
+       unsigned char buf[16384];
+       FILE *fp;
+
+       fp = fopen(file,"a");
+       assert(fp!=NULL);
+
+       for(size_t i=0;i<10000;i++) {
+               memset(buf,i%255,16384);
+               assert(16384==fwrite(buf,sizeof(char),16384,fp));
+       }
+
+       fclose(fp);
+}
+
 static void file_hash_basic_test() {
        struct file *p;
        unsigned char expected1[crypto_hash_sha256_BYTES] = {174,73,190,37,189,244,236,113,18,122,255,20,5,229,77,151,187,72,27,75,203,32,25,154,81,249,19,29,118,7,228,229};
@@ -69,19 +85,9 @@ static void file_hash_basic_test() {
 
 static void file_hash_large_file_test() {
        struct file *p;
-       FILE *fp;
-       unsigned char buf[16384];
        unsigned char expected[crypto_hash_sha256_BYTES] = {58,71,55,213,6,94,65,97,113,64,176,175,139,67,229,164,13,18,213,164,166,129,202,162,224,45,9,227,191,155,144,144};
 
-       fp = fopen(TEST_FILE_1,"a");
-       assert(fp!=NULL);
-
-       for(size_t i=0;i<10000;i++) {
-               memset(buf,i%255,16384);
-               assert(16384==fwrite(buf,sizeof(char),16384,fp));
-       }
-
-       fclose(fp);
+       extend_file(TEST_FILE_1);
 
        assert(file_init(&p,TEST_FILE_1)==1);
        assert(file_hash(p,16384)==1);
@@ -95,7 +101,20 @@ static void file_hash_large_file_test() {
 }
 
 static void file_hash_large_piece_test() {
+       struct file *p;
+       unsigned char expected[crypto_hash_sha256_BYTES] = {8,234,82,230,152,160,118,109,206,217,92,218,63,67,49,228,4,43,121,68,142,69,129,202,31,34,27,245,63,137,179,83};
+
+       extend_file(TEST_FILE_2);
+
+       assert(1==file_init(&p,TEST_FILE_2));
+       assert(file_hash(p,32768)==1);
+       assert(memcmp(p->root,expected,crypto_hash_sha256_BYTES)==0);
+       assert(5000==block_length(p->blocks));
+       assert((10000*16384+26)==p->size);
 
+       file_free(p);
+
+       reset_env();
 }
 
 static void file_hash_random_file_test() {