Skip to content

Commit dc39379

Browse files
test: Added unit test for Port_util
1 parent 6d5055d commit dc39379

3 files changed

Lines changed: 67 additions & 2 deletions

File tree

api/net/port_util.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ class Port_util {
129129
// while(is_bound(ephemeral_)) ++ephemeral_; // worst case is like 16k iterations :D
130130
// need a solution that checks each word of the subset (the dynamic range)
131131
// FIXME: this may happen...
132-
if(UNLIKELY( is_bound(ephemeral_) ))
133-
throw Port_error{"Generated ephemeral port is already bound. Please fix me!"};
132+
Expects(not is_bound(ephemeral_) && "Generated ephemeral port is already bound. Please fix me!");
134133
}
135134
}; // < class Port_util
136135

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ set(TEST_SOURCES
8585
${TEST}/net/unit/ip4_addr.cpp
8686
${TEST}/net/unit/ip4.cpp
8787
${TEST}/net/unit/packets.cpp
88+
${TEST}/net/unit/port_util_test.cpp
8889
${TEST}/net/unit/socket.cpp
8990
${TEST}/net/unit/tcp_packet_test.cpp
9091
${TEST}/net/unit/tcp_write_queue.cpp

test/net/unit/port_util_test.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2017 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#include <common.cxx>
19+
#include <net/port_util.hpp>
20+
21+
CASE("Binding and unbinding")
22+
{
23+
net::Port_util util;
24+
25+
const uint16_t port = 322;
26+
27+
EXPECT(util.is_bound(port) == false);
28+
29+
util.bind(port);
30+
31+
EXPECT(util.is_bound(port) == true);
32+
33+
util.unbind(port);
34+
35+
EXPECT(util.is_bound(port) == false);
36+
}
37+
38+
CASE("Generating ephemeral port throws when all are bound")
39+
{
40+
net::Port_util util;
41+
42+
// Bind all
43+
for(auto i = 0; i < (net::port_ranges::DYNAMIC_END - net::port_ranges::DYNAMIC_START); ++i)
44+
util.bind(util.get_next_ephemeral());
45+
46+
EXPECT_THROWS_AS(util.get_next_ephemeral(), net::Port_error);
47+
}
48+
49+
CASE("[.expectedfailure] Generating ephemeral handles wrap around")
50+
{
51+
net::Port_util util;
52+
53+
auto port = util.get_next_ephemeral();
54+
55+
// bind first one
56+
util.bind(port);
57+
58+
// wrap around
59+
for(auto i = 0; i < (net::port_ranges::DYNAMIC_END - net::port_ranges::DYNAMIC_START - 1); ++i)
60+
util.get_next_ephemeral();
61+
62+
auto port2 = util.get_next_ephemeral();
63+
64+
EXPECT(port != port2);
65+
}

0 commit comments

Comments
 (0)