|
| 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