scclib
Stable Cloud Computing C++ Library
safe_clib.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 <util/safe_clib.h>
33 
40 using std::cout;
41 using std::endl;
52 
53 TEST(safe_unistd, invalid)
54 {
55  int invalid = 87743;
56  int r;
57 
58  ASSERT_NO_THROW(r = safe_close(invalid));
59  ASSERT_EQ(r, -1);
60  ASSERT_THROW(safe_close_throw(invalid), std::system_error);
61  ASSERT_NO_THROW(r = safe_dup(invalid));
62  ASSERT_EQ(r, -1);
63  ASSERT_THROW(safe_dup_throw(invalid), std::system_error);
64  ASSERT_NO_THROW(r = safe_dup2(invalid, invalid));
65  ASSERT_EQ(r, -1);
66  ASSERT_THROW(safe_dup2_throw(invalid, invalid), std::system_error);
67 
68  ssize_t s;
69  ASSERT_NO_THROW(s = safe_read(invalid, nullptr, 123));
70  ASSERT_EQ(s, -1);
71  ASSERT_THROW(safe_read_throw(invalid, nullptr, 123), std::system_error);
72  ASSERT_NO_THROW(s = safe_write(invalid, nullptr, 123));
73  ASSERT_EQ(s, -1);
74  ASSERT_THROW(safe_write_throw(invalid, nullptr, 123), std::system_error);
75 }
int safe_close(int fd)
Signal safe close.
Definition: safe_clib.cc:95
int safe_dup2_throw(int oldfd, int newfd)
Signal safe dup2, throws system_error on error.
Definition: safe_clib.cc:198
ssize_t safe_read_throw(int fd, void *buf, size_t count)
Signal safe read, throws system_error on error.
Definition: safe_clib.cc:129
ssize_t safe_write_throw(int fd, const void *buf, size_t count)
Signal safe write, throws system_error on error.
Definition: safe_clib.cc:152
ssize_t safe_read(int fd, void *buf, size_t count)
Signal safe read.
Definition: safe_clib.cc:118
ssize_t safe_write(int fd, const void *buf, size_t count)
Signal safe write.
Definition: safe_clib.cc:141
int safe_dup_throw(int oldfd)
Signal safe dup, throws system_error on error.
Definition: safe_clib.cc:175
int safe_close_throw(int fd)
Signal safe close, throws system_error on error.
Definition: safe_clib.cc:106
int safe_dup2(int oldfd, int newfd)
Signal safe dup2.
Definition: safe_clib.cc:187
int safe_dup(int oldfd)
Signal safe dup.
Definition: safe_clib.cc:164
Signal-safe C library wrapper.
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521