| 176 | } |
| 177 | |
| 178 | struct SubNetTest { |
| 179 | AuthAllowedSubnet subnet; |
| 180 | SubNetTest(AuthAllowedSubnet&& subnet) : subnet(std::move(subnet)) {} |
| 181 | SubNetTest(AuthAllowedSubnet const& subnet) : subnet(subnet) {} |
| 182 | template <bool V4> |
| 183 | static SubNetTest randomSubNetImpl() { |
| 184 | constexpr int width = V4 ? 4 : 16; |
| 185 | std::array<unsigned char, width> binAddr; |
| 186 | unsigned char rnd[4]; |
| 187 | for (int i = 0; i < binAddr.size(); ++i) { |
| 188 | if (i % 4 == 0) { |
| 189 | auto tmp = deterministicRandom()->randomUInt32(); |
| 190 | ::memcpy(rnd, &tmp, 4); |
| 191 | } |
| 192 | binAddr[i] = rnd[i % 4]; |
| 193 | } |
| 194 | auto netmaskWeight = deterministicRandom()->randomInt(1, width); |
| 195 | std::string address; |
| 196 | if constexpr (V4) { |
| 197 | address_v4 a(binAddr); |
| 198 | address = a.to_string(); |
| 199 | } else { |
| 200 | address_v6 a(binAddr); |
| 201 | address = a.to_string(); |
| 202 | } |
| 203 | return SubNetTest(AuthAllowedSubnet::fromString(fmt::format("{}/{}", address, netmaskWeight))); |
| 204 | } |
| 205 | static SubNetTest randomSubNet() { |
| 206 | if (deterministicRandom()->coinflip()) { |
| 207 | return randomSubNetImpl<true>(); |
| 208 | } else { |
| 209 | return randomSubNetImpl<false>(); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | template <bool V4> |
| 214 | static IPAddress intArrayToAddress(uint32_t* arr) { |
| 215 | if constexpr (V4) { |
| 216 | return IPAddress(arr[0]); |
| 217 | } else { |
| 218 | std::array<unsigned char, 16> res; |
| 219 | memcpy(res.data(), arr, 4); |
| 220 | return IPAddress(res); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | template <class I> |
| 225 | I transformIntToSubnet(I val, I subnetMask, I baseAddress) { |
| 226 | return (val & subnetMask) ^ baseAddress; |
| 227 | } |
| 228 | |
| 229 | template <bool V4> |
| 230 | static IPAddress randomAddress() { |
| 231 | constexpr int width = V4 ? 4 : 16; |
| 232 | uint32_t rnd[width / 4]; |
| 233 | for (int i = 0; i < width / 4; ++i) { |
| 234 | rnd[i] = deterministicRandom()->randomUInt32(); |
| 235 | } |
no outgoing calls
no test coverage detected