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/BUILD.gn b/patchpanel/BUILD.gn
index 7eb382d..9abb963 100644
--- a/patchpanel/BUILD.gn
+++ b/patchpanel/BUILD.gn
@@ -8,7 +8,6 @@
 group("all") {
   deps = [
     ":libpatchpanel",
-    ":libpatchpanel-client",
     ":libpatchpanel-util",
     ":patchpaneld",
   ]
@@ -18,7 +17,6 @@
       ":firewall_fuzzer",
       ":multicast_forwarder_fuzzer",
       ":ndproxy_fuzzer",
-      ":patchpanel_client_fuzzer",
     ]
   }
   if (use.test) {
@@ -114,16 +112,6 @@
   deps = [ ":protos" ]
 }
 
-shared_library("libpatchpanel-client") {
-  configs += [ ":target_defaults" ]
-  all_dependent_pkg_deps = [
-    "protobuf",
-    "system_api",
-  ]
-  sources = [ "client.cc" ]
-  sources += util_sources
-}
-
 executable("patchpaneld") {
   sources = [ "main.cc" ]
   configs += [ ":target_defaults" ]
@@ -165,19 +153,6 @@
     deps = [ ":libpatchpanel" ]
   }
 
-  executable("patchpanel_client_fuzzer") {
-    configs += [
-      "//common-mk/common_fuzzer",
-      ":target_defaults",
-      ":fuzzing_config",
-    ]
-    sources = [ "client_fuzzer.cc" ]
-    deps = [
-      ":libpatchpanel",
-      ":libpatchpanel-client",
-    ]
-  }
-
   executable("firewall_fuzzer") {
     configs += [
       "//common-mk/common_fuzzer",
@@ -198,7 +173,6 @@
     sources = [
       "address_manager_test.cc",
       "arc_service_test.cc",
-      "client_test.cc",
       "counters_service_test.cc",
       "datapath_test.cc",
       "firewall_test.cc",
@@ -220,7 +194,7 @@
     ]
     defines = [ "UNIT_TEST" ]
     deps = [
-      ":libpatchpanel-client",
+      ":libpatchpanel-util",
       ":libpatchpanel_test",
       "//common-mk/testrunner",
     ]
diff --git a/patchpanel/dbus/BUILD.gn b/patchpanel/dbus/BUILD.gn
new file mode 100644
index 0000000..1ee5077
--- /dev/null
+++ b/patchpanel/dbus/BUILD.gn
@@ -0,0 +1,97 @@
+# 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.
+
+import("//common-mk/pkg_config.gni")
+import("//common-mk/proto_library.gni")
+
+group("all") {
+  deps = [ ":libpatchpanel-client" ]
+  if (use.fuzzer) {
+    deps += [ ":patchpanel_client_fuzzer" ]
+  }
+  if (use.test) {
+    deps += [ ":patchpanel-client_testrunner" ]
+  }
+}
+
+pkg_config("target_defaults") {
+  pkg_deps = [
+    "libbrillo",
+    "libchrome-${libbase_ver}",
+    "system_api",
+  ]
+  if (use.fuzzer) {
+    pkg_deps += [ "protobuf" ]
+  } else {
+    pkg_deps += [ "protobuf-lite" ]
+  }
+  defines = [ "USE_ARCVM=${use.arcvm}" ]
+}
+
+proto_library("protos") {
+  configs = [ ":target_defaults" ]
+  proto_in_dir = ".."
+  proto_out_dir = "include/patchpanel"
+  sources = [ "${proto_in_dir}/ipc.proto" ]
+}
+
+libpatchpanel_client_sources = [
+  "../net_util.cc",
+  "client.cc",
+]
+
+static_library("libpatchpanel-client_test") {
+  configs += [ ":target_defaults" ]
+  all_dependent_pkg_deps = [
+    "protobuf",
+    "system_api",
+  ]
+  sources = libpatchpanel_client_sources
+  deps = [ ":protos" ]
+}
+
+shared_library("libpatchpanel-client") {
+  configs += [ ":target_defaults" ]
+  all_dependent_pkg_deps = [
+    "protobuf",
+    "system_api",
+  ]
+  sources = libpatchpanel_client_sources
+}
+
+if (use.fuzzer) {
+  pkg_config("fuzzing_config") {
+    pkg_deps = [ "libchrome-test-${libbase_ver}" ]
+  }
+
+  executable("patchpanel_client_fuzzer") {
+    configs += [
+      "//common-mk/common_fuzzer",
+      ":target_defaults",
+      ":fuzzing_config",
+    ]
+    sources = [ "client_fuzzer.cc" ]
+    deps = [ ":libpatchpanel-client" ]
+  }
+}
+
+if (use.test) {
+  pkg_config("test_config") {
+    pkg_deps = [ "libchrome-test-${libbase_ver}" ]
+  }
+
+  executable("patchpanel-client_testrunner") {
+    sources = [ "client_test.cc" ]
+    configs += [
+      "//common-mk:test",
+      ":target_defaults",
+      ":test_config",
+    ]
+    defines = [ "UNIT_TEST" ]
+    deps = [
+      ":libpatchpanel-client_test",
+      "//common-mk/testrunner",
+    ]
+  }
+}
diff --git a/patchpanel/dbus/OWNERS b/patchpanel/dbus/OWNERS
new file mode 100644
index 0000000..c9cce26
--- /dev/null
+++ b/patchpanel/dbus/OWNERS
@@ -0,0 +1 @@
+include /patchpanel/OWNERS
diff --git a/patchpanel/client.cc b/patchpanel/dbus/client.cc
similarity index 99%
rename from patchpanel/client.cc
rename to patchpanel/dbus/client.cc
index e5cffdd..8f55543 100644
--- a/patchpanel/client.cc
+++ b/patchpanel/dbus/client.cc
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "patchpanel/client.h"
+#include "patchpanel/dbus/client.h"
 
 #include <fcntl.h>
 
diff --git a/patchpanel/client.h b/patchpanel/dbus/client.h
similarity index 96%
rename from patchpanel/client.h
rename to patchpanel/dbus/client.h
index d10eafc..cefeddc 100644
--- a/patchpanel/client.h
+++ b/patchpanel/dbus/client.h
@@ -2,10 +2,11 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef PATCHPANEL_CLIENT_H_
-#define PATCHPANEL_CLIENT_H_
+#ifndef PATCHPANEL_DBUS_CLIENT_H_
+#define PATCHPANEL_DBUS_CLIENT_H_
 
 #include <memory>
+#include <set>
 #include <string>
 #include <utility>
 #include <vector>
@@ -98,4 +99,4 @@
 
 }  // namespace patchpanel
 
-#endif  // PATCHPANEL_CLIENT_H_
+#endif  // PATCHPANEL_DBUS_CLIENT_H_
diff --git a/patchpanel/client_fuzzer.cc b/patchpanel/dbus/client_fuzzer.cc
similarity index 98%
rename from patchpanel/client_fuzzer.cc
rename to patchpanel/dbus/client_fuzzer.cc
index ae811e4..75a328f 100644
--- a/patchpanel/client_fuzzer.cc
+++ b/patchpanel/dbus/client_fuzzer.cc
@@ -8,7 +8,7 @@
 #include <dbus/message.h>
 #include <fuzzer/FuzzedDataProvider.h>
 
-#include "patchpanel/client.h"
+#include "patchpanel/dbus/client.h"
 
 namespace patchpanel {
 
@@ -61,7 +61,7 @@
 };
 
 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
-  // static Environment env;
+  static Environment env;
   dbus::Bus::Options options;
   scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
   scoped_refptr<dbus::ObjectProxy> proxy(new FakeObjectProxy(bus.get()));
diff --git a/patchpanel/client_test.cc b/patchpanel/dbus/client_test.cc
similarity index 98%
rename from patchpanel/client_test.cc
rename to patchpanel/dbus/client_test.cc
index 4cfbc9f..01c7b30 100644
--- a/patchpanel/client_test.cc
+++ b/patchpanel/dbus/client_test.cc
@@ -2,15 +2,15 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "patchpanel/client.h"
+#include "patchpanel/dbus/client.h"
 
 #include <chromeos/dbus/service_constants.h>
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
 #include <dbus/message.h>
-#include <dbus/object_path.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"
 
diff --git a/patchpanel/libpatchpanel-client.pc.in b/patchpanel/dbus/libpatchpanel-client.pc.in
similarity index 100%
rename from patchpanel/libpatchpanel-client.pc.in
rename to patchpanel/dbus/libpatchpanel-client.pc.in
diff --git a/patchpanel/dbus/preinstall.sh b/patchpanel/dbus/preinstall.sh
new file mode 100755
index 0000000..7d2484f
--- /dev/null
+++ b/patchpanel/dbus/preinstall.sh
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+# 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.
+
+set -e
+
+v=$1
+include_dir=$2
+out=$3
+
+sed \
+  -e "s/@BSLOT@/${v}/g" \
+  -e "s:@INCLUDE_DIR@:${include_dir}:g" \
+  "libpatchpanel-client.pc.in" > "${out}/libpatchpanel-client.pc"
diff --git a/patchpanel/preinstall.sh b/patchpanel/preinstall.sh
index dfbef6d..c05d2df 100755
--- a/patchpanel/preinstall.sh
+++ b/patchpanel/preinstall.sh
@@ -14,8 +14,3 @@
   -e "s/@BSLOT@/${v}/g" \
   -e "s:@INCLUDE_DIR@:${include_dir}:g" \
   "libpatchpanel-util.pc.in" > "${out}/libpatchpanel-util.pc"
-
-sed \
-  -e "s/@BSLOT@/${v}/g" \
-  -e "s:@INCLUDE_DIR@:${include_dir}:g" \
-  "libpatchpanel-client.pc.in" > "${out}/libpatchpanel-client.pc"