common_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2011 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 <gtest/gtest.h>
  15. #include <auth/common.h>
  16. #include <auth/spec_config.h>
  17. #include <vector>
  18. #include <string>
  19. #include <cstdio>
  20. #include <boost/foreach.hpp>
  21. using std::pair;
  22. using std::vector;
  23. using std::string;
  24. namespace {
  25. class Paths : public ::testing::Test {
  26. private:
  27. typedef pair<string, string*> Environ;
  28. vector<Environ> restoreEnviron;
  29. public:
  30. void TearDown() {
  31. // Restore the original environment
  32. BOOST_FOREACH(const Environ &env, restoreEnviron) {
  33. if (env.second == NULL) {
  34. EXPECT_EQ(0, unsetenv(env.first.c_str())) <<
  35. "Couldn't restore environment, results of other tests "
  36. "are uncertain";
  37. } else {
  38. EXPECT_EQ(0, setenv(env.first.c_str(), env.second->c_str(),
  39. 1)) << "Couldn't restore environment, "
  40. "results of other tests are uncertain";
  41. }
  42. }
  43. }
  44. protected:
  45. // Sets a temporary value into environment. If value is empty, it deletes
  46. // the variable from environment (just for simplicity).
  47. void setEnv(const string& name, const string& value) {
  48. // Backup the original environment
  49. char* env(getenv(name.c_str()));
  50. restoreEnviron.push_back(Environ(name, env == NULL ? NULL :
  51. new string(env)));
  52. // Set the new value
  53. if (value.empty()) {
  54. EXPECT_EQ(0, unsetenv(name.c_str()));
  55. } else {
  56. EXPECT_EQ(0, setenv(name.c_str(), value.c_str(), 1));
  57. }
  58. }
  59. // Test getter functions for a socket file path under given environment
  60. void testSocketPath(const string& fromBuild, const string& localStateDir,
  61. const string& socketFile, const string& env_name,
  62. const string& expected, string (*actual_fn)())
  63. {
  64. setEnv("B10_FROM_BUILD", fromBuild);
  65. setEnv("B10_FROM_SOURCE_LOCALSTATEDIR", localStateDir);
  66. setEnv(env_name, socketFile);
  67. EXPECT_EQ(expected, actual_fn());
  68. }
  69. };
  70. // Test that when we have no special environment, we get the default from prefix
  71. TEST_F(Paths, xfroutNoEnv) {
  72. testSocketPath("", "", "", "BIND10_XFROUT_SOCKET_FILE",
  73. UNIX_XFROUT_SOCKET_FILE, getXfroutSocketPath);
  74. }
  75. TEST_F(Paths, ddnsNoEnv) {
  76. testSocketPath("", "", "", "BIND10_DDNS_SOCKET_FILE",
  77. UNIX_DDNS_SOCKET_FILE, getDDNSSocketPath);
  78. }
  79. // Override by B10_FROM_BUILD
  80. TEST_F(Paths, xfroutFromBuild) {
  81. testSocketPath("/from/build", "", "/wrong/path",
  82. "BIND10_XFROUT_SOCKET_FILE", "/from/build/auth_xfrout_conn",
  83. getXfroutSocketPath);
  84. }
  85. TEST_F(Paths, ddnsFromBuild) {
  86. testSocketPath("/from/build", "", "/wrong/path", "BIND10_DDNS_SOCKET_FILE",
  87. "/from/build/ddns_socket", getDDNSSocketPath);
  88. }
  89. // Override by B10_FROM_SOURCE_LOCALSTATEDIR
  90. TEST_F(Paths, xfroutLocalStatedir) {
  91. testSocketPath("/wrong/path", "/state/dir", "/wrong/path",
  92. "BIND10_XFROUT_SOCKET_FILE", "/state/dir/auth_xfrout_conn",
  93. getXfroutSocketPath);
  94. }
  95. TEST_F(Paths, ddnsLocalStatedir) {
  96. testSocketPath("/wrong/path", "/state/dir", "/wrong/path",
  97. "BIND10_DDNS_SOCKET_FILE", "/state/dir/ddns_socket",
  98. getDDNSSocketPath);
  99. }
  100. // Override by BIND10_xxx_SOCKET_FILE explicitly
  101. TEST_F(Paths, xfroutFromEnv) {
  102. testSocketPath("", "", "/the/path/to/file", "BIND10_XFROUT_SOCKET_FILE",
  103. "/the/path/to/file", getXfroutSocketPath);
  104. }
  105. TEST_F(Paths, ddnsFromEnv) {
  106. testSocketPath("", "", "/the/path/to/file", "BIND10_DDNS_SOCKET_FILE",
  107. "/the/path/to/file", getDDNSSocketPath);
  108. }
  109. }