OSDN Git Service

implement cookie domain and host check.
authorornse01 <ornse01@users.sourceforge.jp>
Sat, 30 Apr 2011 14:12:14 +0000 (14:12 +0000)
committerornse01 <ornse01@users.sourceforge.jp>
Sat, 30 Apr 2011 14:12:14 +0000 (14:12 +0000)
- implement test.
- fix test case value.
- fix bugs.

git-svn-id: http://svn.sourceforge.jp/svnroot/bchan/bchan/trunk@243 20a0b8eb-f62a-4a12-8fe1-b598822500fb

src/cookiedb.c
src/test_cookiedb.c

index 447697b..c0a1e83 100644 (file)
@@ -86,6 +86,24 @@ EXPORT W ascstr_cmp(ascstr_t *s1, ascstr_t *s2)
        return 0; /* same length */
 }
 
+/*
+ * match example
+ *  astr:    XXXXYYYYZZZZ
+ *  suffix:       YYYZZZZ
+ *
+ */
+EXPORT Bool ascstr_suffixcmp(ascstr_t *astr, ascstr_t *suffix)
+{
+       if (astr->len < suffix->len) {
+               return False;
+       }
+       if (strncmp(astr->str + astr->len - suffix->len, suffix->str, suffix->len) != 0) {
+               return False;
+       }
+
+       return True;
+}
+
 EXPORT W ascstr_initialize(ascstr_t *astr)
 {
        astr->str = malloc(sizeof(UB));
@@ -357,13 +375,32 @@ LOCAL Bool check_specified_TLD(ascstr_t *domain)
        return False;
 }
 
-LOCAL Bool cookiedb_writeiterator_domaincheck(ascstr_t *send_host, ascstr_t *origin_host)
+LOCAL Bool cookiedb_writeiterator_checksendcondition_domaincheck(cookiedb_writeiterator_t *iter, httpcookie_t *cookie)
 {
-       if (origin_host->len < send_host->len) {
-               return False;
-       }
-       if (strncmp(origin_host->str + origin_host->len - send_host->len, send_host->str, send_host->len) != 0) {
-               return False;
+       Bool ok;
+       W count;
+
+       if (cookie->domain.len != 0) {
+               ok = ascstr_suffixcmp(&iter->host, &cookie->domain);
+               if (ok == False) {
+                       return False;
+               }
+               count = count_priod(&cookie->domain);
+               ok = check_specified_TLD(&cookie->domain);
+               if (ok == True) {
+                       if (count < 2) {
+                               return False;
+                       }
+               } else {
+                       if (count < 3) {
+                               return False;
+                       }
+               }
+       } else {
+               ok = ascstr_suffixcmp(&iter->host, &cookie->origin_host);
+               if (ok == False) {
+                       return False;
+               }
        }
 
        return True;
@@ -383,7 +420,6 @@ LOCAL Bool cookiedb_writeitereator_pathcheck(ascstr_t *send_path, ascstr_t *orig
 LOCAL Bool cookiedb_writeiterator_checksendcondition(cookiedb_writeiterator_t *iter, httpcookie_t *cookie)
 {
        Bool ok;
-       W count;
 
        if (cookie->secure == True) {
                if (iter->secure != True) {
@@ -395,27 +431,9 @@ LOCAL Bool cookiedb_writeiterator_checksendcondition(cookiedb_writeiterator_t *i
                        return False;
                }
        }
-       if (cookie->domain.len != 0) {
-               ok = cookiedb_writeiterator_domaincheck(&iter->host, &cookie->domain);
-               if (ok == False) {
-                       return False;
-               }
-               count = count_priod(&cookie->domain);
-               ok = check_specified_TLD(&cookie->domain);
-               if (ok == True) {
-                       if (count < 2) {
-                               return False;
-                       }
-               } else {
-                       if (count < 3) {
-                               return False;
-                       }
-               }
-       } else {
-               ok = cookiedb_writeiterator_domaincheck(&iter->host, &cookie->origin_host);
-               if (ok == False) {
-                       return False;
-               }
+       ok = cookiedb_writeiterator_checksendcondition_domaincheck(iter, cookie);
+       if (ok == False) {
+               return False;
        }
        if (cookie->path.len != 0) {
                ok = cookiedb_writeitereator_pathcheck(&iter->path, &cookie->path);
@@ -826,17 +844,55 @@ LOCAL VOID cookiedb_insertcookie(cookiedb_t *db, httpcookie_t *cookie)
 
 LOCAL Bool cookiedb_checkinsertioncondition(cookiedb_t *db, httpcookie_t *cookie, STIME current)
 {
-       /* TODO: domain chack */
+       W count;
+       Bool ok;
+
+       /* domain check */
+       if (cookie->domain.len != 0) {
+               if (cookie->domain.str[0] != '.') { /* is not error and add period? */
+                       return False;
+               }
+               /* same as cookiedb_writeiterator_checksendcondition */
+               count = count_priod(&cookie->domain);
+               ok = check_specified_TLD(&cookie->domain);
+               if (ok == True) {
+                       if (count < 2) {
+                               return False;
+                       }
+               } else {
+                       if (count < 3) {
+                               return False;
+                       }
+               }
+               /* domain and request host check */
+               if (cookie->domain.len == (cookie->origin_host.len + 1)) {
+                       /* for
+                        *  domain = .xxx.yyy.zzz
+                        *  origin =  xxx.yyy.zzz
+                        */
+                       if (strncmp(cookie->domain.str + 1, cookie->origin_host.str, cookie->origin_host.len) != 0) {
+                               return False;
+                       }
+               } else {
+                       ok = ascstr_suffixcmp(&cookie->origin_host, &cookie->domain);
+                       if (ok == False) {
+                               return False;
+                       }
+               }
+       }
+
+       /* expire check */
        if (cookie->expires == 0) {
                return True;
        }
        if (cookie->expires < current) {
                return True;
        }
+
        return False;
 }
 
-LOCAL VOID cookiedb_inserteachdb(cookiedb_t *db, httpcookie_t *cookie, STIME current)
+LOCAL VOID cookiedb_insertwithcheck(cookiedb_t *db, httpcookie_t *cookie, STIME current)
 {
        Bool save;
        save = cookiedb_checkinsertioncondition(db, cookie, current);
@@ -858,13 +914,13 @@ EXPORT VOID cookiedb_endheaderread(cookiedb_t *db, cookiedb_readheadercontext_t
                        break;
                }
                httpcookie_QueRemove(cookie);
-               cookiedb_inserteachdb(db, cookie, context->current);
+               cookiedb_insertwithcheck(db, cookie, context->current);
        }
        if (context->reading != NULL) {
                ok = httpcookie_isvalueset(context->reading);
                if (ok == True) {
                        httpcookie_QueRemove(context->reading);
-                       cookiedb_inserteachdb(db, context->reading, context->current);
+                       cookiedb_insertwithcheck(db, context->reading, context->current);
                } else {
                        httpcookie_delete(context->reading);
                }
index baf78ce..3f2217e 100644 (file)
@@ -377,6 +377,8 @@ LOCAL TEST_RESULT test_cookiedb_testingseparateinput_order(testcookie_input_t *i
        return TEST_RESULT_PASS;
 }
 
+/* check origin host condition. */
+
 LOCAL UB test_cookiedb_host1[] = ".2ch.net";
 LOCAL UB test_cookiedb_name1[] = "NAME01";
 LOCAL UB test_cookiedb_value1[] = "VALUE01";
@@ -472,14 +474,10 @@ LOCAL TEST_RESULT test_cookiedb_3()
                {
                        "PON", /* name */
                        "xxxxx.yyyyy.zzzz.ad.jp", /* value */
-               },
-               {
-                       "HAP", /* name */
-                       "XYZABCD", /* value */
                }
        };
 
-       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "2ch.net", "/", False, 0x1eec16c0, expected, 2);
+       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "2ch.net", "/", False, 0x1eec16c0, expected, 1);
 }
 
 LOCAL TEST_RESULT test_cookiedb_4()
@@ -510,12 +508,18 @@ LOCAL TEST_RESULT test_cookiedb_4()
                {
                        "HAP", /* name */
                        "XYZABCD", /* value */
+               },
+               {
+                       "PON", /* name */
+                       "xxxxx.yyyyy.zzzz.ad.jp", /* value */
                }
        };
 
-       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "www.2ch.net", "/", False, 0x1eec16c0, expected, 1);
+       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "www.2ch.net", "/", False, 0x1eec16c0, expected, 2);
 }
 
+/* check path condition */
+
 LOCAL TEST_RESULT test_cookiedb_5()
 {
        testcookie_input_t data[] = {
@@ -547,7 +551,7 @@ LOCAL TEST_RESULT test_cookiedb_5()
                }
        };
 
-       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "2ch.net", "/", False, 0x1eec16c0, expected, 1);
+       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "www.2ch.net", "/", False, 0x1eec16c0, expected, 1);
 }
 
 LOCAL TEST_RESULT test_cookiedb_6()
@@ -581,7 +585,7 @@ LOCAL TEST_RESULT test_cookiedb_6()
                }
        };
 
-       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "2ch.net", "/", False, 0x1eec16c0, expected, 1);
+       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "www.2ch.net", "/", False, 0x1eec16c0, expected, 1);
 }
 
 LOCAL TEST_RESULT test_cookiedb_7()
@@ -615,7 +619,7 @@ LOCAL TEST_RESULT test_cookiedb_7()
                }
        };
 
-       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "2ch.net", "/", False, 0x1eec16c0, expected, 1);
+       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "www.2ch.net", "/", False, 0x1eec16c0, expected, 1);
 }
 
 LOCAL TEST_RESULT test_cookiedb_8()
@@ -649,9 +653,11 @@ LOCAL TEST_RESULT test_cookiedb_8()
                }
        };
 
-       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "2ch.net", "/", False, 0x1eec16c0, expected, 1);
+       return test_cookiedb_testingseparateinput(data, 2, 0x1eec16c0, "www.2ch.net", "/", False, 0x1eec16c0, expected, 1);
 }
 
+/* check cookie order by path */
+
 LOCAL TEST_RESULT test_cookiedb_9()
 {
        testcookie_input_t data[] = {
@@ -804,6 +810,192 @@ LOCAL TEST_RESULT test_cookiedb_12()
        return test_cookiedb_testingseparateinput_order(data, 2, 0x1eec16c0, "2ch.net", "/", False, 0x1eec16c0, expected, 2);
 }
 
+/* check domain condition. */
+
+LOCAL TEST_RESULT test_cookiedb_13()
+{
+       testcookie_input_t data[] = {
+               {
+                       "www.2ch.net", /* origin_host */
+                       "/", /* origin_path */
+                       "AAA", /* name */
+                       "BBB", /* value */
+                       ".xxx.www.2ch.net", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.2ch.net", /* origin_host */
+                       "/", /* origin_path */
+                       "CCC", /* name */
+                       "DDD", /* value */
+                       ".www.2ch.net", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.2ch.net", /* origin_host */
+                       "/", /* origin_path */
+                       "EEE", /* name */
+                       "FFF", /* value */
+                       ".2ch.net", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+       };
+       testcookie_expected_t expected[] = {
+               {
+                       "CCC", /* name */
+                       "DDD", /* value */
+               },
+               {
+                       "EEE", /* name */
+                       "FFF", /* value */
+               }
+       };
+
+       return test_cookiedb_testingseparateinput(data, 3, 0x1eec16c0, "xxx.www.2ch.net", "/", False, 0x1eec16c0, expected, 2);
+}
+
+LOCAL TEST_RESULT test_cookiedb_14()
+{
+       testcookie_input_t data[] = {
+               {
+                       "www.xxx.xx.jp", /* origin_host */
+                       "/", /* origin_path */
+                       "AAA", /* name */
+                       "BBB", /* value */
+                       ".xxx.www.xxx.xx.jp", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.xxx.xx.jp", /* origin_host */
+                       "/", /* origin_path */
+                       "CCC", /* name */
+                       "DDD", /* value */
+                       ".www.xxx.xx.jp", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.xxx.xx.jp", /* origin_host */
+                       "/", /* origin_path */
+                       "EEE", /* name */
+                       "FFF", /* value */
+                       ".xxx.xx.jp", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+       };
+       testcookie_expected_t expected[] = {
+               {
+                       "CCC", /* name */
+                       "DDD", /* value */
+               },
+               {
+                       "EEE", /* name */
+                       "FFF", /* value */
+               }
+       };
+
+       return test_cookiedb_testingseparateinput(data, 3, 0x1eec16c0, "xxx.www.xxx.xx.jp", "/", False, 0x1eec16c0, expected, 2);
+}
+
+LOCAL TEST_RESULT test_cookiedb_15()
+{
+       testcookie_input_t data[] = {
+               {
+                       "www.2ch.net", /* origin_host */
+                       "/", /* origin_path */
+                       "AAA", /* name */
+                       "BBB", /* value */
+                       ".xxx.www.2ch.net", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.2ch.net", /* origin_host */
+                       "/", /* origin_path */
+                       "CCC", /* name */
+                       "DDD", /* value */
+                       ".www.2ch.net", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.2ch.net", /* origin_host */
+                       "/", /* origin_path */
+                       "EEE", /* name */
+                       "FFF", /* value */
+                       ".2ch.net", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+       };
+       testcookie_expected_t expected[] = {
+               {
+                       "EEE", /* name */
+                       "FFF", /* value */
+               }
+       };
+
+       return test_cookiedb_testingseparateinput(data, 3, 0x1eec16c0, "xxx.yyy.2ch.net", "/", False, 0x1eec16c0, expected, 1);
+}
+
+LOCAL TEST_RESULT test_cookiedb_16()
+{
+       testcookie_input_t data[] = {
+               {
+                       "www.xxx.xx.jp", /* origin_host */
+                       "/", /* origin_path */
+                       "AAA", /* name */
+                       "BBB", /* value */
+                       ".xxx.www.xxx.xx.jp", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.xxx.xx.jp", /* origin_host */
+                       "/", /* origin_path */
+                       "CCC", /* name */
+                       "DDD", /* value */
+                       ".www.xxx.xx.jp", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+               {
+                       "www.xxx.xx.jp", /* origin_host */
+                       "/", /* origin_path */
+                       "EEE", /* name */
+                       "FFF", /* value */
+                       ".xxx.xx.jp", /* domain */
+                       NULL, /* path */
+                       False, /* secure */
+                       0 /* expires */
+               },
+       };
+       testcookie_expected_t expected[] = {
+               {
+                       "EEE", /* name */
+                       "FFF", /* value */
+               }
+       };
+
+       return test_cookiedb_testingseparateinput(data, 3, 0x1eec16c0, "yyy.zzz.xxx.xx.jp", "/", False, 0x1eec16c0, expected, 1);
+}
+
 LOCAL VOID test_cookiedb_printresult(TEST_RESULT (*proc)(), B *test_name)
 {
        TEST_RESULT result;
@@ -833,4 +1025,8 @@ EXPORT VOID test_cookiedb_main()
        test_cookiedb_printresult(test_cookiedb_10, "test_cookiedb_10");
        test_cookiedb_printresult(test_cookiedb_11, "test_cookiedb_11");
        test_cookiedb_printresult(test_cookiedb_12, "test_cookiedb_12");
+       test_cookiedb_printresult(test_cookiedb_13, "test_cookiedb_13");
+       test_cookiedb_printresult(test_cookiedb_14, "test_cookiedb_14");
+       test_cookiedb_printresult(test_cookiedb_15, "test_cookiedb_15");
+       test_cookiedb_printresult(test_cookiedb_16, "test_cookiedb_16");
 }