proxygen
ShellTest.cpp
Go to the documentation of this file.
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 
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.
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 }
#define FOLLY_GNU_DISABLE_WARNING(warningName)
Definition: Portability.h:180
#define FOLLY_POP_WARNING
Definition: Portability.h:179
#define FOLLY_PUSH_WARNING
Definition: Portability.h:178
std::vector< std::string > shellify(StringPiece format, Arguments &&...arguments)
Definition: Shell.h:88
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::string shellQuote(StringPiece argument)
Definition: Shell.cpp:21
TEST(SequencedExecutor, CPUThreadPoolExecutor)