Logo ROOT   6.14/05
Reference Guide
TUriTest.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_net
3 /// Rudimentary TUri test macro.
4 ///
5 /// \macro_code
6 ///
7 /// \author Gerhard E. Bruckner 2007-10-18
8 
9 #include <TUri.h>
10 
11 
12 Bool_t TestResolutionHelper(TUri reference, TUri nominal, TUri &base)
13 {
14  TUri actual = TUri::Transform(reference, base);
15  if (!(nominal == actual))
16  Printf("\tERROR: %s => %s (should read: %s)", reference.GetUri().Data(), actual.GetUri().Data(), nominal.GetUri().Data());
17  return (nominal == actual);
18 }
19 
20 
21 Bool_t TestResolution()
22 {
23  TUri base = TUri("http://a/b/c/d;p?q");
24  Bool_t success = kTRUE;
25 
26  // 5.4.1. Normal Examples
27  success &= TestResolutionHelper("g:h", "g:h", base);
28  success &= TestResolutionHelper("g", "http://a/b/c/g", base);
29  success &= TestResolutionHelper("./g", "http://a/b/c/g", base);
30  success &= TestResolutionHelper("g/", "http://a/b/c/g/", base);
31  success &= TestResolutionHelper("/g", "http://a/g", base);
32  success &= TestResolutionHelper("//g", "http://g", base);
33  success &= TestResolutionHelper("?y", "http://a/b/c/d;p?y", base);
34  success &= TestResolutionHelper("g?y", "http://a/b/c/g?y", base);
35  success &= TestResolutionHelper("#s", "http://a/b/c/d;p?q#s", base);
36  success &= TestResolutionHelper("g#s", "http://a/b/c/g#s", base);
37  success &= TestResolutionHelper("g?y#s", "http://a/b/c/g?y#s", base);
38  success &= TestResolutionHelper(";x", "http://a/b/c/;x", base);
39  success &= TestResolutionHelper("g;x", "http://a/b/c/g;x", base);
40  success &= TestResolutionHelper("g;x?y#s", "http://a/b/c/g;x?y#s", base);
41  success &= TestResolutionHelper("", "http://a/b/c/d;p?q", base);
42  success &= TestResolutionHelper(".", "http://a/b/c/", base);
43  success &= TestResolutionHelper("./", "http://a/b/c/", base);
44  success &= TestResolutionHelper("..", "http://a/b/", base);
45  success &= TestResolutionHelper("../", "http://a/b/", base);
46  success &= TestResolutionHelper("../g", "http://a/b/g", base);
47  success &= TestResolutionHelper("../..", "http://a/", base);
48  success &= TestResolutionHelper("../../", "http://a/", base);
49  success &= TestResolutionHelper("../../g", "http://a/g", base);
50  // 5.4.2. Abnormal Examples
51  success &= TestResolutionHelper("../../../g", "http://a/g", base);
52  success &= TestResolutionHelper("../../../../g", "http://a/g", base);
53  success &= TestResolutionHelper("/./g", "http://a/g", base);
54  success &= TestResolutionHelper("/../g", "http://a/g", base);
55  success &= TestResolutionHelper("g.", "http://a/b/c/g.", base);
56  success &= TestResolutionHelper(".g", "http://a/b/c/.g", base);
57  success &= TestResolutionHelper("g..", "http://a/b/c/g..", base);
58  success &= TestResolutionHelper("..g", "http://a/b/c/..g", base);
59  success &= TestResolutionHelper("./../g", "http://a/b/g", base);
60  success &= TestResolutionHelper("./g/.", "http://a/b/c/g/", base);
61  success &= TestResolutionHelper("g/./h", "http://a/b/c/g/h", base);
62  success &= TestResolutionHelper("g/../h", "http://a/b/c/h", base);
63  success &= TestResolutionHelper("g;x=1/./y", "http://a/b/c/g;x=1/y", base);
64  success &= TestResolutionHelper("g;x=1/../y", "http://a/b/c/y", base);
65  success &= TestResolutionHelper("g?y/./x", "http://a/b/c/g?y/./x", base);
66  success &= TestResolutionHelper("g?y/../x", "http://a/b/c/g?y/../x", base);
67  success &= TestResolutionHelper("g#s/./x", "http://a/b/c/g#s/./x", base);
68  success &= TestResolutionHelper("g#s/../x", "http://a/b/c/g#s/../x", base);
69  success &= TestResolutionHelper("http:g", "http:g", base);
70  return(success);
71 }
72 
73 
74 Bool_t TestPct()
75 {
76  TString errors = "";
77  for (char i = 0; i < 127; i++) {
78  if (TUri::PctDecode(TUri::PctEncode(i)) != i) {
79  char buffer[10];
80  sprintf(buffer, "0x%02x, ", i);
81  errors = errors + buffer;
82  }
83  }
84  if (!errors.IsNull())
85  Printf("\tERROR at %s", errors.Data());
86  else
87  Printf("\tOK");
88  return errors.IsNull();
89 }
90 
91 Bool_t TestComposition()
92 {
93  TString composed = "http://user:pass@host.org/some/path/file.avi?key1=value1#anchor3";
94  TUri uri;
95  uri.SetScheme("http");
96  uri.SetUserInfo("user:pass");
97  uri.SetHost("host.org");
98  uri.SetPath("/some/path/file.avi");
99  uri.SetQuery("key1=value1");
100  uri.SetFragment("anchor3");
101  return uri.GetUri() == composed;
102 }
103 
104 void Answer(Bool_t success)
105 {
106  if (success)
107  Printf("---> SUCCESS\n");
108  else
109  Printf("---> F A I L E D F A I L E D F A I L E D\n");
110 }
111 
112 Bool_t TestValidation()
113 {
114  // validating examples from RFC chapter 1.1.2
115  Bool_t valid = kTRUE;
116  valid &= TUri("ftp://ftp.is.co.za/rfc/rfc1808.txt").IsUri();
117  valid &= TUri("http://www.ietf.org/rfc/rfc2396.txt").IsUri();
118  // IPV6 example excluded
119  //valid &= TUri("ldap://[2001:db8::7]/c=GB?objectClass?one").IsUri();
120  valid &= TUri("mailto:John.Doe@example.com").IsUri();
121  valid &= TUri("news:comp.infosystems.www.servers.unix").IsUri();
122  valid &= TUri("tel:+1-816-555-1212").IsUri();
123  valid &= TUri("telnet://192.0.2.16:80/").IsUri();
124  valid &= TUri("urn:oasis:names:specification:docbook:dtd:xml:4.1.2").IsUri();
125  return valid;
126 }
127 
128 
129 void TUriTest()
130 {
131  Printf("\n\nTUri test macro ...");
132  Printf("---> Validation");
133  Answer(TestValidation());
134  Printf("---> Reference Resolution");
135  Answer(TestResolution());
136  Printf("---> PCT Conversion");
137  Answer(TestPct());
138  Printf("---> Equivalence and Normalisation");
139  Answer(TUri("example://a/b/c/%7Bfoo%7D") == TUri("eXAMPLE://a/./b/../b/%63/%7bfoo%7d"));
140  Printf("---> Composition");
141  Answer(TestComposition());
142 }
143 
Bool_t IsUri() const
Returns kTRUE if instance qualifies as URI URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] ...
Definition: TUri.cxx:227
Bool_t SetPath(const TString &path)
Set path component of URI: path = path-abempty ; begins with "/" or is empty ...
Definition: TUri.cxx:481
Bool_t SetUserInfo(const TString &userinfo)
Set userinfo component of URI: userinfo = *( unreserved / pct-encoded / sub-delims / ":" )...
Definition: TUri.cxx:405
Basic string class.
Definition: TString.h:131
bool Bool_t
Definition: RtypesCore.h:59
This class represents a RFC 3986 compatible URI.
Definition: TUri.h:35
static const TString PctDecode(const TString &source)
Percent-decode the given string according to chapter 2.1 we assume a valid pct-encoded string...
Definition: TUri.cxx:1086
Bool_t SetQuery(const TString &path)
Set query component of URI: query = *( pchar / "/" / "?" ).
Definition: TUri.cxx:298
static const TString PctEncode(const TString &source)
Percent-encode and return the given string according to RFC 3986 in principle, this function cannot f...
Definition: TUri.cxx:815
Bool_t SetScheme(const TString &scheme)
Set scheme component of URI: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ).
Definition: TUri.cxx:248
const TString GetUri() const
Returns the whole URI - an implementation of chapter 5.3 component recomposition. ...
Definition: TUri.cxx:140
Bool_t SetFragment(const TString &fragment)
Set fragment component of URI: fragment = *( pchar / "/" / "?" ).
Definition: TUri.cxx:498
static TUri Transform(const TUri &reference, const TUri &base)
Transform a URI reference into its target URI using given a base URI.
Definition: TUri.cxx:1122
#define Printf
Definition: TGeoToOCC.h:18
Bool_t IsNull() const
Definition: TString.h:402
Bool_t SetHost(const TString &host)
Set host component of URI: RFC 3986: host = IP-literal / IPv4address / reg-name implemented: host ...
Definition: TUri.cxx:442
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char * Data() const
Definition: TString.h:364