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 | // Tests for the deprecated shellify() function. |
47 | // Don't warn about using this deprecated function in the test for it. |
48 | FOLLY_PUSH_WARNING |
49 | FOLLY_GNU_DISABLE_WARNING("-Wdeprecated-declarations" ) |
50 | TEST(Shell, Shellify_deprecated) { |
51 | auto command = shellify("rm -rf /" ); |
52 | EXPECT_EQ(command[0], "/bin/sh" ); |
53 | EXPECT_EQ(command[1], "-c" ); |
54 | EXPECT_EQ(command[2], "rm -rf /" ); |
55 | |
56 | command = shellify("rm -rf {}" , "someFile.txt" ); |
57 | EXPECT_EQ(command[2], "rm -rf 'someFile.txt'" ); |
58 | |
59 | command = shellify("rm -rf {}" , 5); |
60 | EXPECT_EQ(command[2], "rm -rf '5'" ); |
61 | |
62 | command = shellify("ls {}" , "blah'; rm -rf /" ); |
63 | EXPECT_EQ(command[2], "ls 'blah'\\''; rm -rf /'" ); |
64 | } |
65 | FOLLY_POP_WARNING |
66 | |