123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- // Copyright (C) 2013 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 <exceptions/exceptions.h>
- #include <test_data_files_config.h>
- #include <user_file.h>
- #include <boost/function.hpp>
- #include <boost/bind.hpp>
- #include <boost/shared_ptr.hpp>
- #include <gtest/gtest.h>
- using namespace std;
- using namespace user_chk;
- namespace {
- /// @brief Convenience method for reliably building test file path names.
- ///
- /// Function prefixes the given file name with a path to unit tests directory
- /// so we can reliably find test data files.
- ///
- /// @param name base file name of the test file
- std::string testFilePath(const std::string& name) {
- return (std::string(USER_CHK_TEST_DIR) + "/" + name);
- }
- /// @brief Tests the UserFile constructor.
- TEST(UserFile, construction) {
- // Verify that a UserFile with no file name is rejected.
- ASSERT_THROW(UserFile(""), UserFileError);
- // Verify that a UserFile with a non-blank file name is accepted.
- ASSERT_NO_THROW(UserFile("someName"));
- }
- /// @brief Tests opening and closing UserFile
- TEST(UserFile, openFile) {
- UserFilePtr user_file;
- // Construct a user file that refers to a non existant file.
- ASSERT_NO_THROW(user_file.reset(new UserFile("NoSuchFile")));
- EXPECT_FALSE(user_file->isOpen());
- // Verify a non-existant file fails to open
- ASSERT_THROW(user_file->open(), UserFileError);
- EXPECT_FALSE(user_file->isOpen());
- // Construct a user file that should exist.
- ASSERT_NO_THROW(user_file.reset(new UserFile
- (testFilePath("test_users_1.txt"))));
- // File should not be open.
- EXPECT_FALSE(user_file->isOpen());
- // Verify that we can open it.
- ASSERT_NO_THROW(user_file->open());
- EXPECT_TRUE(user_file->isOpen());
- // Verify that we cannot open an already open file.
- ASSERT_THROW(user_file->open(), UserFileError);
- // Verifyt we can close it.
- ASSERT_NO_THROW(user_file->close());
- EXPECT_FALSE(user_file->isOpen());
- // Verify that we can reopen it.
- ASSERT_NO_THROW(user_file->open());
- EXPECT_TRUE(user_file->isOpen());
- }
- /// @brief Tests makeUser with invalid user strings
- TEST(UserFile, makeUser) {
- const char* invalid_strs[]= {
- // Missinge type element.
- "{ \"id\" : \"01AC00F03344\" }",
- // Invalid id type string value.
- "{ \"type\" : \"BOGUS\", \"id\" : \"01AC00F03344\"}",
- // Non-string id type
- "{ \"type\" : 1, \"id\" : \"01AC00F03344\"}",
- // Missing id element.
- "{ \"type\" : \"HW_ADDR\" }",
- // Odd number of digits in id value.
- "{ \"type\" : \"HW_ADDR\", \"id\" : \"1AC00F03344\"}",
- // Invalid characters in id value.
- "{ \"type\" : \"HW_ADDR\", \"id\" : \"THIS IS BAD!\"}",
- // Empty id value.
- "{ \"type\" : \"HW_ADDR\", \"id\" : \"\"}",
- // Non-string id.
- "{ \"type\" : \"HW_ADDR\", \"id\" : 01AC00F03344 }",
- // Option with non-string value
- "{ \"type\" : \"HW_ADDR\", \"id\" : \"01AC00F03344\", \"opt\" : 4 }",
- NULL
- };
- // Create a UseFile to work with.
- UserFilePtr user_file;
- ASSERT_NO_THROW(user_file.reset(new UserFile("noFile")));
- // Iterate over the list of invalid user strings and verify
- // each one fails.
- const char** tmp = invalid_strs;;
- while (*tmp) {
- EXPECT_THROW(user_file->makeUser(*tmp), UserFileError)
- << "Invalid str not caught: ["
- << *tmp << "]" << std::endl;
- ++tmp;
- }
- }
- /// @brief Test reading from UserFile
- TEST(UserFile, readFile) {
- UserFilePtr user_file;
- // Construct and then open a known file.
- ASSERT_NO_THROW(user_file.reset(new UserFile
- (testFilePath("test_users_1.txt"))));
- ASSERT_NO_THROW(user_file->open());
- EXPECT_TRUE(user_file->isOpen());
- // File should contain four valid users. Read and verify each.
- UserPtr user;
- int i = 0;
- do {
- ASSERT_NO_THROW(user = user_file->readNextUser());
- switch (i++) {
- case 0:
- EXPECT_EQ(UserId::HW_ADDRESS, user->getUserId().getType());
- EXPECT_EQ("01ac00f03344", user->getUserId().toText());
- EXPECT_EQ("true", user->getProperty("opt1"));
- break;
- case 1:
- // File entry should have colons in id.
- EXPECT_EQ(UserId::HW_ADDRESS, user->getUserId().getType());
- EXPECT_EQ("01ac00f03345", user->getUserId().toText());
- EXPECT_EQ("true", user->getProperty("opt1"));
- break;
- case 2:
- EXPECT_EQ(UserId::DUID, user->getUserId().getType());
- EXPECT_EQ("225060de0a0b", user->getUserId().toText());
- EXPECT_EQ("true", user->getProperty("opt1"));
- break;
- case 3:
- // File entry should have colons in id.
- EXPECT_EQ(UserId::DUID, user->getUserId().getType());
- EXPECT_EQ("225060de0a0c", user->getUserId().toText());
- EXPECT_EQ("true", user->getProperty("opt1"));
- break;
- default:
- // Third time around, we are at EOF User should be null.
- ASSERT_FALSE(user);
- break;
- }
- } while (user);
- ASSERT_NO_THROW(user_file->close());
- }
- } // end of anonymous namespace
|