summary refs log tree commit diff stats
path: root/plugins/fishlim/tests
diff options
context:
space:
mode:
authorBakasuraRCE <bakasura@protonmail.ch>2020-07-21 19:16:50 -0500
committerPatrick <tingping@tingping.se>2020-10-16 23:19:10 +0200
commit078af20e8b3bff93dd42972e4ef01702e7e1fe2b (patch)
treedb7196c2ce86d9243a784145b3d7b5a04f00b285 /plugins/fishlim/tests
parentbd3f3fa5f70c624926d06884b328de59730fc93c (diff)
fishlim: Implement correct handling of long and UTF-8 messages
Diffstat (limited to 'plugins/fishlim/tests')
-rw-r--r--plugins/fishlim/tests/tests.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/plugins/fishlim/tests/tests.c b/plugins/fishlim/tests/tests.c
index 45dfd41c..bb841c5e 100644
--- a/plugins/fishlim/tests/tests.c
+++ b/plugins/fishlim/tests/tests.c
@@ -230,6 +230,54 @@ void __base64_cbc_len(void) {
     }
 }
 
+/**
+ * Check the calculation of length limit for a plaintext in each encryption mode
+ */
+void __max_text_command_len(void) {
+    int max_encoded_len, plaintext_len;
+    enum fish_mode mode;
+
+    for (max_encoded_len = 0; max_encoded_len < 10000; ++max_encoded_len) {
+        for (mode = FISH_ECB_MODE; mode <= FISH_CBC_MODE; ++mode) {
+            plaintext_len = max_text_command_len(max_encoded_len, mode);
+            g_assert_cmpuint(encoded_len(plaintext_len, mode), <= , max_encoded_len);
+        }
+    }
+}
+
+/**
+ * Check the calculation of length limit for a plaintext in each encryption mode
+ */
+void __foreach_utf8_data_chunks(void) {
+    GRand *rand = NULL;
+    GString *chunks = NULL;
+    int tests, max_chunks_len, chunks_len;
+    char ascii_message[1001];
+    char *data_chunk = NULL;
+
+    rand = g_rand_new();
+
+    for (tests = 0; tests < 1000; ++tests) {
+
+        max_chunks_len = g_rand_int_range(rand, 2, 301);
+        random_string(ascii_message, 1000);
+
+        data_chunk = ascii_message;
+
+        chunks = g_string_new(NULL);
+
+        while (foreach_utf8_data_chunks(data_chunk, max_chunks_len, &chunks_len)) {
+            g_string_append(chunks, g_strndup(data_chunk, chunks_len));
+            /* Next chunk */
+            data_chunk += chunks_len;
+        }
+        /* Check data loss */
+        g_assert_cmpstr(chunks->str, == , ascii_message);
+        g_string_free(chunks, TRUE);
+    }
+}
+
+
 int main(int argc, char *argv[]) {
 
     g_test_init(&argc, &argv, NULL);
@@ -240,6 +288,8 @@ int main(int argc, char *argv[]) {
     g_test_add_func("/fishlim/__base64_fish_len", __base64_fish_len);
     g_test_add_func("/fishlim/__base64_ecb_len", __base64_ecb_len);
     g_test_add_func("/fishlim/__base64_cbc_len", __base64_cbc_len);
+    g_test_add_func("/fishlim/__max_text_command_len", __max_text_command_len);
+    g_test_add_func("/fishlim/__foreach_utf8_data_chunks", __foreach_utf8_data_chunks);
 
     return g_test_run();
 }