f43b7fc4aadab1afb4c73ca734f366987a74f2e8
[folly.git] / folly / system / test / ShellTest.cpp
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
17 #include <glog/logging.h>
18
19 #include <folly/portability/GTest.h>
20 #include <folly/system/Shell.h>
21
22 using namespace folly;
23
24 TEST(Shell, ShellQuote) {
25   EXPECT_EQ(shellQuote("a"), "'a'");
26   EXPECT_EQ(shellQuote("a'b"), "'a'\\''b'");
27   EXPECT_EQ(shellQuote("a\"b"), "'a\"b'");
28 }
29
30 TEST(Shell, Shellify) {
31   auto command = "rm -rf /"_shellify();
32   EXPECT_EQ(command[0], "/bin/sh");
33   EXPECT_EQ(command[1], "-c");
34   EXPECT_EQ(command[2], "rm -rf /");
35
36   command = "rm -rf {}"_shellify("someFile.txt");
37   EXPECT_EQ(command[2], "rm -rf 'someFile.txt'");
38
39   command = "rm -rf {}"_shellify(5);
40   EXPECT_EQ(command[2], "rm -rf '5'");
41
42   command = "ls {}"_shellify("blah'; rm -rf /");
43   EXPECT_EQ(command[2], "ls 'blah'\\''; rm -rf /'");
44 }
45
46 TEST(Shell, Shellify_deprecated) {
47   auto command = shellify("rm -rf /");
48   EXPECT_EQ(command[0], "/bin/sh");
49   EXPECT_EQ(command[1], "-c");
50   EXPECT_EQ(command[2], "rm -rf /");
51
52   command = shellify("rm -rf {}", "someFile.txt");
53   EXPECT_EQ(command[2], "rm -rf 'someFile.txt'");
54
55   command = shellify("rm -rf {}", 5);
56   EXPECT_EQ(command[2], "rm -rf '5'");
57
58   command = shellify("ls {}", "blah'; rm -rf /");
59   EXPECT_EQ(command[2], "ls 'blah'\\''; rm -rf /'");
60 }