• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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 
16 #include "os/unique_fd.h"
17 
18 #include <gtest/gtest.h>
19 #include <utility>
20 #include <unistd.h>
21 
22 namespace panda::os::unique_fd {
23 
24 enum testValue { DEFAULT_VALUE = -1, STDIN_VALUE, STDOUT_VALUE, STDERR_VALUE };
25 
26 struct DuplicateFD {
27     int stdinValue = ::dup(STDIN_VALUE);
28     int stdoutValue = ::dup(STDOUT_VALUE);
29     int stferrValue = ::dup(STDERR_VALUE);
30 };
31 
32 HWTEST(UniqueFd, Construct, testing::ext::TestSize.Level0)
33 {
34     DuplicateFD dupDF;
35     auto fd_a = UniqueFd();
36     auto fd_b = UniqueFd(dupDF.stdinValue);
37     auto fd_c = UniqueFd(dupDF.stdoutValue);
38     auto fd_d = UniqueFd(dupDF.stferrValue);
39 
40     EXPECT_EQ(fd_a.Get(), DEFAULT_VALUE);
41     EXPECT_EQ(fd_b.Get(), dupDF.stdinValue);
42     EXPECT_EQ(fd_c.Get(), dupDF.stdoutValue);
43     EXPECT_EQ(fd_d.Get(), dupDF.stferrValue);
44 
45     auto fd_e = std::move(fd_a);
46     auto fd_f = std::move(fd_b);
47     auto fd_g = std::move(fd_c);
48     auto fd_h = std::move(fd_d);
49 
50     EXPECT_EQ(fd_a.Get(), DEFAULT_VALUE);
51     EXPECT_EQ(fd_b.Get(), DEFAULT_VALUE);
52     EXPECT_EQ(fd_c.Get(), DEFAULT_VALUE);
53     EXPECT_EQ(fd_d.Get(), DEFAULT_VALUE);
54     EXPECT_EQ(fd_e.Get(), DEFAULT_VALUE);
55     EXPECT_EQ(fd_f.Get(), dupDF.stdinValue);
56     EXPECT_EQ(fd_g.Get(), dupDF.stdoutValue);
57     EXPECT_EQ(fd_h.Get(), dupDF.stferrValue);
58 }
59 
60 HWTEST(UniqueFd, Equal, testing::ext::TestSize.Level0)
61 {
62     DuplicateFD dupDF;
63     auto fd_a = UniqueFd();
64     auto fd_b = UniqueFd(dupDF.stdinValue);
65     auto fd_c = UniqueFd(dupDF.stdoutValue);
66     auto fd_d = UniqueFd(dupDF.stferrValue);
67 
68     auto fd_e = UniqueFd();
69     auto fd_f = UniqueFd();
70     auto fd_g = UniqueFd();
71     auto fd_h = UniqueFd();
72     fd_e = std::move(fd_a);
73     fd_f = std::move(fd_b);
74     fd_g = std::move(fd_c);
75     fd_h = std::move(fd_d);
76 
77     EXPECT_EQ(fd_a.Get(), DEFAULT_VALUE);
78     EXPECT_EQ(fd_b.Get(), DEFAULT_VALUE);
79     EXPECT_EQ(fd_c.Get(), DEFAULT_VALUE);
80     EXPECT_EQ(fd_d.Get(), DEFAULT_VALUE);
81     EXPECT_EQ(fd_e.Get(), DEFAULT_VALUE);
82     EXPECT_EQ(fd_f.Get(), dupDF.stdinValue);
83     EXPECT_EQ(fd_g.Get(), dupDF.stdoutValue);
84     EXPECT_EQ(fd_h.Get(), dupDF.stferrValue);
85 }
86 
87 HWTEST(UniqueFd, Release, testing::ext::TestSize.Level0)
88 {
89     DuplicateFD dupDF;
90     auto fd_a = UniqueFd();
91     auto fd_b = UniqueFd(dupDF.stdinValue);
92     auto fd_c = UniqueFd(dupDF.stdoutValue);
93     auto fd_d = UniqueFd(dupDF.stferrValue);
94 
95     auto num_a = fd_a.Release();
96     auto num_b = fd_b.Release();
97     auto num_c = fd_c.Release();
98     auto num_d = fd_d.Release();
99 
100     EXPECT_EQ(fd_a.Get(), DEFAULT_VALUE);
101     EXPECT_EQ(fd_b.Get(), DEFAULT_VALUE);
102     EXPECT_EQ(fd_c.Get(), DEFAULT_VALUE);
103     EXPECT_EQ(fd_d.Get(), DEFAULT_VALUE);
104     EXPECT_EQ(num_a, DEFAULT_VALUE);
105     EXPECT_EQ(num_b, dupDF.stdinValue);
106     EXPECT_EQ(num_c, dupDF.stdoutValue);
107     EXPECT_EQ(num_d, dupDF.stferrValue);
108 }
109 
110 HWTEST(UniqueFd, Reset, testing::ext::TestSize.Level0)
111 {
112     DuplicateFD dupDF;
113 
114     auto num_a = DEFAULT_VALUE;
115     auto num_b = dupDF.stdinValue;
116     auto num_c = dupDF.stdoutValue;
117     auto num_d = dupDF.stferrValue;
118 
119     auto fd_a = UniqueFd();
120     auto fd_b = UniqueFd();
121     auto fd_c = UniqueFd();
122     auto fd_d = UniqueFd();
123 
124     fd_a.Reset(num_a);
125     fd_b.Reset(num_b);
126     fd_c.Reset(num_c);
127     fd_d.Reset(num_d);
128 
129     EXPECT_EQ(fd_a.Get(), DEFAULT_VALUE);
130     EXPECT_EQ(fd_b.Get(), dupDF.stdinValue);
131     EXPECT_EQ(fd_c.Get(), dupDF.stdoutValue);
132     EXPECT_EQ(fd_d.Get(), dupDF.stferrValue);
133 }
134 
135 }  // namespace panda::os::unique_fd
136