(file) Return to test_xml.cpp CVS log (file) (dir) Up to [OMI] / omi / xml / tests

File: [OMI] / omi / xml / tests / Attic / test_xml.cpp (download)
Revision: 1.1.1.1 (vendor branch), Wed May 30 21:47:40 2012 UTC (12 years, 1 month ago) by mike
Branch: TOG
CVS Tags: OMI_1_0_2_Branch, OMI_1_0_2, OMI_1_0_1_PRE, OMI_1_0_1, OMI_1_0_0
Changes since 1.1: +0 -0 lines
Initial Import

/*
**==============================================================================
**
** Open Management Infrastructure (OMI)
**
** Copyright (c) Microsoft Corporation
** 
** Licensed under the Apache License, Version 2.0 (the "License"); you may not 
** use this file except in compliance with the License. You may obtain a copy 
** of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
** MERCHANTABLITY OR NON-INFRINGEMENT. 
**
** See the Apache 2 License for the specific language governing permissions 
** and limitations under the License.
**
**==============================================================================
*/

#include <iostream>
#include <string>
#include <vector>
#include <ut/ut.h>
#include <common.h>
#include <xml/xml.h>
#include <base/strings.h>
#include <base/dir.h>
#include <base/paths.h>
#include <cstdio>
#include <base/io.h>


#if defined(_MSC_VER)
/* PreFast - reviewed and believed to be false-positive*/

/* warning C6262: Function uses '21344' bytes of stack: exceeds /analyze:stacksize'16384'. Consider moving some data to heap */
//# pragma warning(disable : 6262)

#endif /* _MSC_VER */

using namespace std;

static void setUp()
{
}

static void cleanup()
{
}

static void Test0()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<a>       one         </a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

static void Test1()
{
    XML xml;
    int r;
    XML_Elem e;
    size_t i = 0;
    char data[] = "<a><b><c></c></b></a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
        switch (i)
        {
            case 0:
            {
                UT_ASSERT(strcmp(e.data, "a") == 0);
                UT_ASSERT(e.type == XML_START);
                break;
            }
            case 1:
            {
                UT_ASSERT(strcmp(e.data, "b") == 0);
                UT_ASSERT(e.type == XML_START);
                break;
            }
            case 2:
            {
                UT_ASSERT(strcmp(e.data, "c") == 0);
                UT_ASSERT(e.type == XML_START);
                break;
            }
            case 3:
            {
                UT_ASSERT(strcmp(e.data, "c") == 0);
                UT_ASSERT(e.type == XML_END);
                break;
            }
            case 4:
            {
                UT_ASSERT(strcmp(e.data, "b") == 0);
                UT_ASSERT(e.type == XML_END);
                break;
            }
            case 5:
            {
                UT_ASSERT(strcmp(e.data, "a") == 0);
                UT_ASSERT(e.type == XML_END);
                break;
            }
        }

        i++;
    }

    UT_ASSERT(r == 1);
}

static void Test2()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<a>   <b>   bbb   <c>   ccc    </c> BBB</b> AAA</a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
    }

    UT_ASSERT(r == 1);
}

static void Test3()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<a><b>   &lt;&#65;&#66;&#67;&gt;&#x41;   </b></a>";
    size_t i = 0;

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
        if (i == 2)
        {
            UT_ASSERT(e.type == XML_CHARS);
            UT_ASSERT(strcmp(e.data, "<ABC>A") == 0);
        }

        i++;
    }

    UT_ASSERT(i == 5);
    UT_ASSERT(r == 1);
}

/* Test more than one root element */
static void Test4()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<a></a><a></a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
        ;

    UT_ASSERT(r == -1);
}

/* Test attributes */
static void Test5()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<a x=\"1\" y = \"22\" zzz = \"1234\">Stuff</a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

/* Test empty tags */
static void Test6()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<a><b/></a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

/* Test Person example */
static void Test7()
{
    XML xml;
    int r;
    XML_Elem e;
    const char* tag = NULL;
    const char* first = NULL;
    const char* last = NULL;
    size_t i;
    char data[] = 
        "<Person First='George' Last='Washington' Char='&lt;'>\n"
        "</Person>\n";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
        if (e.type == XML_START && strcmp(e.data, "Person") == 0)
        {
            tag = e.data;

            for (i = 0; i < e.attrsSize; i++)
            {
                if (strcmp(e.attrs[i].name, "First") == 0)
                    first = e.attrs[i].value;
                if (strcmp(e.attrs[i].name, "Last") == 0)
                    last = e.attrs[i].value;
            }
        }
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(tag != 0 && strcmp(tag, "Person") == 0);
    UT_ASSERT(strcmp(first, "George") == 0);
    UT_ASSERT(strcmp(last, "Washington") == 0);

    UT_ASSERT(r == 1);
}

/* Test comments */
static void Test8()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = 
        "<!-- blah blah blah --><a><!--hello-->Stuff</a>";
    bool foundComment1 = false;
    bool foundComment2 = false;

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
        if (e.type == XML_COMMENT)
        {
            if (strcmp(e.data, " blah blah blah ") == 0)
            {
                foundComment1 = true;
            }
            else if (strcmp(e.data, "hello") == 0)
            {
                foundComment2 = true;
            }
        }
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(foundComment1);
    UT_ASSERT(foundComment2);
    UT_ASSERT(r == 1);
}

/* Test <![CDATA[...]]> */
static void Test9()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<a><![CDATA[blah]]></a>";
    bool found = false;

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
        if (e.type == XML_CHARS)
        {
            if (strcmp(e.data, "blah") == 0)
            {
                found = true;
            }
        }
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(found);
    UT_ASSERT(r == 1);
}

/* Test DOCTYPE element */
static void Test10()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = "<!DOCTYPE ingore me><a><!DOCTYPE ingore me></a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

/* Test processing instruction */
static void Test11()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = 
        "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
        "<a>content</a>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

/* Test tag names */
static void Test12()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = 
        "<b-c.d></b-c.d>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

/* Test tag names */
static void Test13()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = 
        "<ns1:tag xmlns:ns1='http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd'>"
        "    <ns2:tag xmlns:ns2='http://microsoft.com/ns2'>"
        "    </ns2:tag>"
        "</ns1:tag>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    XML_RegisterNameSpace(&xml, 'w',
        "http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd");

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

static bool Inhale(const char* path, vector<char>& data)
{
    string tmp = GetPath(ID_PREFIX);
    tmp += "/xml/tests/";
    tmp += path;
    FILE* is = Fopen(tmp.c_str(), "r");

    if (!is)
        return false;

    size_t n;
    char buf[4096];

    while ((n = fread(buf, 1, sizeof(buf), is)) != 0)
    {
        data.insert(data.end(), buf, buf + n);
    }

    data.push_back('\0');

    fclose(is);

    return true;
}

static void Test14()
{
    XML xml;
    int r;
    XML_Elem e;
    vector<char> data;
    vector<XML_Elem> elems;

    UT_ASSERT(Inhale("test14.xml", data));

    XML_Init(&xml);
    XML_SetText(&xml, &data[0]);

    r = XML_RegisterNameSpace(&xml, 'a', "auri");
    UT_ASSERT(r == 0);
    r = XML_RegisterNameSpace(&xml, 'b', "buri");
    UT_ASSERT(r == 0);
    r = XML_RegisterNameSpace(&xml, 'c', "curi");
    UT_ASSERT(r == 0);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        printf("i=%d\n", (int)i);
        XML_Elem_Dump(&e);
        i++;
#endif
        elems.push_back(e);
    }

    UT_ASSERT(r == 1);
    UT_ASSERT(elems.size() == 14);

    UT_ASSERT(strcmp(elems[0].data, "root") == 0);
    UT_ASSERT(strcmp(elems[1].data, "a:anvil") == 0);

    UT_ASSERT(strcmp(elems[2].data, "a:apple") == 0);
    UT_ASSERT(elems[2].attrsSize == 2);
    UT_ASSERT(strcmp(elems[2].attrs[0].name, "a:color") == 0);
    UT_ASSERT(strcmp(elems[2].attrs[0].value, "Red") == 0);
    UT_ASSERT(strcmp(elems[2].attrs[1].name, "a:size") == 0);
    UT_ASSERT(strcmp(elems[2].attrs[1].value, "100") == 0);

    UT_ASSERT(strcmp(elems[3].data, "b:bat") == 0);
    UT_ASSERT(strcmp(elems[4].data, "c:cat") == 0);
    UT_ASSERT(strcmp(elems[5].data, "ddd:dog") == 0);
    UT_ASSERT(strcmp(elems[6].data, "eee:egg") == 0);
    UT_ASSERT(strcmp(elems[7].data, "eee:egg") == 0);
    UT_ASSERT(strcmp(elems[8].data, "ddd:dog") == 0);
    UT_ASSERT(strcmp(elems[9].data, "c:cat") == 0);
    UT_ASSERT(strcmp(elems[10].data, "b:bat") == 0);
    UT_ASSERT(strcmp(elems[11].data, "a:apple") == 0);
    UT_ASSERT(strcmp(elems[12].data, "a:anvil") == 0);
    UT_ASSERT(strcmp(elems[13].data, "root") == 0);
}

/* Test tag names */
static void Test15()
{
    XML xml;
    int r;
    XML_Elem e;
    char data[] = 
        "<apple:root xmlns:apple='http://ns.com/a'>"
        "    <apple:part apple:color='Red'>"
        "    </apple:part>"
        "</apple:root>";

    XML_Init(&xml);
    XML_SetText(&xml, data);

    XML_RegisterNameSpace(&xml, 'a', "http://ns.com/a");

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    UT_ASSERT(r == 1);
}

static void TestFile(const char* path, char* msg, size_t size)
{
    XML xml;
    int r;
    XML_Elem e;
    vector<char> data;

    bool flag = Inhale(path, data);
    UT_ASSERT(flag == true);

    UT_ASSERT(data.size() != 0);

    XML_Init(&xml);
    XML_SetText(&xml, &data[0]);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
    }

    XML_FormatError(&xml, msg, size);

#if 0
    printf("msg[%s]\n", msg);
#endif
}

static void Test16()
{
    char msg[1024];
    TestFile("test16.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "4: error: expected attribute name") == 0);
}

static void Test17()
{
    char msg[1024];
    TestFile("test17.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "1: error: expected '=' character") == 0);
}

static void Test18()
{
    char msg[1024];
    TestFile("test18.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "2: error: expected opening quote") == 0);
}

static void Test19()
{
    char msg[1024];
    TestFile("test19.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "2: error: bad entity reference") == 0);
}

static void Test20()
{
    char msg[1024];
    TestFile("test20.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "3: error: bad character reference") == 0);
}

static void Test21()
{
    char msg[1024];
    TestFile("test21.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "6: error: expected closing quote") == 0);
}

static void Test22()
{
    char msg[1024];
    TestFile("test22.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, 
        "1: error: default namespaces not supported: xmlns") == 0);
}

static void Test23()
{
    char msg[1024];
    TestFile("test23.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "33: error: too many namespaces (>32)") == 0);
}

static void Test24()
{
    char msg[1024];
    TestFile("test24.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "34: error: too many attributes (>32)") == 0);
}

static void Test25()
{
    char msg[1024];
    TestFile("test25.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "5: error: open/close tag mismatch: a/root") == 0);
}

static void Test26()
{
    char msg[1024];
    TestFile("test26.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "3: error: open/close tag mismatch: a/b") == 0);
}

static void Test27()
{
    char msg[1024];
    TestFile("test27.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "8: error: markup outside root element") == 0);
}

static void Test28()
{
    char msg[1024];
    TestFile("test28.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "5: error: expected opening angle bracket") == 0);
}

static void Test29()
{
    char msg[1024];
    TestFile("test29.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "1: error: expected opening angle bracket") == 0);
}

static void Test30()
{
    XML xml;
    int r;
    XML_Elem e;
    size_t i = 0;
    vector<char> data;

    bool flag = Inhale("test30.xml", data);
    UT_ASSERT(flag == true);

    UT_ASSERT(data.size() != 0);
    XML_Init(&xml);
    XML_SetText(&xml, &data[0]);

    while ((r = XML_Next(&xml, &e)) == 0)
    {
#if 0
        XML_Elem_Dump(&e);
#endif
        if (i == 1)
        {
            UT_ASSERT(strcmp(e.data, "{{{<a><b></b></a>}}}") == 0);
        }
        i++;
    }

    assert(i == 3);

    assert(r == 1);
}

static void Test31()
{
    char msg[1024];
    TestFile("test31.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "") == 0);
}

static void Test32()
{
    char msg[1024];
    TestFile("test32.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "29: error: open/close tag mismatch: a/b") == 0);
}

static void Test33()
{
    char msg[1024];
    TestFile("test33.xml", msg, sizeof(msg));
    UT_ASSERT(strcmp(msg, "28: error: expected attribute name") == 0);
}

static void RunTests()
{
    UT_TEST(Test0);
    UT_TEST(Test1);
    UT_TEST(Test2);
    UT_TEST(Test3);
    UT_TEST(Test4);
    UT_TEST(Test5);
    UT_TEST(Test6);
    UT_TEST(Test7);
    UT_TEST(Test8);
    UT_TEST(Test9);
    UT_TEST(Test10);
    UT_TEST(Test11);
    UT_TEST(Test12);
    UT_TEST(Test13);
    UT_TEST(Test14);
    UT_TEST(Test15);
    UT_TEST(Test16);
    UT_TEST(Test17);
    UT_TEST(Test18);
    UT_TEST(Test19);
    UT_TEST(Test20);
    UT_TEST(Test21);
    UT_TEST(Test22);
    UT_TEST(Test23);
    UT_TEST(Test24);
    UT_TEST(Test25);
    UT_TEST(Test26);
    UT_TEST(Test27);
    UT_TEST(Test28);
    UT_TEST(Test29);
    UT_TEST(Test30);
    UT_TEST(Test31);
    UT_TEST(Test32);
    UT_TEST(Test33);
}

UT_ENTRY_POINT(RunTests);

ViewCVS 0.9.2