arc: Move platform2/arc/network/ to platform2/patchpanel
Next step in the arc-networkd -> patchpanel rename, this patch moves the
location of the code.
BUG=b:151879931
TEST=units,flashed image to atlas
TEST=tasts arc.PlayStore, crostini.LaunchTerminal.download
Change-Id: I1b5cf8d670e1631d46f6449b725395157bf88dde
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2115863
Tested-by: Garrick Evans <garrick@chromium.org>
Commit-Queue: Garrick Evans <garrick@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
diff --git a/patchpanel/subnet_pool_test.cc b/patchpanel/subnet_pool_test.cc
new file mode 100644
index 0000000..f0a761d
--- /dev/null
+++ b/patchpanel/subnet_pool_test.cc
@@ -0,0 +1,106 @@
+// Copyright 2018 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <arpa/inet.h>
+#include <stdint.h>
+
+#include <algorithm>
+#include <deque>
+#include <memory>
+#include <random>
+#include <string>
+#include <utility>
+
+#include <base/rand_util.h>
+#include <base/strings/stringprintf.h>
+#include <gtest/gtest.h>
+
+#include "patchpanel/net_util.h"
+#include "patchpanel/subnet.h"
+#include "patchpanel/subnet_pool.h"
+
+using std::string;
+
+namespace patchpanel {
+namespace {
+
+// The maximum number of subnets that can be allocated at a given time.
+constexpr uint32_t kBaseAddress = Ipv4Addr(44, 55, 66, 77);
+constexpr uint32_t kPrefixLength = 30;
+
+} // namespace
+
+// Tests cannot create a pool with more than 32 supported subnets.
+TEST(SubnetPool, MaxSubnets) {
+ auto pool = SubnetPool::New(kBaseAddress, kPrefixLength, kMaxSubnets + 1);
+ EXPECT_TRUE(pool == nullptr);
+}
+
+// Tests that the SubnetPool does not allocate more than max subnets at a time.
+TEST(SubnetPool, AllocationRange) {
+ auto pool = SubnetPool::New(kBaseAddress, kPrefixLength, kMaxSubnets);
+
+ std::deque<std::unique_ptr<Subnet>> subnets;
+ for (size_t i = 0; i < kMaxSubnets; ++i) {
+ auto subnet = pool->Allocate();
+ ASSERT_TRUE(subnet);
+
+ subnets.emplace_back(std::move(subnet));
+ }
+ EXPECT_EQ(subnets.size(), kMaxSubnets);
+ EXPECT_FALSE(pool->Allocate());
+}
+
+// Tests that subnets are properly released and reused.
+TEST(SubnetPool, Release) {
+ auto pool = SubnetPool::New(kBaseAddress, kPrefixLength, kMaxSubnets);
+
+ // First allocate all the subnets.
+ std::deque<std::unique_ptr<Subnet>> subnets;
+ for (size_t i = 0; i < kMaxSubnets; ++i) {
+ auto subnet = pool->Allocate();
+ ASSERT_TRUE(subnet);
+
+ subnets.emplace_back(std::move(subnet));
+ }
+ ASSERT_FALSE(pool->Allocate());
+
+ // Now shuffle the elements.
+ std::shuffle(subnets.begin(), subnets.end(),
+ std::mt19937(base::RandUint64()));
+
+ // Pop off the first element.
+ auto subnet = std::move(subnets.front());
+ subnets.pop_front();
+
+ // Store the gateway and address for testing later.
+ uint32_t gateway = subnet->AddressAtOffset(0);
+ uint32_t address = subnet->AddressAtOffset(1);
+
+ // Release the subnet.
+ subnet.reset();
+
+ // Get a new subnet.
+ subnet = pool->Allocate();
+ ASSERT_TRUE(subnet);
+
+ EXPECT_EQ(gateway, subnet->AddressAtOffset(0));
+ EXPECT_EQ(address, subnet->AddressAtOffset(1));
+}
+
+TEST(SubnetPool, Index) {
+ auto pool = SubnetPool::New(kBaseAddress, kPrefixLength, kMaxSubnets);
+ auto subnet = pool->Allocate(1);
+ ASSERT_TRUE(subnet);
+ EXPECT_FALSE(pool->Allocate(1));
+ EXPECT_TRUE(pool->Allocate(0));
+ EXPECT_TRUE(pool->Allocate());
+ EXPECT_TRUE(pool->Allocate(2));
+ EXPECT_TRUE(pool->Allocate(kMaxSubnets));
+ subnet.reset();
+ EXPECT_TRUE(pool->Allocate(1));
+ EXPECT_FALSE(pool->Allocate(kMaxSubnets + 1));
+}
+
+} // namespace patchpanel