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

Commit a60ff681 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Installd: Refactor binder tests"

parents 4cf7bb91 f448cede
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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 <binder/Status.h>
#include <gtest/gtest.h>
#include <utils/String8.h>

#define ASSERT_BINDER_SUCCESS(expr)                                              \
    ({                                                                           \
        binder::Status expect_status = (expr);                                   \
        ASSERT_TRUE(expect_status.isOk()) << expect_status.toString8().c_str();  \
        expect_status;                                                           \
    })
#define ASSERT_BINDER_FAIL(expr)                \
    ({                                          \
        binder::Status expect_status = (expr);  \
        ASSERT_FALSE(expect_status.isOk());     \
        expect_status;                          \
    })
#define EXPECT_BINDER_SUCCESS(expr)                                              \
    ({                                                                           \
        binder::Status expect_status = (expr);                                   \
        EXPECT_TRUE(expect_status.isOk()) << expect_status.toString8().c_str();  \
        expect_status;                                                           \
    })
#define EXPECT_BINDER_FAIL(expr)                \
    ({                                          \
        binder::Status expect_status = (expr);  \
        EXPECT_FALSE(expect_status.isOk());     \
        expect_status;                          \
    })
+15 −21
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>

#include <binder/Status.h>
#include <cutils/properties.h>

#include <gtest/gtest.h>
@@ -33,6 +33,7 @@
#include <selinux/android.h>
#include <selinux/avc.h>

#include "binder_test_utils.h"
#include "dexopt.h"
#include "InstalldNativeService.h"
#include "globals.h"
@@ -451,11 +452,9 @@ protected:
        std::unique_ptr<std::string> compilation_reason_ptr(new std::string("test-reason"));

        bool prof_result;
        binder::Status prof_binder_result = service_->prepareAppProfile(
        ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
                package_name_, kTestUserId, kTestAppId, *profile_name_ptr, apk_path_,
                /*dex_metadata*/ nullptr, &prof_result);

        ASSERT_TRUE(prof_binder_result.isOk()) << prof_binder_result.toString8().c_str();
                /*dex_metadata*/ nullptr, &prof_result));
        ASSERT_TRUE(prof_result);

        binder::Status result = service_->dexopt(apk_path_,
@@ -760,9 +759,8 @@ class ProfileTest : public DexoptTest {
    void createProfileSnapshot(int32_t appid, const std::string& package_name,
            bool expected_result) {
        bool result;
        binder::Status binder_result = service_->createProfileSnapshot(
                appid, package_name, kPrimaryProfile, apk_path_, &result);
        ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
        ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
                appid, package_name, kPrimaryProfile, apk_path_, &result));
        ASSERT_EQ(expected_result, result);

        if (!expected_result) {
@@ -802,9 +800,8 @@ class ProfileTest : public DexoptTest {
                              const std::string& code_path,
                              bool expected_result) {
        bool result;
        binder::Status binder_result = service_->mergeProfiles(
                kTestAppUid, package_name, code_path, &result);
        ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
        ASSERT_BINDER_SUCCESS(service_->mergeProfiles(
                kTestAppUid, package_name, code_path, &result));
        ASSERT_EQ(expected_result, result);

        if (!expected_result) {
@@ -830,10 +827,9 @@ class ProfileTest : public DexoptTest {
    void preparePackageProfile(const std::string& package_name, const std::string& profile_name,
            bool expected_result) {
        bool result;
        binder::Status binder_result = service_->prepareAppProfile(
        ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
                package_name, kTestUserId, kTestAppId, profile_name, apk_path_,
                /*dex_metadata*/ nullptr, &result);
        ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
                /*dex_metadata*/ nullptr, &result));
        ASSERT_EQ(expected_result, result);

        if (!expected_result) {
@@ -919,8 +915,7 @@ TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
    SetupProfiles(/*setup_ref*/ true);
    createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);

    binder::Status binder_result = service_->destroyProfileSnapshot(package_name_, kPrimaryProfile);
    ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
    ASSERT_BINDER_SUCCESS(service_->destroyProfileSnapshot(package_name_, kPrimaryProfile));
    struct stat st;
    ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
    ASSERT_EQ(ENOENT, errno);
@@ -978,7 +973,7 @@ TEST_F(ProfileTest, ProfileDirOkAfterFixup) {
    ASSERT_EQ(0, chmod(ref_profile_dir.c_str(), 0700));

    // Run createAppData again which will offer to fix-up the profile directories.
    ASSERT_TRUE(service_->createAppData(
    ASSERT_BINDER_SUCCESS(service_->createAppData(
            volume_uuid_,
            package_name_,
            kTestUserId,
@@ -986,7 +981,7 @@ TEST_F(ProfileTest, ProfileDirOkAfterFixup) {
            kTestAppUid,
            se_info_,
            kOSdkVersion,
            &ce_data_inode_).isOk());
            &ce_data_inode_));

    // Check the file access.
    CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
@@ -1032,9 +1027,8 @@ class BootProfileTest : public ProfileTest {

    void createBootImageProfileSnapshot(const std::string& classpath, bool expected_result) {
        bool result;
        binder::Status binder_result = service_->createProfileSnapshot(
                -1, "android", "android.prof", classpath, &result);
        ASSERT_TRUE(binder_result.isOk());
        ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
                -1, "android", "android.prof", classpath, &result));
        ASSERT_EQ(expected_result, result);

        if (!expected_result) {
+34 −29
Original line number Diff line number Diff line
@@ -15,18 +15,23 @@
 */

#include <sstream>
#include <string>

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>
#include <sys/xattr.h>

#include <android-base/logging.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/scopeguard.h>
#include <android-base/stringprintf.h>
#include <cutils/properties.h>
#include <gtest/gtest.h>

#include "binder_test_utils.h"
#include "InstalldNativeService.h"
#include "dexopt.h"
#include "globals.h"
@@ -164,8 +169,8 @@ TEST_F(ServiceTest, HashSecondaryDex) {

    std::vector<uint8_t> result;
    std::string dexPath = get_full_path("com.example/foo/file");
    EXPECT_TRUE(service->hashSecondaryDexFile(
        dexPath, "com.example", 10000, testUuid, FLAG_STORAGE_CE, &result).isOk());
    EXPECT_BINDER_SUCCESS(service->hashSecondaryDexFile(
        dexPath, "com.example", 10000, testUuid, FLAG_STORAGE_CE, &result));

    EXPECT_EQ(result.size(), 32U);

@@ -184,8 +189,8 @@ TEST_F(ServiceTest, HashSecondaryDex_NoSuch) {

    std::vector<uint8_t> result;
    std::string dexPath = get_full_path("com.example/foo/file");
    EXPECT_TRUE(service->hashSecondaryDexFile(
        dexPath, "com.example", 10000, testUuid, FLAG_STORAGE_CE, &result).isOk());
    EXPECT_BINDER_SUCCESS(service->hashSecondaryDexFile(
        dexPath, "com.example", 10000, testUuid, FLAG_STORAGE_CE, &result));

    EXPECT_EQ(result.size(), 0U);
}
@@ -199,8 +204,8 @@ TEST_F(ServiceTest, HashSecondaryDex_Unreadable) {

    std::vector<uint8_t> result;
    std::string dexPath = get_full_path("com.example/foo/file");
    EXPECT_TRUE(service->hashSecondaryDexFile(
        dexPath, "com.example", 10000, testUuid, FLAG_STORAGE_CE, &result).isOk());
    EXPECT_BINDER_SUCCESS(service->hashSecondaryDexFile(
        dexPath, "com.example", 10000, testUuid, FLAG_STORAGE_CE, &result));

    EXPECT_EQ(result.size(), 0U);
}
@@ -214,8 +219,8 @@ TEST_F(ServiceTest, HashSecondaryDex_WrongApp) {

    std::vector<uint8_t> result;
    std::string dexPath = get_full_path("com.example/foo/file");
    EXPECT_FALSE(service->hashSecondaryDexFile(
        dexPath, "com.wrong", 10000, testUuid, FLAG_STORAGE_CE, &result).isOk());
    EXPECT_BINDER_FAIL(service->hashSecondaryDexFile(
        dexPath, "com.wrong", 10000, testUuid, FLAG_STORAGE_CE, &result));
}

TEST_F(ServiceTest, CalculateOat) {
@@ -287,8 +292,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot) {
          0700, 10000, 20000, false /* follow_symlinks */));

  // Request a snapshot of the CE content but not the DE content.
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE).isOk());
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE));

  std::string ce_content, de_content;
  // At this point, we should have the CE content but not the DE content.
@@ -305,8 +310,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot) {
          0700, 10000, 20000, false /* follow_symlinks */));

  // Request a snapshot of the DE content but not the CE content.
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE).isOk());
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE));

  // At this point, both the CE as well as the DE content should be fully
  // populated.
@@ -324,8 +329,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot) {
          0700, 10000, 20000, false /* follow_symlinks */));

  // Request a snapshot of both the CE as well as the DE content.
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE | FLAG_STORAGE_CE).isOk());
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE | FLAG_STORAGE_CE));

  ASSERT_TRUE(android::base::ReadFileToString(
      rollback_ce_dir + "/com.foo/file1", &ce_content, false /* follow_symlinks */));
@@ -351,10 +356,10 @@ TEST_F(ServiceTest, CreateAppDataSnapshot_AppDataAbsent) {

  auto scope_guard = android::base::make_scope_guard(deleter);

  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE).isOk());
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE).isOk());
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE));
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE));

  // The snapshot calls must succeed but there should be no snapshot
  // created.
@@ -403,8 +408,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot_ClearsExistingSnapshot) {
          "TEST_CONTENT_2_DE", fake_package_de_path + "/file2",
          0700, 10000, 20000, false /* follow_symlinks */));

  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE | FLAG_STORAGE_CE).isOk());
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE | FLAG_STORAGE_CE));

  // Previous snapshot (with data for file1) must be cleared.
  struct stat sb;
@@ -429,8 +434,8 @@ TEST_F(ServiceTest, SnapshotAppData_WrongVolumeUuid) {
  };
  auto scope_guard = android::base::make_scope_guard(deleter);

  ASSERT_FALSE(service->snapshotAppData(std::make_unique<std::string>("FOO"),
          "com.foo", 0, FLAG_STORAGE_DE).isOk());
  EXPECT_BINDER_FAIL(service->snapshotAppData(std::make_unique<std::string>("FOO"),
          "com.foo", 0, FLAG_STORAGE_DE));
}

TEST_F(ServiceTest, CreateAppDataSnapshot_ClearsCache) {
@@ -478,8 +483,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot_ClearsCache) {
  ASSERT_TRUE(android::base::WriteStringToFile(
          "TEST_CONTENT_DE", fake_package_de_code_cache_path + "/file1",
          0700, 10000, 20000, false /* follow_symlinks */));
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE | FLAG_STORAGE_DE).isOk());
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE | FLAG_STORAGE_DE));
  // The snapshot call must clear cache.
  struct stat sb;
  ASSERT_EQ(-1, stat((fake_package_ce_cache_path + "/file1").c_str(), &sb));
@@ -529,8 +534,8 @@ TEST_F(ServiceTest, RestoreAppDataSnapshot) {
          "TEST_CONTENT_DE", fake_package_de_path + "/file1",
          0700, 10000, 20000, false /* follow_symlinks */));

  ASSERT_TRUE(service->restoreAppDataSnapshot(std::make_unique<std::string>("TEST"),
          "com.foo", 10000, -1, "", 0, FLAG_STORAGE_DE | FLAG_STORAGE_CE).isOk());
  ASSERT_BINDER_SUCCESS(service->restoreAppDataSnapshot(std::make_unique<std::string>("TEST"),
          "com.foo", 10000, -1, "", 0, FLAG_STORAGE_DE | FLAG_STORAGE_CE));

  std::string ce_content, de_content;
  ASSERT_TRUE(android::base::ReadFileToString(
@@ -568,8 +573,8 @@ TEST_F(ServiceTest, RestoreAppDataSnapshot_WrongVolumeUuid) {
  };
  auto scope_guard = android::base::make_scope_guard(deleter);

  ASSERT_FALSE(service->restoreAppDataSnapshot(std::make_unique<std::string>("BAR"),
          "com.foo", 10000, -1, "", 0, FLAG_STORAGE_DE).isOk());
  EXPECT_BINDER_FAIL(service->restoreAppDataSnapshot(std::make_unique<std::string>("BAR"),
          "com.foo", 10000, -1, "", 0, FLAG_STORAGE_DE));
}

}  // namespace installd
+18 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 <stdlib.h>
#include <string.h>
#include <sys/capability.h>