123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- // $Id$
- //
- // \file TestUtility.cpp
- //
- // Copyright (C) 2012, ARM Research Labs, LLC.
- // See www.armresearch.com for the copyright terms.
- //
- // This is the unit test for the class Utility.
- //
-
- #include <iostream>
- #include <sstream>
- #include <stdexcept>
-
- #include <cstdlib>
- #include <cstring>
- #include <cerrno>
-
- #include "Utility.hpp"
-
- /// Output error.
- #define Error(msg) \
- { \
- std::cerr << "In file " << __FILE__ << ":" << __LINE__ << ": "; \
- std::cerr << msg; \
- }
-
- /// Exit with error.
- #define ErrorExit(msg) \
- { \
- Error(msg) \
- std::exit(-1); \
- }
-
- bool
- TestReplaceXmlAttribute() {
-
- std::string Content;
- std::string ExpectedContent;
- std::string ElementName = "TestElement";
- std::string AttributeName = "TestAttribute";
- std::string AttributeNewValue = "NewValue";
-
- // Test with valid input.
- std::string OriginalContent;
-
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute='testmode' authentication='setuptestingonly'/>\n"
- "<TestXXX XXXAttribute='testmode' authentication='setuptestingonly'/>\n"
- "</snf>\n";
-
- OriginalContent = Content;
-
- ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute='NewValue' authentication='setuptestingonly'/>\n"
- "<TestXXX XXXAttribute='testmode' authentication='setuptestingonly'/>\n"
- "</snf>\n";
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
-
- } catch (std::exception &e) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
- Temp += e.what();
- Temp += "\n\nOriginal Content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- if (Content != ExpectedContent) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
- Temp += Content;
- Temp += "\n\nExpected:\n\n";
- Temp += ExpectedContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\n\t= \n\t\t 'testmode' authentication='setuptestingonly'>\n"
- "</TestElement>"
- "</snf>\n";
-
- OriginalContent = Content;
-
- ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\n\t= \n\t\t 'NewValue' authentication='setuptestingonly'>\n"
- "</TestElement>"
- "</snf>\n";
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
-
- } catch (std::exception &e) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
- Temp += e.what();
- Temp += "\n\nOriginal Content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- if (Content != ExpectedContent) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
- Temp += Content;
- Temp += "\n\nExpected:\n\n";
- Temp += ExpectedContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute=\"testmode\" TestXXAttribute='setuptestingonly'/>\n"
- "</snf>\n";
-
- OriginalContent = Content;
-
- ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute=\"NewValue\" TestXXAttribute='setuptestingonly'/>\n"
- "</snf>\n";
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
-
- } catch (std::exception &e) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
- Temp += e.what();
- Temp += "\n\nOriginal Content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- if (Content != ExpectedContent) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
- Temp += Content;
- Temp += "\n\nExpected:\n\n";
- Temp += ExpectedContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
- "</snf>\n";
-
- OriginalContent = Content;
-
- ExpectedContent = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\t\n= \"NewValue\" authentication='setuptestingonly'/>\n"
- "</snf>\n";
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
-
- } catch (std::exception &e) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception thrown with valid input: ";
- Temp += e.what();
- Temp += "\n\nOriginal Content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- if (Content != ExpectedContent) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
- Temp += Content;
- Temp += "\n\nExpected:\n\n";
- Temp += ExpectedContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- // Element in comment.
- Content = "<!-- License file created by SNFIdentity\n"
- "<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
- "-->\n"
- "<snf>\n"
- "<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
- "</snf>\n";
- OriginalContent = Content;
-
- ExpectedContent = "<!-- License file created by SNFIdentity\n"
- "<TestElement TestAttribute = \"testmode\" authentication='setuptestingonly'/>\n"
- "-->\n"
- "<snf>\n"
- "<TestElement TestAttribute = \"NewValue\" authentication='setuptestingonly'/>\n"
- "</snf>\n";
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
-
- } catch (std::exception &e) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Unexpected exception thrown with valid input: ";
- Temp += e.what();
- Temp += "\n\nOriginal Content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- if (Content != ExpectedContent) {
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Incorrect result with valid input. Content after replacement:\n\n";
- Temp += Content;
- Temp += "\n\nExpected:\n\n";
- Temp += ExpectedContent + "\n";
- Error(Temp);
- return false;
-
- }
-
- // Test with invalid input.
- // Duplicate element.
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
- "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'/>\n"
- "</snf>\n";
- OriginalContent = Content;
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception not thrown with two <TestElement> elements. Original content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- // Missing element.
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "</snf>\n";
- OriginalContent = Content;
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception not thrown with missing <TestElement> element. Original content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- // Malformed element.
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\t\n= \"testmode\" authentication='setuptestingonly'\n"
- "</snf>\n";
- OriginalContent = Content;
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed <TestElement> element. Original content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- // Duplicate attribute.
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\t\n= \"testmode\" TestAttribute='setuptestingonly'/>\n"
- "</snf>\n";
- OriginalContent = Content;
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception not thrown with two TestAttribute attributes. Original content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- // Attribute in another element
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement XXXTestAttribute='setuptestingonly'/>\n"
- "<ATestElement TestAttribute\t\n= \"testmode\"/>\n"
- "</snf>\n";
- OriginalContent = Content;
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception not thrown with TestAttribute attribute in another element. "
- "Original content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- // Missing attribute.
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement XXXTestAttribute\t\n= \"testmode\" YYYTestAttribute='setuptestingonly'/>\n"
- "</snf>\n";
- OriginalContent = Content;
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception not thrown with missing TestAttribute attribute. Original content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- // Malformed attribute value.
- Content = "<!-- License file created by SNFIdentity-->\n"
- "<snf>\n"
- "<TestElement TestAttribute\t\n= \"testmode' />\n"
- "</snf>\n";
- OriginalContent = Content;
-
- try {
-
- Utility::ReplaceXmlAttribute(&Content, ElementName, AttributeName, AttributeNewValue);
- std::string Temp;
-
- Temp = "TestReplaceXmlAttribute: Exception not thrown with malformed TestAttribute value. "
- "Original content:\n\n";
- Temp += OriginalContent + "\n";
- Error(Temp);
- return false;
-
- } catch (std::exception &e) {
-
- }
-
- return true;
-
- }
-
- /// Unit tests for Utility.
- //
- int main(int argc, char* argv[]) {
-
- try { // Catch anything that breaks loose.
-
- // Test ReplaceXmlAttribute().
- if (!TestReplaceXmlAttribute()) {
- ErrorExit("TestReplaceXmlAttribute() failure.\n");
- }
-
- } // That's all folks.
-
- catch(std::exception& e) { // Report any normal exceptions.
-
- std::cerr << "Utility exception: " << e.what() << std::endl;
- return -1;
-
- }
- catch(...) { // Report any unexpected exceptions.
-
- std::cerr << "Panic! Unknown Exception!" << std::endl;
- return -1;
-
- }
-
- return 0; // Normally we return zero.
- }
|