Refactor profile dir creation

Extract the preparation of profile directories in its own method.
Add tests to check it works as expected.

Test: installd_dexopt_test
Bug: 30934496
Bug: 69678790

Change-Id: I16b0e086d4933e6cfb233b005b7bb0b6fbf7490c
diff --git a/cmds/installd/tests/installd_dexopt_test.cpp b/cmds/installd/tests/installd_dexopt_test.cpp
index 19b42b5..ebeae96 100644
--- a/cmds/installd/tests/installd_dexopt_test.cpp
+++ b/cmds/installd/tests/installd_dexopt_test.cpp
@@ -144,10 +144,11 @@
     static constexpr uid_t kSystemGid = 1000;
     static constexpr int32_t kOSdkVersion = 25;
     static constexpr int32_t kAppDataFlags = FLAG_STORAGE_CE | FLAG_STORAGE_DE;
-    static constexpr uid_t kTestAppUid = 19999;
-    static constexpr gid_t kTestAppGid = 19999;
-    static constexpr uid_t kTestAppId = kTestAppUid;
     static constexpr int32_t kTestUserId = 0;
+    static constexpr uid_t kTestAppId = 19999;
+
+    const gid_t kTestAppUid = multiuser_get_uid(kTestUserId, kTestAppId);
+    const uid_t kTestAppGid = multiuser_get_shared_gid(kTestUserId, kTestAppId);
 
     InstalldNativeService* service_;
     std::unique_ptr<std::string> volume_uuid_;
@@ -245,7 +246,10 @@
 
     void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag,
             bool should_binder_call_succeed, bool should_dex_be_compiled = true,
-            int uid = kTestAppUid) {
+            int32_t uid = -1) {
+        if (uid == -1) {
+            uid = kTestAppUid;
+        }
         std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_));
         int32_t dexopt_needed = 0;  // does not matter;
         std::unique_ptr<std::string> out_path = nullptr;  // does not matter
@@ -279,7 +283,10 @@
 
     void reconcile_secondary_dex(const std::string& path, int32_t storage_flag,
             bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted,
-            int uid = kTestAppUid, std::string* package_override = nullptr) {
+            int32_t uid = -1, std::string* package_override = nullptr) {
+        if (uid == -1) {
+            uid = kTestAppUid;
+        }
         std::vector<std::string> isas;
         isas.push_back(kRuntimeIsa);
         bool out_secondary_dex_exists = false;
@@ -556,5 +563,19 @@
     ASSERT_EQ(ENOENT, errno);
 }
 
+TEST_F(ProfileTest, ProfileDirOk) {
+    LOG(INFO) << "ProfileDirOk";
+
+    std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
+            kTestUserId, package_name_);
+    std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
+            /*is_secondary_dex*/false);
+    std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
+
+    CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
+    CheckFileAccess(cur_profile_file, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
+    CheckFileAccess(ref_profile_dir, kTestAppGid, kTestAppGid, 0701 | S_IFDIR);
+}
+
 }  // namespace installd
 }  // namespace android