RIC:1060: Change in PTL
[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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include <type_traits>
23 #include <memory>
24 #include <gtest/gtest.h>
25 #include <gmock/gmock.h>
26 #include "private/filedescriptor.hpp"
27 #include "private/tst/systemmock.hpp"
28
29 using namespace shareddatalayer;
30 using namespace shareddatalayer::tst;
31 using namespace testing;
32
33 namespace
34 {
35     class FileDescriptorTest: public Test
36     {
37     public:
38         SystemMock systemMock;
39         std::function<void(int)> atCloseCb = [&](int fd) { delFD(fd); };
40
41         MOCK_METHOD1(delFD, void(int));
42     };
43 }
44
45 TEST_F(FileDescriptorTest, IsNotCopyable)
46 {
47     EXPECT_FALSE(std::is_copy_assignable<FileDescriptor>::value);
48     EXPECT_FALSE(std::is_copy_constructible<FileDescriptor>::value);
49 }
50
51 TEST_F(FileDescriptorTest, IsNotDefaultConstructible)
52 {
53     EXPECT_FALSE(std::is_default_constructible<FileDescriptor>::value);
54 }
55
56 TEST_F(FileDescriptorTest, IsConvertibleToInt)
57 {
58     EXPECT_CALL(systemMock, close(1)).Times(1);
59     EXPECT_EQ(-1, FileDescriptor(-1));
60     EXPECT_EQ(1, FileDescriptor(systemMock, 1));
61 }
62
63 TEST_F(FileDescriptorTest, WhenDestructedFileDescriptorIsClosed)
64 {
65     EXPECT_CALL(systemMock, close(1)).Times(1);
66     FileDescriptor(systemMock, 1);
67 }
68
69 TEST_F(FileDescriptorTest, InvalidFileDescriptorIsNotClosed)
70 {
71     EXPECT_CALL(systemMock, close(_)).Times(0);
72     FileDescriptor(systemMock, -1);
73 }
74
75 TEST_F(FileDescriptorTest, DetachWhenDestructed)
76 {
77     EXPECT_CALL(*this, delFD(1)).Times(1);
78     EXPECT_CALL(systemMock, close(1)).Times(1);
79     FileDescriptor(systemMock, 1).atClose(atCloseCb);
80 }
81
82 TEST_F(FileDescriptorTest, InvalidFileDescriptorIsNotDetached)
83 {
84     EXPECT_CALL(*this, delFD(_)).Times(0);
85     FileDescriptor(-1).atClose(atCloseCb);
86 }
87
88 TEST_F(FileDescriptorTest, OwnershipIsTransferredInMoveConstructor)
89 {
90     const int rawfd(100);
91     InSequence dummy;
92     std::unique_ptr<FileDescriptor> fd1(new FileDescriptor(systemMock, rawfd));
93     fd1->atClose(atCloseCb);
94     std::unique_ptr<FileDescriptor> fd2(new FileDescriptor(std::move(*fd1)));
95     EXPECT_CALL(systemMock, close(_)).Times(0);
96     EXPECT_CALL(*this, delFD(_)).Times(0);
97     fd1.reset();
98     EXPECT_CALL(*this, delFD(rawfd)).Times(1);
99     EXPECT_CALL(systemMock, close(rawfd)).Times(1);
100     fd2.reset();
101 }
102
103 TEST_F(FileDescriptorTest, OldFdIsClosedAndOwnershipIsTransferredInMoveAssignment)
104 {
105     const int rawfd1(100);
106     const int rawfd2(200);
107     InSequence dummy;
108     std::unique_ptr<FileDescriptor> fd1(new FileDescriptor(systemMock, rawfd1));
109     fd1->atClose(atCloseCb);
110     std::unique_ptr<FileDescriptor> fd2(new FileDescriptor(systemMock, rawfd2));
111     fd2->atClose(atCloseCb);
112     EXPECT_CALL(*this, delFD(rawfd2)).Times(1);
113     EXPECT_CALL(systemMock, close(rawfd2)).Times(1);
114     *fd2 = std::move(*fd1);
115     fd1.reset();
116     EXPECT_CALL(*this, delFD(rawfd1)).Times(1);
117     EXPECT_CALL(systemMock, close(rawfd1)).Times(1);
118     fd2.reset();
119 }