Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdlib.h>
     18 #include <string.h>
     19 
     20 #include <gtest/gtest.h>
     21 
     22 #include "InstalldNativeService.h"
     23 #include "globals.h"
     24 #include "utils.h"
     25 
     26 #undef LOG_TAG
     27 #define LOG_TAG "utils_test"
     28 
     29 #define TEST_DATA_DIR "/data/"
     30 #define TEST_APP_DIR "/data/app/"
     31 #define TEST_APP_PRIVATE_DIR "/data/app-private/"
     32 #define TEST_APP_EPHEMERAL_DIR "/data/app-ephemeral/"
     33 #define TEST_ASEC_DIR "/mnt/asec/"
     34 #define TEST_EXPAND_DIR "/mnt/expand/"
     35 
     36 #define TEST_SYSTEM_DIR1 "/system/app/"
     37 #define TEST_SYSTEM_DIR2 "/vendor/app/"
     38 
     39 #define TEST_PROFILE_DIR "/data/misc/profiles"
     40 
     41 #define REALLY_LONG_APP_NAME "com.example." \
     42         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \
     43         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \
     44         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
     45 
     46 #define REALLY_LONG_LEAF_NAME "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \
     47         "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \
     48         "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \
     49         "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_"
     50 
     51 namespace android {
     52 namespace installd {
     53 
     54 class UtilsTest : public testing::Test {
     55 protected:
     56     virtual void SetUp() {
     57         android_app_dir.path = (char*) TEST_APP_DIR;
     58         android_app_dir.len = strlen(TEST_APP_DIR);
     59 
     60         android_app_private_dir.path = (char*) TEST_APP_PRIVATE_DIR;
     61         android_app_private_dir.len = strlen(TEST_APP_PRIVATE_DIR);
     62 
     63         android_app_ephemeral_dir.path = (char*) TEST_APP_EPHEMERAL_DIR;
     64         android_app_ephemeral_dir.len = strlen(TEST_APP_EPHEMERAL_DIR);
     65 
     66         android_data_dir.path = (char*) TEST_DATA_DIR;
     67         android_data_dir.len = strlen(TEST_DATA_DIR);
     68 
     69         android_asec_dir.path = (char*) TEST_ASEC_DIR;
     70         android_asec_dir.len = strlen(TEST_ASEC_DIR);
     71 
     72         android_mnt_expand_dir.path = (char*) TEST_EXPAND_DIR;
     73         android_mnt_expand_dir.len = strlen(TEST_EXPAND_DIR);
     74 
     75         android_system_dirs.count = 2;
     76 
     77         android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
     78         android_system_dirs.dirs[0].path = (char*) TEST_SYSTEM_DIR1;
     79         android_system_dirs.dirs[0].len = strlen(TEST_SYSTEM_DIR1);
     80 
     81         android_system_dirs.dirs[1].path = (char*) TEST_SYSTEM_DIR2;
     82         android_system_dirs.dirs[1].len = strlen(TEST_SYSTEM_DIR2);
     83 
     84         android_profiles_dir.path = (char*) TEST_PROFILE_DIR;
     85         android_profiles_dir.len = strlen(TEST_PROFILE_DIR);
     86     }
     87 
     88     virtual void TearDown() {
     89         free(android_system_dirs.dirs);
     90     }
     91 };
     92 
     93 TEST_F(UtilsTest, IsValidApkPath_BadPrefix) {
     94     // Bad prefixes directories
     95     const char *badprefix1 = "/etc/passwd";
     96     EXPECT_EQ(-1, validate_apk_path(badprefix1))
     97             << badprefix1 << " should not be allowed as a valid path";
     98 
     99     const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah";
    100     EXPECT_EQ(-1, validate_apk_path(badprefix2))
    101             << badprefix2 << " should not be allowed as a valid path";
    102 
    103     const char *badprefix3 = "init.rc";
    104     EXPECT_EQ(-1, validate_apk_path(badprefix3))
    105             << badprefix3 << " should not be allowed as a valid path";
    106 
    107     const char *badprefix4 = "/init.rc";
    108     EXPECT_EQ(-1, validate_apk_path(badprefix4))
    109             << badprefix4 << " should not be allowed as a valid path";
    110 }
    111 
    112 TEST_F(UtilsTest, IsValidApkPath_Internal) {
    113     // Internal directories
    114     const char *internal1 = TEST_APP_DIR "example.apk";
    115     EXPECT_EQ(0, validate_apk_path(internal1))
    116             << internal1 << " should be allowed as a valid path";
    117 
    118     // b/16888084
    119     const char *path2 = TEST_APP_DIR "example.com/example.apk";
    120     EXPECT_EQ(0, validate_apk_path(path2))
    121             << path2 << " should be allowed as a valid path";
    122 
    123     const char *badint1 = TEST_APP_DIR "../example.apk";
    124     EXPECT_EQ(-1, validate_apk_path(badint1))
    125             << badint1 << " should be rejected as a invalid path";
    126 
    127     const char *badint2 = TEST_APP_DIR "/../example.apk";
    128     EXPECT_EQ(-1, validate_apk_path(badint2))
    129             << badint2 << " should be rejected as a invalid path";
    130 
    131     // Only one subdir should be allowed.
    132     const char *bad_path3 = TEST_APP_DIR "example.com/subdir/pkg.apk";
    133     EXPECT_EQ(-1, validate_apk_path(bad_path3))
    134             << bad_path3 << " should be rejected as a invalid path";
    135 
    136     const char *bad_path4 = TEST_APP_DIR "example.com/subdir/../pkg.apk";
    137     EXPECT_EQ(-1, validate_apk_path(bad_path4))
    138             << bad_path4 << " should be rejected as a invalid path";
    139 
    140     const char *bad_path5 = TEST_APP_DIR "example.com1/../example.com2/pkg.apk";
    141     EXPECT_EQ(-1, validate_apk_path(bad_path5))
    142             << bad_path5 << " should be rejected as a invalid path";
    143 }
    144 
    145 TEST_F(UtilsTest, IsValidApkPath_Private) {
    146     // Internal directories
    147     const char *private1 = TEST_APP_PRIVATE_DIR "example.apk";
    148     EXPECT_EQ(0, validate_apk_path(private1))
    149             << private1 << " should be allowed as a valid path";
    150 
    151     // b/16888084
    152     const char *path2 = TEST_APP_DIR "example.com/example.apk";
    153     EXPECT_EQ(0, validate_apk_path(path2))
    154             << path2 << " should be allowed as a valid path";
    155 
    156     const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk";
    157     EXPECT_EQ(-1, validate_apk_path(badpriv1))
    158             << badpriv1 << " should be rejected as a invalid path";
    159 
    160     const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk";
    161     EXPECT_EQ(-1, validate_apk_path(badpriv2))
    162             << badpriv2 << " should be rejected as a invalid path";
    163 
    164     // Only one subdir should be allowed.
    165     const char *bad_path3 = TEST_APP_PRIVATE_DIR "example.com/subdir/pkg.apk";
    166     EXPECT_EQ(-1, validate_apk_path(bad_path3))
    167             << bad_path3 << " should be rejected as a invalid path";
    168 
    169     const char *bad_path4 = TEST_APP_PRIVATE_DIR "example.com/subdir/../pkg.apk";
    170     EXPECT_EQ(-1, validate_apk_path(bad_path4))
    171             << bad_path4 << " should be rejected as a invalid path";
    172 
    173     const char *bad_path5 = TEST_APP_PRIVATE_DIR "example.com1/../example.com2/pkg.apk";
    174     EXPECT_EQ(-1, validate_apk_path(bad_path5))
    175             << bad_path5 << " should be rejected as a invalid path";
    176 }
    177 
    178 
    179 TEST_F(UtilsTest, IsValidApkPath_AsecGood1) {
    180     const char *asec1 = TEST_ASEC_DIR "example.apk";
    181     EXPECT_EQ(0, validate_apk_path(asec1))
    182             << asec1 << " should be allowed as a valid path";
    183 }
    184 
    185 TEST_F(UtilsTest, IsValidApkPath_AsecGood2) {
    186     const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk";
    187     EXPECT_EQ(0, validate_apk_path(asec2))
    188             << asec2 << " should be allowed as a valid path";
    189 }
    190 
    191 TEST_F(UtilsTest, IsValidApkPath_EscapeFail) {
    192     const char *badasec1 = TEST_ASEC_DIR "../example.apk";
    193     EXPECT_EQ(-1, validate_apk_path(badasec1))
    194             << badasec1 << " should be rejected as a invalid path";
    195 }
    196 
    197 TEST_F(UtilsTest, IsValidApkPath_DoubleSlashFail) {
    198     const char *badasec2 = TEST_ASEC_DIR "com.example.asec//pkg.apk";
    199     EXPECT_EQ(-1, validate_apk_path(badasec2))
    200             << badasec2 << " should be rejected as a invalid path";
    201 }
    202 
    203 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) {
    204     const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk";
    205     EXPECT_EQ(-1, validate_apk_path(badasec3))
    206             << badasec3  << " should be rejected as a invalid path";
    207 }
    208 
    209 TEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) {
    210     const char *badasec4 = TEST_ASEC_DIR "/../example.apk";
    211     EXPECT_EQ(-1, validate_apk_path(badasec4))
    212             << badasec4 << " should be rejected as a invalid path";
    213 }
    214 
    215 TEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) {
    216     const char *badasec5 = TEST_ASEC_DIR ".//../..";
    217     EXPECT_EQ(-1, validate_apk_path(badasec5))
    218             << badasec5 << " should be rejected as a invalid path";
    219 }
    220 
    221 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) {
    222     const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk";
    223     EXPECT_EQ(-1, validate_apk_path(badasec6))
    224             << badasec6 << " should be rejected as a invalid path";
    225 }
    226 
    227 TEST_F(UtilsTest, IsValidApkPath_TwoSubdirFail) {
    228     const char *badasec7 = TEST_ASEC_DIR "com.example.asec/subdir1/pkg.apk";
    229     EXPECT_EQ(-1, validate_apk_path(badasec7))
    230             << badasec7 << " should be rejected as a invalid path";
    231 }
    232 
    233 TEST_F(UtilsTest, CheckSystemApp_Dir1) {
    234     const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk";
    235     EXPECT_EQ(0, validate_system_app_path(sysapp1))
    236             << sysapp1 << " should be allowed as a system path";
    237 }
    238 
    239 TEST_F(UtilsTest, CheckSystemApp_Dir2) {
    240     const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk";
    241     EXPECT_EQ(0, validate_system_app_path(sysapp2))
    242             << sysapp2 << " should be allowed as a system path";
    243 }
    244 
    245 TEST_F(UtilsTest, CheckSystemApp_EscapeFail) {
    246     const char *badapp1 = TEST_SYSTEM_DIR1 "../com.example.apk";
    247     EXPECT_EQ(-1, validate_system_app_path(badapp1))
    248             << badapp1 << " should be rejected not a system path";
    249 }
    250 
    251 TEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) {
    252     const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk";
    253     EXPECT_EQ(-1, validate_system_app_path(badapp2))
    254             << badapp2 << " should be rejected not a system path";
    255 }
    256 
    257 TEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) {
    258     const char *badapp3 = TEST_APP_DIR "/../../com.example.apk";
    259     EXPECT_EQ(-1, validate_system_app_path(badapp3))
    260             << badapp3 << " should be rejected not a system path";
    261 }
    262 
    263 TEST_F(UtilsTest, CheckSystemApp_Subdir) {
    264     const char *sysapp = TEST_SYSTEM_DIR1 "com.example/com.example.apk";
    265     EXPECT_EQ(0, validate_system_app_path(sysapp))
    266             << sysapp << " should be allowed as a system path";
    267 
    268     const char *badapp = TEST_SYSTEM_DIR1 "com.example/subdir/com.example.apk";
    269     EXPECT_EQ(-1, validate_system_app_path(badapp))
    270             << badapp << " should be rejected not a system path";
    271 
    272     const char *badapp1 = TEST_SYSTEM_DIR1 "com.example/subdir/../com.example.apk";
    273     EXPECT_EQ(-1, validate_system_app_path(badapp1))
    274             << badapp1 << " should be rejected not a system path";
    275 
    276     const char *badapp2 = TEST_SYSTEM_DIR1 "com.example1/../com.example2/com.example.apk";
    277     EXPECT_EQ(-1, validate_system_app_path(badapp2))
    278             << badapp2 << " should be rejected not a system path";
    279 }
    280 
    281 TEST_F(UtilsTest, GetPathFromString_NullPathFail) {
    282     dir_rec_t test1;
    283     EXPECT_EQ(-1, get_path_from_string(&test1, (const char *) NULL))
    284             << "Should not allow NULL as a path.";
    285 }
    286 
    287 TEST_F(UtilsTest, GetPathFromString_EmptyPathFail) {
    288     dir_rec_t test1;
    289     EXPECT_EQ(-1, get_path_from_string(&test1, ""))
    290             << "Should not allow empty paths.";
    291 }
    292 
    293 TEST_F(UtilsTest, GetPathFromString_RelativePathFail) {
    294     dir_rec_t test1;
    295     EXPECT_EQ(-1, get_path_from_string(&test1, "mnt/asec"))
    296             << "Should not allow relative paths.";
    297 }
    298 
    299 TEST_F(UtilsTest, GetPathFromString_NonCanonical) {
    300     dir_rec_t test1;
    301 
    302     EXPECT_EQ(0, get_path_from_string(&test1, "/mnt/asec"))
    303             << "Should be able to canonicalize directory /mnt/asec";
    304     EXPECT_STREQ("/mnt/asec/", test1.path)
    305             << "/mnt/asec should be canonicalized to /mnt/asec/";
    306     EXPECT_EQ(10, (ssize_t) test1.len)
    307             << "path len should be equal to the length of /mnt/asec/ (10)";
    308     free(test1.path);
    309 }
    310 
    311 TEST_F(UtilsTest, GetPathFromString_CanonicalPath) {
    312     dir_rec_t test3;
    313     EXPECT_EQ(0, get_path_from_string(&test3, "/data/app/"))
    314             << "Should be able to canonicalize directory /data/app/";
    315     EXPECT_STREQ("/data/app/", test3.path)
    316             << "/data/app/ should be canonicalized to /data/app/";
    317     EXPECT_EQ(10, (ssize_t) test3.len)
    318             << "path len should be equal to the length of /data/app/ (10)";
    319     free(test3.path);
    320 }
    321 
    322 TEST_F(UtilsTest, CreatePkgPath_LongPkgNameSuccess) {
    323     char path[PKG_PATH_MAX];
    324 
    325     // Create long packagename of "aaaaa..."
    326     size_t pkgnameSize = PKG_NAME_MAX;
    327     char pkgname[pkgnameSize + 1];
    328     memset(pkgname, 'a', pkgnameSize);
    329     pkgname[1] = '.';
    330     pkgname[pkgnameSize] = '\0';
    331 
    332     EXPECT_EQ(0, create_pkg_path(path, pkgname, "", 0))
    333             << "Should successfully be able to create package name.";
    334 
    335     std::string prefix = std::string(TEST_DATA_DIR) + PRIMARY_USER_PREFIX;
    336     size_t offset = prefix.length();
    337 
    338     EXPECT_STREQ(pkgname, path + offset)
    339              << "Package path should be a really long string of a's";
    340 }
    341 
    342 TEST_F(UtilsTest, CreatePkgPath_LongPostfixFail) {
    343     char path[PKG_PATH_MAX];
    344 
    345     // Create long packagename of "aaaaa..."
    346     size_t postfixSize = PKG_PATH_MAX;
    347     char postfix[postfixSize + 1];
    348     memset(postfix, 'a', postfixSize);
    349     postfix[postfixSize] = '\0';
    350 
    351     EXPECT_EQ(-1, create_pkg_path(path, "com.example.package", postfix, 0))
    352             << "Should return error because postfix is too long.";
    353 }
    354 
    355 TEST_F(UtilsTest, CreatePkgPath_PrimaryUser) {
    356     char path[PKG_PATH_MAX];
    357 
    358     EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 0))
    359             << "Should return error because postfix is too long.";
    360 
    361     std::string p = std::string(TEST_DATA_DIR)
    362                     + PRIMARY_USER_PREFIX
    363                     + "com.example.package";
    364     EXPECT_STREQ(p.c_str(), path)
    365             << "Package path should be in /data/data/";
    366 }
    367 
    368 TEST_F(UtilsTest, CreatePkgPath_SecondaryUser) {
    369     char path[PKG_PATH_MAX];
    370 
    371     EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 1))
    372             << "Should successfully create package path.";
    373 
    374     std::string p = std::string(TEST_DATA_DIR)
    375                     + SECONDARY_USER_PREFIX
    376                     + "1/com.example.package";
    377     EXPECT_STREQ(p.c_str(), path)
    378             << "Package path should be in /data/user/";
    379 }
    380 
    381 TEST_F(UtilsTest, CreateMovePath_Primary) {
    382     char path[PKG_PATH_MAX];
    383 
    384     EXPECT_EQ(0, create_move_path(path, "com.android.test", "shared_prefs", 0))
    385             << "Should be able to create move path for primary user";
    386 
    387     EXPECT_STREQ("/data/data/com.android.test/shared_prefs", path)
    388             << "Primary user package directory should be created correctly";
    389 }
    390 
    391 TEST_F(UtilsTest, CreateMovePath_Fail_AppTooLong) {
    392     char path[PKG_PATH_MAX];
    393 
    394     EXPECT_EQ(-1, create_move_path(path, REALLY_LONG_APP_NAME, "shared_prefs", 0))
    395             << "Should fail to create move path for primary user";
    396 }
    397 
    398 TEST_F(UtilsTest, CreateMovePath_Fail_LeafTooLong) {
    399     char path[PKG_PATH_MAX];
    400 
    401     EXPECT_EQ(-1, create_move_path(path, "com.android.test", REALLY_LONG_LEAF_NAME, 0))
    402             << "Should fail to create move path for primary user";
    403 }
    404 
    405 TEST_F(UtilsTest, CopyAndAppend_Normal) {
    406     //int copy_and_append(dir_rec_t* dst, dir_rec_t* src, char* suffix)
    407     dir_rec_t dst;
    408     dir_rec_t src;
    409 
    410     src.path = (char*) "/data/";
    411     src.len = strlen(src.path);
    412 
    413     EXPECT_EQ(0, copy_and_append(&dst, &src, "app/"))
    414             << "Should return error because postfix is too long.";
    415 
    416     EXPECT_STREQ("/data/app/", dst.path)
    417             << "Appended path should be correct";
    418 
    419     EXPECT_EQ(10, (ssize_t) dst.len)
    420             << "Appended path should be length of '/data/app/' (10)";
    421 }
    422 
    423 TEST_F(UtilsTest, AppendAndIncrement_Normal) {
    424     size_t dst_size = 10;
    425     char dst[dst_size];
    426     char *dstp = dst;
    427     const char* src = "FOO";
    428 
    429     EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
    430             << "String should append successfully";
    431 
    432     EXPECT_STREQ("FOO", dst)
    433             << "String should append correctly";
    434 
    435     EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
    436             << "String should append successfully again";
    437 
    438     EXPECT_STREQ("FOOFOO", dst)
    439             << "String should append correctly again";
    440 }
    441 
    442 TEST_F(UtilsTest, AppendAndIncrement_TooBig) {
    443     size_t dst_size = 5;
    444     char dst[dst_size];
    445     char *dstp = dst;
    446     const char* src = "FOO";
    447 
    448     EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
    449             << "String should append successfully";
    450 
    451     EXPECT_STREQ("FOO", dst)
    452             << "String should append correctly";
    453 
    454     EXPECT_EQ(-1, append_and_increment(&dstp, src, &dst_size))
    455             << "String should fail because it's too large to fit";
    456 }
    457 
    458 TEST_F(UtilsTest, CreateDataPath) {
    459     EXPECT_EQ("/data", create_data_path(nullptr));
    460     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b",
    461             create_data_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
    462 }
    463 
    464 TEST_F(UtilsTest, CreateDataAppPath) {
    465     EXPECT_EQ("/data/app", create_data_app_path(nullptr));
    466 
    467     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app",
    468             create_data_app_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
    469 }
    470 
    471 TEST_F(UtilsTest, CreateDataUserPath) {
    472     EXPECT_EQ("/data/data", create_data_user_ce_path(nullptr, 0));
    473     EXPECT_EQ("/data/user/10", create_data_user_ce_path(nullptr, 10));
    474 
    475     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0",
    476             create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
    477     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10",
    478             create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
    479 }
    480 
    481 TEST_F(UtilsTest, CreateDataMediaPath) {
    482     EXPECT_EQ("/data/media/0", create_data_media_path(nullptr, 0));
    483     EXPECT_EQ("/data/media/10", create_data_media_path(nullptr, 10));
    484 
    485     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/0",
    486             create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
    487     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/10",
    488             create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
    489 }
    490 
    491 TEST_F(UtilsTest, CreateDataAppPackagePath) {
    492     EXPECT_EQ("/data/app/com.example", create_data_app_package_path(nullptr, "com.example"));
    493 
    494     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app/com.example",
    495             create_data_app_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", "com.example"));
    496 }
    497 
    498 TEST_F(UtilsTest, CreateDataUserPackagePath) {
    499     EXPECT_EQ("/data/data/com.example", create_data_user_ce_package_path(nullptr, 0, "com.example"));
    500     EXPECT_EQ("/data/user/10/com.example", create_data_user_ce_package_path(nullptr, 10, "com.example"));
    501 
    502     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0/com.example",
    503             create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, "com.example"));
    504     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10/com.example",
    505             create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10, "com.example"));
    506 }
    507 
    508 TEST_F(UtilsTest, IsValidPackageName) {
    509     EXPECT_EQ(true, is_valid_package_name("android"));
    510     EXPECT_EQ(true, is_valid_package_name("com.example"));
    511     EXPECT_EQ(true, is_valid_package_name("com.example-1"));
    512     EXPECT_EQ(true, is_valid_package_name("com.example-1024"));
    513     EXPECT_EQ(true, is_valid_package_name("com.example.foo---KiJFj4a_tePVw95pSrjg=="));
    514     EXPECT_EQ(true, is_valid_package_name("really_LONG.a1234.package_name"));
    515 
    516     EXPECT_EQ(false, is_valid_package_name("1234.package"));
    517     EXPECT_EQ(false, is_valid_package_name("com.1234.package"));
    518     EXPECT_EQ(false, is_valid_package_name(""));
    519     EXPECT_EQ(false, is_valid_package_name("."));
    520     EXPECT_EQ(false, is_valid_package_name(".."));
    521     EXPECT_EQ(false, is_valid_package_name("../"));
    522     EXPECT_EQ(false, is_valid_package_name("com.example/../com.evil/"));
    523     EXPECT_EQ(false, is_valid_package_name("com.example-1/../com.evil/"));
    524     EXPECT_EQ(false, is_valid_package_name("/com.evil"));
    525 }
    526 
    527 TEST_F(UtilsTest, CreateDataUserProfilePath) {
    528     EXPECT_EQ("/data/misc/profiles/cur/0", create_primary_cur_profile_dir_path(0));
    529     EXPECT_EQ("/data/misc/profiles/cur/1", create_primary_cur_profile_dir_path(1));
    530 }
    531 
    532 TEST_F(UtilsTest, CreateDataUserProfilePackagePath) {
    533     EXPECT_EQ("/data/misc/profiles/cur/0/com.example",
    534             create_primary_current_profile_package_dir_path(0, "com.example"));
    535     EXPECT_EQ("/data/misc/profiles/cur/1/com.example",
    536             create_primary_current_profile_package_dir_path(1, "com.example"));
    537 }
    538 
    539 TEST_F(UtilsTest, CreateDataRefProfilePath) {
    540     EXPECT_EQ("/data/misc/profiles/ref", create_primary_ref_profile_dir_path());
    541 }
    542 
    543 TEST_F(UtilsTest, CreateDataRefProfilePackagePath) {
    544     EXPECT_EQ("/data/misc/profiles/ref/com.example",
    545         create_primary_reference_profile_package_dir_path("com.example"));
    546 }
    547 
    548 TEST_F(UtilsTest, CreatePrimaryCurrentProfile) {
    549     std::string expected =
    550         create_primary_current_profile_package_dir_path(0, "com.example") + "/primary.prof";
    551     EXPECT_EQ(expected,
    552             create_current_profile_path(/*user*/0, "com.example", /*is_secondary*/false));
    553 }
    554 
    555 TEST_F(UtilsTest, CreatePrimaryReferenceProfile) {
    556     std::string expected =
    557         create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof";
    558     EXPECT_EQ(expected,
    559             create_reference_profile_path("com.example", /*is_secondary*/false));
    560 }
    561 
    562 TEST_F(UtilsTest, CreateSecondaryCurrentProfile) {
    563     EXPECT_EQ("/data/user/0/com.example/secondary.dex.prof",
    564             create_current_profile_path(/*user*/0,
    565                     "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
    566 }
    567 
    568 TEST_F(UtilsTest, CreateSecondaryReferenceProfile) {
    569     EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.prof",
    570             create_reference_profile_path(
    571                     "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
    572 }
    573 
    574 }  // namespace installd
    575 }  // namespace android
    576