installd_utils_test.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. #include <stdlib.h>
  17. #include <string.h>
  18. #include <android-base/logging.h>
  19. #include <android-base/scopeguard.h>
  20. #include <gtest/gtest.h>
  21. #include "InstalldNativeService.h"
  22. #include "MatchExtensionGen.h"
  23. #include "globals.h"
  24. #include "utils.h"
  25. #undef LOG_TAG
  26. #define LOG_TAG "utils_test"
  27. #define TEST_DATA_DIR "/data/"
  28. #define TEST_ROOT_DIR "/system/"
  29. #define TEST_APP_DIR "/data/app/"
  30. #define TEST_APP_PRIVATE_DIR "/data/app-private/"
  31. #define TEST_APP_EPHEMERAL_DIR "/data/app-ephemeral/"
  32. #define TEST_ASEC_DIR "/mnt/asec/"
  33. #define TEST_EXPAND_DIR "/mnt/expand/00000000-0000-0000-0000-000000000000/"
  34. #define TEST_SYSTEM_DIR1 "/system/app/"
  35. #define TEST_SYSTEM_DIR2 "/vendor/app/"
  36. #define TEST_PROFILE_DIR "/data/misc/profiles"
  37. namespace android {
  38. namespace installd {
  39. class UtilsTest : public testing::Test {
  40. protected:
  41. virtual void SetUp() {
  42. setenv("ANDROID_LOG_TAGS", "*:v", 1);
  43. android::base::InitLogging(nullptr);
  44. init_globals_from_data_and_root(TEST_DATA_DIR, TEST_ROOT_DIR);
  45. }
  46. virtual void TearDown() {
  47. }
  48. std::string create_too_long_path(const std::string& seed) {
  49. std::string result = seed;
  50. for (size_t i = seed.size(); i < PKG_PATH_MAX; i++) {
  51. result += "a";
  52. }
  53. return result;
  54. }
  55. };
  56. TEST_F(UtilsTest, IsValidApkPath_BadPrefix) {
  57. // Bad prefixes directories
  58. const char *badprefix1 = "/etc/passwd";
  59. EXPECT_EQ(-1, validate_apk_path(badprefix1))
  60. << badprefix1 << " should not be allowed as a valid path";
  61. const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah";
  62. EXPECT_EQ(-1, validate_apk_path(badprefix2))
  63. << badprefix2 << " should not be allowed as a valid path";
  64. const char *badprefix3 = "init.rc";
  65. EXPECT_EQ(-1, validate_apk_path(badprefix3))
  66. << badprefix3 << " should not be allowed as a valid path";
  67. const char *badprefix4 = "/init.rc";
  68. EXPECT_EQ(-1, validate_apk_path(badprefix4))
  69. << badprefix4 << " should not be allowed as a valid path";
  70. }
  71. TEST_F(UtilsTest, IsValidApkPath_Internal) {
  72. // Internal directories
  73. const char *internal1 = TEST_APP_DIR "example.apk";
  74. EXPECT_EQ(0, validate_apk_path(internal1))
  75. << internal1 << " should be allowed as a valid path";
  76. // b/16888084
  77. const char *path2 = TEST_APP_DIR "example.com/example.apk";
  78. EXPECT_EQ(0, validate_apk_path(path2))
  79. << path2 << " should be allowed as a valid path";
  80. const char *badint1 = TEST_APP_DIR "../example.apk";
  81. EXPECT_EQ(-1, validate_apk_path(badint1))
  82. << badint1 << " should be rejected as a invalid path";
  83. const char *badint2 = TEST_APP_DIR "/../example.apk";
  84. EXPECT_EQ(-1, validate_apk_path(badint2))
  85. << badint2 << " should be rejected as a invalid path";
  86. // Only one subdir should be allowed.
  87. const char *bad_path3 = TEST_APP_DIR "example.com/subdir/pkg.apk";
  88. EXPECT_EQ(-1, validate_apk_path(bad_path3))
  89. << bad_path3 << " should be rejected as a invalid path";
  90. const char *bad_path4 = TEST_APP_DIR "example.com/subdir/../pkg.apk";
  91. EXPECT_EQ(-1, validate_apk_path(bad_path4))
  92. << bad_path4 << " should be rejected as a invalid path";
  93. const char *bad_path5 = TEST_APP_DIR "example.com1/../example.com2/pkg.apk";
  94. EXPECT_EQ(-1, validate_apk_path(bad_path5))
  95. << bad_path5 << " should be rejected as a invalid path";
  96. }
  97. TEST_F(UtilsTest, IsValidApkPath_TopDir) {
  98. EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/com.example"));
  99. EXPECT_EQ(0, validate_apk_path(TEST_EXPAND_DIR "app/com.example"));
  100. EXPECT_EQ(-1, validate_apk_path(TEST_DATA_DIR "data/com.example"));
  101. EXPECT_EQ(-1, validate_apk_path(TEST_EXPAND_DIR "data/com.example"));
  102. }
  103. TEST_F(UtilsTest, IsValidApkPath_TopFile) {
  104. EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/com.example/base.apk"));
  105. EXPECT_EQ(0, validate_apk_path(TEST_EXPAND_DIR "app/com.example/base.apk"));
  106. EXPECT_EQ(-1, validate_apk_path(TEST_DATA_DIR "data/com.example/base.apk"));
  107. EXPECT_EQ(-1, validate_apk_path(TEST_EXPAND_DIR "data/com.example/base.apk"));
  108. }
  109. TEST_F(UtilsTest, IsValidApkPath_OatDir) {
  110. EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat"));
  111. EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat"));
  112. EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat"));
  113. EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat"));
  114. }
  115. TEST_F(UtilsTest, IsValidApkPath_OatDirDir) {
  116. EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat/arm64"));
  117. EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat/arm64"));
  118. EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat/arm64"));
  119. EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat/arm64"));
  120. }
  121. TEST_F(UtilsTest, IsValidApkPath_OatDirDirFile) {
  122. EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat/arm64/base.odex"));
  123. EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat/arm64/base.odex"));
  124. EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat/arm64/base.odex"));
  125. EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat/arm64/base.odex"));
  126. }
  127. TEST_F(UtilsTest, IsValidApkPath_Private) {
  128. // Internal directories
  129. const char *private1 = TEST_APP_PRIVATE_DIR "example.apk";
  130. EXPECT_EQ(0, validate_apk_path(private1))
  131. << private1 << " should be allowed as a valid path";
  132. // b/16888084
  133. const char *path2 = TEST_APP_DIR "example.com/example.apk";
  134. EXPECT_EQ(0, validate_apk_path(path2))
  135. << path2 << " should be allowed as a valid path";
  136. const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk";
  137. EXPECT_EQ(-1, validate_apk_path(badpriv1))
  138. << badpriv1 << " should be rejected as a invalid path";
  139. const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk";
  140. EXPECT_EQ(-1, validate_apk_path(badpriv2))
  141. << badpriv2 << " should be rejected as a invalid path";
  142. // Only one subdir should be allowed.
  143. const char *bad_path3 = TEST_APP_PRIVATE_DIR "example.com/subdir/pkg.apk";
  144. EXPECT_EQ(-1, validate_apk_path(bad_path3))
  145. << bad_path3 << " should be rejected as a invalid path";
  146. const char *bad_path4 = TEST_APP_PRIVATE_DIR "example.com/subdir/../pkg.apk";
  147. EXPECT_EQ(-1, validate_apk_path(bad_path4))
  148. << bad_path4 << " should be rejected as a invalid path";
  149. const char *bad_path5 = TEST_APP_PRIVATE_DIR "example.com1/../example.com2/pkg.apk";
  150. EXPECT_EQ(-1, validate_apk_path(bad_path5))
  151. << bad_path5 << " should be rejected as a invalid path";
  152. }
  153. TEST_F(UtilsTest, IsValidApkPath_AsecGood1) {
  154. const char *asec1 = TEST_ASEC_DIR "example.apk";
  155. EXPECT_EQ(0, validate_apk_path(asec1))
  156. << asec1 << " should be allowed as a valid path";
  157. }
  158. TEST_F(UtilsTest, IsValidApkPath_AsecGood2) {
  159. const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk";
  160. EXPECT_EQ(0, validate_apk_path(asec2))
  161. << asec2 << " should be allowed as a valid path";
  162. }
  163. TEST_F(UtilsTest, IsValidApkPath_EscapeFail) {
  164. const char *badasec1 = TEST_ASEC_DIR "../example.apk";
  165. EXPECT_EQ(-1, validate_apk_path(badasec1))
  166. << badasec1 << " should be rejected as a invalid path";
  167. }
  168. TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) {
  169. const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk";
  170. EXPECT_EQ(-1, validate_apk_path(badasec3))
  171. << badasec3 << " should be rejected as a invalid path";
  172. }
  173. TEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) {
  174. const char *badasec4 = TEST_ASEC_DIR "/../example.apk";
  175. EXPECT_EQ(-1, validate_apk_path(badasec4))
  176. << badasec4 << " should be rejected as a invalid path";
  177. }
  178. TEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) {
  179. const char *badasec5 = TEST_ASEC_DIR ".//../..";
  180. EXPECT_EQ(-1, validate_apk_path(badasec5))
  181. << badasec5 << " should be rejected as a invalid path";
  182. }
  183. TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) {
  184. const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk";
  185. EXPECT_EQ(-1, validate_apk_path(badasec6))
  186. << badasec6 << " should be rejected as a invalid path";
  187. }
  188. TEST_F(UtilsTest, IsValidApkPath_TwoSubdirFail) {
  189. const char *badasec7 = TEST_ASEC_DIR "com.example.asec/subdir1/pkg.apk";
  190. EXPECT_EQ(-1, validate_apk_path(badasec7))
  191. << badasec7 << " should be rejected as a invalid path";
  192. }
  193. TEST_F(UtilsTest, CheckSystemApp_Dir1) {
  194. const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk";
  195. EXPECT_EQ(0, validate_system_app_path(sysapp1))
  196. << sysapp1 << " should be allowed as a system path";
  197. }
  198. TEST_F(UtilsTest, CheckSystemApp_Dir2) {
  199. const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk";
  200. EXPECT_EQ(0, validate_system_app_path(sysapp2))
  201. << sysapp2 << " should be allowed as a system path";
  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. TEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) {
  209. const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk";
  210. EXPECT_EQ(-1, validate_system_app_path(badapp2))
  211. << badapp2 << " should be rejected not a system path";
  212. }
  213. TEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) {
  214. const char *badapp3 = TEST_APP_DIR "/../../com.example.apk";
  215. EXPECT_EQ(-1, validate_system_app_path(badapp3))
  216. << badapp3 << " should be rejected not a system path";
  217. }
  218. TEST_F(UtilsTest, CheckSystemApp_Subdir) {
  219. const char *sysapp = TEST_SYSTEM_DIR1 "com.example/com.example.apk";
  220. EXPECT_EQ(0, validate_system_app_path(sysapp))
  221. << sysapp << " should be allowed as a system path";
  222. const char *badapp = TEST_SYSTEM_DIR1 "com.example/subdir/com.example.apk";
  223. EXPECT_EQ(-1, validate_system_app_path(badapp))
  224. << badapp << " should be rejected not a system path";
  225. const char *badapp1 = TEST_SYSTEM_DIR1 "com.example/subdir/../com.example.apk";
  226. EXPECT_EQ(-1, validate_system_app_path(badapp1))
  227. << badapp1 << " should be rejected not a system path";
  228. const char *badapp2 = TEST_SYSTEM_DIR1 "com.example1/../com.example2/com.example.apk";
  229. EXPECT_EQ(-1, validate_system_app_path(badapp2))
  230. << badapp2 << " should be rejected not a system path";
  231. }
  232. TEST_F(UtilsTest, CreateDataPath) {
  233. EXPECT_EQ("/data", create_data_path(nullptr));
  234. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b",
  235. create_data_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
  236. }
  237. TEST_F(UtilsTest, CreateDataAppPath) {
  238. EXPECT_EQ("/data/app", create_data_app_path(nullptr));
  239. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app",
  240. create_data_app_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
  241. }
  242. TEST_F(UtilsTest, CreateDataUserPath) {
  243. EXPECT_EQ("/data/data", create_data_user_ce_path(nullptr, 0));
  244. EXPECT_EQ("/data/user/10", create_data_user_ce_path(nullptr, 10));
  245. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0",
  246. create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
  247. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10",
  248. create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
  249. }
  250. TEST_F(UtilsTest, CreateDataMediaPath) {
  251. EXPECT_EQ("/data/media/0", create_data_media_path(nullptr, 0));
  252. EXPECT_EQ("/data/media/10", create_data_media_path(nullptr, 10));
  253. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/0",
  254. create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
  255. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/10",
  256. create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
  257. }
  258. TEST_F(UtilsTest, CreateDataAppPackagePath) {
  259. EXPECT_EQ("/data/app/com.example", create_data_app_package_path(nullptr, "com.example"));
  260. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app/com.example",
  261. create_data_app_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", "com.example"));
  262. }
  263. TEST_F(UtilsTest, CreateDataUserPackagePath) {
  264. EXPECT_EQ("/data/data/com.example", create_data_user_ce_package_path(nullptr, 0, "com.example"));
  265. EXPECT_EQ("/data/user/10/com.example", create_data_user_ce_package_path(nullptr, 10, "com.example"));
  266. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0/com.example",
  267. create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, "com.example"));
  268. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10/com.example",
  269. create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10, "com.example"));
  270. }
  271. TEST_F(UtilsTest, IsValidPackageName) {
  272. EXPECT_EQ(true, is_valid_package_name("android"));
  273. EXPECT_EQ(true, is_valid_package_name("com.example"));
  274. EXPECT_EQ(true, is_valid_package_name("com.example-1"));
  275. EXPECT_EQ(true, is_valid_package_name("com.example-1024"));
  276. EXPECT_EQ(true, is_valid_package_name("com.example.foo---KiJFj4a_tePVw95pSrjg=="));
  277. EXPECT_EQ(true, is_valid_package_name("really_LONG.a1234.package_name"));
  278. EXPECT_EQ(false, is_valid_package_name("1234.package"));
  279. EXPECT_EQ(false, is_valid_package_name("com.1234.package"));
  280. EXPECT_EQ(false, is_valid_package_name(""));
  281. EXPECT_EQ(false, is_valid_package_name("."));
  282. EXPECT_EQ(false, is_valid_package_name(".."));
  283. EXPECT_EQ(false, is_valid_package_name("../"));
  284. EXPECT_EQ(false, is_valid_package_name("com.example/../com.evil/"));
  285. EXPECT_EQ(false, is_valid_package_name("com.example-1/../com.evil/"));
  286. EXPECT_EQ(false, is_valid_package_name("/com.evil"));
  287. }
  288. TEST_F(UtilsTest, CreateDataUserProfilePath) {
  289. EXPECT_EQ("/data/misc/profiles/cur/0", create_primary_cur_profile_dir_path(0));
  290. EXPECT_EQ("/data/misc/profiles/cur/1", create_primary_cur_profile_dir_path(1));
  291. }
  292. TEST_F(UtilsTest, CreateDataUserProfilePackagePath) {
  293. EXPECT_EQ("/data/misc/profiles/cur/0/com.example",
  294. create_primary_current_profile_package_dir_path(0, "com.example"));
  295. EXPECT_EQ("/data/misc/profiles/cur/1/com.example",
  296. create_primary_current_profile_package_dir_path(1, "com.example"));
  297. }
  298. TEST_F(UtilsTest, CreateDataRefProfilePath) {
  299. EXPECT_EQ("/data/misc/profiles/ref", create_primary_ref_profile_dir_path());
  300. }
  301. TEST_F(UtilsTest, CreateDataRefProfilePackagePath) {
  302. EXPECT_EQ("/data/misc/profiles/ref/com.example",
  303. create_primary_reference_profile_package_dir_path("com.example"));
  304. }
  305. TEST_F(UtilsTest, CreatePrimaryCurrentProfile) {
  306. std::string expected_base =
  307. create_primary_current_profile_package_dir_path(0, "com.example") + "/primary.prof";
  308. EXPECT_EQ(expected_base,
  309. create_current_profile_path(/*user*/0, "com.example", "primary.prof",
  310. /*is_secondary*/false));
  311. std::string expected_split =
  312. create_primary_current_profile_package_dir_path(0, "com.example") + "/split.prof";
  313. EXPECT_EQ(expected_split,
  314. create_current_profile_path(/*user*/0, "com.example", "split.prof",
  315. /*is_secondary*/false));
  316. }
  317. TEST_F(UtilsTest, CreatePrimaryReferenceProfile) {
  318. std::string expected_base =
  319. create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof";
  320. EXPECT_EQ(expected_base,
  321. create_reference_profile_path("com.example", "primary.prof", /*is_secondary*/false));
  322. std::string expected_split =
  323. create_primary_reference_profile_package_dir_path("com.example") + "/split.prof";
  324. EXPECT_EQ(expected_split,
  325. create_reference_profile_path("com.example", "split.prof", /*is_secondary*/false));
  326. }
  327. TEST_F(UtilsTest, CreateProfileSnapshot) {
  328. std::string expected_base =
  329. create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof.snapshot";
  330. EXPECT_EQ(expected_base, create_snapshot_profile_path("com.example", "primary.prof"));
  331. std::string expected_split =
  332. create_primary_reference_profile_package_dir_path("com.example") + "/split.prof.snapshot";
  333. EXPECT_EQ(expected_split, create_snapshot_profile_path("com.example", "split.prof"));
  334. }
  335. TEST_F(UtilsTest, CreateSecondaryCurrentProfile) {
  336. EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.cur.prof",
  337. create_current_profile_path(/*user*/0, "com.example",
  338. "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
  339. }
  340. TEST_F(UtilsTest, CreateSecondaryReferenceProfile) {
  341. EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.prof",
  342. create_reference_profile_path("com.example",
  343. "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
  344. }
  345. static void pass_secondary_dex_validation(const std::string& package_name,
  346. const std::string& dex_path, int uid, int storage_flag) {
  347. EXPECT_TRUE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
  348. storage_flag))
  349. << dex_path << " should be allowed as a valid secondary dex path";
  350. }
  351. static void fail_secondary_dex_validation(const std::string& package_name,
  352. const std::string& dex_path, int uid, int storage_flag) {
  353. EXPECT_FALSE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
  354. storage_flag))
  355. << dex_path << " should not be allowed as a valid secondary dex path";
  356. }
  357. TEST_F(UtilsTest, ValidateSecondaryDexFilesPath) {
  358. std::string package_name = "com.test.app";
  359. std::string app_dir_ce_user_0 = "/data/data/" + package_name;
  360. std::string app_dir_ce_user_0_link = "/data/user/0/" + package_name;
  361. std::string app_dir_ce_user_10 = "/data/user/10/" + package_name;
  362. std::string app_dir_de_user_0 = "/data/user_de/0/" + package_name;
  363. std::string app_dir_de_user_10 = "/data/user_de/10/" + package_name;
  364. EXPECT_EQ(app_dir_ce_user_0,
  365. create_data_user_ce_package_path(nullptr, 0, package_name.c_str()));
  366. EXPECT_EQ(app_dir_ce_user_10,
  367. create_data_user_ce_package_path(nullptr, 10, package_name.c_str()));
  368. EXPECT_EQ(app_dir_de_user_0,
  369. create_data_user_de_package_path(nullptr, 0, package_name.c_str()));
  370. EXPECT_EQ(app_dir_de_user_10,
  371. create_data_user_de_package_path(nullptr, 10, package_name.c_str()));
  372. uid_t app_uid_for_user_0 = multiuser_get_uid(/*user_id*/0, /*app_id*/ 1234);
  373. uid_t app_uid_for_user_10 = multiuser_get_uid(/*user_id*/10, /*app_id*/ 1234);
  374. // Standard path for user 0 on CE storage.
  375. pass_secondary_dex_validation(
  376. package_name, app_dir_ce_user_0 + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
  377. pass_secondary_dex_validation(
  378. package_name, app_dir_ce_user_0_link + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
  379. // Standard path for user 10 on CE storage.
  380. pass_secondary_dex_validation(
  381. package_name, app_dir_ce_user_10 + "/ce10.dex", app_uid_for_user_10, FLAG_STORAGE_CE);
  382. // Standard path for user 0 on DE storage.
  383. pass_secondary_dex_validation(
  384. package_name, app_dir_de_user_0 + "/de0.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
  385. // Standard path for user 10 on DE storage.
  386. pass_secondary_dex_validation(
  387. package_name, app_dir_de_user_10 + "/de0.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
  388. // Dex path for user 0 accessed from user 10.
  389. fail_secondary_dex_validation(
  390. package_name, app_dir_ce_user_0 + "/path0_from10.dex",
  391. app_uid_for_user_10, FLAG_STORAGE_CE);
  392. // Dex path for CE storage accessed with DE.
  393. fail_secondary_dex_validation(
  394. package_name, app_dir_ce_user_0 + "/ce_from_de.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
  395. // Dex path for DE storage accessed with CE.
  396. fail_secondary_dex_validation(
  397. package_name, app_dir_de_user_0 + "/de_from_ce.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
  398. // Location which does not start with '/'.
  399. fail_secondary_dex_validation(
  400. package_name, "without_slash.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
  401. // The dex file is not in the specified package directory.
  402. fail_secondary_dex_validation(
  403. "another.package", app_dir_ce_user_0 + "/for_another_package.dex",
  404. app_uid_for_user_0, FLAG_STORAGE_DE);
  405. // The dex path contains indirect directories.
  406. fail_secondary_dex_validation(
  407. package_name, app_dir_ce_user_0 + "/1/../foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
  408. fail_secondary_dex_validation(
  409. package_name, app_dir_ce_user_0 + "/1/./foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
  410. // Super long path.
  411. std::string too_long = create_too_long_path("too_long_");
  412. fail_secondary_dex_validation(
  413. package_name, app_dir_ce_user_10 + "/" + too_long, app_uid_for_user_10, FLAG_STORAGE_CE);
  414. }
  415. TEST_F(UtilsTest, ValidateApkPath) {
  416. EXPECT_EQ(0, validate_apk_path("/data/app/com.example"));
  417. EXPECT_EQ(0, validate_apk_path("/data/app/com.example/file"));
  418. EXPECT_EQ(0, validate_apk_path("/data/app/com.example//file"));
  419. EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/"));
  420. EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/file"));
  421. EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/file"));
  422. EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir//file"));
  423. EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir/file"));
  424. EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir//file"));
  425. }
  426. TEST_F(UtilsTest, ValidateApkPathSubdirs) {
  427. EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example"));
  428. EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/file"));
  429. EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example//file"));
  430. EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/"));
  431. EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/file"));
  432. EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/file"));
  433. EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir//file"));
  434. EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/file"));
  435. EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir//file"));
  436. }
  437. TEST_F(UtilsTest, MatchExtension_Valid) {
  438. EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mpg"));
  439. EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mpeg"));
  440. EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mPeG"));
  441. EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("MPEG"));
  442. }
  443. TEST_F(UtilsTest, MatchExtension_Invalid) {
  444. EXPECT_EQ(0, MatchExtension("log"));
  445. EXPECT_EQ(0, MatchExtension("3amp"));
  446. EXPECT_EQ(0, MatchExtension("fpe"));
  447. EXPECT_EQ(0, MatchExtension("docx"));
  448. }
  449. TEST_F(UtilsTest, TestRollbackPaths) {
  450. EXPECT_EQ("/data/misc_ce/0/rollback/239/com.foo",
  451. create_data_misc_ce_rollback_package_path(nullptr, 0, 239, "com.foo"));
  452. EXPECT_EQ("/data/misc_ce/10/rollback/37/com.foo",
  453. create_data_misc_ce_rollback_package_path(nullptr, 10, 37, "com.foo"));
  454. EXPECT_EQ("/data/misc_de/0/rollback/73/com.foo",
  455. create_data_misc_de_rollback_package_path(nullptr, 0, 73, "com.foo"));
  456. EXPECT_EQ("/data/misc_de/10/rollback/13/com.foo",
  457. create_data_misc_de_rollback_package_path(nullptr, 10, 13, "com.foo"));
  458. EXPECT_EQ("/data/misc_ce/0/rollback/57",
  459. create_data_misc_ce_rollback_path(nullptr, 0, 57));
  460. EXPECT_EQ("/data/misc_ce/10/rollback/1543",
  461. create_data_misc_ce_rollback_path(nullptr, 10, 1543));
  462. EXPECT_EQ("/data/misc_de/0/rollback/43",
  463. create_data_misc_de_rollback_path(nullptr, 0, 43));
  464. EXPECT_EQ("/data/misc_de/10/rollback/41",
  465. create_data_misc_de_rollback_path(nullptr, 10, 41));
  466. EXPECT_EQ("/data/misc_ce/0/rollback/17/com.foo",
  467. create_data_misc_ce_rollback_package_path(nullptr, 0, 17, "com.foo", 0));
  468. EXPECT_EQ("/data/misc_ce/0/rollback/19/com.foo",
  469. create_data_misc_ce_rollback_package_path(nullptr, 0, 19, "com.foo", 239));
  470. auto rollback_ce_path = create_data_misc_ce_rollback_path(nullptr, 0, 53);
  471. auto rollback_ce_package_path = create_data_misc_ce_rollback_package_path(nullptr, 0, 53,
  472. "com.foo");
  473. auto deleter = [&rollback_ce_path]() {
  474. delete_dir_contents_and_dir(rollback_ce_path, true /* ignore_if_missing */);
  475. };
  476. auto scope_guard = android::base::make_scope_guard(deleter);
  477. EXPECT_NE(-1, mkdir(rollback_ce_path.c_str(), 700));
  478. EXPECT_NE(-1, mkdir(rollback_ce_package_path.c_str(), 700));
  479. ino_t ce_data_inode;
  480. EXPECT_EQ(0, get_path_inode(rollback_ce_package_path, &ce_data_inode));
  481. EXPECT_EQ("/data/misc_ce/0/rollback/53/com.foo",
  482. create_data_misc_ce_rollback_package_path(nullptr, 0, 53, "com.foo", ce_data_inode));
  483. // Check that path defined by inode is picked even if it's not the same as
  484. // the fallback one.
  485. EXPECT_EQ("/data/misc_ce/0/rollback/53/com.foo",
  486. create_data_misc_ce_rollback_package_path(nullptr, 0, 53, "com.bar", ce_data_inode));
  487. // These last couple of cases are never exercised in production because we
  488. // only snapshot apps in the primary data partition. Exercise them here for
  489. // the sake of completeness.
  490. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/misc_ce/0/rollback/7/com.example",
  491. create_data_misc_ce_rollback_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, 7,
  492. "com.example"));
  493. EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/misc_de/0/rollback/11/com.example",
  494. create_data_misc_de_rollback_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, 11,
  495. "com.example"));
  496. }
  497. TEST_F(UtilsTest, TestCreateDirIfNeeded) {
  498. system("mkdir -p /data/local/tmp/user/0");
  499. auto deleter = [&]() {
  500. delete_dir_contents_and_dir("/data/local/tmp/user/0", true /* ignore_if_missing */);
  501. };
  502. auto scope_guard = android::base::make_scope_guard(deleter);
  503. // Create folder and check it's permissions.
  504. ASSERT_EQ(0, create_dir_if_needed("/data/local/tmp/user/0/foo", 0700));
  505. struct stat st;
  506. ASSERT_EQ(0, stat("/data/local/tmp/user/0/foo", &st));
  507. ASSERT_EQ(0700, st.st_mode & ALLPERMS);
  508. // Check that create_dir_if_needed is no-op if folder already exists with
  509. // correct permissions.
  510. ASSERT_EQ(0, create_dir_if_needed("/data/local/tmp/user/0/foo", 0700));
  511. // Check -1 is returned if folder exists but with different permissions.
  512. ASSERT_EQ(-1, create_dir_if_needed("/data/local/tmp/user/0/foo", 0750));
  513. // Check that call fails if parent doesn't exist.
  514. ASSERT_NE(0, create_dir_if_needed("/data/local/tmp/user/0/bar/baz", 0700));
  515. }
  516. } // namespace installd
  517. } // namespace android