TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataprotocoltest.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // testing the data tdeioslave
  2. // (C) 2002, 2003 Leo Savernik
  3. //
  4. // invoke "make dataprotocoltest" to generate the binary inside KDE CVS
  5. // invoke "make test" to generate the binary outside KDE CVS
  6. // fix the symptoms, not the illness ;-)
  7. #ifdef QT_NO_ASCII_CAST
  8. # undef QT_NO_ASCII_CAST
  9. #endif
  10. #ifdef DATAKIOSLAVE
  11. # undef DATAKIOSLAVE
  12. #endif
  13. #ifndef TESTKIO
  14. # define TESTKIO
  15. #endif
  16. #include <tdeio/global.h>
  17. #include <tqcstring.h>
  18. #include <tqstring.h>
  19. #include <iostream.h>
  20. class KURL;
  21. class TestSlave {
  22. public:
  23. TestSlave() {
  24. }
  25. virtual ~TestSlave() {
  26. }
  27. virtual void get(const KURL &) = 0;
  28. virtual void mimetype(const KURL &) = 0;
  29. void mimeType(const TQString &type) {
  30. testStrings("MIME Type: ",mime_type_expected,type);
  31. }
  32. void totalSize(TDEIO::filesize_t bytes) {
  33. // cout << "content size: " << bytes << " bytes" << endl;
  34. }
  35. void setMetaData(const TQString &key, const TQString &value) {
  36. // meta_data[key] = value;
  37. // cout << "§ " << key << " = " << value << endl;
  38. TQString prefix = "Metadata[\""+key+"\"]: ";
  39. TDEIO::MetaData::Iterator it = attributes_expected.find(key);
  40. if (it != attributes_expected.end()) {
  41. testStrings(prefix,it.data(),value);
  42. // remove key from map
  43. attributes_expected.remove(it);
  44. } else {
  45. cout << endl << prefix << " no such key expected";
  46. total++;
  47. }
  48. }
  49. void sendMetaData() {
  50. // check here if attributes_expected contains any excess keys
  51. TDEIO::MetaData::ConstIterator it = attributes_expected.begin();
  52. TDEIO::MetaData::ConstIterator end = attributes_expected.end();
  53. for (; it != end; ++it) {
  54. cout << endl << "Metadata[\"" << it.key()
  55. << "\"] was expected but not defined";
  56. total++;
  57. }
  58. }
  59. void data(const TQByteArray &a) {
  60. if (a.isEmpty())
  61. /* cout << "<no more data>" << endl*/;
  62. else {
  63. testStrings("Content: ",content_expected,a);
  64. }/*end if*/
  65. }
  66. void finished() {
  67. }
  68. void dispatchLoop() {
  69. // dummy to make kde_main happy
  70. }
  71. // == stuff for regression testing
  72. private:
  73. int testcaseno; // number of testcase
  74. bool failure; // true if any testcase failed
  75. TQMap<int,bool> failed_testcases;
  76. // -- testcase related members
  77. TQString mime_type_expected; // expected mime type
  78. /** contains all attributes and values the testcase has to set */
  79. TDEIO::MetaData attributes_expected;
  80. /** contains the content as it is expected to be returned */
  81. TQByteArray content_expected;
  82. int passed; // # of passed tests
  83. int total; // # of total tests
  84. /**
  85. * compares two strings, printing an error message if they don't match.
  86. * @param prefix prefix string for output in case of mismatch
  87. * @param templat template string
  88. * @param s string to compare to template
  89. * @param casesensitive true if case sensitive compare (currently not used)
  90. */
  91. void testStrings(const TQString &prefix, const TQString &templat,
  92. const TQString &s, bool /*casesensitive*/ = true) {
  93. if (templat == s)
  94. passed++;
  95. else {
  96. cout << endl << prefix << "expected \"" << templat << "\", found \""
  97. << s << "\"";
  98. failure = true;
  99. }/*end if*/
  100. total++;
  101. }
  102. public:
  103. /** begins a testrun over all testcases */
  104. void initTestrun() {
  105. testcaseno = 0;
  106. failure = false;
  107. }
  108. /** reuturns true if any testcase failed
  109. */
  110. bool hasFailedTestcases() const { return failure; }
  111. /**
  112. * sets up a new testcase
  113. * @param name screen name for testcase
  114. */
  115. void beginTestcase(const char *name) {
  116. passed = 0;
  117. total = 0;
  118. testcaseno++;
  119. cout << "Testcase " << testcaseno << ": [" << name << "] ";
  120. }
  121. /**
  122. * sets the mime type that this testcase is expected to return
  123. */
  124. void setExpectedMimeType(const TQString &mime_type) {
  125. mime_type_expected = mime_type;
  126. }
  127. /**
  128. * sets all attribute-value pairs the testcase must deliver.
  129. */
  130. void setExpectedAttributes(const TDEIO::MetaData &attres) {
  131. attributes_expected = attres;
  132. }
  133. /**
  134. * sets content as expected to be delivered by the testcase.
  135. */
  136. void setExpectedContent(const TQByteArray &content) {
  137. content_expected = content;
  138. }
  139. /**
  140. * closes testcase, printing out stats
  141. */
  142. void endTestcase() {
  143. bool failed = passed < total;
  144. if (failed) {
  145. failure = true;
  146. failed_testcases[testcaseno] = true;
  147. cout << endl;
  148. }
  149. cout << "(" << passed << " of " << total << ") " << (failed ? "failed"
  150. : "passed") << endl;
  151. }
  152. void endTestrun() {
  153. if (failure) {
  154. TQMap<int,bool>::ConstIterator it = failed_testcases.begin();
  155. for (; it != failed_testcases.end(); ++it) {
  156. cout << "Testcase " << it.key() << " failed" << endl;
  157. }
  158. }
  159. }
  160. };
  161. #include "dataprotocol.cpp" // we need access to static data & functions
  162. // == general functionality
  163. const struct {
  164. const char * const name;
  165. const char * const exp_mime_type; // 0 means "text/plain"
  166. const struct {
  167. const char * const key;
  168. const char * const value;
  169. } exp_attrs[10]; // ended with a key==0, value==0 pair
  170. const char * const exp_content;
  171. const char * const url;
  172. } testcases[] = {
  173. // -----------------------------------------------------------------
  174. { "escape resolving", 0, {}, "blah blah", "data:,blah%20blah" },
  175. // --------------------
  176. { "mime type, escape resolving", "text/html", {},
  177. "<div style=\"border:thin orange solid;padding:1ex;background-color:"
  178. "yellow;color:black\">Rich <b>text</b></div>",
  179. "data:text/html,<div%20style=\"border:thin%20orange%20solid;"
  180. "padding:1ex;background-color:yellow;color:black\">Rich%20<b>text</b>"
  181. "</div>" },
  182. // -------------------- whitespace test I
  183. { "whitespace test I", "text/css", {
  184. { "charset", "iso-8859-15" }, { 0,0 } },
  185. " body { color: yellow; background:darkblue; font-weight:bold }",
  186. "data:text/css ; charset = iso-8859-15 , body { color: yellow; "
  187. "background:darkblue; font-weight:bold }" },
  188. // -------------------- out of spec argument order, base64 decoding,
  189. // whitespace test II
  190. { "out of spec argument order, base64 decoding, whitespace test II",
  191. 0, {
  192. { "charset", "iso-8859-1" }, { 0,0 } },
  193. "paaaaaaaasd!!\n",
  194. "data: ; base64 ; charset = \"iso-8859-1\" ,cGFhYWFhYWFhc2QhIQo=" },
  195. // -------------------- arbitrary keys, reserved names as keys,
  196. // whitespace test III
  197. { "arbitrary keys, reserved names as keys, whitespace test III", 0, {
  198. { "base64", "nospace" }, { "key", "onespaceinner" },
  199. { "key2", "onespaceouter" }, { "charset", "utf8" },
  200. { "<<empty>>", "" }, { 0,0 } },
  201. "Die, Allied Schweinehund (C) 1990 Wolfenstein 3D",
  202. "data: ;base64=nospace;key = onespaceinner; key2=onespaceouter ;"
  203. " charset = utf8 ; <<empty>>= ,Die, Allied Schweinehund "
  204. "(C) 1990 Wolfenstein 3D" },
  205. // -------------------- string literal with escaped chars, testing
  206. // delimiters within string
  207. { "string literal with escaped chars, testing delimiters within "
  208. "string", 0, {
  209. { "fortune-cookie", "Master Leep say: \"Rabbit is humble, "
  210. "Rabbit is gentle; follow the Rabbit\"" }, { 0,0 } },
  211. "(C) 1997 Shadow Warrior ;-)",
  212. "data:;fortune-cookie=\"Master Leep say: \\\"Rabbit is humble, "
  213. "Rabbit is gentle; follow the Rabbit\\\"\",(C) 1997 Shadow Warrior "
  214. ";-)" },
  215. };
  216. #if 0
  217. // == charset tests
  218. // -------------------- string
  219. const QChar
  220. const TQChar * const charset_urls[] = {
  221. #endif
  222. int main(int /*argc*/,char* /*argv*/[]) {
  223. DataProtocol tdeio_data;
  224. tdeio_data.initTestrun();
  225. for (uint i = 0; i < sizeof testcases/sizeof testcases[0]; i++) {
  226. tdeio_data.beginTestcase(testcases[i].name);
  227. tdeio_data.setExpectedMimeType(testcases[i].exp_mime_type != 0
  228. ? testcases[i].exp_mime_type : "text/plain");
  229. bool has_charset = false;
  230. MetaData exp_attrs;
  231. if (testcases[i].exp_attrs != 0) {
  232. for (uint j = 0; testcases[i].exp_attrs[j].key != 0; j++) {
  233. exp_attrs[testcases[i].exp_attrs[j].key] = testcases[i].exp_attrs[j].value;
  234. if (strcmp(testcases[i].exp_attrs[j].key,"charset") == 0)
  235. has_charset = true;
  236. }/*next j*/
  237. }
  238. if (!has_charset) exp_attrs["charset"] = "us-ascii";
  239. tdeio_data.setExpectedAttributes(exp_attrs);
  240. TQByteArray exp_content;
  241. uint exp_content_len = strlen(testcases[i].exp_content);
  242. exp_content.setRawData(testcases[i].exp_content,exp_content_len);
  243. tdeio_data.setExpectedContent(exp_content);
  244. tdeio_data.get(testcases[i].url);
  245. tdeio_data.endTestcase();
  246. exp_content.resetRawData(testcases[i].exp_content,exp_content_len);
  247. }/*next i*/
  248. tdeio_data.endTestrun();
  249. return tdeio_data.hasFailedTestcases() ? 1 : 0;
  250. }