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

Commit 50ce99e1 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Installd: Refactor binder tests"

parents 0a1e8185 8f7140dc
Loading
Loading
Loading
Loading
+46 −0
Original line number Original line 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 Original line Diff line number Diff line
@@ -25,7 +25,7 @@
#include <android-base/logging.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
#include <android-base/unique_fd.h>

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


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


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


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

        ASSERT_TRUE(prof_binder_result.isOk()) << prof_binder_result.toString8().c_str();
        ASSERT_TRUE(prof_result);
        ASSERT_TRUE(prof_result);


        binder::Status result = service_->dexopt(apk_path_,
        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,
    void createProfileSnapshot(int32_t appid, const std::string& package_name,
            bool expected_result) {
            bool expected_result) {
        bool result;
        bool result;
        binder::Status binder_result = service_->createProfileSnapshot(
        ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
                appid, package_name, kPrimaryProfile, apk_path_, &result);
                appid, package_name, kPrimaryProfile, apk_path_, &result));
        ASSERT_TRUE(binder_result.isOk()) << binder_result.toString8().c_str();
        ASSERT_EQ(expected_result, result);
        ASSERT_EQ(expected_result, result);


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


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


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


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


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


    // Check the file access.
    // Check the file access.
    CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
    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) {
    void createBootImageProfileSnapshot(const std::string& classpath, bool expected_result) {
        bool result;
        bool result;
        binder::Status binder_result = service_->createProfileSnapshot(
        ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
                -1, "android", "android.prof", classpath, &result);
                -1, "android", "android.prof", classpath, &result));
        ASSERT_TRUE(binder_result.isOk());
        ASSERT_EQ(expected_result, result);
        ASSERT_EQ(expected_result, result);


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


#include <sstream>
#include <sstream>
#include <string>

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


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


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


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


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


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


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


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


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


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


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


TEST_F(ServiceTest, CalculateOat) {
TEST_F(ServiceTest, CalculateOat) {
@@ -288,8 +293,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot) {


  // Request a snapshot of the CE content but not the DE content.
  // Request a snapshot of the CE content but not the DE content.
  int64_t ce_snapshot_inode;
  int64_t ce_snapshot_inode;
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE, &ce_snapshot_inode).isOk());
          "com.foo", 0, FLAG_STORAGE_CE, &ce_snapshot_inode));
  struct stat buf;
  struct stat buf;
  memset(&buf, 0, sizeof(buf));
  memset(&buf, 0, sizeof(buf));
  ASSERT_EQ(0, stat((rollback_ce_dir + "/com.foo").c_str(), &buf));
  ASSERT_EQ(0, stat((rollback_ce_dir + "/com.foo").c_str(), &buf));
@@ -310,8 +315,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot) {
          0700, 10000, 20000, false /* follow_symlinks */));
          0700, 10000, 20000, false /* follow_symlinks */));


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


@@ -331,8 +336,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot) {
          0700, 10000, 20000, false /* follow_symlinks */));
          0700, 10000, 20000, false /* follow_symlinks */));


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


  ASSERT_TRUE(android::base::ReadFileToString(
  ASSERT_TRUE(android::base::ReadFileToString(
      rollback_ce_dir + "/com.foo/file1", &ce_content, false /* follow_symlinks */));
      rollback_ce_dir + "/com.foo/file1", &ce_content, false /* follow_symlinks */));
@@ -359,10 +364,10 @@ TEST_F(ServiceTest, CreateAppDataSnapshot_AppDataAbsent) {
  auto scope_guard = android::base::make_scope_guard(deleter);
  auto scope_guard = android::base::make_scope_guard(deleter);


  int64_t ce_snapshot_inode;
  int64_t ce_snapshot_inode;
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_CE, &ce_snapshot_inode).isOk());
          "com.foo", 0, FLAG_STORAGE_CE, &ce_snapshot_inode));
  ASSERT_TRUE(service->snapshotAppData(std::make_unique<std::string>("TEST"),
  ASSERT_BINDER_SUCCESS(service->snapshotAppData(std::make_unique<std::string>("TEST"),
          "com.foo", 0, FLAG_STORAGE_DE, nullptr).isOk());
          "com.foo", 0, FLAG_STORAGE_DE, nullptr));
  // No CE content snapshot was performed.
  // No CE content snapshot was performed.
  ASSERT_EQ(ce_snapshot_inode, 0);
  ASSERT_EQ(ce_snapshot_inode, 0);


@@ -413,8 +418,8 @@ TEST_F(ServiceTest, CreateAppDataSnapshot_ClearsExistingSnapshot) {
          "TEST_CONTENT_2_DE", fake_package_de_path + "/file2",
          "TEST_CONTENT_2_DE", fake_package_de_path + "/file2",
          0700, 10000, 20000, false /* follow_symlinks */));
          0700, 10000, 20000, false /* follow_symlinks */));


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


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


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


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


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


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


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


}  // namespace installd
}  // namespace installd
+18 −0
Original line number Original line 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 <stdlib.h>
#include <string.h>
#include <string.h>
#include <sys/capability.h>
#include <sys/capability.h>