]>
Commit | Line | Data |
---|---|---|
c9a4e310 | 1 | #include "squid.h" |
c9a4e310 | 2 | #include "base/CharacterSet.h" |
5d4cfe02 AJ |
3 | #include "parser/Tokenizer.h" |
4 | #include "testTokenizer.h" | |
c9a4e310 FC |
5 | |
6 | CPPUNIT_TEST_SUITE_REGISTRATION( testTokenizer ); | |
7 | ||
8 | SBuf text("GET http://resource.com/path HTTP/1.1\r\n" | |
9 | "Host: resource.com\r\n" | |
10 | "Cookie: laijkpk3422r j1noin \r\n" | |
11 | "\r\n"); | |
12 | const CharacterSet alpha("alpha","abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); | |
13 | const CharacterSet whitespace("whitespace"," \r\n"); | |
14 | const CharacterSet crlf("crlf","\r\n"); | |
15 | const CharacterSet tab("tab","\t"); | |
16 | const CharacterSet numbers("numbers","0123456789"); | |
17 | ||
18 | void | |
19 | testTokenizer::testTokenizerPrefix() | |
20 | { | |
21 | Parser::Tokenizer t(text); | |
22 | SBuf s; | |
23 | ||
24 | // successful prefix tokenization | |
25 | CPPUNIT_ASSERT(t.prefix(s,alpha)); | |
26 | CPPUNIT_ASSERT_EQUAL(SBuf("GET"),s); | |
27 | CPPUNIT_ASSERT(t.prefix(s,whitespace)); | |
28 | CPPUNIT_ASSERT_EQUAL(SBuf(" "),s); | |
29 | ||
30 | //no match (first char is not in the prefix set) | |
31 | CPPUNIT_ASSERT(!t.prefix(s,whitespace)); | |
32 | CPPUNIT_ASSERT_EQUAL(SBuf(" "),s); | |
33 | ||
34 | // one more match to set S to something meaningful | |
35 | CPPUNIT_ASSERT(t.prefix(s,alpha)); | |
36 | CPPUNIT_ASSERT_EQUAL(SBuf("http"),s); | |
37 | ||
38 | //no match (no characters from the character set in the prefix) | |
39 | CPPUNIT_ASSERT(!t.prefix(s,tab)); | |
40 | CPPUNIT_ASSERT_EQUAL(SBuf("http"),s); //output SBuf left untouched | |
41 | ||
42 | // match until the end of the sample | |
43 | CharacterSet all(whitespace); | |
44 | all += alpha; | |
45 | all += crlf; | |
46 | all += numbers; | |
47 | all.add(':').add('.').add('/'); | |
48 | CPPUNIT_ASSERT(t.prefix(s,all)); | |
49 | CPPUNIT_ASSERT_EQUAL(SBuf(),t.remaining()); | |
50 | } | |
51 | ||
52 | void | |
53 | testTokenizer::testTokenizerSkip() | |
54 | { | |
55 | Parser::Tokenizer t(text); | |
56 | SBuf s; | |
57 | ||
58 | // first scenario: patterns match | |
59 | // prep for test | |
60 | CPPUNIT_ASSERT(t.prefix(s,alpha)); | |
61 | CPPUNIT_ASSERT_EQUAL(SBuf("GET"),s); | |
62 | ||
63 | // test skip testing character set | |
64 | CPPUNIT_ASSERT(t.skip(whitespace)); | |
65 | // check that skip was right | |
66 | CPPUNIT_ASSERT(t.prefix(s,alpha)); | |
67 | CPPUNIT_ASSERT_EQUAL(SBuf("http"),s); | |
68 | ||
69 | //check skip prefix | |
70 | CPPUNIT_ASSERT(t.skip(SBuf("://"))); | |
71 | // verify | |
72 | CPPUNIT_ASSERT(t.prefix(s,alpha)); | |
73 | CPPUNIT_ASSERT_EQUAL(SBuf("resource"),s); | |
74 | ||
75 | // no skip | |
76 | CPPUNIT_ASSERT(!t.skip(alpha)); | |
77 | CPPUNIT_ASSERT(!t.skip(SBuf("://"))); | |
78 | CPPUNIT_ASSERT(!t.skip('a')); | |
79 | ||
80 | } | |
81 | ||
82 | void | |
83 | testTokenizer::testTokenizerToken() | |
84 | { | |
85 | Parser::Tokenizer t(text); | |
86 | SBuf s; | |
87 | ||
88 | // first scenario: patterns match | |
89 | CPPUNIT_ASSERT(t.token(s,whitespace)); | |
90 | CPPUNIT_ASSERT_EQUAL(SBuf("GET"),s); | |
91 | CPPUNIT_ASSERT(t.token(s,whitespace)); | |
92 | CPPUNIT_ASSERT_EQUAL(SBuf("http://resource.com/path"),s); | |
93 | CPPUNIT_ASSERT(t.token(s,whitespace)); | |
94 | CPPUNIT_ASSERT_EQUAL(SBuf("HTTP/1.1"),s); | |
95 | CPPUNIT_ASSERT(t.token(s,whitespace)); | |
96 | CPPUNIT_ASSERT_EQUAL(SBuf("Host:"),s); | |
97 | ||
c9a4e310 FC |
98 | } |
99 | ||
100 | void | |
101 | testTokenizer::testCharacterSet() | |
102 | { | |
103 | ||
104 | } |