Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
12Bool_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
21Bool_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
74Bool_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
91Bool_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
104void 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
112Bool_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("mailto:John.Doe@example.com").IsUri();
120 valid &= TUri("news:comp.infosystems.www.servers.unix").IsUri();
121 valid &= TUri("tel:+1-816-555-1212").IsUri();
122 valid &= TUri("telnet://192.0.2.16:80/").IsUri();
123 valid &= TUri("urn:oasis:names:specification:docbook:dtd:xml:4.1.2").IsUri();
124 return valid;
125}
126
127
128void TUriTest()
129{
130 Printf("\n\nTUri test macro ...");
131 Printf("---> Validation");
132 Answer(TestValidation());
133 Printf("---> Reference Resolution");
134 Answer(TestResolution());
135 Printf("---> PCT Conversion");
136 Answer(TestPct());
137 Printf("---> Equivalence and Normalisation");
138 Answer(TUri("example://a/b/c/%7Bfoo%7D") == TUri("eXAMPLE://a/./b/../b/%63/%7bfoo%7d"));
139 Printf("---> Composition");
140 Answer(TestComposition());
141}
142
bool Bool_t
Definition RtypesCore.h:63
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2503
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
Bool_t IsNull() const
Definition TString.h:414
This class represents a RFC 3986 compatible URI.
Definition TUri.h:35
Bool_t SetScheme(const TString &scheme)
Set scheme component of URI:
Definition TUri.cxx:248
Bool_t SetFragment(const TString &fragment)
Set fragment component of URI:
Definition TUri.cxx:498
Bool_t SetPath(const TString &path)
Set path component of URI:
Definition TUri.cxx:481
Bool_t SetUserInfo(const TString &userinfo)
Set userinfo component of URI:
Definition TUri.cxx:405
Bool_t IsUri() const
Returns kTRUE if instance qualifies as URI URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ] ...
Definition TUri.cxx:227
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
const TString GetUri() const
Returns the whole URI - an implementation of chapter 5.3 component recomposition.
Definition TUri.cxx:140
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
Bool_t SetQuery(const TString &path)
Set query component of URI:
Definition TUri.cxx:298
Bool_t SetHost(const TString &host)
Set host component of URI:
Definition TUri.cxx:442
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