summary refs log tree commit diff stats
path: root/plugins/fishlim/tests/tests.c
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2022-04-11 21:54:33 -0300
committerSoniEx2 <endermoneymod@gmail.com>2022-04-21 20:23:35 -0300
commit5f137d2c81878c41aa3b893fb56b1116bc27bc78 (patch)
tree2d6eadb2474e2d7b234b3dda9efd7559335adce7 /plugins/fishlim/tests/tests.c
parente2cfba040e26927b94a4e311a0a61365a81a41b1 (diff)
parent133f62806441a5db1156653aa1f077d2e8deeb34 (diff)
Merge upstream changes
Diffstat (limited to 'plugins/fishlim/tests/tests.c')
-rw-r--r--plugins/fishlim/tests/tests.c178
1 files changed, 83 insertions, 95 deletions
diff --git a/plugins/fishlim/tests/tests.c b/plugins/fishlim/tests/tests.c
index bb841c5e..12b10d1d 100644
--- a/plugins/fishlim/tests/tests.c
+++ b/plugins/fishlim/tests/tests.c
@@ -1,5 +1,4 @@
 /*
-
   Copyright (c) 2020 <bakasura@protonmail.ch>
 
   Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -22,22 +21,20 @@
 
 */
 
-#ifndef PLUGIN_HEXCHAT_FISHLIM_TEST_H
-#define PLUGIN_HEXCHAT_FISHLIM_TEST_H
-
-// Libs
+#include <string.h>
 #include <glib.h>
-// Project Libs
-#include "../fish.h"
-#include "../utils.h"
-#include "old_version/fish.h"
+
+#include "fish.h"
+#include "utils.h"
 
 /**
  * Auxiliary function: Generate a random string
  * @param out Preallocated string to fill
  * @param len Size of bytes to fill
  */
-void random_string(char *out, size_t len) {
+static void
+random_string(char *out, size_t len)
+{
     GRand *rand = NULL;
     int i = 0;
 
@@ -51,13 +48,14 @@ void random_string(char *out, size_t len) {
     g_rand_free(rand);
 }
 
-
 /**
- * Check encrypt and decrypt in ECB mode and compare with old implementation
+ * Check encrypt and decrypt in ECB mode
  */
-void __ecb(void) {
-    char *bo64 = NULL, *b64 = NULL;
-    char *deo = NULL, *de = NULL;
+static void
+test_ecb(void)
+{
+    char *b64 = NULL;
+    char *de = NULL;
     int key_len, message_len = 0;
     char key[57];
     char message[1000];
@@ -71,36 +69,20 @@ void __ecb(void) {
             random_string(message, message_len);
 
             /* Encrypt */
-            bo64 = __old_fish_encrypt(key, key_len, message);
-            g_assert_nonnull(bo64);
             b64 = fish_encrypt(key, key_len, message, message_len, FISH_ECB_MODE);
             g_assert_nonnull(b64);
-            g_assert_cmpuint(g_strcmp0(b64, bo64), == , 0);
 
             /* Decrypt */
             /* Linear */
-            deo = __old_fish_decrypt(key, key_len, bo64);
             de = fish_decrypt_str(key, key_len, b64, FISH_ECB_MODE);
-            g_assert_nonnull(deo);
-            g_assert_nonnull(de);
-            g_assert_cmpuint(g_strcmp0(de, message), == , 0);
-            g_assert_cmpuint(g_strcmp0(deo, message), == , 0);
-            g_assert_cmpuint(g_strcmp0(de, deo), == , 0);
-            g_free(deo);
+			g_assert_cmpstr (de, ==, message);
             g_free(de);
+	
             /* Mixed */
-            deo = __old_fish_decrypt(key, key_len, b64);
-            de = fish_decrypt_str(key, key_len, bo64, FISH_ECB_MODE);
-            g_assert_nonnull(deo);
-            g_assert_nonnull(de);
-            g_assert_cmpuint(g_strcmp0(de, message), == , 0);
-            g_assert_cmpuint(g_strcmp0(deo, message), == , 0);
-            g_assert_cmpuint(g_strcmp0(de, deo), == , 0);
-            g_free(deo);
+            de = fish_decrypt_str(key, key_len, b64, FISH_ECB_MODE);
+			g_assert_cmpstr (de, ==, message);
             g_free(de);
 
-            /* Free */
-            g_free(bo64);
             g_free(b64);
         }
     }
@@ -109,7 +91,9 @@ void __ecb(void) {
 /**
  * Check encrypt and decrypt in CBC mode
  */
-void __cbc(void) {
+static void
+test_cbc(void)
+{
     char *b64 = NULL;
     char *de = NULL;
     int key_len, message_len = 0;
@@ -131,11 +115,9 @@ void __cbc(void) {
             /* Decrypt */
             /* Linear */
             de = fish_decrypt_str(key, key_len, b64, FISH_CBC_MODE);
-            g_assert_nonnull(de);
-            g_assert_cmpuint(g_strcmp0(de, message), == , 0);
+            g_assert_cmpstr (de, ==, message);
             g_free(de);
 
-            /* Free */
             g_free(b64);
         }
     }
@@ -144,48 +126,49 @@ void __cbc(void) {
 /**
  * Check the calculation of final length from an encoded string in Base64
  */
-void __base64_len(void) {
+static void
+test_base64_len (void)
+{
     char *b64 = NULL;
-    int i, message_len = 0;
     char message[1000];
+    int message_end = sizeof (message) - 1;
 
-    for (i = 0; i < 10; ++i) {
+    random_string(message, message_end);
 
-        for (message_len = 1; message_len < 1000; ++message_len) {
-            random_string(message, message_len);
-            b64 = g_base64_encode((const unsigned char *) message, message_len);
-            g_assert_nonnull(b64);
-            g_assert_cmpuint(strlen(b64), == , base64_len(message_len));
-            g_free(b64);
-        }
+    for (; message_end >= 0; --message_end) {
+        message[message_end] = '\0'; /* Truncate instead of generating new strings */
+        b64 = g_base64_encode((const unsigned char *) message, message_end);
+        g_assert_nonnull(b64);
+        g_assert_cmpuint(strlen(b64), == , base64_len(message_end));
+        g_free(b64);
     }
 }
 
 /**
  * Check the calculation of final length from an encoded string in BlowcryptBase64
  */
-void __base64_fish_len(void) {
+static void
+test_base64_fish_len (void)
+{
     char *b64 = NULL;
-    int i, message_len = 0;
+    int message_len = 0;
     char message[1000];
 
-    for (i = 0; i < 10; ++i) {
-
-        for (message_len = 1; message_len < 1000; ++message_len) {
-            random_string(message, message_len);
-            b64 = fish_base64_encode(message, message_len);
-            g_assert_nonnull(b64);
-            g_assert_cmpuint(strlen(b64), == , base64_fish_len(message_len));
-            g_free(b64);
-        }
+    for (message_len = 1; message_len < 1000; ++message_len) {
+        random_string(message, message_len);
+        b64 = fish_base64_encode(message, message_len);
+        g_assert_nonnull(b64);
+        g_assert_cmpuint(strlen(b64), == , base64_fish_len(message_len));
+        g_free(b64);
     }
 }
 
-
 /**
  * Check the calculation of final length from an encrypted string in ECB mode
  */
-void __base64_ecb_len(void) {
+static void
+test_base64_ecb_len(void)
+{
     char *b64 = NULL;
     int key_len, message_len = 0;
     char key[57];
@@ -209,7 +192,9 @@ void __base64_ecb_len(void) {
 /**
  * Check the calculation of final length from an encrypted string in CBC mode
  */
-void __base64_cbc_len(void) {
+static void
+test_base64_cbc_len(void)
+{
     char *b64 = NULL;
     int key_len, message_len = 0;
     char key[57];
@@ -233,7 +218,9 @@ void __base64_cbc_len(void) {
 /**
  * Check the calculation of length limit for a plaintext in each encryption mode
  */
-void __max_text_command_len(void) {
+static void
+test_max_text_command_len(void)
+{
     int max_encoded_len, plaintext_len;
     enum fish_mode mode;
 
@@ -248,50 +235,51 @@ void __max_text_command_len(void) {
 /**
  * Check the calculation of length limit for a plaintext in each encryption mode
  */
-void __foreach_utf8_data_chunks(void) {
+static void
+test_foreach_utf8_data_chunks(void)
+{
     GRand *rand = NULL;
     GString *chunks = NULL;
-    int tests, max_chunks_len, chunks_len;
+    int  max_chunks_len, chunks_len;
     char ascii_message[1001];
     char *data_chunk = NULL;
 
     rand = g_rand_new();
+    max_chunks_len = g_rand_int_range(rand, 2, 301);
+    random_string(ascii_message, 1000);
 
-    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;
 
-        data_chunk = ascii_message;
+    chunks = g_string_new(NULL);
 
-        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);
+    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);
+    g_rand_free (rand);
+}
 
-int main(int argc, char *argv[]) {
+int
+main(int argc, char *argv[]) {
 
     g_test_init(&argc, &argv, NULL);
 
-    g_test_add_func("/fishlim/__ecb", __ecb);
-    g_test_add_func("/fishlim/__cbc", __ecb);
-    g_test_add_func("/fishlim/__base64_len", __base64_len);
-    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();
+    g_test_add_func("/fishlim/ecb", test_ecb);
+    g_test_add_func("/fishlim/cbc", test_cbc);
+    g_test_add_func("/fishlim/base64_len", test_base64_len);
+    g_test_add_func("/fishlim/base64_fish_len", test_base64_fish_len);
+    g_test_add_func("/fishlim/base64_ecb_len", test_base64_ecb_len);
+    g_test_add_func("/fishlim/base64_cbc_len", test_base64_cbc_len);
+    g_test_add_func("/fishlim/max_text_command_len", test_max_text_command_len);
+    g_test_add_func("/fishlim/foreach_utf8_data_chunks", test_foreach_utf8_data_chunks);
+
+    fish_init();
+    int ret = g_test_run();
+    fish_deinit();
+    return ret;
 }
-
-#endif //PLUGIN_HEXCHAT_FISHLIM_TEST_H
\ No newline at end of file