1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "include/instruction_test.h"
16
17 #include <fcntl.h>
18 #include <sys/stat.h>
19
20 #include <ditto/read_directory.h>
21 #include <ditto/syscall.h>
22
23 class ReadDirectoryTest : public InstructionTest {
24 protected:
25 std::string directory_name = "test_directory";
26 std::string path = absolute_path + directory_name;
27 std::vector<std::string> files{path + "/test1", path + "/test2", path + "/test3"};
28
29 // Create folder with several files for testing
SetUp()30 void SetUp() override {
31 InstructionTest::SetUp();
32 ASSERT_NE(mkdir(path.c_str(), S_IRWXU), -1);
33 for (const auto& file : files) {
34 ASSERT_NE(open(file.c_str(), O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR), -1);
35 }
36 }
37 // Remove the folder and files that were created in SetUp()
TearDown()38 void TearDown() override {
39 for (const auto& file : files) {
40 ASSERT_NE(unlink(file.c_str()), -1);
41 }
42 ASSERT_NE(rmdir(path.c_str()), -1);
43 }
44 };
45
TEST_F(ReadDirectoryTest,ReadDirectoryTestRun)46 TEST_F(ReadDirectoryTest, ReadDirectoryTestRun) {
47 auto output_key = dittosuite::SharedVariables::GetKey(thread_ids, "file_list");
48
49 dittosuite::ReadDirectory instruction(dittosuite::Syscall::GetSyscall(), 1, directory_name,
50 output_key);
51 instruction.Run();
52
53 auto output = std::get<std::vector<std::string>>(dittosuite::SharedVariables::Get(output_key));
54 sort(output.begin(), output.end());
55
56 ASSERT_EQ(output, files);
57 }
58