aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-09-19 13:17:35 +0200
committerTheJackiMonster <thejackimonster@gmail.com>2022-09-19 13:17:35 +0200
commitf109d8a868546f7a7557c5667c7d76f14f262296 (patch)
tree213287f09e5c2da52a0723cce5012df8ef627263
parentdf221ee6f08296510edd171b2ee9415cfd4d3e66 (diff)
downloadlibgnunetchat-f109d8a868546f7a7557c5667c7d76f14f262296.tar.gz
libgnunetchat-f109d8a868546f7a7557c5667c7d76f14f262296.zip
Adjusted file test and handled empty files
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--src/gnunet_chat_util.c36
-rw-r--r--tests/test_gnunet_chat_file.c15
2 files changed, 32 insertions, 19 deletions
diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c
index b97e0f1..a017b26 100644
--- a/src/gnunet_chat_util.c
+++ b/src/gnunet_chat_util.c
@@ -66,21 +66,32 @@ util_hash_file (const char *filename, struct GNUNET_HashCode *hash)
66 return GNUNET_SYSERR; 66 return GNUNET_SYSERR;
67 67
68 struct GNUNET_DISK_MapHandle *mapping; 68 struct GNUNET_DISK_MapHandle *mapping;
69 const void* data = GNUNET_DISK_file_map( 69 const void* data;
70 file, &mapping, GNUNET_DISK_MAP_TYPE_READ, size
71 );
72 70
73 if (!data) 71 if (size > 0)
74 { 72 {
75 GNUNET_DISK_file_close(file); 73 data = GNUNET_DISK_file_map(
76 return GNUNET_SYSERR; 74 file, &mapping, GNUNET_DISK_MAP_TYPE_READ, size
75 );
76
77 if ((!data) || (!mapping))
78 {
79 GNUNET_DISK_file_close(file);
80 return GNUNET_SYSERR;
81 }
82 }
83 else
84 {
85 mapping = NULL;
86 data = NULL;
77 } 87 }
78 88
79 GNUNET_CRYPTO_hash(data, size, hash); 89 GNUNET_CRYPTO_hash(data, size, hash);
80 90
81 GNUNET_DISK_file_unmap(mapping); 91 if (mapping)
82 GNUNET_DISK_file_close(file); 92 GNUNET_DISK_file_unmap(mapping);
83 93
94 GNUNET_DISK_file_close(file);
84 return GNUNET_OK; 95 return GNUNET_OK;
85} 96}
86 97
@@ -104,8 +115,11 @@ util_encrypt_file (const char *filename,
104 if (!file) 115 if (!file)
105 return GNUNET_SYSERR; 116 return GNUNET_SYSERR;
106 117
107 struct GNUNET_DISK_MapHandle *mapping = NULL; 118 if (size <= 0)
108 void* data = GNUNET_DISK_file_map( 119 return GNUNET_DISK_file_close(file);
120
121 struct GNUNET_DISK_MapHandle *mapping;
122 const void* data = GNUNET_DISK_file_map(
109 file, &mapping, GNUNET_DISK_MAP_TYPE_READWRITE, size 123 file, &mapping, GNUNET_DISK_MAP_TYPE_READWRITE, size
110 ); 124 );
111 125
@@ -146,7 +160,7 @@ util_encrypt_file (const char *filename,
146 break; 160 break;
147 } 161 }
148 162
149 if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping)) 163 if ((mapping) && (GNUNET_OK != GNUNET_DISK_file_unmap(mapping)))
150 result = -1; 164 result = -1;
151 165
152 if (GNUNET_OK != GNUNET_DISK_file_sync(file)) 166 if (GNUNET_OK != GNUNET_DISK_file_sync(file))
diff --git a/tests/test_gnunet_chat_file.c b/tests/test_gnunet_chat_file.c
index 784fe04..f9783ed 100644
--- a/tests/test_gnunet_chat_file.c
+++ b/tests/test_gnunet_chat_file.c
@@ -101,7 +101,7 @@ on_gnunet_chat_file_send_msg(void *cls,
101skip_search_account: 101skip_search_account:
102 if ((GNUNET_CHAT_KIND_LOGIN != kind) || 102 if ((GNUNET_CHAT_KIND_LOGIN != kind) ||
103 (context)) 103 (context))
104 goto skip_first_login; 104 goto skip_file_upload;
105 105
106 106
107 ck_assert(kind == GNUNET_CHAT_KIND_LOGIN); 107 ck_assert(kind == GNUNET_CHAT_KIND_LOGIN);
@@ -120,22 +120,21 @@ skip_search_account:
120 120
121 ck_assert_ptr_ne(group_context, NULL); 121 ck_assert_ptr_ne(group_context, NULL);
122 122
123skip_first_login: 123 char *filename = GNUNET_DISK_mktemp("gnunet_chat_file_send_name");
124 if (GNUNET_CHAT_KIND_JOIN != kind)
125 goto skip_file_upload;
126 124
127 ck_assert(kind == GNUNET_CHAT_KIND_JOIN); 125 ck_assert_ptr_ne(filename, NULL);
128 ck_assert_ptr_ne(context, NULL);
129 126
130 struct GNUNET_CHAT_File *file = GNUNET_CHAT_context_send_file( 127 struct GNUNET_CHAT_File *file = GNUNET_CHAT_context_send_file(
131 context, 128 group_context,
132 "Makefile", 129 filename,
133 on_gnunet_chat_file_send_upload, 130 on_gnunet_chat_file_send_upload,
134 handle 131 handle
135 ); 132 );
136 133
137 ck_assert_ptr_ne(file, NULL); 134 ck_assert_ptr_ne(file, NULL);
138 135
136 GNUNET_free(filename);
137
139skip_file_upload: 138skip_file_upload:
140 if (GNUNET_CHAT_KIND_FILE != kind) 139 if (GNUNET_CHAT_KIND_FILE != kind)
141 return GNUNET_YES; 140 return GNUNET_YES;