Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of libdom test suite.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdbool.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #include <dom/dom.h>
  14.  
  15. #include "domts.h"
  16.  
  17. void __assert2(const char *expr, const char *function,
  18.                 const char *file, int line)
  19. {
  20.         UNUSED(function);
  21.         UNUSED(file);
  22.  
  23.         printf("FAIL - %s at line %d\n", expr, line);
  24.  
  25.         exit(EXIT_FAILURE);
  26. }
  27.  
  28. /**
  29.  * Following are the test conditions which defined in the DOMTS, please refer
  30.  * the DOM Test Suite for details
  31.  */
  32.  
  33. bool is_true(bool arg)
  34. {
  35.         return arg == true;
  36. }
  37.  
  38. bool is_null(void *arg)
  39. {
  40.         return arg == NULL;
  41. }
  42.  
  43. bool is_same(void *expected, void *actual)
  44. {
  45.         return expected == actual;
  46. }
  47.  
  48. bool is_same_int(int expected, int actual)
  49. {
  50.         return expected == actual;
  51. }
  52.  
  53. bool is_same_unsigned_int32_t(uint32_t expected, uint32_t actual)
  54. {
  55.         return expected == actual;
  56. }
  57.  
  58. bool is_equals_int(int expected, int actual, bool dummy)
  59. {
  60.         UNUSED(dummy);
  61.        
  62.         return expected == actual;
  63. }
  64.  
  65. bool is_equals_bool(bool expected, bool actual, bool dummy)
  66. {
  67.         UNUSED(dummy);
  68.  
  69.         return expected == actual;
  70. }
  71.  
  72. bool is_equals_unsigned_int32_t(uint32_t expected, uint32_t actual, bool dummy)
  73. {
  74.         UNUSED(dummy);
  75.  
  76.         return expected == actual;
  77. }
  78.  
  79. /**
  80.  * Test whether two string are equal
  81.  *
  82.  * \param expected      The expected string
  83.  * \param actual        The actual string
  84.  * \param ignoreCase    Whether to ignore letter case
  85.  */
  86. bool is_equals_string(const char *expected, dom_string *actual,
  87.                 bool ignoreCase)
  88. {
  89.         dom_string *exp;
  90.         dom_exception err;
  91.         bool ret;
  92.  
  93.         err = dom_string_create((const uint8_t *)expected, strlen(expected),
  94.                         &exp);
  95.         if (err != DOM_NO_ERR)
  96.                 return false;
  97.  
  98.         if (ignoreCase == true)
  99.                 ret = dom_string_caseless_isequal(exp, actual);
  100.         else
  101.                 ret = dom_string_isequal(exp, actual);
  102.        
  103.         dom_string_unref(exp);
  104.         return ret;
  105. }
  106.  
  107. /* Compare whether two dom_string are equal */
  108. bool is_equals_domstring(dom_string *expected, dom_string *actual,
  109.                 bool ignoreCase)
  110. {
  111.         if (ignoreCase == true)
  112.                 return dom_string_caseless_isequal(expected, actual);
  113.         else
  114.                 return dom_string_isequal(expected, actual);
  115. }
  116.  
  117. /* The param actual should always contain dom_sting and expectd should
  118.  * contain char * */
  119. bool is_equals_list(list *expected, list *actual, bool ignoreCase)
  120. {
  121.         assert((expected->type && 0xff00) == (actual->type && 0xff00));
  122.  
  123.         comparator cmp = NULL;
  124.         comparator rcmp = NULL;
  125.  
  126.         if (expected->type == INT)
  127.                 cmp = int_comparator;
  128.         if (expected->type == STRING) {
  129.                 if (actual->type == DOM_STRING) {
  130.                         cmp = ignoreCase? str_icmp : str_cmp;
  131.                         rcmp = ignoreCase? str_icmp_r : str_cmp_r;
  132.                 }
  133.         }
  134.         if (expected->type == DOM_STRING) {
  135.                 if (actual->type == STRING) {
  136.                         cmp = ignoreCase? str_icmp_r : str_cmp_r;
  137.                         rcmp = ignoreCase? str_icmp : str_cmp;
  138.                 }
  139.         }
  140.  
  141.         assert(cmp != NULL);
  142.  
  143.         return list_contains_all(expected, actual, cmp) && list_contains_all(actual, expected, rcmp);
  144. }
  145.  
  146.  
  147.  
  148. bool is_instanceof(const char *type, dom_node *node)
  149. {
  150.         assert("There is no instanceOf in the test-suite" == NULL);
  151.        
  152.         (void)type;
  153.         (void)node;
  154.        
  155.         return false;
  156. }
  157.  
  158.  
  159. bool is_size_domnamednodemap(uint32_t size, dom_namednodemap *map)
  160. {
  161.         uint32_t len;
  162.         dom_exception err;
  163.  
  164.         err = dom_namednodemap_get_length(map, &len);
  165.         if (err != DOM_NO_ERR) {
  166.                 assert("Exception occured" == NULL);
  167.                 return false;
  168.         }
  169.  
  170.         return size == len;
  171. }
  172.  
  173. bool is_size_domnodelist(uint32_t size, dom_nodelist *list)
  174. {
  175.         uint32_t len;
  176.         dom_exception err;
  177.  
  178.         err = dom_nodelist_get_length(list, &len);
  179.         if (err != DOM_NO_ERR) {
  180.                 assert("Exception occured" == NULL);
  181.                 return false;
  182.         }
  183.  
  184.         return size == len;
  185. }
  186.  
  187. bool is_size_list(uint32_t size, list *list)
  188. {
  189.         return size == list->size;
  190. }
  191.  
  192.  
  193. bool is_uri_equals(const char *scheme, const char *path, const char *host,
  194.                    const char *file, const char *name, const char *query,
  195.                    const char *fragment, const char *isAbsolute,
  196.                    dom_string *actual)
  197. {
  198.         const char *_ptr = actual != NULL ? dom_string_data(actual) : NULL;
  199.         const size_t slen = actual != NULL ? dom_string_byte_length(actual) : 0;
  200.         char *_sptr = actual != NULL ? domts_strndup(_ptr, slen) : NULL;
  201.         char *sptr = _sptr;
  202.         bool result = false;
  203.        
  204.         /* Used farther down */
  205.         const char *firstColon = NULL;
  206.         const char *firstSlash = NULL;
  207.         char *actualPath = NULL;
  208.         char *actualScheme = NULL;
  209.         char *actualHost = NULL;
  210.         char *actualFile = NULL;
  211.         char *actualName = NULL;
  212.        
  213.         assert(sptr != NULL);
  214.        
  215.         /* Note, from here on down, this is essentially a semi-direct
  216.          * reimplementation of assertURIEquals in the Java DOMTS.
  217.          */
  218.        
  219.         /* Attempt to check fragment */
  220.         {
  221.                 char *fptr = strrchr(sptr, '#');
  222.                 const char *cfptr = fptr + 1;
  223.                 if (fptr != NULL) {
  224.                         *fptr = '\0'; /* Remove fragment from sptr */
  225.                 } else {
  226.                         cfptr = "";
  227.                 }
  228.                 if (fragment != NULL) {
  229.                         if (strcmp(fragment, cfptr) != 0)
  230.                                 goto out;
  231.                 }
  232.         }
  233.         /* Attempt to check query string */
  234.         {
  235.                 char *qptr = strrchr(sptr, '?');
  236.                 const char *cqptr = qptr + 1;
  237.                 if (qptr != NULL) {
  238.                         *qptr = '\0'; /* Remove query from sptr */
  239.                 } else {
  240.                         cqptr = "";
  241.                 }
  242.                 if (query != NULL) {
  243.                         if (strcmp(query, cqptr) != 0)
  244.                                 goto out;
  245.                 }
  246.         }
  247.        
  248.         /* Scheme and path */
  249.         firstColon = strchr(sptr, ':');
  250.         firstSlash = strchr(sptr, '/');
  251.         actualPath = strdup(sptr);
  252.         actualScheme = strdup("");
  253.         if (firstColon != NULL && firstColon < firstSlash) {
  254.                 free(actualScheme);
  255.                 free(actualPath);
  256.                 actualScheme = domts_strndup(sptr, firstColon - sptr);
  257.                 actualPath = strdup(firstColon + 1);
  258.         }
  259.         if (scheme != NULL) {
  260.                 if (strcmp(scheme, actualScheme) != 0)
  261.                         goto out;
  262.         }
  263.         if (path != NULL) {
  264.                 if (strcmp(path, actualPath) != 0)
  265.                         goto out;
  266.         }
  267.        
  268.         /* host */
  269.         if (host != NULL) {
  270.                 if (actualPath[0] == '/' &&
  271.                     actualPath[1] == '/') {
  272.                         const char *termslash = strchr(actualPath + 2, '/');
  273.                         actualHost = domts_strndup(actualPath,
  274.                                              termslash - actualPath);
  275.                 } else {
  276.                         actualHost = strdup("");
  277.                 }
  278.                 if (strcmp(actualHost, host) != 0)
  279.                         goto out;
  280.         }
  281.        
  282.        
  283.         /* file */
  284.         actualFile = strdup(actualPath);
  285.         if (file != NULL || name != NULL) {
  286.                 const char *finalSlash = strrchr(actualPath, '/');
  287.                 if (finalSlash != NULL) {
  288.                         free(actualFile);
  289.                         actualFile = strdup(finalSlash + 1);
  290.                 }
  291.                 if (file != NULL) {
  292.                         if (strcmp(actualFile, file) != 0)
  293.                                 goto out;
  294.                 }
  295.         }
  296.        
  297.         /* name */
  298.         if (name != NULL) {
  299.                 const char *finalPeriod = strrchr(actualFile, '.');
  300.                 if (finalPeriod != NULL) {
  301.                         actualName = domts_strndup(actualFile,
  302.                                              finalPeriod - actualFile);
  303.                 } else {
  304.                         actualName = strdup(actualFile);
  305.                 }
  306.                 if (strcmp(actualName, name) != 0)
  307.                         goto out;
  308.         }
  309.        
  310.         /* isAbsolute */
  311.         if (isAbsolute != NULL) {
  312.                 bool startslash = *actualPath == '/';
  313.                 bool isabsolute = strcasecmp(isAbsolute, "true") == 0;
  314.                 isabsolute |= (strcasecmp(isAbsolute, "yes") == 0);
  315.                 isabsolute |= (strcmp(isAbsolute, "1") == 0);
  316.                 startslash |= (strncmp(actualPath, "file:/", 6) == 0);
  317.                 if (isabsolute != startslash)
  318.                         goto out;
  319.         }
  320.  
  321.         result = true;
  322. out:
  323.         if (actualPath != NULL)
  324.                 free(actualPath);
  325.         if (actualScheme != NULL)
  326.                 free(actualScheme);
  327.         if (actualHost != NULL)
  328.                 free(actualHost);
  329.         if (actualFile != NULL)
  330.                 free(actualFile);
  331.         if (actualName != NULL)
  332.                 free(actualName);
  333.         free(_sptr);
  334.         return result;
  335. }
  336.  
  337.  
  338. bool is_contenttype(const char *type)
  339. {
  340.         /* Now, we use the libxml2 parser for DOM parsing, so the content type
  341.          * is always "text/xml" */
  342.         if (strcmp(type, "text/xml") == 0)
  343.                 return true;
  344.         else
  345.                 return false;
  346. }
  347.  
  348. bool has_feature(const char *feature, const char *version)
  349. {
  350.         dom_exception err;
  351.         bool ret;
  352.  
  353.         if (feature == NULL)
  354.                 feature = "";
  355.  
  356.         if (version == NULL)
  357.                 version = "";
  358.  
  359.         err = dom_implementation_has_feature(feature, version, &ret);
  360.         /* Here, when we come with exception, we should return false,
  361.          * TODO: this need to be improved, but I can't figure out how */
  362.         if (err != DOM_NO_ERR) {
  363.                 return false;
  364.         }
  365.  
  366.         return ret;
  367. }
  368.  
  369. bool implementation_attribute(char *name, bool value)
  370. {
  371.         /* We didnot support DOMConfigure for implementation now */
  372.         UNUSED(name);
  373.         UNUSED(value);
  374.  
  375.         return true;
  376. }
  377.