5598a28b93c8609bc87f7d62ffa4a2af831a0921
[ric-plt/sdl.git] / tst / filedescriptor_test.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
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 <type_traits>
18 #include <memory>
19 #include <gtest/gtest.h>
20 #include <gmock/gmock.h>
21 #include "private/filedescriptor.hpp"
22 #include "private/tst/systemmock.hpp"
23
24 using namespace shareddatalayer;
25 using namespace shareddatalayer::tst;
26 using namespace testing;
27
28 namespace
29 {
30     class FileDescriptorTest: public Test
31     {
32     public:
33         SystemMock systemMock;
34         std::function<void(int)> atCloseCb = [&](int fd) { delFD(fd); };
35
36         MOCK_METHOD1(delFD, void(int));
37     };
38 }
39
40 TEST_F(FileDescriptorTest, IsNotCopyable)
41 {
42     EXPECT_FALSE(std::is_copy_assignable<FileDescriptor>::value);
43     EXPECT_FALSE(std::is_copy_constructible<FileDescriptor>::value);
44 }
45
46 TEST_F(FileDescriptorTest, IsNotDefaultConstructible)
47 {
48     EXPECT_FALSE(std::is_default_constructible<FileDescriptor>::value);
49 }
50
51 TEST_F(FileDescriptorTest, IsConvertibleToInt)
52 {
53     EXPECT_CALL(systemMock, close(1)).Times(1);
54     EXPECT_EQ(-1, FileDescriptor(-1));
55     EXPECT_EQ(1, FileDescriptor(systemMock, 1));
56 }
57
58 TEST_F(FileDescriptorTest, WhenDestructedFileDescriptorIsClosed)
59 {
60     EXPECT_CALL(systemMock, close(1)).Times(1);
61     FileDescriptor(systemMock, 1);
62 }
63
64 TEST_F(FileDescriptorTest, InvalidFileDescriptorIsNotClosed)
65 {
66     EXPECT_CALL(systemMock, close(_)).Times(0);
67     FileDescriptor(systemMock, -1);
68 }
69
70 TEST_F(FileDescriptorTest, DetachWhenDestructed)
71 {
72     EXPECT_CALL(*this, delFD(1)).Times(1);
73     EXPECT_CALL(systemMock, close(1)).Times(1);
74     FileDescriptor(systemMock, 1).atClose(atCloseCb);
75 }
76
77 TEST_F(FileDescriptorTest, InvalidFileDescriptorIsNotDetached)
78 {
79     EXPECT_CALL(*this, delFD(_)).Times(0);
80     FileDescriptor(-1).atClose(atCloseCb);
81 }
82
83 TEST_F(FileDescriptorTest, OwnershipIsTransferredInMoveConstructor)
84 {
85     const int rawfd(100);
86     InSequence dummy;
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);
92     fd1.reset();
93     EXPECT_CALL(*this, delFD(rawfd)).Times(1);
94     EXPECT_CALL(systemMock, close(rawfd)).Times(1);
95     fd2.reset();
96 }
97
98 TEST_F(FileDescriptorTest, OldFdIsClosedAndOwnershipIsTransferredInMoveAssignment)
99 {
100     const int rawfd1(100);
101     const int rawfd2(200);
102     InSequence dummy;
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);
110     fd1.reset();
111     EXPECT_CALL(*this, delFD(rawfd1)).Times(1);
112     EXPECT_CALL(systemMock, close(rawfd1)).Times(1);
113     fd2.reset();
114 }