OpenVPN 3 Core Library
Loading...
Searching...
No Matches
test_clamp_typerange.cpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2023- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12
13
14#include "test_common.hpp"
15
16#include <cstdint>
17
19
20using namespace openvpn::numeric_util;
21
22
23/* ============================================================================================================= */
24// clamp_to_typerange
25/* ============================================================================================================= */
26
27
28TEST(ClampToTyperange, SameTypeNocast1)
29{
30 const int32_t i32 = -1;
31 auto result = clamp_to_typerange<int32_t>(i32);
32 EXPECT_EQ(result, i32);
33}
34
35TEST(ClampToTyperange, SignMismatch321)
36{
37 const int32_t i32 = -1;
38 auto result = clamp_to_typerange<uint32_t>(i32);
39 EXPECT_EQ(result, 0);
40}
41
42TEST(ClampToTyperange, SignMismatch322)
43{
44 const uint32_t u32 = std::numeric_limits<uint32_t>::max();
45 auto result = clamp_to_typerange<int32_t>(u32);
46 EXPECT_EQ(result, std::numeric_limits<int32_t>::max());
47}
48
49TEST(ClampToTyperange, SignMismatch323)
50{
51 const uint32_t u32 = 0;
52 auto result = clamp_to_typerange<int32_t>(u32);
53 EXPECT_EQ(result, 0);
54}
55
56TEST(ClampToTyperange, SignMismatch324)
57{
58 const uint32_t u32 = 42;
59 auto result = clamp_to_typerange<int32_t>(u32);
60 EXPECT_EQ(result, 42);
61}
62
63TEST(ClampToTyperange, SignMismatch325)
64{
65 const uint32_t u32 = uint32_t(std::numeric_limits<int32_t>::max());
66 auto result = clamp_to_typerange<int32_t>(u32);
67 EXPECT_EQ(result, std::numeric_limits<int32_t>::max());
68}
69
70TEST(ClampToTyperange, SignMismatch326)
71{
72 const int32_t s32 = std::numeric_limits<int32_t>::max();
73 auto result = clamp_to_typerange<uint8_t>(s32);
74 EXPECT_EQ(result, std::numeric_limits<uint8_t>::max());
75}
76
77TEST(ClampToTyperange, SignMismatch327)
78{
79 const int32_t s32 = 42;
80 auto result = clamp_to_typerange<uint8_t>(s32);
81 EXPECT_EQ(result, 42);
82}
83
84TEST(ClampToTyperange, SRangeMismatch16641)
85{
86 const int64_t s64 = std::numeric_limits<int64_t>::max();
87 auto result = clamp_to_typerange<int16_t>(s64);
88 EXPECT_EQ(result, std::numeric_limits<int16_t>::max());
89}
90
91TEST(ClampToTyperange, SRangeMatch16641)
92{
93 const int64_t s64 = 0;
94 auto result = clamp_to_typerange<int16_t>(s64);
95 EXPECT_EQ(result, 0);
96}
97
98TEST(ClampToTyperange, URangeMismatch16641)
99{
100 const uint64_t u64 = std::numeric_limits<uint64_t>::max();
101 auto result = clamp_to_typerange<uint16_t>(u64);
102 EXPECT_EQ(result, std::numeric_limits<uint16_t>::max());
103}
104
105
106/* ============================================================================================================= */
107// clamp_to_default
108/* ============================================================================================================= */
109
110
111TEST(ClampToDefault, SameTypeNocast1)
112{
113 const int32_t i32 = -1;
114 auto result = clamp_to_default<int32_t>(i32, 0);
115 EXPECT_EQ(result, i32);
116}
117
118TEST(ClampToDefault, SignMismatch321)
119{
120 const int32_t i32 = -1;
121 auto result = clamp_to_default<uint32_t>(i32, 42);
122 EXPECT_EQ(result, 42);
123}
124
125TEST(ClampToDefault, SignMismatch322)
126{
127 const uint32_t u32 = std::numeric_limits<uint32_t>::max();
128 auto result = clamp_to_default<int32_t>(u32, 1);
129 EXPECT_EQ(result, 1);
130}
131
132TEST(ClampToDefault, SignMismatch323)
133{
134 const uint32_t u32 = 0;
135 auto result = clamp_to_default<int32_t>(u32, 42);
136 EXPECT_EQ(result, 0);
137}
138
139TEST(ClampToDefault, SignMismatch324)
140{
141 const uint32_t u32 = 42;
142 auto result = clamp_to_default<int32_t>(u32, 0);
143 EXPECT_EQ(result, 42);
144}
145
146TEST(ClampToDefault, SignMismatch325)
147{
148 const uint32_t u32 = uint32_t(std::numeric_limits<int32_t>::max());
149 auto result = clamp_to_default<int32_t>(u32, -1);
150 EXPECT_EQ(result, std::numeric_limits<int32_t>::max());
151}
152
153TEST(ClampToDefault, SignMismatch326)
154{
155 const int32_t s32 = std::numeric_limits<int32_t>::max();
156 auto result = clamp_to_default<uint8_t>(s32, 0);
157 EXPECT_EQ(result, 0);
158}
159
160TEST(ClampToDefault, SignMismatch327)
161{
162 const int32_t s32 = 42;
163 auto result = clamp_to_default<uint8_t>(s32, -1);
164 EXPECT_EQ(result, 42);
165}
166
167TEST(ClampToDefault, SRangeMismatch16641)
168{
169 const int64_t s64 = std::numeric_limits<int64_t>::max();
170 auto result = clamp_to_default<int16_t>(s64, 0);
171 EXPECT_EQ(result, 0);
172}
173
174TEST(ClampToDefault, SRangeMatch16641)
175{
176 const int64_t s64 = 0;
177 auto result = clamp_to_default<int16_t>(s64, -1);
178 EXPECT_EQ(result, 0);
179}
180
181TEST(ClampToDefault, URangeMismatch16641)
182{
183 const uint64_t u64 = std::numeric_limits<uint64_t>::max();
184 auto result = clamp_to_default<uint16_t>(u64, 42);
185 EXPECT_EQ(result, 42);
186}
187
188/* ============================================================================================================= */
189// clamp_notify
190/* ============================================================================================================= */
191
192
193TEST(ClampNotify, SameTypeNocast1)
194{
195 const int32_t i32 = -1;
196 auto result = clamp_notify<int32_t>(i32, [](int32_t inVal)
197 { return 0; });
198 EXPECT_EQ(result, i32);
199}
200
201TEST(ClampNotify, SignMismatch321)
202{
203 const int32_t i32 = -1;
204 auto result = clamp_notify<uint32_t>(i32, [](int32_t inVal)
205 { return 42; });
206 EXPECT_EQ(result, 42);
207}
208
209TEST(ClampNotify, SignMismatch322)
210{
211 const uint32_t u32 = std::numeric_limits<uint32_t>::max();
212 auto result = clamp_notify<int32_t>(u32, [](uint32_t inVal)
213 { return 1; });
214 EXPECT_EQ(result, 1);
215}
216
217TEST(ClampNotify, SignMismatch323)
218{
219 const uint32_t u32 = 0;
220 auto result = clamp_notify<int32_t>(u32, [](uint32_t inVal)
221 { return 42; });
222 EXPECT_EQ(result, 0);
223}
224
225TEST(ClampNotify, SignMismatch324)
226{
227 const uint32_t u32 = 42;
228 auto result = clamp_notify<int32_t>(u32, [](uint32_t inVal)
229 { return 0; });
230 EXPECT_EQ(result, 42);
231}
232
233TEST(ClampNotify, SignMismatch325)
234{
235 const uint32_t u32 = uint32_t(std::numeric_limits<int32_t>::max());
236 auto result = clamp_notify<int32_t>(u32, [](uint32_t inVal)
237 { return -1; });
238 EXPECT_EQ(result, std::numeric_limits<int32_t>::max());
239}
240
241TEST(ClampNotify, SignMismatch326)
242{
243 const int32_t s32 = std::numeric_limits<int32_t>::max();
244 auto result = clamp_notify<uint8_t>(s32, [](int32_t inVal) -> uint8_t
245 { return 0; });
246 EXPECT_EQ(result, 0);
247}
248
249TEST(ClampNotify, SignMismatch327)
250{
251 const int32_t s32 = 42;
252 auto result = clamp_notify<uint8_t>(s32, [](int32_t inVal) -> uint8_t
253 { return 0; });
254 EXPECT_EQ(result, 42);
255}
256
257TEST(ClampNotify, SRangeMismatch16641)
258{
259 const int64_t s64 = std::numeric_limits<int64_t>::max();
260 auto result = clamp_notify<int16_t>(s64, [](int64_t inVal) -> int16_t
261 { return 0; });
262 EXPECT_EQ(result, 0);
263}
264
265TEST(ClampNotify, SRangeMatch16641)
266{
267 const int64_t s64 = 0;
268 auto result = clamp_notify<int16_t>(s64, [](int64_t inVal) -> int16_t
269 { return -1; });
270 EXPECT_EQ(result, 0);
271}
272
273TEST(ClampNotify, URangeMismatch16641)
274{
275 const uint64_t u64 = std::numeric_limits<uint64_t>::max();
276 auto result = clamp_notify<uint16_t>(u64, [](uint64_t inVal) -> uint16_t
277 { return 42; });
278 EXPECT_EQ(result, 42);
279}
TEST(ClampToTyperange, SameTypeNocast1)