Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83c945db authored by Ken Chen's avatar Ken Chen Committed by Android (Google) Code Review
Browse files

Merge "Add a .pbtxt to .pb auto converter" into rvc-dev

parents fbc434b3 7ded4b36
Loading
Loading
Loading
Loading
+41 −1
Original line number Diff line number Diff line
@@ -16,6 +16,46 @@ cc_test_library {
    ],
}

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 @@ cc_test {
    // 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",
    ],
+1 −2
Original line number Diff line number Diff line
@@ -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 {
+92 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+16 −4
Original line number Diff line number Diff line
@@ -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 @@ enum class DnsProtocol { CLEARTEXT, TLS };
// 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 @@ const std::vector<std::string> kGoldFilesGetHostByNameTls = {
// 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 @@ TEST_P(ResolvGoldTest, GoldData) {
        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();
−672 B

File deleted.

Loading