/src/mongo/util/ntservice_test.cpp

https://github.com/tadmarshall/mongo · C++ · 110 lines · 76 code · 13 blank · 21 comment · 5 complexity · ec7bf207c4958520ba661f2fc65654b9 MD5 · raw file

  1. /*
  2. * Copyright 2012 10gen Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "mongo/platform/basic.h"
  17. #include <cstdarg>
  18. #include <cstdlib>
  19. #include <shellapi.h>
  20. #include <string>
  21. #include <vector>
  22. #include "mongo/db/client.h"
  23. #include "mongo/unittest/unittest.h"
  24. #include "mongo/util/ntservice.h"
  25. #include "mongo/util/text.h"
  26. using namespace mongo;
  27. static std::vector<std::string> svec(const char* first, ...) {
  28. std::vector<std::string> result;
  29. if (first) {
  30. result.push_back(first);
  31. va_list ap;
  32. va_start(ap, first);
  33. const char* curr;
  34. while (NULL != (curr = va_arg(ap, const char*))) {
  35. result.push_back(curr);
  36. }
  37. va_end(ap);
  38. }
  39. return result;
  40. }
  41. TEST(NtService, ConstructServiceCommandLine) {
  42. ASSERT_TRUE(svec("--dbpath=C:\\Data\\",
  43. "-logpath",
  44. "C:\\Program Files (x86)\\MongoDB\\Logs\\MongoDB.log",
  45. "--service",
  46. NULL) ==
  47. ntservice::constructServiceArgv(
  48. svec("-service", "--service",
  49. "--dbpath=C:\\Data\\",
  50. "--install", "-install",
  51. "--reinstall", "-reinstall",
  52. "--servicePassword==a\\b\\",
  53. "--servicePassword", "=a\\b\\",
  54. "--serviceUser", "andy",
  55. "--serviceName", "MongoDB",
  56. "-servicePassword==a\\b\\",
  57. "-servicePassword", "=a\\b\\",
  58. "-serviceUser", "andy",
  59. "-serviceName", "MongoDB",
  60. "-logpath",
  61. "C:\\Program Files (x86)\\MongoDB\\Logs\\MongoDB.log",
  62. NULL)));
  63. }
  64. TEST(NtService, RegressionSERVER_7252) {
  65. // Test that we generate a correct service command line from the literal command line supplied
  66. // in ticket SERVER-7252.
  67. const wchar_t inputCommandLine[] =
  68. L"mongod --install --serviceName=\"My Service\" --serviceDescription \"My Service\" "
  69. L"--serviceDisplayName \"My Service\" --dbpath C:\\mongo\\data\\config --port 20001 "
  70. L"--logpath C:\\mongo\\logs\\mongo_config.log.txt --configsvr";
  71. const char expectedServiceCommandLine[] =
  72. "mongod --dbpath C:\\mongo\\data\\config --port 20001 "
  73. "--logpath C:\\mongo\\logs\\mongo_config.log.txt --configsvr --service";
  74. // Convert the input wide-character command line into a UTF-8 vector of std::string.
  75. int inputArgc;
  76. LPWSTR* inputArgvWide = CommandLineToArgvW(inputCommandLine, &inputArgc);
  77. ASSERT_TRUE(NULL != inputArgvWide);
  78. ASSERT_GREATER_THAN_OR_EQUALS(inputArgc, 0);
  79. std::vector<std::string> inputArgvUtf8(inputArgc);
  80. ASSERT_TRUE(inputArgvUtf8.end() == std::transform(inputArgvWide,
  81. inputArgvWide + inputArgc,
  82. inputArgvUtf8.begin(),
  83. toUtf8String));
  84. LocalFree(inputArgvWide);
  85. // Finally, confirm that we properly transform the argument vector and from it construct a legit
  86. // service command line.
  87. ASSERT_EQUALS(expectedServiceCommandLine,
  88. constructUtf8WindowsCommandLine(ntservice::constructServiceArgv(inputArgvUtf8)));
  89. }
  90. // CRUTCHES!
  91. namespace mongo {
  92. enum ExitCode;
  93. void exitCleanly(ExitCode ignored) { std::abort(); }
  94. Client& Client::initThread(const char* desc, AbstractMessagingPort* mp) {
  95. std::abort(); return *reinterpret_cast<Client*>(NULL);
  96. }
  97. } // namespace mongo