Tizen 2.1 base
[framework/web/wrt-commons.git] / tests / ace / TestSuite05.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file    TestSuite05.cpp
18  * @author  unknown
19  * @version 1.0
20  * @brief   Test cases for Attribute class.
21  */
22
23 #include <iostream>
24 #include <list>
25 #include <set>
26 #include <string>
27
28 #include <dpl/test/test_runner.h>
29
30 #include <dpl/ace/Attribute.h>
31 #include <dpl/ace-dao-ro/BaseAttribute.h>
32
33 #define TESTSUITE05(n) \
34 RUNNER_TEST(ts05_attr_tests_ ## n)
35
36 class AT : public Attribute {
37 public:
38     AT(std::string nm)
39       : Attribute(nm)
40     {}
41
42     static std::string* uriAuthorityStatic(const std::string *input) {
43         AT at("Micky");
44         return at.uriAuthority(input);
45     }
46
47     static std::string* uriHostStatic(const std::string *input) {
48         AT at("Pluto");
49         return at.uriHost(input);
50     }
51
52     static std::string* uriSchemeStatic(const std::string *input) {
53         AT at("Donald");
54         return at.uriScheme(input);
55     }
56
57     static std::string* uriSchemeAuthorityStatic(const std::string *input) {
58         AT at("Winnie the Pooh");
59         return at.uriSchemeAuthority(input);
60     }
61
62     static std::string* uriPathStatic(const std::string *input) {
63         AT at("Hannibal");
64         return at.uriPath(input);
65     }
66
67     static bool markTest(){
68         bool result = true;
69         for(int i =0; i<128; ++i){
70             if( i == '-' || i == '_' || i == '.'|| i == '!'|| i == '~' || i == '*' || i == '\'' || i == ')' || i == '(' ){
71                 if (!mark[i]){
72                     result = false;
73                     break;
74                 }
75             }
76             else{
77                 if(mark[i]){
78                     result =false;
79                     break;
80                 }
81             }
82         }
83         return result;
84     }
85
86     static bool digitTest(){
87         bool result = true;
88         for(int i =0; i<128; ++i){
89             if( i > 47 && i < 58 ){
90                 if (!digit[i]){
91                     result = false;
92                     break;
93                 }
94             }
95             else{
96                 if(digit[i]){
97                     result =false;
98                     break;
99                 }
100             }
101         }
102         return result;
103     }
104
105     static bool alphaTest(){
106         bool result = true;
107         for(int i =0; i<128; ++i){
108             if( ( i>64 && i<91 ) || ( i>96 && i<123  ) ) {
109                 if (!alpha[i]){
110                     result = false;
111                     break;
112                 }
113             }
114             else{
115                 if(alpha[i]){
116                     result =false;
117                     break;
118                 }
119             }
120         }
121         return result;
122     }
123
124     static bool isEscapedStatic(const char esc[3]) {
125         AT at("Swinka");
126         return at.isEscaped(esc);
127     }
128 };
129
130 bool assertEqual(const std::string * actual, const char * intended) {
131     if (actual == NULL || intended == NULL) {
132         if (intended == NULL && actual == NULL) {
133             return true;
134         }
135         else {
136             return false;
137         }
138     }
139
140     std::string temp(intended);
141
142     if (temp == *actual) {
143         return true;
144     }
145     else {
146         return false;
147     }
148 }
149
150 bool assertTrue(bool condition){
151     return condition;
152 }
153
154 bool assertFalse(bool condition){
155     return !condition;
156 }
157
158 TESTSUITE05(01_uriAuthority){
159     std::string * outcome = NULL;
160     std::string query("http://www.wp.pl");
161     outcome = AT::uriAuthorityStatic(&query);
162     RUNNER_ASSERT(assertEqual(outcome, "www.wp.pl"));
163     delete outcome;
164 }
165
166 TESTSUITE05(02_uriAuthority){
167     std::string * outcome = NULL;
168     std::string query("http://authority?path/asdf");
169     outcome = AT::uriAuthorityStatic(&query);
170     RUNNER_ASSERT(assertEqual(outcome, "authority"));
171     delete outcome;
172 }
173
174 TESTSUITE05(03_uriAuthority){
175     std::string * outcome = NULL;
176     std::string query("abcd"); //This should be interpreted as schema
177     outcome = AT::uriAuthorityStatic(&query);
178     RUNNER_ASSERT(assertEqual(outcome, ""));
179     delete outcome;
180 }
181
182 TESTSUITE05(04_uriAuthority){
183     std::string * outcome = NULL;
184     std::string query("http://authority/asdf");
185     outcome = AT::uriAuthorityStatic(&query);
186     RUNNER_ASSERT(assertEqual(outcome, "authority"));
187     delete outcome;
188 }
189
190 TESTSUITE05(05_uriAuthority){
191     std::string * outcome = NULL;
192     std::string query("http://user@host:20?ds");
193     outcome = AT::uriAuthorityStatic(&query);
194     RUNNER_ASSERT(assertEqual(outcome, "user@host:20"));
195     delete outcome;
196 }
197
198 TESTSUITE05(06_uriAuthority){
199     std::string * outcome = NULL;
200     std::string query("http://hostname:23");
201     outcome = AT::uriAuthorityStatic(&query);
202     RUNNER_ASSERT(assertEqual(outcome, "hostname:23"));
203     delete outcome;
204 }
205
206 TESTSUITE05(07_uriAuthority){
207     std::string * outcome = NULL;
208     std::string query("http://user@host:port");
209     outcome = AT::uriAuthorityStatic(&query);
210     RUNNER_ASSERT(assertEqual(outcome, "user@host:port"));
211     delete outcome;
212 }
213
214 TESTSUITE05(08_uriAuthority){
215     std::string * outcome = NULL;
216     std::string query("http://1user@host:port"); //This is a VALID URI
217     outcome = AT::uriAuthorityStatic(&query);
218     RUNNER_ASSERT(assertEqual(outcome, "1user@host:port"));
219     delete outcome;
220 }
221
222 TESTSUITE05(09_uriAuthority){
223     std::string * outcome = NULL;
224     std::string query("http://abc%30"); //This is not a valid uri
225     outcome = AT::uriAuthorityStatic(&query);
226     RUNNER_ASSERT(assertEqual(outcome, "abc%30"));
227     delete outcome;
228 }
229
230 TESTSUITE05(10_uriAuthority){
231     std::string * outcome = NULL;
232     std::string query("http:///asd"); //This is not a valid uri
233     outcome = AT::uriAuthorityStatic(&query);
234     RUNNER_ASSERT(assertEqual(outcome, ""));
235     delete outcome;
236 }
237
238 TESTSUITE05(11_uriAuthority){
239     std::string * outcome = NULL;
240     std::string query("http://?asd"); //This is not a valid uri
241     outcome = AT::uriAuthorityStatic(&query);
242     RUNNER_ASSERT(assertEqual(outcome, ""));
243     delete outcome;
244 }
245
246 TESTSUITE05(12_uriAuthority){
247     std::string * outcome = NULL;
248     std::string query("http://%34%56%67%ab");
249     outcome = AT::uriAuthorityStatic(&query);
250     RUNNER_ASSERT(assertEqual(outcome, "%34%56%67%ab"));
251     delete outcome;
252 }
253
254 TESTSUITE05(13_uriAuthority){
255     std::string * outcome = NULL;
256     std::string query("http://<>"); //This is not a valid uri
257     outcome = AT::uriAuthorityStatic(&query);
258     RUNNER_ASSERT(assertEqual(outcome, NULL));
259     delete outcome;
260 }
261
262 TESTSUITE05(14_uriAuthority){
263     std::string * outcome = NULL;
264     std::string query("http://\\/"); //This is not a valid uri
265     outcome = AT::uriAuthorityStatic(&query);
266     RUNNER_ASSERT(assertEqual(outcome, NULL));
267     delete outcome;
268 }
269
270 TESTSUITE05(15_uriHost){
271     std::string * outcome = NULL;
272     std::string query("http://user@host:23");
273     outcome = AT::uriHostStatic(&query);
274     RUNNER_ASSERT(assertEqual(outcome, "host"));
275     delete outcome;
276 }
277
278 TESTSUITE05(16_uriHost){
279     std::string * outcome = NULL;
280     std::string query("http://user@host:name"); //This is not a valid uri
281     outcome = AT::uriHostStatic(&query);
282     RUNNER_ASSERT(assertEqual(outcome, ""));
283     delete outcome;
284 }
285
286 TESTSUITE05(17_uriHost){
287     std::string * outcome = NULL;
288     std::string query("http::::"); //This is a valid uri
289     outcome = AT::uriHostStatic(&query);
290     RUNNER_ASSERT(assertEqual(outcome, ""));
291     delete outcome;
292 }
293
294 TESTSUITE05(18_uriHost){
295     std::string * outcome = NULL;
296     std::string query("..%%%."); //This is not a valid uri
297     outcome = AT::uriHostStatic(&query);
298     RUNNER_ASSERT(assertEqual(outcome, NULL));
299     delete outcome;
300 }
301
302 TESTSUITE05(19_uriHost){
303     std::string * outcome = NULL;
304     std::string query("ftp://abds.eu/fda");
305     outcome = AT::uriHostStatic(&query);
306     RUNNER_ASSERT(assertEqual(outcome, "abds.eu"));
307     delete outcome;
308 }
309
310 TESTSUITE05(20_uriHost){
311     std::string * outcome = NULL;
312     std::string query("abs%14ccc");
313     //This is a valid uri because it's interpreted as a path not a host
314     outcome = AT::uriHostStatic(&query);
315     RUNNER_ASSERT(assertEqual(outcome, ""));
316     delete outcome;
317 }
318
319 TESTSUITE05(21_uriHost){
320     std::string * outcome = NULL;
321     std::string query("http://abc@123.2.23.213:982");
322     outcome = AT::uriHostStatic(&query);
323     RUNNER_ASSERT(assertEqual(outcome, "123.2.23.213"));
324     delete outcome;
325 }
326
327 TESTSUITE05(22_uriHost){
328     std::string * outcome = NULL;
329     std::string query("http://abc@1233.2.23.213:982");
330     //Hostname is invalid, but uri is valid
331     outcome = AT::uriHostStatic(&query);
332     RUNNER_ASSERT(assertEqual(outcome, ""));
333     delete outcome;
334 }
335
336 TESTSUITE05(23_uriHost){
337     std::string * outcome = NULL;
338     std::string query("http://ab%23c@host"); //Valid escaped characters
339     outcome = AT::uriHostStatic(&query);
340     RUNNER_ASSERT(assertEqual(outcome, "host"));
341     delete outcome;
342 }
343
344 TESTSUITE05(24_uriHost){
345     std::string * outcome = NULL;
346     std::string query("http://ab%23c@host%34"); //Invalid escaped characters in hostname
347     outcome = AT::uriHostStatic(&query);
348     RUNNER_ASSERT(assertEqual(outcome, ""));
349     delete outcome;
350 }
351
352 TESTSUITE05(25_uriHost){
353     std::string * outcome = NULL;
354     std::string query("http://ab%GGc@host"); //Wrong character %
355     outcome = AT::uriHostStatic(&query);
356     RUNNER_ASSERT(assertEqual(outcome, NULL));
357     delete outcome;
358 }
359
360 TESTSUITE05(26_uriHost){
361     std::string * outcome = NULL;
362     std::string query("http://www.example.pl");
363     outcome = AT::uriHostStatic(&query);
364     RUNNER_ASSERT(assertEqual(outcome, "www.example.pl"));
365     delete outcome;
366 }
367
368 TESTSUITE05(27_uriScheme){
369     std::string * outcome = NULL;
370     std::string query("http://host");
371     outcome = AT::uriSchemeStatic(&query);
372     RUNNER_ASSERT(assertEqual(outcome, "http"));
373     delete outcome;
374 }
375
376 TESTSUITE05(28_uriScheme){
377     std::string * outcome = NULL;
378     //Wrong character '1' in scheme , it's not an URI because two slashes are not acceptable
379     //in any other place than in separation between scheme and pat
380     std::string query("1http://host");
381     outcome = AT::uriSchemeStatic(&query);
382     RUNNER_ASSERT(assertEqual(outcome, NULL));
383     delete outcome;
384 }
385
386 TESTSUITE05(29_uriScheme){
387     std::string * outcome = NULL;
388     std::string query("ftp+a-fdf.ads://host");
389     outcome = AT::uriSchemeStatic(&query);
390     RUNNER_ASSERT(assertEqual(outcome, "ftp+a-fdf.ads"));
391     delete outcome;
392 }
393
394 TESTSUITE05(30_uriScheme){
395     std::string * outcome = NULL;
396     //Scheme cannot start with plus, it's not an URI because two slashes are not acceptable
397     //in any other place than in separation between scheme and path
398     std::string query("+++://host");
399     outcome = AT::uriSchemeStatic(&query);
400     RUNNER_ASSERT(assertEqual(outcome, NULL));
401     delete outcome;
402 }
403
404 TESTSUITE05(31_uriScheme){
405     std::string * outcome = NULL;
406     std::string query("aaaac"); //It's a path not a scheme'a
407     outcome = AT::uriSchemeStatic(&query);
408     RUNNER_ASSERT(assertEqual(outcome, ""));
409     delete outcome;
410 }
411
412 TESTSUITE05(32_uriScheme){
413     std::string * outcome = NULL;
414     //no escaped characters in schema, it's not an URI because two slashes are not acceptable
415     //in any other place than in separation between scheme and path
416     std::string query("ftpa%34fdfads://host");
417     outcome = AT::uriSchemeStatic(&query);
418     RUNNER_ASSERT(assertEqual(outcome, NULL));
419     delete outcome;
420 }
421
422 TESTSUITE05(33_uriScheme){
423     std::string * outcome = NULL;
424     //no escaped characters in schema, it's not an URI because two slashes are not acceptable
425     //in any other place than in separation between scheme and path
426     std::string query("meaninglessstring://host%34");
427     outcome = AT::uriSchemeStatic(&query);
428     RUNNER_ASSERT(assertEqual(outcome, "meaninglessstring"));
429     delete outcome;
430 }
431
432 TESTSUITE05(34_uriScheme){
433     std::string * outcome = NULL;
434     std::string query("meaninglessstring2://");
435     outcome = AT::uriSchemeStatic(&query);
436     RUNNER_ASSERT(assertEqual(outcome, "meaninglessstring2"));
437     delete outcome;
438 }
439
440 TESTSUITE05(35_uriScheme){
441     std::string * outcome = NULL;
442     std::string query("http://www.samsung.com/ace/bondi#5");
443     outcome = AT::uriSchemeStatic(&query);
444     RUNNER_ASSERT(assertEqual(outcome, "http"));
445     delete outcome;
446 }
447
448 TESTSUITE05(36_uriScheme){
449     std::string * outcome = NULL;
450     std::string query("www.samsung.com");
451     outcome = AT::uriSchemeStatic(&query);
452     RUNNER_ASSERT(assertEqual(outcome, ""));
453     delete outcome;
454 }
455
456 TESTSUITE05(37_uriSchemeAuthority){
457     std::string * outcome = NULL;
458     std::string query("http://www.samsung.com");
459     outcome = AT::uriSchemeAuthorityStatic(&query);
460     RUNNER_ASSERT(assertEqual(outcome, "http://www.samsung.com"));
461     delete outcome;
462 }
463
464 TESTSUITE05(38_uriSchemeAuthority){
465     std::string * outcome = NULL;
466     std::string query("ftp23://www.samsung.com/avc%23");
467     outcome = AT::uriSchemeAuthorityStatic(&query);
468     RUNNER_ASSERT(assertEqual(outcome, "ftp23://www.samsung.com"));
469     delete outcome;
470 }
471
472 TESTSUITE05(39_uriSchemeAuthority){
473     std::string * outcome = NULL;
474     std::string query("ftp++://anonymous@hostname:12/avc%23");
475     outcome = AT::uriSchemeAuthorityStatic(&query);
476     RUNNER_ASSERT(assertEqual(outcome, "ftp++://anonymous@hostname:12"));
477     delete outcome;
478 }
479
480 TESTSUITE05(40_uriSchemeAuthority){
481     std::string * outcome = NULL;
482     std::string query("32ftp://anonymous@hostname:12/avc%23");
483     outcome = AT::uriSchemeAuthorityStatic(&query);
484     RUNNER_ASSERT(assertEqual(outcome, NULL));
485     delete outcome;
486 }
487
488 TESTSUITE05(41_uriSchemeAuthority){
489     std::string * outcome = NULL;
490     std::string query("http://aaabbb?acd");
491     outcome = AT::uriSchemeAuthorityStatic(&query);
492     RUNNER_ASSERT(assertEqual(outcome, "http://aaabbb"));
493     delete outcome;
494 }
495
496 TESTSUITE05(42_uriSchemeAuthority){
497     std::string * outcome = NULL;
498     std::string query("http://aaabbb/acd;sdf;sdf");
499     outcome = AT::uriSchemeAuthorityStatic(&query);
500     RUNNER_ASSERT(assertEqual(outcome, "http://aaabbb"));
501     delete outcome;
502 }
503
504 TESTSUITE05(43_uriPath){
505     std::string * outcome = NULL;
506     std::string query("ftp://authority//invalidpath");
507     outcome = AT::uriPathStatic(&query);
508     RUNNER_ASSERT(assertEqual(outcome, NULL));
509     delete outcome;
510 }
511
512 TESTSUITE05(44_uriPath){
513     std::string * outcome = NULL;
514     std::string query("ftp://authority/validpath");
515     outcome = AT::uriPathStatic(&query);
516     RUNNER_ASSERT(assertEqual(outcome, "validpath"));
517     delete outcome;
518 }
519
520 TESTSUITE05(45_uriPath){
521     std::string * outcome = NULL;
522     std::string query("ftp://authority/validpath;param;param");
523     outcome = AT::uriPathStatic(&query);
524     RUNNER_ASSERT(assertEqual(outcome, "validpath;param;param"));
525     delete outcome;
526 }
527
528 TESTSUITE05(46_uriPath){
529     std::string * outcome = NULL;
530     std::string query("ftp://authority/validpath;param;param?query");
531     outcome = AT::uriPathStatic(&query);
532     RUNNER_ASSERT(assertEqual(outcome, "validpath;param;param"));
533     delete outcome;
534 }
535
536 TESTSUITE05(47_uriPath){
537     std::string * outcome = NULL;
538     std::string query("ftp://authority/validpath;?param;param?query");
539     outcome = AT::uriPathStatic(&query);
540     RUNNER_ASSERT(assertEqual(outcome, "validpath;"));
541     delete outcome;
542 }
543
544 TESTSUITE05(48_uriPath){
545     std::string * outcome = NULL;
546     std::string query("ftp://authority/validpath;param?;param?query");
547     outcome = AT::uriPathStatic(&query);
548     RUNNER_ASSERT(assertEqual(outcome, "validpath;param"));
549     delete outcome;
550 }
551
552 TESTSUITE05(49_uriPath){
553     std::string * outcome = NULL;
554     std::string query("ftp://authority/valid:path?query");
555     outcome = AT::uriPathStatic(&query);
556     RUNNER_ASSERT(assertEqual(outcome, "valid:path"));
557     delete outcome;
558 }
559
560 TESTSUITE05(50_uriPath){
561     std::string * outcome = NULL;
562     std::string query("ftp://authority/:::");
563     outcome = AT::uriPathStatic(&query);
564     RUNNER_ASSERT(assertEqual(outcome, ":::"));
565     delete outcome;
566 }
567
568 TESTSUITE05(51_uriPath){
569     std::string * outcome = NULL;
570     std::string query("/path1/path2?abc#fragment");
571     outcome = AT::uriPathStatic(&query);
572     RUNNER_ASSERT(assertEqual(outcome, "path1/path2"));
573     delete outcome;
574 }
575
576 TESTSUITE05(52_uriPath){
577     std::string * outcome = NULL;
578     std::string query("http://www.samsung.com/normalpath/path2?query");
579     outcome = AT::uriPathStatic(&query);
580     RUNNER_ASSERT(assertEqual(outcome, "normalpath/path2"));
581     delete outcome;
582 }
583
584 TESTSUITE05(53_markTest){
585     RUNNER_ASSERT(AT::markTest());
586 }
587
588 TESTSUITE05(54_digitTest){
589     RUNNER_ASSERT(AT::digitTest());
590 }
591
592 TESTSUITE05(55_alphaTest){
593     RUNNER_ASSERT(AT::alphaTest());
594 }
595
596 TESTSUITE05(56_escapedTest){
597     const char * query = "%23";
598     RUNNER_ASSERT(assertTrue(AT::isEscapedStatic(query)));
599     query = "%ab";
600     RUNNER_ASSERT(assertTrue(AT::isEscapedStatic(query)));
601
602     query = "%a";
603     RUNNER_ASSERT(assertFalse(AT::isEscapedStatic(query)));
604
605     query = "%rw";
606     RUNNER_ASSERT(assertFalse(AT::isEscapedStatic(query)));
607
608     query = NULL;
609     RUNNER_ASSERT(assertFalse(AT::isEscapedStatic(query)));
610
611     query = "abc";
612     RUNNER_ASSERT(assertFalse(AT::isEscapedStatic(query)));
613
614     query = "%bc";
615     RUNNER_ASSERT(assertTrue(AT::isEscapedStatic(query)));
616
617     query = "%DF";
618     RUNNER_ASSERT(assertTrue(AT::isEscapedStatic(query)));
619 }
620