update changelog
[debian/iodine.git] / tests / encoding.c
index dcde87b5bb5ca14bedc62c2bbde7399ca637dd1d..ac224521e193b20addfa687a9145544cc7951b78 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
+ * Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
 
 #include "encoding.h"
 #include "test.h"
+#include "base32.h"
+#include "base64.h"
 
-START_TEST(test_encoding_base32)
+#define TUPLES 4
+
+static struct tuple
+{
+       char *a;
+       char *b;
+} dottests[] = {
+       { "aaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+         "aaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaa"},
+       { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."},
+       { "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
+       { "abc123", "abc123" },
+       { NULL, NULL }
+};
+
+START_TEST(test_inline_dotify)
+{
+       char temp[1024];
+       char *b;
+
+       memset(temp, 0, sizeof(temp));
+       strcpy(temp, dottests[_i].a);
+       b = temp;
+       inline_dotify(b, sizeof(temp));
+
+       fail_unless(strcmp(dottests[_i].b, temp) == 0,
+                       "'%s' != '%s'", temp, dottests[_i].b);
+}
+END_TEST
+
+START_TEST(test_inline_undotify)
 {
-       char temp[256];
-       char *start = "HELLOTEST";
-       char *out = "1HELLOTEST";
-       char end[256];
-       char *tempend;
-       int codedlength;
+       char temp[1024];
+       char *b;
 
        memset(temp, 0, sizeof(temp));
-       memset(end, 0, sizeof(end));
+       strcpy(temp, dottests[_i].b);
+       b = temp;
+       inline_undotify(b, sizeof(temp));
+
+       fail_unless(strcmp(dottests[_i].a, temp) == 0,
+                       "'%s' != '%s'", temp, dottests[_i].a);
+}
+END_TEST
+
+START_TEST(test_build_hostname)
+{
+       char data[256];
+       char buf[1024];
+       char *topdomain = "a.c";
+       int buflen;
+       int i;
+
+       for (i = 0; i < sizeof(data); i++) {
+               data[i] = i & 0xFF;
+       }
 
-       codedlength = encode_data(start, strlen(start), sizeof(temp) - 1, temp + 1);
-       temp[0] = '1';
-       tempend = temp + strlen(temp);
-       decode_data(end, sizeof(end), temp, tempend);
+       buflen = sizeof(buf);
+       
+       for (i = 1; i < sizeof(data); i++) {
+               int len = build_hostname(buf, buflen, data, i, topdomain, get_base32_encoder(), sizeof(buf));
 
-       fail_unless(strcmp(out, end) == 0, NULL);
+               fail_if(len > i);
+               fail_if(strstr(buf, ".."), "Found double dots when encoding data len %d! buf: %s", i, buf);
+       }
 }
 END_TEST
 
@@ -49,7 +100,9 @@ test_encoding_create_tests()
        TCase *tc;
 
        tc = tcase_create("Encoding");
-       tcase_add_test(tc, test_encoding_base32);
+       tcase_add_loop_test(tc, test_inline_dotify, 0, TUPLES);
+       tcase_add_loop_test(tc, test_inline_undotify, 0, TUPLES);
+       tcase_add_test(tc, test_build_hostname);
 
        return tc;
 }