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 #define LOG_TAG "utils_test" 21 #include <utils/Log.h> 22 23 #include <gtest/gtest.h> 24 25 extern "C" { 26 #include "installd.h" 27 } 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_ASEC_DIR "/mnt/asec/" 33 34 #define TEST_SYSTEM_DIR1 "/system/app/" 35 #define TEST_SYSTEM_DIR2 "/vendor/app/" 36 37 #define REALLY_LONG_APP_NAME "com.example." \ 38 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \ 39 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa." \ 40 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 41 42 #define REALLY_LONG_LEAF_NAME "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \ 43 "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \ 44 "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" \ 45 "shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_shared_prefs_" 46 47 namespace android { 48 49 class UtilsTest : public testing::Test { 50 protected: 51 virtual void SetUp() { 52 android_app_dir.path = TEST_APP_DIR; 53 android_app_dir.len = strlen(TEST_APP_DIR); 54 55 android_app_private_dir.path = TEST_APP_PRIVATE_DIR; 56 android_app_private_dir.len = strlen(TEST_APP_PRIVATE_DIR); 57 58 android_data_dir.path = TEST_DATA_DIR; 59 android_data_dir.len = strlen(TEST_DATA_DIR); 60 61 android_asec_dir.path = TEST_ASEC_DIR; 62 android_asec_dir.len = strlen(TEST_ASEC_DIR); 63 64 android_system_dirs.count = 2; 65 66 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t)); 67 android_system_dirs.dirs[0].path = TEST_SYSTEM_DIR1; 68 android_system_dirs.dirs[0].len = strlen(TEST_SYSTEM_DIR1); 69 70 android_system_dirs.dirs[1].path = TEST_SYSTEM_DIR2; 71 android_system_dirs.dirs[1].len = strlen(TEST_SYSTEM_DIR2); 72 } 73 74 virtual void TearDown() { 75 free(android_system_dirs.dirs); 76 } 77 }; 78 79 TEST_F(UtilsTest, IsValidApkPath_BadPrefix) { 80 // Bad prefixes directories 81 const char *badprefix1 = "/etc/passwd"; 82 EXPECT_EQ(-1, validate_apk_path(badprefix1)) 83 << badprefix1 << " should be allowed as a valid path"; 84 85 const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah"; 86 EXPECT_EQ(-1, validate_apk_path(badprefix2)) 87 << badprefix2 << " should be allowed as a valid path"; 88 89 const char *badprefix3 = "init.rc"; 90 EXPECT_EQ(-1, validate_apk_path(badprefix3)) 91 << badprefix3 << " should be allowed as a valid path"; 92 93 const char *badprefix4 = "/init.rc"; 94 EXPECT_EQ(-1, validate_apk_path(badprefix4)) 95 << badprefix4 << " should be allowed as a valid path"; 96 } 97 98 TEST_F(UtilsTest, IsValidApkPath_Internal) { 99 // Internal directories 100 const char *internal1 = TEST_APP_DIR "example.apk"; 101 EXPECT_EQ(0, validate_apk_path(internal1)) 102 << internal1 << " should be allowed as a valid path"; 103 104 const char *badint1 = TEST_APP_DIR "../example.apk"; 105 EXPECT_EQ(-1, validate_apk_path(badint1)) 106 << badint1 << " should be rejected as a invalid path"; 107 108 const char *badint2 = TEST_APP_DIR "/../example.apk"; 109 EXPECT_EQ(-1, validate_apk_path(badint2)) 110 << badint2 << " should be rejected as a invalid path"; 111 112 const char *badint3 = TEST_APP_DIR "example.com/pkg.apk"; 113 EXPECT_EQ(-1, validate_apk_path(badint3)) 114 << badint3 << " should be rejected as a invalid path"; 115 } 116 117 TEST_F(UtilsTest, IsValidApkPath_Private) { 118 // Internal directories 119 const char *private1 = TEST_APP_PRIVATE_DIR "example.apk"; 120 EXPECT_EQ(0, validate_apk_path(private1)) 121 << private1 << " should be allowed as a valid path"; 122 123 const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk"; 124 EXPECT_EQ(-1, validate_apk_path(badpriv1)) 125 << badpriv1 << " should be rejected as a invalid path"; 126 127 const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk"; 128 EXPECT_EQ(-1, validate_apk_path(badpriv2)) 129 << badpriv2 << " should be rejected as a invalid path"; 130 131 const char *badpriv3 = TEST_APP_PRIVATE_DIR "example.com/pkg.apk"; 132 EXPECT_EQ(-1, validate_apk_path(badpriv3)) 133 << badpriv3 << " should be rejected as a invalid path"; 134 } 135 136 137 TEST_F(UtilsTest, IsValidApkPath_AsecGood1) { 138 const char *asec1 = TEST_ASEC_DIR "example.apk"; 139 EXPECT_EQ(0, validate_apk_path(asec1)) 140 << asec1 << " should be allowed as a valid path"; 141 } 142 143 TEST_F(UtilsTest, IsValidApkPath_AsecGood2) { 144 const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk"; 145 EXPECT_EQ(0, validate_apk_path(asec2)) 146 << asec2 << " should be allowed as a valid path"; 147 } 148 149 TEST_F(UtilsTest, IsValidApkPath_EscapeFail) { 150 const char *badasec1 = TEST_ASEC_DIR "../example.apk"; 151 EXPECT_EQ(-1, validate_apk_path(badasec1)) 152 << badasec1 << " should be rejected as a invalid path"; 153 } 154 155 TEST_F(UtilsTest, IsValidApkPath_DoubleSlashFail) { 156 const char *badasec2 = TEST_ASEC_DIR "com.example.asec//pkg.apk"; 157 EXPECT_EQ(-1, validate_apk_path(badasec2)) 158 << badasec2 << " should be rejected as a invalid path"; 159 } 160 161 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) { 162 const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk"; 163 EXPECT_EQ(-1, validate_apk_path(badasec3)) 164 << badasec3 << " should be rejected as a invalid path"; 165 } 166 167 TEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) { 168 const char *badasec4 = TEST_ASEC_DIR "/../example.apk"; 169 EXPECT_EQ(-1, validate_apk_path(badasec4)) 170 << badasec4 << " should be rejected as a invalid path"; 171 } 172 173 TEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) { 174 const char *badasec5 = TEST_ASEC_DIR ".//../.."; 175 EXPECT_EQ(-1, validate_apk_path(badasec5)) 176 << badasec5 << " should be rejected as a invalid path"; 177 } 178 179 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) { 180 const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk"; 181 EXPECT_EQ(-1, validate_apk_path(badasec6)) 182 << badasec6 << " should be rejected as a invalid path"; 183 } 184 185 TEST_F(UtilsTest, IsValidApkPath_TwoSubdirFail) { 186 const char *badasec7 = TEST_ASEC_DIR "com.example.asec/subdir1/pkg.apk"; 187 EXPECT_EQ(-1, validate_apk_path(badasec7)) 188 << badasec7 << " should be rejected as a invalid path"; 189 } 190 191 TEST_F(UtilsTest, CheckSystemApp_Dir1) { 192 const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk"; 193 EXPECT_EQ(0, validate_system_app_path(sysapp1)) 194 << sysapp1 << " should be allowed as a system path"; 195 } 196 197 TEST_F(UtilsTest, CheckSystemApp_Dir2) { 198 const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk"; 199 EXPECT_EQ(0, validate_system_app_path(sysapp2)) 200 << sysapp2 << " should be allowed as a system path"; 201 } 202 203 TEST_F(UtilsTest, CheckSystemApp_EscapeFail) { 204 const char *badapp1 = TEST_SYSTEM_DIR1 "../com.example.apk"; 205 EXPECT_EQ(-1, validate_system_app_path(badapp1)) 206 << badapp1 << " should be rejected not a system path"; 207 } 208 209 TEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) { 210 const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk"; 211 EXPECT_EQ(-1, validate_system_app_path(badapp2)) 212 << badapp2 << " should be rejected not a system path"; 213 } 214 215 TEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) { 216 const char *badapp3 = TEST_APP_DIR "/../../com.example.apk"; 217 EXPECT_EQ(-1, validate_system_app_path(badapp3)) 218 << badapp3 << " should be rejected not a system path"; 219 } 220 221 TEST_F(UtilsTest, GetPathFromString_NullPathFail) { 222 dir_rec_t test1; 223 EXPECT_EQ(-1, get_path_from_string(&test1, (const char *) NULL)) 224 << "Should not allow NULL as a path."; 225 } 226 227 TEST_F(UtilsTest, GetPathFromString_EmptyPathFail) { 228 dir_rec_t test1; 229 EXPECT_EQ(-1, get_path_from_string(&test1, "")) 230 << "Should not allow empty paths."; 231 } 232 233 TEST_F(UtilsTest, GetPathFromString_RelativePathFail) { 234 dir_rec_t test1; 235 EXPECT_EQ(-1, get_path_from_string(&test1, "mnt/asec")) 236 << "Should not allow relative paths."; 237 } 238 239 TEST_F(UtilsTest, GetPathFromString_NonCanonical) { 240 dir_rec_t test1; 241 242 EXPECT_EQ(0, get_path_from_string(&test1, "/mnt/asec")) 243 << "Should be able to canonicalize directory /mnt/asec"; 244 EXPECT_STREQ("/mnt/asec/", test1.path) 245 << "/mnt/asec should be canonicalized to /mnt/asec/"; 246 EXPECT_EQ(10, (ssize_t) test1.len) 247 << "path len should be equal to the length of /mnt/asec/ (10)"; 248 free(test1.path); 249 } 250 251 TEST_F(UtilsTest, GetPathFromString_CanonicalPath) { 252 dir_rec_t test3; 253 EXPECT_EQ(0, get_path_from_string(&test3, "/data/app/")) 254 << "Should be able to canonicalize directory /data/app/"; 255 EXPECT_STREQ("/data/app/", test3.path) 256 << "/data/app/ should be canonicalized to /data/app/"; 257 EXPECT_EQ(10, (ssize_t) test3.len) 258 << "path len should be equal to the length of /data/app/ (10)"; 259 free(test3.path); 260 } 261 262 TEST_F(UtilsTest, CreatePkgPath_LongPkgNameSuccess) { 263 char path[PKG_PATH_MAX]; 264 265 // Create long packagename of "aaaaa..." 266 size_t pkgnameSize = PKG_NAME_MAX; 267 char pkgname[pkgnameSize + 1]; 268 memset(pkgname, 'a', pkgnameSize); 269 pkgname[pkgnameSize] = '\0'; 270 271 EXPECT_EQ(0, create_pkg_path(path, pkgname, "", 0)) 272 << "Should successfully be able to create package name."; 273 274 const char *prefix = TEST_DATA_DIR PRIMARY_USER_PREFIX; 275 size_t offset = strlen(prefix); 276 EXPECT_STREQ(pkgname, path + offset) 277 << "Package path should be a really long string of a's"; 278 } 279 280 TEST_F(UtilsTest, CreatePkgPath_LongPkgNameFail) { 281 char path[PKG_PATH_MAX]; 282 283 // Create long packagename of "aaaaa..." 284 size_t pkgnameSize = PKG_NAME_MAX + 1; 285 char pkgname[pkgnameSize + 1]; 286 memset(pkgname, 'a', pkgnameSize); 287 pkgname[pkgnameSize] = '\0'; 288 289 EXPECT_EQ(-1, create_pkg_path(path, pkgname, "", 0)) 290 << "Should return error because package name is too long."; 291 } 292 293 TEST_F(UtilsTest, CreatePkgPath_LongPostfixFail) { 294 char path[PKG_PATH_MAX]; 295 296 // Create long packagename of "aaaaa..." 297 size_t postfixSize = PKG_PATH_MAX; 298 char postfix[postfixSize + 1]; 299 memset(postfix, 'a', postfixSize); 300 postfix[postfixSize] = '\0'; 301 302 EXPECT_EQ(-1, create_pkg_path(path, "com.example.package", postfix, 0)) 303 << "Should return error because postfix is too long."; 304 } 305 306 TEST_F(UtilsTest, CreatePkgPath_PrimaryUser) { 307 char path[PKG_PATH_MAX]; 308 309 EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 0)) 310 << "Should return error because postfix is too long."; 311 312 EXPECT_STREQ(TEST_DATA_DIR PRIMARY_USER_PREFIX "com.example.package", path) 313 << "Package path should be in /data/data/"; 314 } 315 316 TEST_F(UtilsTest, CreatePkgPath_SecondaryUser) { 317 char path[PKG_PATH_MAX]; 318 319 EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 1)) 320 << "Should successfully create package path."; 321 322 EXPECT_STREQ(TEST_DATA_DIR SECONDARY_USER_PREFIX "1/com.example.package", path) 323 << "Package path should be in /data/user/"; 324 } 325 326 TEST_F(UtilsTest, CreatePkgPathInDir_ProtectedDir) { 327 char path[PKG_PATH_MAX]; 328 329 dir_rec_t dir; 330 dir.path = "/data/app-private/"; 331 dir.len = strlen(dir.path); 332 333 EXPECT_EQ(0, create_pkg_path_in_dir(path, &dir, "com.example.package", ".apk")) 334 << "Should successfully create package path."; 335 336 EXPECT_STREQ("/data/app-private/com.example.package.apk", path) 337 << "Package path should be in /data/app-private/"; 338 } 339 340 TEST_F(UtilsTest, CreatePersonaPath_Primary) { 341 char path[PKG_PATH_MAX]; 342 343 EXPECT_EQ(0, create_user_path(path, 0)) 344 << "Should successfully build primary user path."; 345 346 EXPECT_STREQ("/data/data/", path) 347 << "Primary user should have correct path"; 348 } 349 350 TEST_F(UtilsTest, CreatePersonaPath_Secondary) { 351 char path[PKG_PATH_MAX]; 352 353 EXPECT_EQ(0, create_user_path(path, 1)) 354 << "Should successfully build primary user path."; 355 356 EXPECT_STREQ("/data/user/1/", path) 357 << "Primary user should have correct path"; 358 } 359 360 TEST_F(UtilsTest, CreateMovePath_Primary) { 361 char path[PKG_PATH_MAX]; 362 363 EXPECT_EQ(0, create_move_path(path, "com.android.test", "shared_prefs", 0)) 364 << "Should be able to create move path for primary user"; 365 366 EXPECT_STREQ("/data/data/com.android.test/shared_prefs", path) 367 << "Primary user package directory should be created correctly"; 368 } 369 370 TEST_F(UtilsTest, CreateMovePath_Fail_AppTooLong) { 371 char path[PKG_PATH_MAX]; 372 373 EXPECT_EQ(-1, create_move_path(path, REALLY_LONG_APP_NAME, "shared_prefs", 0)) 374 << "Should fail to create move path for primary user"; 375 } 376 377 TEST_F(UtilsTest, CreateMovePath_Fail_LeafTooLong) { 378 char path[PKG_PATH_MAX]; 379 380 EXPECT_EQ(-1, create_move_path(path, "com.android.test", REALLY_LONG_LEAF_NAME, 0)) 381 << "Should fail to create move path for primary user"; 382 } 383 384 TEST_F(UtilsTest, CopyAndAppend_Normal) { 385 //int copy_and_append(dir_rec_t* dst, dir_rec_t* src, char* suffix) 386 dir_rec_t dst; 387 dir_rec_t src; 388 389 src.path = "/data/"; 390 src.len = strlen(src.path); 391 392 EXPECT_EQ(0, copy_and_append(&dst, &src, "app/")) 393 << "Should return error because postfix is too long."; 394 395 EXPECT_STREQ("/data/app/", dst.path) 396 << "Appended path should be correct"; 397 398 EXPECT_EQ(10, (ssize_t) dst.len) 399 << "Appended path should be length of '/data/app/' (10)"; 400 } 401 402 TEST_F(UtilsTest, AppendAndIncrement_Normal) { 403 size_t dst_size = 10; 404 char dst[dst_size]; 405 char *dstp = dst; 406 const char* src = "FOO"; 407 408 EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size)) 409 << "String should append successfully"; 410 411 EXPECT_STREQ("FOO", dst) 412 << "String should append correctly"; 413 414 EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size)) 415 << "String should append successfully again"; 416 417 EXPECT_STREQ("FOOFOO", dst) 418 << "String should append correctly again"; 419 } 420 421 TEST_F(UtilsTest, AppendAndIncrement_TooBig) { 422 size_t dst_size = 5; 423 char dst[dst_size]; 424 char *dstp = dst; 425 const char* src = "FOO"; 426 427 EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size)) 428 << "String should append successfully"; 429 430 EXPECT_STREQ("FOO", dst) 431 << "String should append correctly"; 432 433 EXPECT_EQ(-1, append_and_increment(&dstp, src, &dst_size)) 434 << "String should fail because it's too large to fit"; 435 } 436 437 } 438