-rw-r--r-- | test/test.cc | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/test/test.cc b/test/test.cc index 4bb2969..2143ac0 100644 --- a/test/test.cc +++ b/test/test.cc | |||
@@ -1,104 +1,106 @@ | |||
1 | #include <iostream> | 1 | #include <iostream> |
2 | #include <stdexcept> | 2 | #include <stdexcept> |
3 | using namespace std; | 3 | using namespace std; |
4 | #include <opkele/exception.h> | 4 | #include <opkele/exception.h> |
5 | #include <opkele/util.h> | 5 | #include <opkele/util.h> |
6 | 6 | ||
7 | #include "config.h" | 7 | #include "config.h" |
8 | 8 | ||
9 | class failed_test : public opkele::exception { | 9 | class failed_test : public opkele::exception { |
10 | public: | 10 | public: |
11 | failed_test(OPKELE_E_PARS) | 11 | failed_test(OPKELE_E_PARS) |
12 | : exception(OPKELE_E_CONS) { } | 12 | : exception(OPKELE_E_CONS) { } |
13 | }; | 13 | }; |
14 | 14 | ||
15 | void test_rfc_3986_normalize_uri(const string &ouri,bool success,const string& nuri="") { | 15 | void test_rfc_3986_normalize_uri(const string &ouri,bool success,const string& nuri="") { |
16 | try { | 16 | try { |
17 | string n = opkele::util::rfc_3986_normalize_uri(ouri); | 17 | string n = opkele::util::rfc_3986_normalize_uri(ouri); |
18 | if(!success) | 18 | if(!success) |
19 | throw failed_test(OPKELE_CP_ "Normalized URI when it shouldn't ('"+ouri+"' normalization resulted in '"+n+"')"); | 19 | throw failed_test(OPKELE_CP_ "Normalized URI when it shouldn't ('"+ouri+"' normalization resulted in '"+n+"')"); |
20 | if(n!=nuri) | 20 | if(n!=nuri) |
21 | throw failed_test(OPKELE_CP_ "rfc_3986_test_failed for '"+ouri+"' failed, expected '"+nuri+"', got '"+n+"'"); | 21 | throw failed_test(OPKELE_CP_ "rfc_3986_test_failed for '"+ouri+"' failed, expected '"+nuri+"', got '"+n+"'"); |
22 | }catch(opkele::bad_input& obi) { | 22 | }catch(opkele::bad_input& obi) { |
23 | if(success) | 23 | if(success) |
24 | throw failed_test(OPKELE_CP_ "Test '"+ouri+"' failed due to 'bad_input'["+obi.what()+"]"); | 24 | throw failed_test(OPKELE_CP_ "Test '"+ouri+"' failed due to 'bad_input'["+obi.what()+"]"); |
25 | }catch(opkele::not_implemented& oni) { | 25 | }catch(opkele::not_implemented& oni) { |
26 | if(success) | 26 | if(success) |
27 | throw failed_test(OPKELE_CP_ "Test '"+ouri+"' failed due to 'not_implemented'["+oni.what()+"]"); | 27 | throw failed_test(OPKELE_CP_ "Test '"+ouri+"' failed due to 'not_implemented'["+oni.what()+"]"); |
28 | } | 28 | } |
29 | } | 29 | } |
30 | 30 | ||
31 | void test_rfc_3986_normalize_uri() { | 31 | void test_rfc_3986_normalize_uri() { |
32 | test_rfc_3986_normalize_uri( | 32 | test_rfc_3986_normalize_uri( |
33 | "invalid", false ); | 33 | "invalid", false ); |
34 | test_rfc_3986_normalize_uri( | 34 | test_rfc_3986_normalize_uri( |
35 | "http://", false ); | 35 | "http://", false ); |
36 | test_rfc_3986_normalize_uri( | 36 | test_rfc_3986_normalize_uri( |
37 | "http:/hacker.klever.net/", false ); | 37 | "http:/hacker.klever.net/", false ); |
38 | test_rfc_3986_normalize_uri( | 38 | test_rfc_3986_normalize_uri( |
39 | "hTTp://hacker.klever.net#uh?oh", true, "http://hacker.klever.net/#uh?oh" ); | 39 | "hTTp://hacker.klever.net#uh?oh", true, "http://hacker.klever.net/#uh?oh" ); |
40 | test_rfc_3986_normalize_uri( | 40 | test_rfc_3986_normalize_uri( |
41 | "http://hacker.klever.net?uh#oh", true, "http://hacker.klever.net/?uh#oh" ); | 41 | "http://hacker.klever.net?uh#oh", true, "http://hacker.klever.net/?uh#oh" ); |
42 | test_rfc_3986_normalize_uri( | 42 | test_rfc_3986_normalize_uri( |
43 | "http://hacker.klever.net:80/", true, "http://hacker.klever.net/" ); | 43 | "http://hacker.klever.net:80/", true, "http://hacker.klever.net/" ); |
44 | test_rfc_3986_normalize_uri( | 44 | test_rfc_3986_normalize_uri( |
45 | "http://hacker.klever.net:80?uh", true, "http://hacker.klever.net/?uh" ); | 45 | "http://hacker.klever.net:80?uh", true, "http://hacker.klever.net/?uh" ); |
46 | test_rfc_3986_normalize_uri( | 46 | test_rfc_3986_normalize_uri( |
47 | "http://hacker.klever.net:80#uh", true, "http://hacker.klever.net/#uh" ); | 47 | "http://hacker.klever.net:80#uh", true, "http://hacker.klever.net/#uh" ); |
48 | test_rfc_3986_normalize_uri( | 48 | test_rfc_3986_normalize_uri( |
49 | "https://hacker.klever.net:443", true, "https://hacker.klever.net/" ); | 49 | "https://hacker.klever.net:443", true, "https://hacker.klever.net/" ); |
50 | test_rfc_3986_normalize_uri( | 50 | test_rfc_3986_normalize_uri( |
51 | "http://hacker.klever.net:?oh", true, "http://hacker.klever.net/?oh" ); | 51 | "http://hacker.klever.net:?oh", true, "http://hacker.klever.net/?oh" ); |
52 | test_rfc_3986_normalize_uri( | 52 | test_rfc_3986_normalize_uri( |
53 | "http://hacker.klever.net/ah%2E", true, "http://hacker.klever.net/ah." ); | 53 | "http://hacker.klever.net/ah%2E", true, "http://hacker.klever.net/ah." ); |
54 | test_rfc_3986_normalize_uri( | 54 | test_rfc_3986_normalize_uri( |
55 | "http://hacker.klever.net/ah/%2E/", true, "http://hacker.klever.net/ah/" ); | 55 | "http://hacker.klever.net/ah/%2E/", true, "http://hacker.klever.net/ah/" ); |
56 | test_rfc_3986_normalize_uri( | 56 | test_rfc_3986_normalize_uri( |
57 | "http://hacker.klever.net/ah/%2b/", true, "http://hacker.klever.net/ah/%2B/" ); | 57 | "http://hacker.klever.net/ah/%2b/", true, "http://hacker.klever.net/ah/%2B/" ); |
58 | test_rfc_3986_normalize_uri( | 58 | test_rfc_3986_normalize_uri( |
59 | "http://hacker.klever.net/ah/./oh?eh", true, "http://hacker.klever.net/ah/oh?eh" ); | 59 | "http://hacker.klever.net/ah/./oh?eh", true, "http://hacker.klever.net/ah/oh?eh" ); |
60 | test_rfc_3986_normalize_uri( | 60 | test_rfc_3986_normalize_uri( |
61 | "http://hacker.klever.net/ah/../oh?", true, "http://hacker.klever.net/oh?" ); | 61 | "http://hacker.klever.net/ah/../oh?", true, "http://hacker.klever.net/oh?" ); |
62 | test_rfc_3986_normalize_uri( | 62 | test_rfc_3986_normalize_uri( |
63 | "http://hacker.klever.net/ah//oh?", true, "http://hacker.klever.net/ah/oh?" ); | 63 | "http://hacker.klever.net/ah//oh?", true, "http://hacker.klever.net/ah/oh?" ); |
64 | test_rfc_3986_normalize_uri( | 64 | test_rfc_3986_normalize_uri( |
65 | "http://hacker.klever.net/ah/?", true, "http://hacker.klever.net/ah/?" ); | 65 | "http://hacker.klever.net/ah/?", true, "http://hacker.klever.net/ah/?" ); |
66 | test_rfc_3986_normalize_uri( | 66 | test_rfc_3986_normalize_uri( |
67 | "http://hacker.klever.net/ah/%", false ); | 67 | "http://hacker.klever.net/ah/%", false ); |
68 | test_rfc_3986_normalize_uri( | 68 | test_rfc_3986_normalize_uri( |
69 | "http://hacker.klever.net/ah/%a", false ); | 69 | "http://hacker.klever.net/ah/%a", false ); |
70 | test_rfc_3986_normalize_uri( | 70 | test_rfc_3986_normalize_uri( |
71 | "http://hacker.klever.net/ah/%zx", false ); | 71 | "http://hacker.klever.net/ah/%zx", false ); |
72 | test_rfc_3986_normalize_uri( | 72 | test_rfc_3986_normalize_uri( |
73 | "http://hacker.klever.net/ah/%5x", false ); | 73 | "http://hacker.klever.net/ah/%5x", false ); |
74 | test_rfc_3986_normalize_uri( | 74 | test_rfc_3986_normalize_uri( |
75 | "Http://Hacker.Klever.Net:", true, "http://hacker.klever.net/" ); | 75 | "Http://Hacker.Klever.Net:", true, "http://hacker.klever.net/" ); |
76 | test_rfc_3986_normalize_uri( | ||
77 | "http://www.xxx.com/openid/1", true, "http://www.xxx.com/openid/1" ); | ||
76 | } | 78 | } |
77 | 79 | ||
78 | void test_w3c_to_time(const char *w3c,time_t expected) { | 80 | void test_w3c_to_time(const char *w3c,time_t expected) { |
79 | time_t t = opkele::util::w3c_to_time(w3c); | 81 | time_t t = opkele::util::w3c_to_time(w3c); |
80 | if(t!=expected) { | 82 | if(t!=expected) { |
81 | char tmp[512]; snprintf(tmp,sizeof(tmp)-1, | 83 | char tmp[512]; snprintf(tmp,sizeof(tmp)-1, |
82 | "test failed for %s, expected %lu, got %lu (expected-got == %lu)", | 84 | "test failed for %s, expected %lu, got %lu (expected-got == %lu)", |
83 | w3c, (unsigned long)expected, (unsigned long)t, | 85 | w3c, (unsigned long)expected, (unsigned long)t, |
84 | (unsigned long)(expected-t) ); | 86 | (unsigned long)(expected-t) ); |
85 | throw failed_test(OPKELE_CP_ tmp); | 87 | throw failed_test(OPKELE_CP_ tmp); |
86 | } | 88 | } |
87 | } | 89 | } |
88 | 90 | ||
89 | void test_w3c_to_time() { | 91 | void test_w3c_to_time() { |
90 | test_w3c_to_time("2008-06-29T12:33:44",1214742824); | 92 | test_w3c_to_time("2008-06-29T12:33:44",1214742824); |
91 | } | 93 | } |
92 | 94 | ||
93 | int main() { | 95 | int main() { |
94 | try { | 96 | try { |
95 | test_w3c_to_time(); | 97 | test_w3c_to_time(); |
96 | test_rfc_3986_normalize_uri(); | 98 | test_rfc_3986_normalize_uri(); |
97 | }catch(failed_test& ft) { | 99 | }catch(failed_test& ft) { |
98 | cerr << "Test failed: " << ft.what() << endl; | 100 | cerr << "Test failed: " << ft.what() << endl; |
99 | }catch(exception& e) { | 101 | }catch(exception& e) { |
100 | cerr << "oops: " << e.what() << endl; | 102 | cerr << "oops: " << e.what() << endl; |
101 | _exit(1); | 103 | _exit(1); |
102 | } | 104 | } |
103 | _exit(0); | 105 | _exit(0); |
104 | } | 106 | } |