[epiphany/wip/google-safe-browsing: 8/22] tests: Add test for ephy_gsb_utils_compute_hashes()



commit eba13b35e7e02917a54b9cb232e3311eb6b15c3a
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Sat Sep 16 01:18:38 2017 +0300

    tests: Add test for ephy_gsb_utils_compute_hashes()

 tests/ephy-gsb-utils-test.c |   89 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 89 insertions(+), 0 deletions(-)
---
diff --git a/tests/ephy-gsb-utils-test.c b/tests/ephy-gsb-utils-test.c
index 982f814..86ea371 100644
--- a/tests/ephy-gsb-utils-test.c
+++ b/tests/ephy-gsb-utils-test.c
@@ -82,6 +82,93 @@ test_ephy_gsb_utils_canonicalize (void)
   }
 }
 
+static char *
+bytes_to_hex (const guint8 *bytes,
+              gsize         length)
+{
+  const char *hex_digits = "0123456789abcdef";
+  char *hex;
+
+  hex = g_malloc (length * 2 + 1);
+  for (gsize i = 0; i < length; i++) {
+    hex[2 * i] = hex_digits[bytes[i] >> 4];
+    hex[2 * i + 1] = hex_digits[bytes[i] & 0xf];
+  }
+  hex[length * 2] = 0;
+
+  return hex;
+}
+
+typedef struct {
+  const char *url;
+  guint       num_hashes;
+  const char *hashes_hex[64];
+} URLHashesTest;
+
+/*
+ * Tests from: https://developers.google.com/safe-browsing/v4/urls-hashing#suffixprefix-expressions
+ */
+static const URLHashesTest url_hash_tests[] = {
+  {
+    "http://a.b.c/1/2.html?param=1";,
+    8,
+    {
+      "1cd5cf5ed8e6df424bdbb400f7b2a3fcb215c4c3f7fa2965a11446cde3c162f3",
+      "8b19a5a51125f023af4a26e2aef4caae352623d05ffdc859433be84823ec4053",
+      "f9c142c4c0c9e669e0924b45f5b1b8dd1fdf85d182b674a4ec415b1f58ac2667",
+      "59e650c465d9cbded1f95322e19fb1481f9500342a240c4a18a7a5ef4b103e1c",
+      "9b7d85bbdfa3c8ba1796a96ea91094730350c8b12a9552028123b1cc1918cc56",
+      "1803dee47cc6adec025aefd26ff5b44408f14d6e250defe7d0ae2444f0f8e106",
+      "b225cf5dcf266f3ff0b32319a72cf23fca7c53c98cb4af1a7bbfe413415407f1",
+      "ac5f446d55d0807d211e05fd5482534b0dc99d7b9f255174f9dba30b9ebc01ac"
+    }
+  },
+  {
+    "http://a.b.c.d.e.f.g/1.html";,
+    10,
+    {
+      "8c39d0c311331cfae87867aa52a98ef3c995b121c0f7bc750164996a4b3ab43f",
+      "ce385c58c19493d2e4ac23fbb1d4faccde65b73bfcc4f3b6ba62addf905fbf41",
+      "37a343cf5d2e00eeb103175c8e4b0adddbef6348f6c60e732a4952fc0a053d89",
+      "f1930a298cf214f0459049ad655838b080a9ba886dd0c759e21c8af005528d14",
+      "0285b5d5ad2aa12ff24d0fc9ac820725061a659fdd369857a422cfe4cbb04e4e",
+      "4fd37f62520c129f29525fd3d1eb9b04511b632e4aef190dbc23f8519d7ccd7e",
+      "a5a5563280f2da618e8a6b14060d909679446767c7d3bbcc23c9b02419b12289",
+      "4e378632a186388136b13689a85bf63d2f8fcf50c93b1468c4e20cd12423f2f8",
+      "e42d99efd820eeb6fad77109534a6af1b5cb6bd7755958fead91e0790850a303",
+      "9401530ee6371f3f1cb82e463223e7bf5fd3ab8b85872d477509110467b4c9e1"
+    }
+  },
+  {
+    "http://1.2.3.4/1/";,
+    2,
+    {
+      "5c9f354119e8d3f82e1bc01545ec7a656da70453e6bfc053ac8b257bdd4d8ef6",
+      "3f008b863ca6e954c31859665454f9cbcb10760acb7ebc536d6da1ccac94618d"
+    }
+  }
+};
+
+static void
+test_ephy_gsb_utils_compute_hashes (void)
+{
+  for (guint i = 0; i < G_N_ELEMENTS (url_hash_tests); i++) {
+    URLHashesTest test = url_hash_tests[i];
+    GList *hashes, *h;
+
+    h = hashes = ephy_gsb_utils_compute_hashes (test.url);
+    g_assert_cmpuint (g_list_length (hashes), ==, test.num_hashes);
+
+    for (guint k = 0; k < test.num_hashes; k++, h = h->next) {
+      char *hash_hex = bytes_to_hex (h->data, 32);
+      g_assert_cmpstr (hash_hex, ==, test.hashes_hex[k]);
+      g_free (hash_hex);
+    }
+
+    g_list_free_full (hashes, g_free);
+  }
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -89,6 +176,8 @@ main (int argc, char *argv[])
 
   g_test_add_func ("/lib/safe-browsing/ephy-gsb-utils/test_ephy_gsb_utils_canonicalize",
                    test_ephy_gsb_utils_canonicalize);
+  g_test_add_func ("/lib/safe-browsing/ephy-gsb-utils/test_ephy_gsb_utils_compute_hashes",
+                   test_ephy_gsb_utils_compute_hashes);
 
   return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]