changeset 2757:a079f7693df8

make test/cookies somewhat usable by others
author corvid <corvid@lavabit.com>
date Tue, 08 Jan 2013 22:36:53 +0000
parents eee956d397fd
children d7c3484c49a8
files test/cookies.c
diffstat 1 files changed, 84 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/test/cookies.c	Tue Jan 08 02:00:03 2013 +0000
+++ b/test/cookies.c	Tue Jan 08 22:36:53 2013 +0000
@@ -44,6 +44,7 @@
    } D_STMT_END
 
 #define MSG(...) MSG_INNARDS("", __VA_ARGS__)
+#define MSG_WARN(...) MSG_INNARDS("** WARNING **: ", __VA_ARGS__)
 #define MSG_ERR(...) MSG_INNARDS("** ERROR **: ", __VA_ARGS__)
 
 
@@ -778,8 +779,87 @@
    expect(__LINE__, "Cookie: name=val\r\n", "http", "p6.com", "/dir/subdir/s");
 }
 
+int Cookies_rc_check()
+{
+   const int line_maxlen = 4096;
+   FILE *stream;
+   char *filename;
+   char line[line_maxlen];
+   bool_t default_deny = TRUE;
+
+   /* Get a file pointer */
+   filename = dStrconcat(dGethomedir(), "/.dillo/cookiesrc", NULL);
+   stream = fopen(filename, "r");
+   dFree(filename);
+
+   if (!stream) {
+      MSG_ERR("Cannot run test; cannot open cookiesrc.\n");
+      return 1;
+   }
+
+   /* Get all lines in the file */
+   while (!feof(stream)) {
+      char *rc;
+
+      line[0] = '\0';
+      rc = fgets(line, line_maxlen, stream);
+      if (!rc && ferror(stream)) {
+         MSG_ERR("Error while reading rule from cookiesrc: %s\n",
+             dStrerror(errno));
+         fclose(stream);
+         return 2;
+      }
+
+      /* Remove leading and trailing whitespaces */
+      dStrstrip(line);
+
+      if (line[0] != '\0' && line[0] != '#') {
+         int domain_end, i = 0;
+         const char *rule;
+
+         /* Get the domain */
+         while (line[i] != '\0' && !dIsspace(line[i]))
+            i++;
+         domain_end = i;
+
+         /* Skip past whitespace */
+         while (dIsspace(line[i]))
+            i++;
+         line[domain_end] = '\0';
+
+         /* Get the rule */
+         rule = line + i;
+         while (line[i] != '\0' && !dIsspace(line[i]))
+            i++;
+         line[i] = '\0';
+
+         if (!dStrAsciiCasecmp(line, "DEFAULT")) {
+            if (!dStrAsciiCasecmp(rule, "ACCEPT") ||
+                !dStrAsciiCasecmp(rule, "ACCEPT_SESSION"))
+               default_deny = FALSE;
+         } else {
+            if (!dStrAsciiCasecmp(rule, "DENY"))
+               MSG_WARN("DENY rules in cookiesrc can interfere with test.\n");
+         }
+      }
+   }
+   fclose(stream);
+
+   if (default_deny) {
+      MSG_ERR("Cannot run test with cookiesrc default of deny.\n");
+      return 1;
+   } else {
+      return 0;
+   }
+}
+
 int main()
 {
+   if (Cookies_rc_check()) {
+      MSG("If you change cookiesrc, remember to stop the DPIs via dpidc.\n");
+      return 1;
+   }
+
    a_Cookies_set("name=val", "ordinary.com", "/", NULL);
    expect(__LINE__, "Cookie: name=val\r\n", "http", "ordinary.com", "/");
 
@@ -959,5 +1039,9 @@
 #endif
 
    MSG("TESTS: passed: %u failed: %u\n", passed, failed);
+
+   MSG("Now that everything is full of fake cookies, you should run "
+       "'dpidc stop', plus delete cookies.txt if necessary.\n");
+
    return 0;
 }