TUriTest.C

Go to the documentation of this file.
00001 // TUriTest.C - rudimentary TUri test macro - Gerhard E. Bruckner 2007-10-18
00002 
00003 #include <TUri.h>
00004 
00005 
00006 Bool_t TestResolutionHelper(TUri reference, TUri nominal, TUri &base)
00007 {
00008    TUri actual = TUri::Transform(reference, base);
00009    if (!(nominal == actual))
00010       Printf("\tERROR: %s => %s (should read: %s)", reference.GetUri().Data(), actual.GetUri().Data(), nominal.GetUri().Data());
00011    return (nominal == actual);
00012 }
00013 
00014 
00015 Bool_t TestResolution()
00016 {
00017    TUri base = TUri("http://a/b/c/d;p?q");
00018    Bool_t success = kTRUE;
00019 
00020    // 5.4.1. Normal Examples
00021    success &= TestResolutionHelper("g:h", "g:h", base);
00022    success &= TestResolutionHelper("g", "http://a/b/c/g", base);
00023    success &= TestResolutionHelper("./g", "http://a/b/c/g", base);
00024    success &= TestResolutionHelper("g/", "http://a/b/c/g/", base);
00025    success &= TestResolutionHelper("/g", "http://a/g", base);
00026    success &= TestResolutionHelper("//g", "http://g", base);
00027    success &= TestResolutionHelper("?y", "http://a/b/c/d;p?y", base);
00028    success &= TestResolutionHelper("g?y", "http://a/b/c/g?y", base);
00029    success &= TestResolutionHelper("#s", "http://a/b/c/d;p?q#s", base);
00030    success &= TestResolutionHelper("g#s", "http://a/b/c/g#s", base);
00031    success &= TestResolutionHelper("g?y#s", "http://a/b/c/g?y#s", base);
00032    success &= TestResolutionHelper(";x", "http://a/b/c/;x", base);
00033    success &= TestResolutionHelper("g;x", "http://a/b/c/g;x", base);
00034    success &= TestResolutionHelper("g;x?y#s", "http://a/b/c/g;x?y#s", base);
00035    success &= TestResolutionHelper("", "http://a/b/c/d;p?q", base);
00036    success &= TestResolutionHelper(".", "http://a/b/c/", base);
00037    success &= TestResolutionHelper("./", "http://a/b/c/", base);
00038    success &= TestResolutionHelper("..", "http://a/b/", base);
00039    success &= TestResolutionHelper("../", "http://a/b/", base);
00040    success &= TestResolutionHelper("../g", "http://a/b/g", base);
00041    success &= TestResolutionHelper("../..", "http://a/", base);
00042    success &= TestResolutionHelper("../../", "http://a/", base);
00043    success &= TestResolutionHelper("../../g", "http://a/g", base);
00044    //  5.4.2. Abnormal Examples
00045    success &= TestResolutionHelper("../../../g", "http://a/g", base);
00046    success &= TestResolutionHelper("../../../../g", "http://a/g", base);
00047    success &= TestResolutionHelper("/./g", "http://a/g", base);
00048    success &= TestResolutionHelper("/../g", "http://a/g", base);
00049    success &= TestResolutionHelper("g.", "http://a/b/c/g.", base);
00050    success &= TestResolutionHelper(".g", "http://a/b/c/.g", base);
00051    success &= TestResolutionHelper("g..", "http://a/b/c/g..", base);
00052    success &= TestResolutionHelper("..g", "http://a/b/c/..g", base);
00053    success &= TestResolutionHelper("./../g", "http://a/b/g", base);
00054    success &= TestResolutionHelper("./g/.", "http://a/b/c/g/", base);
00055    success &= TestResolutionHelper("g/./h", "http://a/b/c/g/h", base);
00056    success &= TestResolutionHelper("g/../h", "http://a/b/c/h", base);
00057    success &= TestResolutionHelper("g;x=1/./y", "http://a/b/c/g;x=1/y", base);
00058    success &= TestResolutionHelper("g;x=1/../y", "http://a/b/c/y", base);
00059    success &= TestResolutionHelper("g?y/./x", "http://a/b/c/g?y/./x", base);
00060    success &= TestResolutionHelper("g?y/../x", "http://a/b/c/g?y/../x", base);
00061    success &= TestResolutionHelper("g#s/./x", "http://a/b/c/g#s/./x", base);
00062    success &= TestResolutionHelper("g#s/../x", "http://a/b/c/g#s/../x", base);
00063    success &= TestResolutionHelper("http:g", "http:g", base);
00064    return(success);
00065 }
00066 
00067 
00068 Bool_t TestPct()
00069 {
00070    TString errors = "";
00071    for (char i = 0; i < 127; i++) {
00072       if (TUri::PctDecode(TUri::PctEncode(i)) != i) {
00073          char buffer[10];
00074          sprintf(buffer, "0x%02x, ", i);
00075          errors = errors + buffer;
00076       }
00077    }
00078    if (!errors.IsNull())
00079       Printf("\tERROR at %s", errors.Data());
00080    else
00081       Printf("\tOK");
00082    return errors.IsNull();
00083 }
00084 
00085 Bool_t TestComposition()
00086 {
00087    TString composed = "http://user:pass@host.org/some/path/file.avi?key1=value1#anchor3";
00088    TUri uri;
00089    uri.SetScheme("http");
00090    uri.SetUserInfo("user:pass");
00091    uri.SetHost("host.org");
00092    uri.SetPath("/some/path/file.avi");
00093    uri.SetQuery("key1=value1");
00094    uri.SetFragment("anchor3");
00095    return uri.GetUri() == composed;
00096 }
00097 
00098 void Answer(Bool_t success)
00099 {
00100    if (success)
00101       Printf("---> SUCCESS\n");
00102    else
00103       Printf("---> F A I L E D   F A I L E D   F A I L E D\n");
00104 }
00105 
00106 Bool_t TestValidation()
00107 {
00108    // validating examples from RFC chapter 1.1.2
00109    Bool_t valid = kTRUE;
00110    valid &= TUri("ftp://ftp.is.co.za/rfc/rfc1808.txt").IsUri();
00111    valid &= TUri("http://www.ietf.org/rfc/rfc2396.txt").IsUri();
00112    // IPV6 example excluded
00113    //valid &= TUri("ldap://[2001:db8::7]/c=GB?objectClass?one").IsUri();
00114    valid &= TUri("mailto:John.Doe@example.com").IsUri();
00115    valid &= TUri("news:comp.infosystems.www.servers.unix").IsUri();
00116    valid &= TUri("tel:+1-816-555-1212").IsUri();
00117    valid &= TUri("telnet://192.0.2.16:80/").IsUri();
00118    valid &= TUri("urn:oasis:names:specification:docbook:dtd:xml:4.1.2").IsUri();
00119    return valid;
00120 }
00121 
00122 
00123 void TUriTest()
00124 {
00125    Printf("\n\nTUri test macro ...");
00126    Printf("---> Validation");
00127    Answer(TestValidation());
00128    Printf("---> Reference Resolution");
00129    Answer(TestResolution());
00130    Printf("---> PCT Conversion");
00131    Answer(TestPct());
00132    Printf("---> Equivalence and Normalisation");
00133    Answer(TUri("example://a/b/c/%7Bfoo%7D") == TUri("eXAMPLE://a/./b/../b/%63/%7bfoo%7d"));
00134    Printf("---> Composition");
00135    Answer(TestComposition());
00136 }
00137 

Generated on Tue Jul 5 15:44:52 2011 for ROOT_528-00b_version by  doxygen 1.5.1