scclib
Stable Cloud Computing C++ Library
filedesc.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <gtest/gtest.h>
32 #include <cstring>
33 #include <iostream>
34 #include <cstdio>
35 #include <util/filedesc.h>
36 
43 using std::cout;
44 using std::endl;
46 
47 void pr(int fd)
48 {
49  cout << "fd: " << static_cast<int>(fd) << endl;
50 }
51 
52 TEST(FileDesc_test, nil)
53 {
54  FileDesc fd;
55  ASSERT_EQ(static_cast<int>(fd), -1);
56  pr(fd);
57 }
58 
59 TEST(FileDesc_test, dup_stdout)
60 {
61  cout << "dup stdout:" << endl;
62  FileDesc fd;
63  cout << "fd: " << (int)fd << endl;
64  dprintf(1, "print to stdout\n");
65  fd.dup(1); // stdout
66  cout << "fd: " << (int)fd << endl;
67  dprintf(fd, "print to dup\n");
68  ASSERT_NE(static_cast<int>(fd), -1);
69  ASSERT_NE(static_cast<int>(fd), 1);
70  dprintf(1, "print to stdout again\n");
71 }
72 
73 TEST(FileDesc_test, copy_construct)
74 {
75  cout << "copy construct:" << endl;
76  FileDesc fd;
77  cout << "fd: " << (int)fd << endl;
78  dprintf(1, "print to stdout\n");
79  fd.dup(1); // stdout
80  cout << "fd: " << (int)fd << endl;
81  dprintf(fd, "print to dup\n");
82  FileDesc fd2(fd);
83  cout << "fd: " << (int)fd << endl;
84  cout << "fd2: " << (int)fd2 << endl;
85  dprintf(fd2, "print to dup2\n");
86  ASSERT_NE(static_cast<int>(fd), -1);
87  ASSERT_NE(static_cast<int>(fd2), -1);
88  ASSERT_NE(static_cast<int>(fd), static_cast<int>(fd2));
89 }
90 
91 TEST(FileDesc_test, copy)
92 {
93  cout << "copy:" << endl;
94  FileDesc fd, fd2;
95  cout << "fd: " << (int)fd << endl;
96  dprintf(1, "print to stdout\n");
97  fd.dup(1); // stdout
98  cout << "fd: " << (int)fd << endl;
99  dprintf(fd, "print to dup\n");
100  fd2 = fd;
101  cout << "fd: " << (int)fd << endl;
102  cout << "fd2: " << (int)fd2 << endl;
103  dprintf(fd2, "print to dup2\n");
104  ASSERT_NE(static_cast<int>(fd), -1);
105  ASSERT_NE(static_cast<int>(fd2), -1);
106  ASSERT_NE(static_cast<int>(fd), static_cast<int>(fd2));
107 }
108 
109 TEST(FileDesc_test, move_construct)
110 {
111  cout << "move construct:" << endl;
112  FileDesc fd;
113  cout << "fd: " << (int)fd << endl;
114  dprintf(1, "print to stdout\n");
115  fd.dup(1); // stdout
116  cout << "fd: " << (int)fd << endl;
117  dprintf(fd, "print to dup\n");
118  FileDesc fd2 = std::move(fd);
119  cout << "fd: " << (int)fd << endl;
120  cout << "fd2: " << (int)fd2 << endl;
121  dprintf(fd2, "print to dup2\n");
122  ASSERT_EQ(static_cast<int>(fd), -1);
123  ASSERT_NE(static_cast<int>(fd2), -1);
124 }
125 
126 TEST(FileDesc_test, move)
127 {
128  cout << "move:" << endl;
129  FileDesc fd, fd2;
130  cout << "fd: " << (int)fd << endl;
131  dprintf(1, "print to stdout\n");
132  fd.dup(1); // stdout
133  cout << "fd: " << (int)fd << endl;
134  dprintf(fd, "print to dup\n");
135  fd2 = std::move(fd);
136  cout << "fd: " << (int)fd << endl;
137  cout << "fd2: " << (int)fd2 << endl;
138  dprintf(fd2, "print to dup2\n");
139  ASSERT_EQ(static_cast<int>(fd), -1);
140  ASSERT_NE(static_cast<int>(fd2), -1);
141 }
File descriptor.
Definition: filedesc.h:66
void dup(int)
Duplicate a file descriptor.
Definition: filedesc.cc:50
File descriptor.
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521