Skip to content

Commit

Permalink
guessPath not to depend on the first level of app directory
Browse files Browse the repository at this point in the history
We are updating apps' apk path to have a two-level structure.

Default apk path of an installed app:
Before: /data/app/[packageName]-[randomString]/base.apk
After: /data/app/[randomStringA]/[packageName]-[randomStringB]/base.apk

This CL fixes the path guessing assumption the apk dir is only one level
below the /data/app dir.

Fixes #155.
  • Loading branch information
schfan-1 authored and ttanxu committed Jan 24, 2020
1 parent 4d0ce55 commit 6986374
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,28 @@
public final class AppDataDirGuesserTest {
@Test
public void testGuessCacheDir_SimpleExample() {
guessCacheDirFor("/data/app/a.b.c.apk").shouldGive("/data/data/a.b.c/cache");
guessCacheDirFor("/data/app/a.b.c.tests.apk").shouldGive("/data/data/a.b.c.tests/cache");
guessCacheDirFor("/data/app/a.b.c-xxx/base.apk")
.shouldGive("/data/data/a.b.c/cache");
guessCacheDirFor("/data/app/a.b.c.tests-xxx/base.apk")
.shouldGive("/data/data/a.b.c.tests/cache");
}

@Test
public void testGuessCacheDir_MultipleResultsSeparatedByColon() {
guessCacheDirFor("/data/app/a.b.c.apk:/data/app/d.e.f.apk")
guessCacheDirFor("/data/app/a.b.c-xxx/base.apk:/data/app/d.e.f-xxx/base.apk")
.shouldGive("/data/data/a.b.c/cache", "/data/data/d.e.f/cache");
}

@Test
public void testGuessCacheDir_NotWriteableSkipped() {
guessCacheDirFor("/data/app/a.b.c.apk:/data/app/d.e.f.apk")
guessCacheDirFor("/data/app/a.b.c-xxx/base.apk:/data/app/d.e.f-xxx/base.apk")
.withNonWriteable("/data/data/a.b.c/cache")
.shouldGive("/data/data/d.e.f/cache");
}

@Test
public void testGuessCacheDir_ForSecondaryUser() {
guessCacheDirFor("/data/app/a.b.c.apk:/data/app/d.e.f.apk")
guessCacheDirFor("/data/app/a.b.c-xxx/base.apk:/data/app/d.e.f-xxx/base.apk")
.withNonWriteable("/data/data/a.b.c", "/data/data/d.e.f")
.withProcessUid(1110009)
.shouldGive("/data/user/11/a.b.c/cache", "/data/user/11/d.e.f/cache");
Expand All @@ -64,8 +66,10 @@ public void testGuessCacheDir_StripHyphenatedSuffixes() {

@Test
public void testGuessCacheDir_LeadingAndTrailingColonsIgnored() {
guessCacheDirFor("/data/app/a.b.c.apk:asdf:").shouldGive("/data/data/a.b.c/cache");
guessCacheDirFor(":asdf:/data/app/a.b.c.apk").shouldGive("/data/data/a.b.c/cache");
guessCacheDirFor("/data/app/a.b.c-xxx/base.apk:asdf:")
.shouldGive("/data/data/a.b.c/cache");
guessCacheDirFor(":asdf:/data/app/a.b.c-xxx/base.apk")
.shouldGive("/data/data/a.b.c/cache");
}

@Test
Expand All @@ -89,6 +93,26 @@ public void testGuessCacheDir_RealWorldExample() {
.shouldGive("/data/data/com.google.android.voicesearch/cache");
}

@Test
public void testGuessCacheDir_RealWorldExampleWithOneLevelSubDirectories() {
String realPath = "/system/framework/android.test.runner.jar:" +
"/data/app/com.google.android.voicesearch.tests-abcde/base.apk:" +
"/data/app/com.google.android.voicesearch-fghij/base.apk";
guessCacheDirFor(realPath)
.withNonWriteable("/data/data/com.google.android.voicesearch.tests/cache")
.shouldGive("/data/data/com.google.android.voicesearch/cache");
}

@Test
public void testGuessCacheDir_RealWorldExampleWithTwoLevelSubDirectories() {
String realPath = "/system/framework/android.test.runner.jar:" +
"/data/app/abcde/com.google.android.voicesearch.tests-fghij/base.apk:" +
"/data/app/klmno/com.google.android.voicesearch-pqrst/base.apk";
guessCacheDirFor(realPath)
.withNonWriteable("/data/data/com.google.android.voicesearch.tests/cache")
.shouldGive("/data/data/com.google.android.voicesearch/cache");
}

@Test
public void testSplitPathList() {
final String[] expected = { "foo", "bar" };
Expand Down
10 changes: 7 additions & 3 deletions dexmaker/src/main/java/com/android/dx/AppDataDirGuesser.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,18 @@ File[] guessPath(String input) {
if (!potential.startsWith("/data/app/")) {
continue;
}
int start = "/data/app/".length();
int end = potential.lastIndexOf(".apk");
if (end != potential.length() - 4) {
continue;
}
int dash = potential.indexOf("-");
if (dash != -1) {
end = dash;
if (dash == -1) {
continue;
}
end = dash;
int start = potential.lastIndexOf("/", dash) + 1;
if (start == 0) {
continue;
}
String packageName = potential.substring(start, end);
File dataDir = getWriteableDirectory("/data/data/" + packageName);
Expand Down

0 comments on commit 6986374

Please sign in to comment.