user_file_unittests.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include <test_data_files_config.h>
  16. #include <user_file.h>
  17. #include <boost/function.hpp>
  18. #include <boost/bind.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <gtest/gtest.h>
  21. using namespace std;
  22. using namespace user_chk;
  23. namespace {
  24. /// @brief Convenience method for reliably building test file path names.
  25. ///
  26. /// Function prefixes the given file name with a path to unit tests directory
  27. /// so we can reliably find test data files.
  28. ///
  29. /// @param name base file name of the test file
  30. std::string testFilePath(const std::string& name) {
  31. return (std::string(USER_CHK_TEST_DIR) + "/" + name);
  32. }
  33. /// @brief Tests the UserFile constructor.
  34. TEST(UserFile, construction) {
  35. // Verify that a UserFile with no file name is rejected.
  36. ASSERT_THROW(UserFile(""), UserFileError);
  37. // Verify that a UserFile with a non-blank file name is accepted.
  38. ASSERT_NO_THROW(UserFile("someName"));
  39. }
  40. /// @brief Tests opening and closing UserFile
  41. TEST(UserFile, openFile) {
  42. UserFilePtr user_file;
  43. // Construct a user file that refers to a non existant file.
  44. ASSERT_NO_THROW(user_file.reset(new UserFile("NoSuchFile")));
  45. EXPECT_FALSE(user_file->isOpen());
  46. // Verify a non-existant file fails to open
  47. ASSERT_THROW(user_file->open(), UserFileError);
  48. EXPECT_FALSE(user_file->isOpen());
  49. // Construct a user file that should exist.
  50. ASSERT_NO_THROW(user_file.reset(new UserFile
  51. (testFilePath("test_users_1.txt"))));
  52. // File should not be open.
  53. EXPECT_FALSE(user_file->isOpen());
  54. // Verify that we can open it.
  55. ASSERT_NO_THROW(user_file->open());
  56. EXPECT_TRUE(user_file->isOpen());
  57. // Verify that we cannot open an already open file.
  58. ASSERT_THROW(user_file->open(), UserFileError);
  59. // Verifyt we can close it.
  60. ASSERT_NO_THROW(user_file->close());
  61. EXPECT_FALSE(user_file->isOpen());
  62. // Verify that we can reopen it.
  63. ASSERT_NO_THROW(user_file->open());
  64. EXPECT_TRUE(user_file->isOpen());
  65. }
  66. /// @brief Tests makeUser with invalid user strings
  67. TEST(UserFile, makeUser) {
  68. const char* invalid_strs[]= {
  69. // Missinge type element.
  70. "{ \"id\" : \"01AC00F03344\" }",
  71. // Invalid id type string value.
  72. "{ \"type\" : \"BOGUS\", \"id\" : \"01AC00F03344\"}",
  73. // Non-string id type
  74. "{ \"type\" : 1, \"id\" : \"01AC00F03344\"}",
  75. // Missing id element.
  76. "{ \"type\" : \"HW_ADDR\" }",
  77. // Odd number of digits in id value.
  78. "{ \"type\" : \"HW_ADDR\", \"id\" : \"1AC00F03344\"}",
  79. // Invalid characters in id value.
  80. "{ \"type\" : \"HW_ADDR\", \"id\" : \"THIS IS BAD!\"}",
  81. // Empty id value.
  82. "{ \"type\" : \"HW_ADDR\", \"id\" : \"\"}",
  83. // Non-string id.
  84. "{ \"type\" : \"HW_ADDR\", \"id\" : 01AC00F03344 }",
  85. // Option with non-string value
  86. "{ \"type\" : \"HW_ADDR\", \"id\" : \"01AC00F03344\", \"opt\" : 4 }",
  87. NULL
  88. };
  89. // Create a UseFile to work with.
  90. UserFilePtr user_file;
  91. ASSERT_NO_THROW(user_file.reset(new UserFile("noFile")));
  92. // Iterate over the list of invalid user strings and verify
  93. // each one fails.
  94. const char** tmp = invalid_strs;;
  95. while (*tmp) {
  96. EXPECT_THROW(user_file->makeUser(*tmp), UserFileError)
  97. << "Invalid str not caught: ["
  98. << *tmp << "]" << std::endl;
  99. ++tmp;
  100. }
  101. }
  102. /// @brief Test reading from UserFile
  103. TEST(UserFile, readFile) {
  104. UserFilePtr user_file;
  105. // Construct and then open a known file.
  106. ASSERT_NO_THROW(user_file.reset(new UserFile
  107. (testFilePath("test_users_1.txt"))));
  108. ASSERT_NO_THROW(user_file->open());
  109. EXPECT_TRUE(user_file->isOpen());
  110. // File should contain four valid users. Read and verify each.
  111. UserPtr user;
  112. int i = 0;
  113. do {
  114. ASSERT_NO_THROW(user = user_file->readNextUser());
  115. switch (i++) {
  116. case 0:
  117. EXPECT_EQ(UserId::HW_ADDRESS, user->getUserId().getType());
  118. EXPECT_EQ("01ac00f03344", user->getUserId().toText());
  119. EXPECT_EQ("true", user->getProperty("opt1"));
  120. break;
  121. case 1:
  122. // File entry should have colons in id.
  123. EXPECT_EQ(UserId::HW_ADDRESS, user->getUserId().getType());
  124. EXPECT_EQ("01ac00f03345", user->getUserId().toText());
  125. EXPECT_EQ("true", user->getProperty("opt1"));
  126. break;
  127. case 2:
  128. EXPECT_EQ(UserId::DUID, user->getUserId().getType());
  129. EXPECT_EQ("225060de0a0b", user->getUserId().toText());
  130. EXPECT_EQ("true", user->getProperty("opt1"));
  131. break;
  132. case 3:
  133. // File entry should have colons in id.
  134. EXPECT_EQ(UserId::DUID, user->getUserId().getType());
  135. EXPECT_EQ("225060de0a0c", user->getUserId().toText());
  136. EXPECT_EQ("true", user->getProperty("opt1"));
  137. break;
  138. default:
  139. // Third time around, we are at EOF User should be null.
  140. ASSERT_FALSE(user);
  141. break;
  142. }
  143. } while (user);
  144. ASSERT_NO_THROW(user_file->close());
  145. }
  146. } // end of anonymous namespace