installd_cache_test.cpp revision 871a8f236ef2a055b9955b47a342b2c44c020ef7
1871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey/*
2871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * Copyright (C) 2017 The Android Open Source Project
3871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey *
4871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
5871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * you may not use this file except in compliance with the License.
6871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * You may obtain a copy of the License at
7871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey *
8871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
9871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey *
10871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
11871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
12871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * See the License for the specific language governing permissions and
14871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey * limitations under the License.
15871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey */
16871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
17871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <stdlib.h>
18871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <string.h>
19871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <sys/statvfs.h>
20871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <sys/xattr.h>
21871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
22871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <android-base/logging.h>
23871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <android-base/stringprintf.h>
24871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <cutils/properties.h>
25871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <gtest/gtest.h>
26871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
27871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include "InstalldNativeService.h"
28871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include "globals.h"
29871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include "utils.h"
30871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
31871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyusing android::base::StringPrintf;
32871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
33871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeynamespace android {
34871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeynamespace installd {
35871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
36871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyconstexpr const char* kTestUuid = "TEST";
37871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
38871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyconstexpr int64_t kKbInBytes = 1024;
39871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyconstexpr int64_t kMbInBytes = 1024 * kKbInBytes;
40871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyconstexpr int64_t kGbInBytes = 1024 * kMbInBytes;
41871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyconstexpr int64_t kTbInBytes = 1024 * kGbInBytes;
42871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
43871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic constexpr int FLAG_FREE_CACHE_V2 = 1 << 13;
44871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic constexpr int FLAG_FREE_CACHE_V2_DEFY_QUOTA = 1 << 14;
45871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
46871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyint get_property(const char *key, char *value, const char *default_value) {
47871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    return property_get(key, value, default_value);
48871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
49871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
50871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeybool calculate_oat_file_path(char path[PKG_PATH_MAX] ATTRIBUTE_UNUSED,
51871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        const char *oat_dir ATTRIBUTE_UNUSED,
52871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        const char *apk_path ATTRIBUTE_UNUSED,
53871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        const char *instruction_set ATTRIBUTE_UNUSED) {
54871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    return false;
55871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
56871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
57871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeybool calculate_odex_file_path(char path[PKG_PATH_MAX] ATTRIBUTE_UNUSED,
58871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        const char *apk_path ATTRIBUTE_UNUSED,
59871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        const char *instruction_set ATTRIBUTE_UNUSED) {
60871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    return false;
61871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
62871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
63871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeybool create_cache_path(char path[PKG_PATH_MAX] ATTRIBUTE_UNUSED,
64871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        const char *src ATTRIBUTE_UNUSED,
65871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        const char *instruction_set ATTRIBUTE_UNUSED) {
66871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    return false;
67871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
68871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
69871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic void mkdir(const char* path) {
70871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    const char* fullPath = StringPrintf("/data/local/tmp/user/0/%s", path).c_str();
71871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    ::mkdir(fullPath, 0755);
72871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
73871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
74871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic void touch(const char* path, int len, int time) {
75871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    const char* fullPath = StringPrintf("/data/local/tmp/user/0/%s", path).c_str();
76871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    int fd = ::open(fullPath, O_RDWR | O_CREAT, 0644);
77871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    ::fallocate(fd, 0, 0, len);
78871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    ::close(fd);
79871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    struct utimbuf times;
80871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    times.actime = times.modtime = std::time(0) + time;
81871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    ::utime(fullPath, &times);
82871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
83871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
84871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic int exists(const char* path) {
85871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    const char* fullPath = StringPrintf("/data/local/tmp/user/0/%s", path).c_str();
86871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    return ::access(fullPath, F_OK);
87871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
88871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
89871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic int64_t size(const char* path) {
90871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    const char* fullPath = StringPrintf("/data/local/tmp/user/0/%s", path).c_str();
91871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    struct stat buf;
92871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    if (!stat(fullPath, &buf)) {
93871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        return buf.st_size;
94871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    } else {
95871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        return -1;
96871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    }
97871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
98871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
99871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic int64_t free() {
100871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    struct statvfs buf;
101871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    if (!statvfs("/data/local/tmp", &buf)) {
102871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        return buf.f_bavail * buf.f_bsize;
103871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    } else {
104871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        return -1;
105871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    }
106871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
107871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
108871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeystatic void setxattr(const char* path, const char* key) {
109871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    const char* fullPath = StringPrintf("/data/local/tmp/user/0/%s", path).c_str();
110871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    ::setxattr(fullPath, key, "", 0, 0);
111871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
112871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
113871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyclass CacheTest : public testing::Test {
114871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkeyprotected:
115871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    InstalldNativeService* service;
116871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    std::unique_ptr<std::string> testUuid;
117871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
118871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    virtual void SetUp() {
119871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        setenv("ANDROID_LOG_TAGS", "*:v", 1);
120871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        android::base::InitLogging(nullptr);
121871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
122871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        service = new InstalldNativeService();
123871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        testUuid = std::make_unique<std::string>();
124871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        *testUuid = std::string(kTestUuid);
125871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        system("mkdir -p /data/local/tmp/user/0");
126871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    }
127871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
128871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    virtual void TearDown() {
129871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        delete service;
130871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        system("rm -rf /data/local/tmp/user");
131871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    }
132871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey};
133871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
134871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff SharkeyTEST_F(CacheTest, FreeCache_All) {
135871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example");
136871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/normal", 1 * kMbInBytes, 60);
137871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache");
138871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/foo");
139871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/one", 1 * kMbInBytes, 60);
140871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/two", 2 * kMbInBytes, 120);
141871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
142871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/normal"));
143871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/foo/one"));
144871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/foo/two"));
145871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
146871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, kTbInBytes,
147871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
148871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
149871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/normal"));
150871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/one"));
151871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/two"));
152871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
153871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
154871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff SharkeyTEST_F(CacheTest, FreeCache_Age) {
155871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example");
156871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache");
157871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/foo");
158871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/one", kMbInBytes, 60);
159871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/two", kMbInBytes, 120);
160871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
161871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, free() + kKbInBytes,
162871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
163871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
164871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/one"));
165871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/foo/two"));
166871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
167871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, free() + kKbInBytes,
168871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
169871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
170871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/one"));
171871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/two"));
172871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
173871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
174871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff SharkeyTEST_F(CacheTest, FreeCache_Tombstone) {
175871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example");
176871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache");
177871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/foo");
178871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/foo1", 1 * kMbInBytes, 60);
179871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/foo2", 1 * kMbInBytes, 60);
180871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/bar");
181871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/bar/bar1", 2 * kMbInBytes, 120);
182871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/bar/bar2", 2 * kMbInBytes, 120);
183871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
184871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    setxattr("com.example/cache/bar", "user.cache_tombstone");
185871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
186871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/foo/foo1"));
187871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/foo/foo2"));
188871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/bar/bar1"));
189871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/bar/bar2"));
190871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(2 * kMbInBytes, size("com.example/cache/bar/bar1"));
191871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(2 * kMbInBytes, size("com.example/cache/bar/bar2"));
192871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
193871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, kTbInBytes,
194871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
195871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
196871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/foo1"));
197871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/foo2"));
198871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/bar/bar1"));
199871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, exists("com.example/cache/bar/bar2"));
200871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/bar/bar1"));
201871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/bar/bar2"));
202871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
203871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
204871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff SharkeyTEST_F(CacheTest, FreeCache_Atomic) {
205871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example");
206871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache");
207871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/foo");
208871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/foo1", 1 * kMbInBytes, 60);
209871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/foo/foo2", 1 * kMbInBytes, 120);
210871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
211871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    setxattr("com.example/cache/foo", "user.cache_atomic");
212871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
213871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, free() + kKbInBytes,
214871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
215871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
216871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/foo1"));
217871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, exists("com.example/cache/foo/foo2"));
218871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
219871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
220871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff SharkeyTEST_F(CacheTest, FreeCache_AtomicTombstone) {
221871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    LOG(INFO) << "FreeCache_AtomicTombstone";
222871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
223871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example");
224871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache");
225871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
226871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    // this dir must look really old for some reason?
227871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/atomic");
228871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/file1", kMbInBytes, 120);
229871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/file2", kMbInBytes, 120);
230871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/atomic/dir");
231871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/dir/file1", kMbInBytes, 120);
232871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/dir/file2", kMbInBytes, 120);
233871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/atomic/tomb");
234871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/tomb/file1", kMbInBytes, 120);
235871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/tomb/file2", kMbInBytes, 120);
236871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/atomic/tomb/dir");
237871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/tomb/dir/file1", kMbInBytes, 120);
238871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/atomic/tomb/dir/file2", kMbInBytes, 120);
239871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
240871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/tomb");
241871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/file1", kMbInBytes, 240);
242871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/file2", kMbInBytes, 240);
243871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/tomb/dir");
244871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/dir/file1", kMbInBytes, 240);
245871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/dir/file2", kMbInBytes, 240);
246871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/tomb/atomic");
247871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/atomic/file1", kMbInBytes, 60);
248871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/atomic/file2", kMbInBytes, 60);
249871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    mkdir("com.example/cache/tomb/atomic/dir");
250871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/atomic/dir/file1", kMbInBytes, 60);
251871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    touch("com.example/cache/tomb/atomic/dir/file2", kMbInBytes, 60);
252871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
253871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    setxattr("com.example/cache/atomic", "user.cache_atomic");
254871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    setxattr("com.example/cache/atomic/tomb", "user.cache_tombstone");
255871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    setxattr("com.example/cache/tomb", "user.cache_tombstone");
256871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    setxattr("com.example/cache/tomb/atomic", "user.cache_atomic");
257871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
258871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, free() + kKbInBytes,
259871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
260871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
261871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/file1"));
262871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/file2"));
263871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/dir/file1"));
264871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/dir/file2"));
265871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/tomb/file1"));
266871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/tomb/file2"));
267871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/tomb/dir/file1"));
268871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/atomic/tomb/dir/file2"));
269871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
270871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/file1"));
271871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/file2"));
272871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/dir/file1"));
273871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/dir/file2"));
274871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/file1"));
275871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/file2"));
276871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/dir/file1"));
277871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/dir/file2"));
278871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
279871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, free() + kKbInBytes,
280871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
281871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
282871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/file1"));
283871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/file2"));
284871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/dir/file1"));
285871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/dir/file2"));
286871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/file1"));
287871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/file2"));
288871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/dir/file1"));
289871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/dir/file2"));
290871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
291871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/file1"));
292871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/file2"));
293871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/dir/file1"));
294871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(kMbInBytes, size("com.example/cache/tomb/dir/file2"));
295871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/file1"));
296871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/file2"));
297871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/dir/file1"));
298871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/dir/file2"));
299871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
300871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    service->freeCache(testUuid, kTbInBytes,
301871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey            FLAG_FREE_CACHE_V2 | FLAG_FREE_CACHE_V2_DEFY_QUOTA);
302871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
303871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/file1"));
304871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/file2"));
305871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/dir/file1"));
306871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(-1, size("com.example/cache/atomic/dir/file2"));
307871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/file1"));
308871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/file2"));
309871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/dir/file1"));
310871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/atomic/tomb/dir/file2"));
311871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
312871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/file1"));
313871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/file2"));
314871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/dir/file1"));
315871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/dir/file2"));
316871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/file1"));
317871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/file2"));
318871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/dir/file1"));
319871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    EXPECT_EQ(0, size("com.example/cache/tomb/atomic/dir/file2"));
320871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}
321871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey
322871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}  // namespace installd
323871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey}  // namespace android
324