/src/mongo/util/text_test.cpp

https://github.com/paralect/mongo · C++ · 92 lines · 56 code · 8 blank · 28 comment · 3 complexity · 6e0ce37df1563bd031c726f52c67c97b MD5 · raw file

  1. /**
  2. * Copyright (C) 2018-present MongoDB, Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the Server Side Public License, version 1,
  6. * as published by MongoDB, Inc.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * Server Side Public License for more details.
  12. *
  13. * You should have received a copy of the Server Side Public License
  14. * along with this program. If not, see
  15. * <http://www.mongodb.com/licensing/server-side-public-license>.
  16. *
  17. * As a special exception, the copyright holders give permission to link the
  18. * code of portions of this program with the OpenSSL library under certain
  19. * conditions as described in each individual source file and distribute
  20. * linked combinations including the program with the OpenSSL library. You
  21. * must comply with the Server Side Public License in all respects for
  22. * all of the code used other than as permitted herein. If you modify file(s)
  23. * with this exception, you may extend this exception to your version of the
  24. * file(s), but you are not obligated to do so. If you do not wish to do so,
  25. * delete this exception statement from your version. If you delete this
  26. * exception statement from all source files in the program, then also delete
  27. * it in the license file.
  28. */
  29. #include <cstdarg>
  30. #include <string>
  31. #include <vector>
  32. #include "mongo/unittest/unittest.h"
  33. #include "mongo/util/text.h"
  34. using namespace mongo;
  35. static std::vector<std::string> svec(const char* first, ...) {
  36. std::vector<std::string> result;
  37. if (first) {
  38. result.push_back(first);
  39. va_list ap;
  40. va_start(ap, first);
  41. const char* curr;
  42. while (NULL != (curr = va_arg(ap, const char*))) {
  43. result.push_back(curr);
  44. }
  45. va_end(ap);
  46. }
  47. return result;
  48. }
  49. TEST(WindowsCommandLineConstruction, EmptyCommandLine) {
  50. ASSERT_EQUALS("", constructUtf8WindowsCommandLine(svec(NULL)));
  51. }
  52. TEST(WindowsCommandLineConstruction, NothingToQuote) {
  53. ASSERT_EQUALS("abc d \"\" e", constructUtf8WindowsCommandLine(svec("abc", "d", "", "e", NULL)));
  54. }
  55. TEST(WindowsCommandLineConstruction, ThingsToQuote) {
  56. ASSERT_EQUALS("a\\\\\\b \"de fg\" h",
  57. constructUtf8WindowsCommandLine(svec("a\\\\\\b", "de fg", "h", NULL)));
  58. ASSERT_EQUALS("\"a\\\\b c\" d e",
  59. constructUtf8WindowsCommandLine(svec("a\\\\b c", "d", "e", NULL)));
  60. ASSERT_EQUALS("\"a \\\\\" \\", constructUtf8WindowsCommandLine(svec("a \\", "\\", NULL)));
  61. ASSERT_EQUALS("\"\\\\\\\\\\\"\"", constructUtf8WindowsCommandLine(svec("\\\\\"", NULL)));
  62. }
  63. TEST(WindowsCommandLineConstruction, RegressionSERVER_7252) {
  64. ASSERT_EQUALS(
  65. "mongod \"--serviceName=My Service\" --serviceDescription \"My Service\" "
  66. "--serviceDisplayName \"My Service\" --dbpath C:\\mongo\\data\\config "
  67. "--port 20001 --logpath C:\\mongo\\logs\\mongo_config.log.txt "
  68. "--configsvr --service",
  69. constructUtf8WindowsCommandLine(svec("mongod",
  70. "--serviceName=My Service",
  71. "--serviceDescription",
  72. "My Service",
  73. "--serviceDisplayName",
  74. "My Service",
  75. "--dbpath",
  76. "C:\\mongo\\data\\config",
  77. "--port",
  78. "20001",
  79. "--logpath",
  80. "C:\\mongo\\logs\\mongo_config.log.txt",
  81. "--configsvr",
  82. "--service",
  83. NULL)));
  84. }