Commit e22700fc authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[trac813] added some test utilities for tests that will be added soon.

parent 98d147ea
......@@ -12,6 +12,8 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <fstream>
#include <boost/scoped_ptr.hpp>
#include <exceptions/exceptions.h>
......@@ -19,6 +21,9 @@
#include <util/buffer.h>
#include <util/time_utilities.h>
#include <util/unittests/testdata.h>
#include <util/unittests/textdata.h>
#include <dns/edns.h>
#include <dns/exceptions.h>
#include <dns/message.h>
......@@ -675,6 +680,15 @@ TEST_F(MessageTest, toWireWithoutRcode) {
EXPECT_THROW(message_render.toWire(renderer), InvalidMessageOperation);
}
TEST_F(MessageTest, toText) {
ifstream ifs;
unittests::openTestData("message_toText1.txt", ifs);
factoryFromFile(message_parse, "message_toText1.wire");
unittests::matchTextData(ifs, message_parse.toText());
}
TEST_F(MessageTest, toTextWithoutOpcode) {
message_render.setRcode(Rcode::NOERROR());
EXPECT_THROW(message_render.toText(), InvalidMessageOperation);
......
......@@ -14,13 +14,16 @@
#include <gtest/gtest.h>
#include <util/unittests/testdata.h>
#include <dns/tests/unittest_util.h>
int
main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);
isc::UnitTestUtil::addDataPath(TEST_DATA_SRCDIR);
isc::util::unittests::addTestDataPath(TEST_DATA_SRCDIR);
isc::UnitTestUtil::addDataPath(TEST_DATA_BUILDDIR);
isc::util::unittests::addTestDataPath(TEST_DATA_BUILDDIR);
return (RUN_ALL_TESTS());
}
......@@ -7,6 +7,7 @@ BUILT_SOURCES += message_fromWire12.wire message_fromWire13.wire
BUILT_SOURCES += message_fromWire14.wire message_fromWire15.wire
BUILT_SOURCES += message_fromWire16.wire
BUILT_SOURCES += message_toWire2.wire message_toWire3.wire
BUILT_SOURCES += message_toText1.wire
BUILT_SOURCES += name_toWire5.wire name_toWire6.wire
BUILT_SOURCES += rdatafields1.wire rdatafields2.wire rdatafields3.wire
BUILT_SOURCES += rdatafields4.wire rdatafields5.wire rdatafields6.wire
......@@ -54,6 +55,7 @@ EXTRA_DIST += message_fromWire11.spec message_fromWire12.spec
EXTRA_DIST += message_fromWire13.spec message_fromWire14.spec
EXTRA_DIST += message_fromWire15.spec message_fromWire16.spec
EXTRA_DIST += message_toWire1 message_toWire2.spec message_toWire3.spec
EXTRA_DIST += message_toText1.txt message_toText1.spec
EXTRA_DIST += name_fromWire1 name_fromWire2 name_fromWire3_1 name_fromWire3_2
EXTRA_DIST += name_fromWire4 name_fromWire6 name_fromWire7 name_fromWire8
EXTRA_DIST += name_fromWire9 name_fromWire10 name_fromWire11 name_fromWire12
......
......@@ -4,5 +4,7 @@ AM_CXXFLAGS = $(B10_CXXFLAGS)
lib_LTLIBRARIES = libutil_unittests.la
libutil_unittests_la_SOURCES = fork.h fork.cc resolver.h
libutil_unittests_la_SOURCES += newhook.h newhook.cc
libutil_unittests_la_SOURCES += testdata.h testdata.cc
libutil_unittests_la_SOURCES += textdata.h
CLEANFILES = *.gcno *.gcda
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <string>
#include <stdexcept>
#include <fstream>
#include <vector>
#include "testdata.h"
using namespace std;
namespace {
vector<string>&
getDataPaths() {
static vector<string> data_path;
return (data_path);
}
}
namespace isc {
namespace util {
namespace unittests {
void
addTestDataPath(const string& path) {
getDataPaths().push_back(path);
}
void
openTestData(const char* const datafile, ifstream& ifs) {
vector<string>::const_iterator it = getDataPaths().begin();
for (; it != getDataPaths().end(); ++it) {
string data_path = *it;
if (data_path.empty() || *data_path.rbegin() != '/') {
data_path.push_back('/');
}
ifs.open((data_path + datafile).c_str(), ios_base::in);
if (!ifs.fail()) {
return;
}
}
throw runtime_error("failed to open data file in data paths: " +
string(datafile));
}
}
}
}
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#ifndef __UTIL_UNITTESTS_TESTDATA_H
#define __UTIL_UNITTESTS_TESTDATA_H 1
/**
* @file testdata.h
* @short Manipulating test data files.
*
* This utility defines functions that help test case handle test data
* stored in a file.
*/
namespace isc {
namespace util {
namespace unittests {
/// Add a path (directory) that \c openTestData() will search for test data
/// files.
void addTestDataPath(const std::string& path);
/// Open a file specified by 'datafile' using the data paths registered via
/// addTestDataPath(). On success, ifs will be ready for reading the data
/// stored in 'datafile'. If the data file cannot be open with any of the
/// registered paths, a runtime_error exception will be thrown.
void openTestData(const char* const datafile, std::ifstream& ifs);
}
}
}
#endif // __UTIL_UNITTESTS_TESTDATA_H
// Local Variables:
// mode: c++
// End:
// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <istream>
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#ifndef __UTIL_UNITTESTS_TEXTDATA_H
#define __UTIL_UNITTESTS_TEXTDATA_H 1
/**
* @file textdata.h
* @short Utilities for tests with text data.
*
* This utility provides convenient helper functions for unit tests using
* textual data.
*/
namespace isc {
namespace util {
namespace unittests {
/// Line-by-line text comparison.
///
/// This templated function takes two standard input streams, extracts
/// strings from them, and compares the two sets of strings line by line.
template <typename EXPECTED_STREAM, typename ACTUAL_STREAM>
void
matchTextData(EXPECTED_STREAM& expected, ACTUAL_STREAM& actual) {
std::string actual_line;
std::string expected_line;
while (std::getline(actual, actual_line), !actual.eof()) {
std::getline(expected, expected_line);
if (expected.eof()) {
ASSERT_FALSE(true) << "Redundant line in actual output: "
<< actual_line;
break;
}
if (actual.bad() || actual.fail() ||
expected.bad() || expected.fail()) {
throw std::runtime_error("Unexpected error in data streams");
}
EXPECT_EQ(expected_line, actual_line);
}
while (std::getline(expected, expected_line), !expected.eof()) {
ASSERT_FALSE(true) << "Missing line in actual output: "
<< expected_line;
}
}
/// Similar to the fully templated version, but takes string for the second
/// (actual) data.
///
/// Due to the nature of textual data, it will often be the case that test
/// data is given as a string object. This shortcut version helps such cases
/// so that the test code doesn't have to create a string stream with the
/// string data just for testing.
template <typename EXPECTED_STREAM>
void
matchTextData(EXPECTED_STREAM& expected, const std::string& actual_text) {
std::istringstream iss(actual_text);
matchTextData(expected, iss);
}
/// Same for the previous version, but the first argument is string.
template <typename ACTUAL_STREAM>
void
matchTextData(const std::string& expected_text, ACTUAL_STREAM& actual) {
std::istringstream iss(expected_text);
matchTextData(iss, actual);
}
/// Same for the previous two, but takes strings for both expected and
/// actual data.
void
matchTextData(const std::string& expected_text,
const std::string& actual_text)
{
std::istringstream expected_is(expected_text);
std::istringstream actual_is(actual_text);
matchTextData(expected_is, actual_is);
}
}
}
}
#endif // __UTIL_UNITTESTS_TEXTDATA_H
// Local Variables:
// mode: c++
// End:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment