blob: 01c7b30935ff29fbb467146ebd9112b741fc543a [file] [log] [blame]
Hugo Benichicc6850f2020-01-17 13:26:06 +09001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +09005#include "patchpanel/dbus/client.h"
Hugo Benichicc6850f2020-01-17 13:26:06 +09006
7#include <chromeos/dbus/service_constants.h>
Hugo Benichicc6850f2020-01-17 13:26:06 +09008#include <dbus/message.h>
Hugo Benichicc6850f2020-01-17 13:26:06 +09009#include <dbus/mock_bus.h>
10#include <dbus/mock_object_proxy.h>
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +090011#include <dbus/object_path.h>
12#include <gmock/gmock.h>
13#include <gtest/gtest.h>
Hugo Benichicc6850f2020-01-17 13:26:06 +090014
Garrick Evans3388a032020-03-24 11:25:55 +090015#include "patchpanel/net_util.h"
Hugo Benichicc6850f2020-01-17 13:26:06 +090016
17namespace patchpanel {
18
Hugo Benichicc6850f2020-01-17 13:26:06 +090019using ::testing::_;
20using ::testing::ByMove;
21using ::testing::Return;
22
23namespace {
24
25scoped_refptr<dbus::MockBus> MockDBus() {
26 return new dbus::MockBus{dbus::Bus::Options{}};
27}
28
29scoped_refptr<dbus::MockObjectProxy> PatchPanelMockProxy(dbus::MockBus* dbus) {
30 return new dbus::MockObjectProxy(dbus, kPatchPanelServiceName,
31 dbus::ObjectPath(kPatchPanelServicePath));
32}
33
34} // namespace
35
36TEST(ClientTest, ConnectNamespace) {
37 auto dbus = MockDBus();
38 auto proxy = PatchPanelMockProxy(dbus.get());
39 pid_t pid = 3456;
40 std::string outboud_ifname = "";
41
42 Client client(dbus, proxy.get());
43
44 // Failure case
45 auto result = client.ConnectNamespace(pid, outboud_ifname, false);
46 EXPECT_FALSE(result.first.is_valid());
Hugo Benichi2fd0c6e2020-04-17 16:12:05 +090047 EXPECT_TRUE(result.second.peer_ifname().empty());
48 EXPECT_TRUE(result.second.host_ifname().empty());
49 EXPECT_EQ(0, result.second.peer_ipv4_address());
50 EXPECT_EQ(0, result.second.host_ipv4_address());
Hugo Benichicc6850f2020-01-17 13:26:06 +090051 EXPECT_EQ(0, result.second.ipv4_subnet().base_addr());
52 EXPECT_EQ(0, result.second.ipv4_subnet().prefix_len());
Hugo Benichicc6850f2020-01-17 13:26:06 +090053
54 // Success case
55 patchpanel::ConnectNamespaceResponse response_proto;
Hugo Benichi2fd0c6e2020-04-17 16:12:05 +090056 response_proto.set_peer_ifname("veth0");
57 response_proto.set_host_ifname("arc_ns0");
58 response_proto.set_peer_ipv4_address(Ipv4Addr(100, 115, 92, 130));
59 response_proto.set_host_ipv4_address(Ipv4Addr(100, 115, 92, 129));
Hugo Benichicc6850f2020-01-17 13:26:06 +090060 auto* response_subnet = response_proto.mutable_ipv4_subnet();
61 response_subnet->set_prefix_len(30);
62 response_subnet->set_base_addr(Ipv4Addr(100, 115, 92, 128));
Hugo Benichicc6850f2020-01-17 13:26:06 +090063 std::unique_ptr<dbus::Response> response = dbus::Response::CreateEmpty();
64 dbus::MessageWriter response_writer(response.get());
65 response_writer.AppendProtoAsArrayOfBytes(response_proto);
66 EXPECT_CALL(*proxy, CallMethodAndBlock(_, _))
67 .WillOnce(Return(ByMove(std::move(response))));
68
69 result = client.ConnectNamespace(pid, outboud_ifname, false);
70 EXPECT_TRUE(result.first.is_valid());
Hugo Benichi2fd0c6e2020-04-17 16:12:05 +090071 EXPECT_EQ("arc_ns0", result.second.host_ifname());
72 EXPECT_EQ("veth0", result.second.peer_ifname());
Hugo Benichicc6850f2020-01-17 13:26:06 +090073 EXPECT_EQ(30, result.second.ipv4_subnet().prefix_len());
74 EXPECT_EQ(Ipv4Addr(100, 115, 92, 128),
75 result.second.ipv4_subnet().base_addr());
Hugo Benichi2fd0c6e2020-04-17 16:12:05 +090076 EXPECT_EQ(Ipv4Addr(100, 115, 92, 129), result.second.host_ipv4_address());
77 EXPECT_EQ(Ipv4Addr(100, 115, 92, 130), result.second.peer_ipv4_address());
Hugo Benichicc6850f2020-01-17 13:26:06 +090078}
79
80} // namespace patchpanel