2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <type_traits>
19 #include <gtest/gtest.h>
20 #include <gmock/gmock.h>
21 #include "private/filedescriptor.hpp"
22 #include "private/tst/systemmock.hpp"
24 using namespace shareddatalayer;
25 using namespace shareddatalayer::tst;
26 using namespace testing;
30 class FileDescriptorTest: public Test
33 SystemMock systemMock;
34 std::function<void(int)> atCloseCb = [&](int fd) { delFD(fd); };
36 MOCK_METHOD1(delFD, void(int));
40 TEST_F(FileDescriptorTest, IsNotCopyable)
42 EXPECT_FALSE(std::is_copy_assignable<FileDescriptor>::value);
43 EXPECT_FALSE(std::is_copy_constructible<FileDescriptor>::value);
46 TEST_F(FileDescriptorTest, IsNotDefaultConstructible)
48 EXPECT_FALSE(std::is_default_constructible<FileDescriptor>::value);
51 TEST_F(FileDescriptorTest, IsConvertibleToInt)
53 EXPECT_CALL(systemMock, close(1)).Times(1);
54 EXPECT_EQ(-1, FileDescriptor(-1));
55 EXPECT_EQ(1, FileDescriptor(systemMock, 1));
58 TEST_F(FileDescriptorTest, WhenDestructedFileDescriptorIsClosed)
60 EXPECT_CALL(systemMock, close(1)).Times(1);
61 FileDescriptor(systemMock, 1);
64 TEST_F(FileDescriptorTest, InvalidFileDescriptorIsNotClosed)
66 EXPECT_CALL(systemMock, close(_)).Times(0);
67 FileDescriptor(systemMock, -1);
70 TEST_F(FileDescriptorTest, DetachWhenDestructed)
72 EXPECT_CALL(*this, delFD(1)).Times(1);
73 EXPECT_CALL(systemMock, close(1)).Times(1);
74 FileDescriptor(systemMock, 1).atClose(atCloseCb);
77 TEST_F(FileDescriptorTest, InvalidFileDescriptorIsNotDetached)
79 EXPECT_CALL(*this, delFD(_)).Times(0);
80 FileDescriptor(-1).atClose(atCloseCb);
83 TEST_F(FileDescriptorTest, OwnershipIsTransferredInMoveConstructor)
87 std::unique_ptr<FileDescriptor> fd1(new FileDescriptor(systemMock, rawfd));
88 fd1->atClose(atCloseCb);
89 std::unique_ptr<FileDescriptor> fd2(new FileDescriptor(std::move(*fd1)));
90 EXPECT_CALL(systemMock, close(_)).Times(0);
91 EXPECT_CALL(*this, delFD(_)).Times(0);
93 EXPECT_CALL(*this, delFD(rawfd)).Times(1);
94 EXPECT_CALL(systemMock, close(rawfd)).Times(1);
98 TEST_F(FileDescriptorTest, OldFdIsClosedAndOwnershipIsTransferredInMoveAssignment)
100 const int rawfd1(100);
101 const int rawfd2(200);
103 std::unique_ptr<FileDescriptor> fd1(new FileDescriptor(systemMock, rawfd1));
104 fd1->atClose(atCloseCb);
105 std::unique_ptr<FileDescriptor> fd2(new FileDescriptor(systemMock, rawfd2));
106 fd2->atClose(atCloseCb);
107 EXPECT_CALL(*this, delFD(rawfd2)).Times(1);
108 EXPECT_CALL(systemMock, close(rawfd2)).Times(1);
109 *fd2 = std::move(*fd1);
111 EXPECT_CALL(*this, delFD(rawfd1)).Times(1);
112 EXPECT_CALL(systemMock, close(rawfd1)).Times(1);