summaryrefslogtreecommitdiffabout
path: root/test/test.cc
Unidiff
Diffstat (limited to 'test/test.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--test/test.cc49
1 files changed, 0 insertions, 49 deletions
diff --git a/test/test.cc b/test/test.cc
index 1a012b5..35f58ab 100644
--- a/test/test.cc
+++ b/test/test.cc
@@ -1,139 +1,90 @@
1#include <iostream> 1#include <iostream>
2#include <stdexcept> 2#include <stdexcept>
3using namespace std; 3using namespace std;
4#include <opkele/exception.h> 4#include <opkele/exception.h>
5#include <opkele/consumer.h>
6#include <opkele/util.h> 5#include <opkele/util.h>
7 6
8#include "config.h" 7#include "config.h"
9 8
10class failed_test : public opkele::exception { 9class failed_test : public opkele::exception {
11 public: 10 public:
12 failed_test(OPKELE_E_PARS) 11 failed_test(OPKELE_E_PARS)
13 : exception(OPKELE_E_CONS) { } 12 : exception(OPKELE_E_CONS) { }
14}; 13};
15 14
16class dummy_consumer_t : public opkele::consumer_t {
17 public:
18 virtual opkele::assoc_t store_assoc(const string& /* server */,const string& /* handle */,const opkele::secret_t& /* secret */,int /* expires_in */) {
19 throw opkele::not_implemented(OPKELE_CP_ "Not implemented");
20 }
21 virtual opkele::assoc_t retrieve_assoc(const string& /* server */ ,const string& /* handle */) {
22 throw opkele::not_implemented(OPKELE_CP_ "Not implemented");
23 }
24 virtual void invalidate_assoc(const string& /* server */,const string& /* handle */) {
25 throw opkele::not_implemented(OPKELE_CP_ "Not implemented");
26 }
27};
28
29
30void test_retrieve_links(const string& f,bool success,const string& s="",const string& d="") {
31 dummy_consumer_t dc;
32 string server, delegate;
33 try {
34 dc.retrieve_links("file://" OPKELE_SRC_DIR "/test/html/"+f,server,delegate);
35 if(!success)
36 throw failed_test(OPKELE_CP_ "Retrieved links when it shouldn't");
37 if(server!=s)
38 throw failed_test(OPKELE_CP_ "retrieve_links test failed, expected server '"+s+"', got '"+server+"'");
39 if(delegate!=d)
40 throw failed_test(OPKELE_CP_ "retrieve_links test failed, expected delegate '"+d+"', got '"+delegate+"'");
41 }catch(opkele::bad_input& obi) {
42 if(success)
43 throw failed_test(OPKELE_CP_ "Test '"+f+"' failed due to 'bad_input'["+obi.what()+"]");
44 }catch(opkele::failed_assertion& ofa) {
45 if(success)
46 throw failed_test(OPKELE_CP_ "Test '"+f+"' failed due to 'failed_assertion'["+ofa.what()+"]");
47 }
48}
49
50void test_retrieve_links() {
51 test_retrieve_links("empty.html",false);
52 test_retrieve_links("in-body.html",false);
53 test_retrieve_links("head-in-body.html",false);
54 test_retrieve_links("hkn.html",true,"http://www.klever.net/openid.server","http://hacker.klever.net/");
55 test_retrieve_links("hkn-server.html",true,"http://www.klever.net/openid.server");
56 test_retrieve_links("hkn-delegate.html",false);
57 test_retrieve_links("unclosed-head.html",true,"http://www.klever.net/openid.server","http://hacker.klever.net/");
58 test_retrieve_links("spaced-links.html",true,"http://www.klever.net/openid.server","http://hacker.klever.net/");
59 test_retrieve_links("spaced-link-attrs.html",true,"http://www.klever.net/openid.server","http://hacker.klever.net/");
60 test_retrieve_links("2rels.html",true,"http://www.klever.net/openid.server","http://www.klever.net/openid.server");
61}
62
63void test_rfc_3986_normalize_uri(const string &ouri,bool success,const string& nuri="") { 15void test_rfc_3986_normalize_uri(const string &ouri,bool success,const string& nuri="") {
64 try { 16 try {
65 string n = opkele::util::rfc_3986_normalize_uri(ouri); 17 string n = opkele::util::rfc_3986_normalize_uri(ouri);
66 if(!success) 18 if(!success)
67 throw failed_test(OPKELE_CP_ "Normalized URI when it shouldn't"); 19 throw failed_test(OPKELE_CP_ "Normalized URI when it shouldn't");
68 if(n!=nuri) 20 if(n!=nuri)
69 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+"'");
70 }catch(opkele::bad_input& obi) { 22 }catch(opkele::bad_input& obi) {
71 if(success) 23 if(success)
72 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()+"]");
73 }catch(opkele::not_implemented& oni) { 25 }catch(opkele::not_implemented& oni) {
74 if(success) 26 if(success)
75 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()+"]");
76 } 28 }
77} 29}
78 30
79void test_rfc_3986_normalize_uri() { 31void test_rfc_3986_normalize_uri() {
80 test_rfc_3986_normalize_uri( 32 test_rfc_3986_normalize_uri(
81 "invalid", false ); 33 "invalid", false );
82 test_rfc_3986_normalize_uri( 34 test_rfc_3986_normalize_uri(
83 "ftp://hacker.klever.net/", false ); 35 "ftp://hacker.klever.net/", false );
84 test_rfc_3986_normalize_uri( 36 test_rfc_3986_normalize_uri(
85 "http://", false ); 37 "http://", false );
86 test_rfc_3986_normalize_uri( 38 test_rfc_3986_normalize_uri(
87 "http:/hacker.klever.net/", false ); 39 "http:/hacker.klever.net/", false );
88 test_rfc_3986_normalize_uri( 40 test_rfc_3986_normalize_uri(
89 "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" );
90 test_rfc_3986_normalize_uri( 42 test_rfc_3986_normalize_uri(
91 "http://hacker.klever.net?uh#oh", true, "http://hacker.klever.net/?uh#oh" ); 43 "http://hacker.klever.net?uh#oh", true, "http://hacker.klever.net/?uh#oh" );
92 test_rfc_3986_normalize_uri( 44 test_rfc_3986_normalize_uri(
93 "http://hacker.klever.net:80/", true, "http://hacker.klever.net/" ); 45 "http://hacker.klever.net:80/", true, "http://hacker.klever.net/" );
94 test_rfc_3986_normalize_uri( 46 test_rfc_3986_normalize_uri(
95 "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" );
96 test_rfc_3986_normalize_uri( 48 test_rfc_3986_normalize_uri(
97 "http://hacker.klever.net:80#uh", true, "http://hacker.klever.net/#uh" ); 49 "http://hacker.klever.net:80#uh", true, "http://hacker.klever.net/#uh" );
98 test_rfc_3986_normalize_uri( 50 test_rfc_3986_normalize_uri(
99 "https://hacker.klever.net:443", true, "https://hacker.klever.net/" ); 51 "https://hacker.klever.net:443", true, "https://hacker.klever.net/" );
100 test_rfc_3986_normalize_uri( 52 test_rfc_3986_normalize_uri(
101 "http://hacker.klever.net:?oh", true, "http://hacker.klever.net/?oh" ); 53 "http://hacker.klever.net:?oh", true, "http://hacker.klever.net/?oh" );
102 test_rfc_3986_normalize_uri( 54 test_rfc_3986_normalize_uri(
103 "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." );
104 test_rfc_3986_normalize_uri( 56 test_rfc_3986_normalize_uri(
105 "http://hacker.klever.net/ah/%2E/", true, "http://hacker.klever.net/ah/" ); 57 "http://hacker.klever.net/ah/%2E/", true, "http://hacker.klever.net/ah/" );
106 test_rfc_3986_normalize_uri( 58 test_rfc_3986_normalize_uri(
107 "http://hacker.klever.net/ah/%2b/", true, "http://hacker.klever.net/ah/%2B/" ); 59 "http://hacker.klever.net/ah/%2b/", true, "http://hacker.klever.net/ah/%2B/" );
108 test_rfc_3986_normalize_uri( 60 test_rfc_3986_normalize_uri(
109 "http://hacker.klever.net/ah/./oh?eh", true, "http://hacker.klever.net/ah/oh?eh" ); 61 "http://hacker.klever.net/ah/./oh?eh", true, "http://hacker.klever.net/ah/oh?eh" );
110 test_rfc_3986_normalize_uri( 62 test_rfc_3986_normalize_uri(
111 "http://hacker.klever.net/ah/../oh?", true, "http://hacker.klever.net/oh?" ); 63 "http://hacker.klever.net/ah/../oh?", true, "http://hacker.klever.net/oh?" );
112 test_rfc_3986_normalize_uri( 64 test_rfc_3986_normalize_uri(
113 "http://hacker.klever.net/ah//oh?", true, "http://hacker.klever.net/ah/oh?" ); 65 "http://hacker.klever.net/ah//oh?", true, "http://hacker.klever.net/ah/oh?" );
114 test_rfc_3986_normalize_uri( 66 test_rfc_3986_normalize_uri(
115 "http://hacker.klever.net/ah/?", true, "http://hacker.klever.net/ah/?" ); 67 "http://hacker.klever.net/ah/?", true, "http://hacker.klever.net/ah/?" );
116 test_rfc_3986_normalize_uri( 68 test_rfc_3986_normalize_uri(
117 "http://hacker.klever.net/ah/%", false ); 69 "http://hacker.klever.net/ah/%", false );
118 test_rfc_3986_normalize_uri( 70 test_rfc_3986_normalize_uri(
119 "http://hacker.klever.net/ah/%a", false ); 71 "http://hacker.klever.net/ah/%a", false );
120 test_rfc_3986_normalize_uri( 72 test_rfc_3986_normalize_uri(
121 "http://hacker.klever.net/ah/%zx", false ); 73 "http://hacker.klever.net/ah/%zx", false );
122 test_rfc_3986_normalize_uri( 74 test_rfc_3986_normalize_uri(
123 "http://hacker.klever.net/ah/%5x", false ); 75 "http://hacker.klever.net/ah/%5x", false );
124 test_rfc_3986_normalize_uri( 76 test_rfc_3986_normalize_uri(
125 "Http://Hacker.Klever.Net:", true, "http://hacker.klever.net/" ); 77 "Http://Hacker.Klever.Net:", true, "http://hacker.klever.net/" );
126} 78}
127 79
128int main() { 80int main() {
129 try { 81 try {
130 test_rfc_3986_normalize_uri(); 82 test_rfc_3986_normalize_uri();
131 test_retrieve_links();
132 }catch(failed_test& ft) { 83 }catch(failed_test& ft) {
133 cerr << "Test failed: " << ft.what() << endl; 84 cerr << "Test failed: " << ft.what() << endl;
134 }catch(exception& e) { 85 }catch(exception& e) {
135 cerr << "oops: " << e.what() << endl; 86 cerr << "oops: " << e.what() << endl;
136 _exit(1); 87 _exit(1);
137 } 88 }
138 _exit(0); 89 _exit(0);
139} 90}