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 <ditto/close_file.h>
18 #include <ditto/syscall.h>
19
20 using ::dittosuite::SharedVariables;
21 using ::testing::_;
22 using ::testing::Return;
23
24 class CloseFileTest : public InstructionTest {
25 protected:
26 int fd_ = MockSyscall::kDefaultFileDescriptor;
27 int input_key_;
28
29 // Set input fd
SetUp()30 void SetUp() override {
31 InstructionTest::SetUp();
32 input_key_ = SharedVariables::GetKey(thread_ids, "test_input");
33 SharedVariables::Set(input_key_, fd_);
34 }
35 };
36
37 using CloseFileDeathTest = CloseFileTest;
38
TEST_F(CloseFileTest,ClosedFile)39 TEST_F(CloseFileTest, ClosedFile) {
40 EXPECT_CALL(syscall_, Close(fd_));
41
42 dittosuite::CloseFile instruction(syscall_, 1, input_key_);
43 instruction.Run();
44 }
45
TEST_F(CloseFileDeathTest,DiedDueToInvalidFd)46 TEST_F(CloseFileDeathTest, DiedDueToInvalidFd) {
47 SharedVariables::Set(input_key_, -1);
48 EXPECT_CALL(syscall_, Close(_)).WillRepeatedly(Return(-1));
49
50 dittosuite::CloseFile instruction(syscall_, 1, input_key_);
51 EXPECT_DEATH(instruction.Run(), _);
52 }
53