ShellTest.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2016-present Facebook, 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 <glog/logging.h>
  17. #include <folly/portability/GTest.h>
  18. #include <folly/system/Shell.h>
  19. using namespace folly;
  20. TEST(Shell, ShellQuote) {
  21. EXPECT_EQ(shellQuote("a"), "'a'");
  22. EXPECT_EQ(shellQuote("a'b"), "'a'\\''b'");
  23. EXPECT_EQ(shellQuote("a\"b"), "'a\"b'");
  24. }
  25. TEST(Shell, Shellify) {
  26. auto command = "rm -rf /"_shellify();
  27. EXPECT_EQ(command[0], "/bin/sh");
  28. EXPECT_EQ(command[1], "-c");
  29. EXPECT_EQ(command[2], "rm -rf /");
  30. command = "rm -rf {}"_shellify("someFile.txt");
  31. EXPECT_EQ(command[2], "rm -rf 'someFile.txt'");
  32. command = "rm -rf {}"_shellify(5);
  33. EXPECT_EQ(command[2], "rm -rf '5'");
  34. command = "ls {}"_shellify("blah'; rm -rf /");
  35. EXPECT_EQ(command[2], "ls 'blah'\\''; rm -rf /'");
  36. }
  37. // Tests for the deprecated shellify() function.
  38. // Don't warn about using this deprecated function in the test for it.
  39. FOLLY_PUSH_WARNING
  40. FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations")
  41. TEST(Shell, Shellify_deprecated) {
  42. auto command = shellify("rm -rf /");
  43. EXPECT_EQ(command[0], "/bin/sh");
  44. EXPECT_EQ(command[1], "-c");
  45. EXPECT_EQ(command[2], "rm -rf /");
  46. command = shellify("rm -rf {}", "someFile.txt");
  47. EXPECT_EQ(command[2], "rm -rf 'someFile.txt'");
  48. command = shellify("rm -rf {}", 5);
  49. EXPECT_EQ(command[2], "rm -rf '5'");
  50. command = shellify("ls {}", "blah'; rm -rf /");
  51. EXPECT_EQ(command[2], "ls 'blah'\\''; rm -rf /'");
  52. }
  53. FOLLY_POP_WARNING