/src/mongo/util/text_test.cpp

https://github.com/tadmarshall/mongo · C++ · 81 lines · 57 code · 9 blank · 15 comment · 3 complexity · 5e6b63aeb67abec368fda55b781e564f 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 <cstdarg>
  17. #include <string>
  18. #include <vector>
  19. #include "mongo/unittest/unittest.h"
  20. #include "mongo/util/text.h"
  21. using namespace mongo;
  22. static std::vector<std::string> svec(const char* first, ...) {
  23. std::vector<std::string> result;
  24. if (first) {
  25. result.push_back(first);
  26. va_list ap;
  27. va_start(ap, first);
  28. const char* curr;
  29. while (NULL != (curr = va_arg(ap, const char*))) {
  30. result.push_back(curr);
  31. }
  32. va_end(ap);
  33. }
  34. return result;
  35. }
  36. TEST(WindowsCommandLineConstruction, EmptyCommandLine) {
  37. ASSERT_EQUALS("", constructUtf8WindowsCommandLine(svec(NULL)));
  38. }
  39. TEST(WindowsCommandLineConstruction, NothingToQuote) {
  40. ASSERT_EQUALS("abc d \"\" e",
  41. constructUtf8WindowsCommandLine(svec("abc", "d", "", "e", NULL)));
  42. }
  43. TEST(WindowsCommandLineConstruction, ThingsToQuote) {
  44. ASSERT_EQUALS("a\\\\\\b \"de fg\" h",
  45. constructUtf8WindowsCommandLine(svec("a\\\\\\b", "de fg", "h", NULL)));
  46. ASSERT_EQUALS("\"a\\\\b c\" d e",
  47. constructUtf8WindowsCommandLine(svec("a\\\\b c", "d" , "e", NULL)));
  48. ASSERT_EQUALS("\"a \\\\\" \\",
  49. constructUtf8WindowsCommandLine(svec("a \\", "\\", NULL)));
  50. ASSERT_EQUALS("\"\\\\\\\\\\\"\"",
  51. constructUtf8WindowsCommandLine(svec("\\\\\"", NULL)));
  52. }
  53. TEST(WindowsCommandLineConstruction, RegressionSERVER_7252) {
  54. ASSERT_EQUALS("mongod \"--serviceName=My Service\" --serviceDescription \"My Service\" "
  55. "--serviceDisplayName \"My Service\" --dbpath C:\\mongo\\data\\config "
  56. "--port 20001 --logpath C:\\mongo\\logs\\mongo_config.log.txt "
  57. "--configsvr --service",
  58. constructUtf8WindowsCommandLine(svec("mongod",
  59. "--serviceName=My Service",
  60. "--serviceDescription",
  61. "My Service",
  62. "--serviceDisplayName",
  63. "My Service",
  64. "--dbpath",
  65. "C:\\mongo\\data\\config",
  66. "--port", "20001",
  67. "--logpath",
  68. "C:\\mongo\\logs\\mongo_config.log.txt",
  69. "--configsvr",
  70. "--service",
  71. NULL)));
  72. }