patchpanel: Extract patchpanel-client into its own subdir
Move patchpanel-client into its own package. By doing so,
patchpanel-util will be removed from patchpanel-client.
Systems that previously use patchpanel-util need to update
its build rule to use patchpanel-util.
This is done to avoid dependency loops (e.g. chromium:2359478).
Other system can depend on patchpanel-client instead of
patchpanel after this patch.
BUG=b:166193772
TEST=./build_packages --board=atlas;
TEST=FEATURES=test emerge-atlas patchpanel-client \
patchpanel permission_broker system-proxy \
vm_host_tools
TEST=/usr/libexec/fuzzers/patchpanel_client_fuzzer
TEST=tryjob --hwtest
TEST=tast run <DUT> platform.Firewall
TEST=Crostini and ARC running
Cq-Depend: chromium:2382997
Change-Id: I6244b4808c75a75b69b0276aa10489b1d2501025
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2384496
Tested-by: Jason Jeremy Iman <jasongustaman@chromium.org>
Reviewed-by: Jorge Lucangeli Obes <jorgelo@chromium.org>
Reviewed-by: Yusuke Sato <yusukes@chromium.org>
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Reviewed-by: Hugo Benichi <hugobenichi@google.com>
Reviewed-by: Garrick Evans <garrick@chromium.org>
Commit-Queue: Jason Jeremy Iman <jasongustaman@chromium.org>
diff --git a/patchpanel/dbus/client_test.cc b/patchpanel/dbus/client_test.cc
new file mode 100644
index 0000000..01c7b30
--- /dev/null
+++ b/patchpanel/dbus/client_test.cc
@@ -0,0 +1,80 @@
+// Copyright 2020 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 "patchpanel/dbus/client.h"
+
+#include <chromeos/dbus/service_constants.h>
+#include <dbus/message.h>
+#include <dbus/mock_bus.h>
+#include <dbus/mock_object_proxy.h>
+#include <dbus/object_path.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "patchpanel/net_util.h"
+
+namespace patchpanel {
+
+using ::testing::_;
+using ::testing::ByMove;
+using ::testing::Return;
+
+namespace {
+
+scoped_refptr<dbus::MockBus> MockDBus() {
+ return new dbus::MockBus{dbus::Bus::Options{}};
+}
+
+scoped_refptr<dbus::MockObjectProxy> PatchPanelMockProxy(dbus::MockBus* dbus) {
+ return new dbus::MockObjectProxy(dbus, kPatchPanelServiceName,
+ dbus::ObjectPath(kPatchPanelServicePath));
+}
+
+} // namespace
+
+TEST(ClientTest, ConnectNamespace) {
+ auto dbus = MockDBus();
+ auto proxy = PatchPanelMockProxy(dbus.get());
+ pid_t pid = 3456;
+ std::string outboud_ifname = "";
+
+ Client client(dbus, proxy.get());
+
+ // Failure case
+ auto result = client.ConnectNamespace(pid, outboud_ifname, false);
+ EXPECT_FALSE(result.first.is_valid());
+ EXPECT_TRUE(result.second.peer_ifname().empty());
+ EXPECT_TRUE(result.second.host_ifname().empty());
+ EXPECT_EQ(0, result.second.peer_ipv4_address());
+ EXPECT_EQ(0, result.second.host_ipv4_address());
+ EXPECT_EQ(0, result.second.ipv4_subnet().base_addr());
+ EXPECT_EQ(0, result.second.ipv4_subnet().prefix_len());
+
+ // Success case
+ patchpanel::ConnectNamespaceResponse response_proto;
+ response_proto.set_peer_ifname("veth0");
+ response_proto.set_host_ifname("arc_ns0");
+ response_proto.set_peer_ipv4_address(Ipv4Addr(100, 115, 92, 130));
+ response_proto.set_host_ipv4_address(Ipv4Addr(100, 115, 92, 129));
+ auto* response_subnet = response_proto.mutable_ipv4_subnet();
+ response_subnet->set_prefix_len(30);
+ response_subnet->set_base_addr(Ipv4Addr(100, 115, 92, 128));
+ std::unique_ptr<dbus::Response> response = dbus::Response::CreateEmpty();
+ dbus::MessageWriter response_writer(response.get());
+ response_writer.AppendProtoAsArrayOfBytes(response_proto);
+ EXPECT_CALL(*proxy, CallMethodAndBlock(_, _))
+ .WillOnce(Return(ByMove(std::move(response))));
+
+ result = client.ConnectNamespace(pid, outboud_ifname, false);
+ EXPECT_TRUE(result.first.is_valid());
+ EXPECT_EQ("arc_ns0", result.second.host_ifname());
+ EXPECT_EQ("veth0", result.second.peer_ifname());
+ EXPECT_EQ(30, result.second.ipv4_subnet().prefix_len());
+ EXPECT_EQ(Ipv4Addr(100, 115, 92, 128),
+ result.second.ipv4_subnet().base_addr());
+ EXPECT_EQ(Ipv4Addr(100, 115, 92, 129), result.second.host_ipv4_address());
+ EXPECT_EQ(Ipv4Addr(100, 115, 92, 130), result.second.peer_ipv4_address());
+}
+
+} // namespace patchpanel