summaryrefslogtreecommitdiff
path: root/libproxy/test/url-test.cpp
blob: b7b54d89a2491f511b8cf6f24859ea3cad81df35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <cassert>
#include <iostream>
#include <sstream>
#include <string>


#include "url.hpp"

using namespace libproxy;

void assert_equal_str (const std::string &what,
					   const std::string &in,
					   const std::string &value,
					   const std::string &ref)
{
	if (value != ref)
		throw logic_error(what + " in URL " + in + " is '"
							+ value + "' instead of '" + ref + "'");
}

void assert_equal_int (const std::string &what,
					   const std::string &in,
					   int value,
					   int ref)
{
	if (value != ref) {
		ostringstream s;
		s << what << " in URL " << in << " is "
			<< value << " instead of " << ref;
		throw logic_error(s.str());
	}
}

void try_url (std::string link, bool &rtv,
			  const std::string &scheme,
			  const std::string &user,
			  const std::string &pass,
			  const std::string &host,
			  int port,
			  const std::string &path)
{
  try {
      url u(link);

	  assert_equal_str ("Scheme", u.to_string(), u.get_scheme(), scheme);
	  assert_equal_str ("User", u.to_string(), u.get_username(), user);
	  assert_equal_str ("Pass", u.to_string(), u.get_password(), pass);
	  assert_equal_str ("Host", u.to_string(), u.get_host(), host);
	  assert_equal_int ("Port", u.to_string(), u.get_port(), port);
	  assert_equal_str ("Path", u.to_string(), u.get_path(), path);
  }
  catch (exception &e) {
      std::cerr << e.what() << std::endl;
      rtv = false;
  }
}

int main()
{
  bool rtv = true;

  try_url ("file:///allo", rtv,
		  "file",
		  "", "",
		  "", 0,
		  "/allo");

  try_url ("http://test.com", rtv,
		  "http",
		  "", "",
		  "test.com", 80,
		  "");

  try_url ("http://test.com/", rtv,
		  "http",
		  "", "",
		  "test.com", 80,
		  "/");

  try_url ("http://test.com#", rtv,
		  "http",
		  "", "",
		  "test.com", 80,
		  "");

  try_url ("http://test.com?", rtv,
		  "http",
		  "", "",
		  "test.com", 80,
		  "");

  try_url ("http://nicolas@test.com", rtv,
		  "http",
		  "nicolas", "",
		  "test.com", 80,
		  "");

  try_url ("http://nicolas:@test.com", rtv,
		  "http",
		  "nicolas", "",
		  "test.com", 80,
		  "");

  try_url ("http://nicolas:secret@test.com", rtv,
		  "http",
		  "nicolas", "secret",
		  "test.com", 80,
		  "");

  try_url ("http://:secret@test.com", rtv,
		  "http",
		  "", "secret",
		  "test.com", 80,
		  "");

  try_url ("http+ssh://:secret@test.com", rtv,
		  "http+ssh",
		  "", "secret",
		  "test.com", 22,
		  "");

  try_url ("HtTp://TeSt.CoM/ALLO", rtv,
		  "http",
		  "", "",
		  "test.com", 80,
		  "/ALLO");

  /* This is a very uncommon but valid case that use to cause crash */
  try_url ("www.google.com:80", rtv,
		   "www.google.com",
		   "", "",
		   "", 0,
		   "80");
  
  return !rtv;
}