Merge "use vector<uint8_t> for byte[] in AIDL"
diff --git a/Android.bp b/Android.bp
index a3ab8e1..d02a403 100644
--- a/Android.bp
+++ b/Android.bp
@@ -23,6 +23,11 @@
         "netd_event_listener_interface",
     ],
     backend: {
+        java: {
+            apex_available: [
+                "com.android.bluetooth.updatable",
+            ],
+        },
         ndk: {
             gen_log: true,
         },
diff --git a/tests/Android.bp b/tests/Android.bp
index bbcd668..b83bbab 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -16,6 +16,46 @@
     ],
 }
 
+cc_library_host_static {
+    name: "golddata_proto_host",
+    proto: {
+        export_proto_headers: true,
+        type: "full",
+    },
+    srcs: [
+        "golddata.proto",
+    ],
+}
+
+cc_binary_host {
+    name: "resolv_gold_test_pbtxt2pb_host",
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+    srcs: ["pbtxt2pb_converter_host.cpp"],
+    static_libs: [
+        "golddata_proto_host",
+        "libc++fs",
+        "libprotobuf-cpp-full",
+    ],
+}
+
+genrule {
+    name: "resolv_gold_test_pbtxt2pb",
+    tools: [
+        "resolv_gold_test_pbtxt2pb_host",
+        "soong_zip",
+    ],
+    srcs: ["testdata/*.pbtxt"],
+    // convert .pbtxt to .pb files; zip them as a single pb.zip.
+    cmd: "mkdir $(genDir)/pb && for fname in $(in); " +
+         "do $(location resolv_gold_test_pbtxt2pb_host) --in_file=$$fname " +
+         "--out_dir=$(genDir)/pb; done && " +
+         "$(location soong_zip) -o $(out) -C $(genDir)/pb -D $(genDir)/pb",
+    out: ["testdata/pb.zip"],
+}
+
 cc_library_static {
     name: "golddata_proto",
     defaults: ["netd_defaults"],
@@ -37,7 +77,7 @@
     // TODO: Remove the xml after MTS fixing the problem.
     test_config: "resolv_gold_test_config.xml",
     defaults: ["netd_defaults", "resolv_test_defaults"],
-    data: ["testdata/pb/*.pb"],
+    data: [":resolv_gold_test_pbtxt2pb"],
     srcs: [
         "resolv_gold_test.cpp",
     ],
diff --git a/tests/golddata.proto b/tests/golddata.proto
index 23a32d0..3a512b4 100644
--- a/tests/golddata.proto
+++ b/tests/golddata.proto
@@ -16,7 +16,6 @@
 
 syntax = "proto3";
 package android.net;
-option optimize_for = LITE_RUNTIME;
 
 // Used to indicate which call is invoked to send DNS lookups.
 enum CallType {
@@ -200,4 +199,4 @@
     // addMappingBinaryPacket() in
     // packages/modules/DnsResolver/tests/dns_responder/dns_responder.cpp.
     repeated PacketMapping packet_mapping = 3;
-}
\ No newline at end of file
+}
diff --git a/tests/pbtxt2pb_converter_host.cpp b/tests/pbtxt2pb_converter_host.cpp
new file mode 100644
index 0000000..90fe455
--- /dev/null
+++ b/tests/pbtxt2pb_converter_host.cpp
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <fcntl.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+#include <google/protobuf/text_format.h>
+
+#include <filesystem>
+
+#include "golddata.pb.h"
+
+using namespace std;
+
+bool ConvertPbtxtToPb(const filesystem::path& pbtxtFile, const filesystem::path& pbOutDir) {
+    // parse plain text from .pbtxt.
+    android::net::GoldTest goldTest;
+
+    int fd = open(pbtxtFile.c_str(), O_RDONLY);
+    if (fd < 0) {
+        cerr << "Failed to open " << pbtxtFile << ", " << strerror(errno) << endl;
+        return false;
+    }
+    {
+        google::protobuf::io::FileInputStream fileInput(fd);
+        fileInput.SetCloseOnDelete(true);
+        if (!google::protobuf::TextFormat::Parse(&fileInput, &goldTest)) {
+            cerr << "Failed to parse " << pbtxtFile << endl;
+            return false;
+        }
+    }
+
+    // write marshalled message into .pb file
+    filesystem::path pbFile = pbOutDir / pbtxtFile.filename();
+    pbFile.replace_extension(".pb");
+    fd = open(pbFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0660);
+    if (fd < 0) {
+        cerr << "Failed to open " << pbFile << ", " << strerror(errno) << endl;
+        return false;
+    }
+    {
+        google::protobuf::io::FileOutputStream fileOutputStream(fd);
+        fileOutputStream.SetCloseOnDelete(true);
+        if (!goldTest.SerializeToZeroCopyStream(&fileOutputStream)) {
+            cerr << "Failed to serialize " << pbFile << endl;
+            filesystem::remove(pbFile);
+            return false;
+        }
+    }
+
+    cout << "Generate " << pbFile << " successfully" << endl;
+    return true;
+}
+
+int main(int argc, char const* const* argv) {
+    filesystem::path pbtxtFile;
+    filesystem::path pbOutDir;
+    const string arg_in = "--in_file=";
+    const string arg_out = "--out_dir=";
+
+    for (int i = 1; i < argc; i++) {
+        std::string arg = argv[i];
+        if (arg.find(arg_in) == 0) {
+            pbtxtFile = filesystem::path(arg.substr(arg_in.size()));
+        } else if (arg.find(arg_out) == 0) {
+            pbOutDir = filesystem::path(arg.substr(arg_out.size()));
+        } else {
+            cerr << "Unknown argument: " << arg << endl;
+            exit(1);
+        }
+    }
+
+    if (pbtxtFile.empty() || pbOutDir.empty()) {
+        cerr << arg_in << " or " << arg_out << " is unassigned" << endl;
+        exit(1);
+    }
+    if (!ConvertPbtxtToPb(pbtxtFile, pbOutDir)) {
+        cerr << "Failed to convert " << pbtxtFile << endl;
+        exit(1);
+    }
+}
diff --git a/tests/resolv_gold_test.cpp b/tests/resolv_gold_test.cpp
index 99bcb92..d8e9a6c 100644
--- a/tests/resolv_gold_test.cpp
+++ b/tests/resolv_gold_test.cpp
@@ -20,6 +20,7 @@
 #include <Fwmark.h>
 #include <android-base/chrono_utils.h>
 #include <android-base/file.h>
+#include <android-base/logging.h>
 #include <android-base/result.h>
 #include <android-base/stringprintf.h>
 #include <gmock/gmock-matchers.h>
@@ -49,9 +50,8 @@
 // TODO: Consider moving to packages/modules/DnsResolver/tests/resolv_test_utils.h.
 constexpr unsigned int MAXPACKET = 8 * 1024;
 
-// The testdata/pb/*.pb are generated from testdata/*.pbtext.
-// TODO: Generate .pb files via precompiler.
-const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/pb/";
+// The testdata/*.pb are generated from testdata/*.pbtext.
+const std::string kTestDataPath = android::base::GetExecutableDirectory() + "/testdata/";
 const std::vector<std::string> kGoldFilesGetAddrInfo = {
         "getaddrinfo.topsite.google.pb",    "getaddrinfo.topsite.youtube.pb",
         "getaddrinfo.topsite.amazon.pb",    "getaddrinfo.topsite.yahoo.pb",
@@ -66,6 +66,18 @@
 // Fixture test class definition.
 class TestBase : public ::testing::Test {
   protected:
+    static void SetUpTestSuite() {
+        // Unzip *.pb from pb.zip. The unzipped files get 777 permission by default. Remove execute
+        // permission so that Trade Federation test harness has no chance mis-executing on *.pb.
+        const std::string unzipCmd = "unzip -o " + kTestDataPath + "pb.zip -d " + kTestDataPath +
+                                     "&& chmod -R 666 " + kTestDataPath;
+        // NOLINTNEXTLINE(cert-env33-c)
+        if (W_EXITCODE(0, 0) != system(unzipCmd.c_str())) {
+            LOG(ERROR) << "fail to inflate .pb files";
+            GTEST_LOG_(FATAL) << "fail to inflate .pb files";
+        }
+    }
+
     void SetUp() override {
         // Create cache for test
         resolv_create_cache_for_net(TEST_NETID);
@@ -423,7 +435,7 @@
         tls.clearQueries();
     }
 
-    // Read test configuration from proto text file to proto.
+    // Read test configuration from serialized binary to proto.
     const Result<GoldTest> result = ToProto(file);
     ASSERT_TRUE(result.ok()) << result.error().message();
     const GoldTest& goldtest = result.value();
diff --git a/tests/testdata/pb/getaddrinfo.tls.topsite.google.pb b/tests/testdata/pb/getaddrinfo.tls.topsite.google.pb
deleted file mode 100644
index f314333..0000000
--- a/tests/testdata/pb/getaddrinfo.tls.topsite.google.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.amazon.pb b/tests/testdata/pb/getaddrinfo.topsite.amazon.pb
deleted file mode 100644
index 7f9f047..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.amazon.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.bing.pb b/tests/testdata/pb/getaddrinfo.topsite.bing.pb
deleted file mode 100644
index 714adfb..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.bing.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.ebay.pb b/tests/testdata/pb/getaddrinfo.topsite.ebay.pb
deleted file mode 100644
index 679320a..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.ebay.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.facebook.pb b/tests/testdata/pb/getaddrinfo.topsite.facebook.pb
deleted file mode 100644
index f3a8015..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.facebook.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.google.pb b/tests/testdata/pb/getaddrinfo.topsite.google.pb
deleted file mode 100644
index 70502b8..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.google.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.netflix.pb b/tests/testdata/pb/getaddrinfo.topsite.netflix.pb
deleted file mode 100644
index ed415fa..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.netflix.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.reddit.pb b/tests/testdata/pb/getaddrinfo.topsite.reddit.pb
deleted file mode 100644
index fe8eb22..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.reddit.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.wikipedia.pb b/tests/testdata/pb/getaddrinfo.topsite.wikipedia.pb
deleted file mode 100644
index 56455b4..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.wikipedia.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.yahoo.pb b/tests/testdata/pb/getaddrinfo.topsite.yahoo.pb
deleted file mode 100644
index 924378e..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.yahoo.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/getaddrinfo.topsite.youtube.pb b/tests/testdata/pb/getaddrinfo.topsite.youtube.pb
deleted file mode 100644
index c6b0204..0000000
--- a/tests/testdata/pb/getaddrinfo.topsite.youtube.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/gethostbyname.tls.topsite.youtube.pb b/tests/testdata/pb/gethostbyname.tls.topsite.youtube.pb
deleted file mode 100644
index 6253c22..0000000
--- a/tests/testdata/pb/gethostbyname.tls.topsite.youtube.pb
+++ /dev/null
Binary files differ
diff --git a/tests/testdata/pb/gethostbyname.topsite.youtube.pb b/tests/testdata/pb/gethostbyname.topsite.youtube.pb
deleted file mode 100644
index 505b40a..0000000
--- a/tests/testdata/pb/gethostbyname.topsite.youtube.pb
+++ /dev/null
Binary files differ