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