| /* | |
| * Copyright (C) 2024 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 "DnsResolverService.h" | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| #include <thread> | |
| #include <aidl/android/net/resolv/unstable/BnDnsResolverUnstableCallback.h> | |
| #include <gtest/gtest.h> | |
| #include <netdutils/NetNativeTestBase.h> | |
| #include <private/android_filesystem_config.h> // AID_SYSTEM, AID_ROOT | |
| #include "DnsResolverUnstableService.h" | |
| #include "tests/resolv_test_utils.h" | |
| #include "util.h" | |
| namespace android { | |
| namespace net { | |
| using aidl::android::net::resolv::unstable::BnDnsResolverUnstableCallback; | |
| using aidl::android::net::resolv::unstable::IDnsResolverUnstableCallback; | |
| class TestUnstableCallback : public BnDnsResolverUnstableCallback { | |
| public: | |
| explicit TestUnstableCallback(bool allow) : mAllow(allow) {} | |
| ::ndk::ScopedAStatus checkPermission(const std::string& /*permission*/, int32_t /*pid*/, | |
| int32_t /*uid*/, bool* _aidl_return) override { | |
| *_aidl_return = mAllow; | |
| return ::ndk::ScopedAStatus::ok(); | |
| } | |
| private: | |
| bool mAllow; | |
| }; | |
| class DnsResolverServiceTest : public NetNativeTestBase { | |
| protected: | |
| ::ndk::ScopedAStatus getUnstableInterface(const std::shared_ptr<DnsResolverService>& service, | |
| ::ndk::SpAIBinder* binder) { | |
| ::ndk::ScopedAStatus status; | |
| std::thread t([&]() { status = service->getUnstableInterface(binder); }); | |
| t.join(); | |
| return status; | |
| } | |
| }; | |
| TEST_F(DnsResolverServiceTest, GetInstance_ReturnsSameInstance) { | |
| auto& instance1 = DnsResolverService::getInstance(); | |
| auto& instance2 = DnsResolverService::getInstance(); | |
| EXPECT_EQ(&instance1, &instance2); | |
| } | |
| TEST_F(DnsResolverServiceTest, CheckCallingPermission_WithExplicitCallback) { | |
| auto allowedCallback = ::ndk::SharedRefBase::make<TestUnstableCallback>(true); | |
| auto deniedCallback = ::ndk::SharedRefBase::make<TestUnstableCallback>(false); | |
| constexpr uid_t kUid = 12345; | |
| constexpr pid_t kPid = 6789; | |
| EXPECT_TRUE( | |
| checkCallingPermission("android.permission.TEST", kPid, kUid, allowedCallback.get())); | |
| EXPECT_FALSE( | |
| checkCallingPermission("android.permission.TEST", kPid, kUid, deniedCallback.get())); | |
| EXPECT_FALSE(checkCallingPermission("android.permission.TEST", kPid, kUid, nullptr)); | |
| } | |
| TEST_F(DnsResolverServiceTest, GetUnstableInterface_SystemUid) { | |
| // We need to run this test as root to be able to change UID. | |
| ASSERT_EQ(static_cast<uid_t>(AID_ROOT), getuid()); | |
| auto service = ::ndk::SharedRefBase::make<DnsResolverService>(); | |
| ::ndk::SpAIBinder binder; | |
| { | |
| ScopedChangeUID scopedUid(AID_SYSTEM); | |
| // First call by AID_SYSTEM succeeds | |
| EXPECT_TRUE(getUnstableInterface(service, &binder).isOk()); | |
| // Second call by AID_SYSTEM fails with EX_UNSUPPORTED_OPERATION | |
| auto status = getUnstableInterface(service, &binder); | |
| EXPECT_FALSE(status.isOk()); | |
| EXPECT_EQ(EX_UNSUPPORTED_OPERATION, status.getExceptionCode()); | |
| } | |
| } | |
| TEST_F(DnsResolverServiceTest, GetUnstableInterface_UnauthorizedUid) { | |
| // We need to run this test as root to be able to change UID. | |
| ASSERT_EQ(static_cast<uid_t>(AID_ROOT), getuid()); | |
| auto service = ::ndk::SharedRefBase::make<DnsResolverService>(); | |
| ::ndk::SpAIBinder binder; | |
| { | |
| constexpr uid_t kUnauthorizedUid = 12345; | |
| ScopedChangeUID scopedUid(kUnauthorizedUid); | |
| auto status = getUnstableInterface(service, &binder); | |
| EXPECT_FALSE(status.isOk()); | |
| EXPECT_EQ(EX_SECURITY, status.getExceptionCode()); | |
| } | |
| } | |
| TEST_F(DnsResolverServiceTest, GetUnstableInterface_NetworkStackUid) { | |
| // We need to run this test as root to be able to change UID. | |
| ASSERT_EQ(static_cast<uid_t>(AID_ROOT), getuid()); | |
| auto service = ::ndk::SharedRefBase::make<DnsResolverService>(); | |
| ::ndk::SpAIBinder binder; | |
| { | |
| ScopedChangeUID scopedUid(AID_NETWORK_STACK); | |
| auto status = getUnstableInterface(service, &binder); | |
| EXPECT_TRUE(status.isOk()); | |
| EXPECT_NE(nullptr, binder.get()); | |
| } | |
| } | |
| TEST_F(DnsResolverServiceTest, GetUnstableInterface_RootUid) { | |
| // When running in resolv_unit_test as root (AID_ROOT == 0), verify behavior based on | |
| // isDebuggable() | |
| ASSERT_EQ(static_cast<uid_t>(AID_ROOT), getuid()); | |
| auto service = ::ndk::SharedRefBase::make<DnsResolverService>(); | |
| ::ndk::SpAIBinder binder; | |
| auto status = getUnstableInterface(service, &binder); | |
| if (isDebuggable()) { | |
| EXPECT_TRUE(status.isOk()); | |
| } else { | |
| EXPECT_FALSE(status.isOk()); | |
| EXPECT_EQ(EX_SECURITY, status.getExceptionCode()); | |
| } | |
| } | |
| TEST_F(DnsResolverServiceTest, UninitializedGuard) { | |
| auto service = ::ndk::SharedRefBase::make<DnsResolverService>(); | |
| auto& unstableService = service->getUnstableService(); | |
| EXPECT_FALSE(unstableService.isInitialized()); | |
| // IDnsResolver calls fail with EX_ILLEGAL_STATE before initialization. | |
| auto status = service->setDnsForwarding(0, std::nullopt); | |
| EXPECT_FALSE(status.isOk()); | |
| EXPECT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode()); | |
| // IDnsResolverUnstable calls fail with EX_ILLEGAL_STATE before initialization. | |
| bool isProxyEnabled; | |
| status = unstableService.isProxyEnabled(&isProxyEnabled); | |
| EXPECT_FALSE(status.isOk()); | |
| EXPECT_EQ(EX_ILLEGAL_STATE, status.getExceptionCode()); | |
| unstableService.initializeForTest(); | |
| EXPECT_TRUE(unstableService.isInitialized()); | |
| // After initialization, calls on IDnsResolver don't fail with EX_ILLEGAL_STATE. | |
| // setDnsForwarding() still returns EX_UNSUPPORTED_OPERATION. | |
| status = service->setDnsForwarding(0, std::nullopt); | |
| EXPECT_FALSE(status.isOk()); | |
| EXPECT_NE(EX_ILLEGAL_STATE, status.getExceptionCode()); | |
| // After initialization, calls on IDnsResolverUnstable don't fail with EX_ILLEGAL_STATE. | |
| status = unstableService.isProxyEnabled(&isProxyEnabled); | |
| EXPECT_TRUE(status.isOk()); | |
| } | |
| } // namespace net | |
| } // namespace android |