multiuser_test.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2016 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 <cutils/multiuser.h>
  17. #include <gtest/gtest.h>
  18. static constexpr auto ERR_GID = static_cast<gid_t>(-1);
  19. TEST(MultiuserTest, TestMerge) {
  20. EXPECT_EQ(0U, multiuser_get_uid(0, 0));
  21. EXPECT_EQ(1000U, multiuser_get_uid(0, 1000));
  22. EXPECT_EQ(10000U, multiuser_get_uid(0, 10000));
  23. EXPECT_EQ(50000U, multiuser_get_uid(0, 50000));
  24. EXPECT_EQ(1000000U, multiuser_get_uid(10, 0));
  25. EXPECT_EQ(1001000U, multiuser_get_uid(10, 1000));
  26. EXPECT_EQ(1010000U, multiuser_get_uid(10, 10000));
  27. EXPECT_EQ(1050000U, multiuser_get_uid(10, 50000));
  28. }
  29. TEST(MultiuserTest, TestSplitUser) {
  30. EXPECT_EQ(0U, multiuser_get_user_id(0));
  31. EXPECT_EQ(0U, multiuser_get_user_id(1000));
  32. EXPECT_EQ(0U, multiuser_get_user_id(10000));
  33. EXPECT_EQ(0U, multiuser_get_user_id(50000));
  34. EXPECT_EQ(10U, multiuser_get_user_id(1000000));
  35. EXPECT_EQ(10U, multiuser_get_user_id(1001000));
  36. EXPECT_EQ(10U, multiuser_get_user_id(1010000));
  37. EXPECT_EQ(10U, multiuser_get_user_id(1050000));
  38. }
  39. TEST(MultiuserTest, TestSplitApp) {
  40. EXPECT_EQ(0U, multiuser_get_app_id(0));
  41. EXPECT_EQ(1000U, multiuser_get_app_id(1000));
  42. EXPECT_EQ(10000U, multiuser_get_app_id(10000));
  43. EXPECT_EQ(50000U, multiuser_get_app_id(50000));
  44. EXPECT_EQ(0U, multiuser_get_app_id(1000000));
  45. EXPECT_EQ(1000U, multiuser_get_app_id(1001000));
  46. EXPECT_EQ(10000U, multiuser_get_app_id(1010000));
  47. EXPECT_EQ(50000U, multiuser_get_app_id(1050000));
  48. }
  49. TEST(MultiuserTest, TestCache) {
  50. EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 0));
  51. EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 1000));
  52. EXPECT_EQ(20000U, multiuser_get_cache_gid(0, 10000));
  53. EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(0, 50000));
  54. EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 0));
  55. EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 1000));
  56. EXPECT_EQ(1020000U, multiuser_get_cache_gid(10, 10000));
  57. EXPECT_EQ(ERR_GID, multiuser_get_cache_gid(10, 50000));
  58. }
  59. TEST(MultiuserTest, TestExt) {
  60. EXPECT_EQ(ERR_GID, multiuser_get_ext_gid(0, 0));
  61. EXPECT_EQ(ERR_GID, multiuser_get_ext_gid(0, 1000));
  62. EXPECT_EQ(30000U, multiuser_get_ext_gid(0, 10000));
  63. EXPECT_EQ(ERR_GID, multiuser_get_ext_gid(0, 50000));
  64. EXPECT_EQ(1030000U, multiuser_get_ext_gid(10, 10000));
  65. }
  66. TEST(MultiuserTest, TestExtCache) {
  67. EXPECT_EQ(ERR_GID, multiuser_get_ext_cache_gid(0, 0));
  68. EXPECT_EQ(ERR_GID, multiuser_get_ext_cache_gid(0, 1000));
  69. EXPECT_EQ(40000U, multiuser_get_ext_cache_gid(0, 10000));
  70. EXPECT_EQ(ERR_GID, multiuser_get_ext_cache_gid(0, 50000));
  71. EXPECT_EQ(1040000U, multiuser_get_ext_cache_gid(10, 10000));
  72. }
  73. TEST(MultiuserTest, TestShared) {
  74. EXPECT_EQ(0U, multiuser_get_shared_gid(0, 0));
  75. EXPECT_EQ(1000U, multiuser_get_shared_gid(0, 1000));
  76. EXPECT_EQ(50000U, multiuser_get_shared_gid(0, 10000));
  77. EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(0, 50000));
  78. EXPECT_EQ(0U, multiuser_get_shared_gid(10, 0));
  79. EXPECT_EQ(1000U, multiuser_get_shared_gid(10, 1000));
  80. EXPECT_EQ(50000U, multiuser_get_shared_gid(10, 10000));
  81. EXPECT_EQ(ERR_GID, multiuser_get_shared_gid(10, 50000));
  82. }