From bec99f419003e3228ccd785362882d18c730b4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thi=C3=A9baud=20Weksteen?= Date: Thu, 2 May 2024 14:31:52 +1000 Subject: [PATCH] Add unit tests Add unit tests for is_app_data_path, is_credential_encrypted_path and extract_pkgname_and_userid. Test: atest --host libselinux_test Bug: 317296680 Change-Id: Ib5f528d8beb62db0c59207ee88b6503d0f5845f3 --- libselinux/src/android/android_unittest.cpp | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/libselinux/src/android/android_unittest.cpp b/libselinux/src/android/android_unittest.cpp index 1eb9056e..28a75247 100644 --- a/libselinux/src/android/android_unittest.cpp +++ b/libselinux/src/android/android_unittest.cpp @@ -172,3 +172,61 @@ TEST(AndroidSeAppTest, ParseOverflow) ret = parse_seinfo(seinfo.c_str(), &info); EXPECT_EQ(ret, -1); } + +TEST(AndroidSELinuxPathTest, IsAppDataPath) +{ + EXPECT_TRUE(is_app_data_path("/data/data")); + EXPECT_TRUE(is_app_data_path("/data/user/0")); + + EXPECT_FALSE(is_app_data_path("/data")); +} + +TEST(AndroidSELinuxPathTest, IsCredentialEncryptedPath) +{ + EXPECT_TRUE(is_credential_encrypted_path("/data/system_ce/0")); + EXPECT_TRUE(is_credential_encrypted_path("/data/system_ce/0/backup")); + EXPECT_TRUE(is_credential_encrypted_path("/data/misc_ce/0")); + EXPECT_TRUE(is_credential_encrypted_path("/data/misc_ce/0/apexdata")); + EXPECT_TRUE(is_credential_encrypted_path("/data/vendor_ce/0")); + EXPECT_TRUE(is_credential_encrypted_path("/data/vendor_ce/0/data")); + + EXPECT_FALSE(is_credential_encrypted_path("/data")); + EXPECT_FALSE(is_credential_encrypted_path("/data/data")); + EXPECT_FALSE(is_credential_encrypted_path("/data/user/0")); +} + +TEST(AndroidSELinuxPathTest, ExtractPkgnameAndUserid) +{ + char *pkgname = NULL; + unsigned int userid; + + EXPECT_EQ(extract_pkgname_and_userid("/data/", &pkgname, &userid), -1); + + char const* path = "/data/user/0/com.android.myapp"; + EXPECT_EQ(extract_pkgname_and_userid(path, &pkgname, &userid), 0); + EXPECT_STREQ("com.android.myapp", pkgname); + EXPECT_EQ(userid, 0); + free(pkgname); + pkgname = NULL; + + path = "/data/user/0/com.android.myapp/som/subdir"; + EXPECT_EQ(extract_pkgname_and_userid(path, &pkgname, &userid), 0); + EXPECT_STREQ("com.android.myapp", pkgname); + EXPECT_EQ(userid, 0); + free(pkgname); + pkgname = NULL; + + path = "/data/data/com.android.myapp2"; + EXPECT_EQ(extract_pkgname_and_userid(path, &pkgname, &userid), 0); + EXPECT_STREQ("com.android.myapp2", pkgname); + EXPECT_EQ(userid, 0); + free(pkgname); + pkgname = NULL; + + path = "/data/misc_de/10/sdksandbox/com.android.myapp3"; + EXPECT_EQ(extract_pkgname_and_userid(path, &pkgname, &userid), 0); + EXPECT_STREQ("com.android.myapp3", pkgname); + EXPECT_EQ(userid, 10); + free(pkgname); + pkgname = NULL; +}