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

Commit 628db71c authored by Junyu Lai's avatar Junyu Lai Committed by Gerrit Code Review
Browse files

Merge "Refactor 1.0 TetheroffloadControl VTS"

parents 4b276a76 6f2cd06e
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -15,10 +15,18 @@
cc_test {
    name: "VtsHalTetheroffloadControlV1_0TargetTest",
    defaults: ["VtsHalTargetTestDefaults"],
    srcs: ["VtsHalTetheroffloadControlV1_0TargetTest.cpp"],
    local_include_dirs: ["include"],
    srcs: [
        "VtsHalTetheroffloadControlV1_0TargetTest.cpp",
        "OffloadControlTestBase.cpp",
        "OffloadControlTestUtils.cpp",
    ],
    static_libs: [
        "android.hardware.tetheroffload.config@1.0",
        "android.hardware.tetheroffload.control@1.0",
    ],
    test_suites: ["general-tests", "vts"],
    test_suites: [
        "general-tests",
        "vts",
    ],
}
+94 −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 <OffloadControlTestBase.h>

void OffloadControlTestBase::TearDown() {
    // For good measure, the teardown should try stopOffload() once more, since
    // different HAL call test cycles might enter this function. Also the
    // return code cannot be actually expected for all cases, hence ignore it.
    stopOffload(ExpectBoolean::Ignored);
}

// The IOffloadConfig HAL is tested more thoroughly elsewhere. Here the class
// just setup everything correctly and verify basic readiness.
void OffloadControlTestBase::setupConfigHal() {
    config = IOffloadConfig::getService(std::get<0>(GetParam()));
    ASSERT_NE(nullptr, config.get()) << "Could not get HIDL instance";

    unique_fd fd1(conntrackSocket(NF_NETLINK_CONNTRACK_NEW | NF_NETLINK_CONNTRACK_DESTROY));
    if (fd1.get() < 0) {
        ALOGE("Unable to create conntrack handles: %d/%s", errno, strerror(errno));
        FAIL();
    }
    native_handle_t* const nativeHandle1 = native_handle_create(1, 0);
    nativeHandle1->data[0] = fd1.release();
    hidl_handle h1;
    h1.setTo(nativeHandle1, true);

    unique_fd fd2(conntrackSocket(NF_NETLINK_CONNTRACK_UPDATE | NF_NETLINK_CONNTRACK_DESTROY));
    if (fd2.get() < 0) {
        ALOGE("Unable to create conntrack handles: %d/%s", errno, strerror(errno));
        FAIL();
    }
    native_handle_t* const nativeHandle2 = native_handle_create(1, 0);
    nativeHandle2->data[0] = fd2.release();
    hidl_handle h2;
    h2.setTo(nativeHandle2, true);

    const Return<void> ret = config->setHandles(h1, h2, ASSERT_TRUE_CALLBACK);
    ASSERT_TRUE(ret.isOk());
}

void OffloadControlTestBase::prepareControlHal() {
    control = createControl(std::get<1>(GetParam()));
    ASSERT_NE(nullptr, control.get()) << "Could not get HIDL instance";

    control_cb = new TetheringOffloadCallback();
    ASSERT_NE(nullptr, control_cb.get()) << "Could not get get offload callback";
}

void OffloadControlTestBase::initOffload(const bool expected_result) {
    auto init_cb = [&](bool success, std::string errMsg) {
        std::string msg = StringPrintf("Unexpectedly %s to init offload: %s",
                                       success ? "succeeded" : "failed", errMsg.c_str());
        ASSERT_EQ(expected_result, success) << msg;
    };
    const Return<void> ret = control->initOffload(control_cb, init_cb);
    ASSERT_TRUE(ret.isOk());
}

void OffloadControlTestBase::setupControlHal() {
    prepareControlHal();
    initOffload(true);
}

void OffloadControlTestBase::stopOffload(const ExpectBoolean value) {
    auto cb = [&](bool success, const hidl_string& errMsg) {
        switch (value) {
            case ExpectBoolean::False:
                ASSERT_EQ(false, success) << "Unexpectedly able to stop offload: " << errMsg;
                break;
            case ExpectBoolean::True:
                ASSERT_EQ(true, success) << "Unexpectedly failed to stop offload: " << errMsg;
                break;
            case ExpectBoolean::Ignored:
                break;
        }
    };
    const Return<void> ret = control->stopOffload(cb);
    ASSERT_TRUE(ret.isOk());
}
+53 −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 <OffloadControlTestUtils.h>
#include <android-base/unique_fd.h>

using android::base::unique_fd;

inline const sockaddr* asSockaddr(const sockaddr_nl* nladdr) {
    return reinterpret_cast<const sockaddr*>(nladdr);
}

int conntrackSocket(unsigned groups) {
    unique_fd s(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_NETFILTER));
    if (s.get() < 0) {
        return -errno;
    }

    const struct sockaddr_nl bind_addr = {
            .nl_family = AF_NETLINK,
            .nl_pad = 0,
            .nl_pid = 0,
            .nl_groups = groups,
    };
    if (::bind(s.get(), asSockaddr(&bind_addr), sizeof(bind_addr)) < 0) {
        return -errno;
    }

    const struct sockaddr_nl kernel_addr = {
            .nl_family = AF_NETLINK,
            .nl_pad = 0,
            .nl_pid = 0,
            .nl_groups = groups,
    };
    if (connect(s.get(), asSockaddr(&kernel_addr), sizeof(kernel_addr)) != 0) {
        return -errno;
    }

    return s.release();
}
 No newline at end of file
+53 −262

File changed.

Preview size limit exceeded, changes collapsed.

+105 −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.
 */

#pragma once

#include <OffloadControlTestUtils.h>
#include <VtsHalHidlTargetCallbackBase.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
#include <android/hardware/tetheroffload/config/1.0/IOffloadConfig.h>
#include <android/hardware/tetheroffload/control/1.0/IOffloadControl.h>
#include <android/hardware/tetheroffload/control/1.0/types.h>
#include <gtest/gtest.h>
#include <linux/netfilter/nfnetlink.h>
#include <log/log.h>

using android::sp;
using android::base::StringPrintf;
using android::base::unique_fd;
using android::hardware::hidl_handle;
using android::hardware::hidl_string;
using android::hardware::hidl_vec;
using android::hardware::Return;
using android::hardware::Void;
using android::hardware::tetheroffload::config::V1_0::IOffloadConfig;
using android::hardware::tetheroffload::control::V1_0::IOffloadControl;
using android::hardware::tetheroffload::control::V1_0::ITetheringOffloadCallback;
using android::hardware::tetheroffload::control::V1_0::NatTimeoutUpdate;
using android::hardware::tetheroffload::control::V1_0::OffloadCallbackEvent;

constexpr char kCallbackOnEvent[] = "onEvent";
constexpr char kCallbackUpdateTimeout[] = "updateTimeout";

enum class ExpectBoolean {
    Ignored = -1,
    False = 0,
    True = 1,
};

class TetheringOffloadCallbackArgs {
  public:
    OffloadCallbackEvent last_event;
    NatTimeoutUpdate last_params;
};

class OffloadControlTestBase : public testing::TestWithParam<std::tuple<std::string, std::string>> {
  public:
    virtual void SetUp() = 0;

    virtual void TearDown();

    // Called once in setup stage to retrieve correct version of
    // IOffloadControl object.
    virtual sp<IOffloadControl> createControl(const std::string& serviceName) = 0;

    // The IOffloadConfig HAL is tested more thoroughly elsewhere. Here the
    // class just setup everything correctly and verify basic readiness.
    void setupConfigHal();

    void prepareControlHal();

    void initOffload(const bool expected_result);

    void setupControlHal();

    void stopOffload(const ExpectBoolean value);

    // Callback class for both events and NAT timeout updates.
    class TetheringOffloadCallback
        : public testing::VtsHalHidlTargetCallbackBase<TetheringOffloadCallbackArgs>,
          public ITetheringOffloadCallback {
      public:
        TetheringOffloadCallback() = default;
        virtual ~TetheringOffloadCallback() = default;

        Return<void> onEvent(OffloadCallbackEvent event) override {
            const TetheringOffloadCallbackArgs args{.last_event = event};
            NotifyFromCallback(kCallbackOnEvent, args);
            return Void();
        };

        Return<void> updateTimeout(const NatTimeoutUpdate& params) override {
            const TetheringOffloadCallbackArgs args{.last_params = params};
            NotifyFromCallback(kCallbackUpdateTimeout, args);
            return Void();
        };
    };

    sp<IOffloadConfig> config;
    sp<IOffloadControl> control;
    sp<TetheringOffloadCallback> control_cb;
};
 No newline at end of file
Loading