DrmSessionManager_test.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2015 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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "DrmSessionManager_test"
  18. #include <utils/Log.h>
  19. #include <gtest/gtest.h>
  20. #include <media/stagefright/foundation/ADebug.h>
  21. #include <media/stagefright/ProcessInfoInterface.h>
  22. #include <mediadrm/DrmHal.h>
  23. #include <mediadrm/DrmSessionClientInterface.h>
  24. #include <mediadrm/DrmSessionManager.h>
  25. namespace android {
  26. struct FakeProcessInfo : public ProcessInfoInterface {
  27. FakeProcessInfo() {}
  28. virtual ~FakeProcessInfo() {}
  29. virtual bool getPriority(int pid, int* priority) {
  30. // For testing, use pid as priority.
  31. // Lower the value higher the priority.
  32. *priority = pid;
  33. return true;
  34. }
  35. virtual bool isValidPid(int /* pid */) {
  36. return true;
  37. }
  38. private:
  39. DISALLOW_EVIL_CONSTRUCTORS(FakeProcessInfo);
  40. };
  41. struct FakeDrm : public DrmSessionClientInterface {
  42. FakeDrm() {}
  43. virtual ~FakeDrm() {}
  44. virtual bool reclaimSession(const Vector<uint8_t>& sessionId) {
  45. mReclaimedSessions.push_back(sessionId);
  46. return true;
  47. }
  48. const Vector<Vector<uint8_t> >& reclaimedSessions() const {
  49. return mReclaimedSessions;
  50. }
  51. private:
  52. Vector<Vector<uint8_t> > mReclaimedSessions;
  53. DISALLOW_EVIL_CONSTRUCTORS(FakeDrm);
  54. };
  55. static const int kTestPid1 = 30;
  56. static const int kTestPid2 = 20;
  57. static const uint8_t kTestSessionId1[] = {1, 2, 3};
  58. static const uint8_t kTestSessionId2[] = {4, 5, 6, 7, 8};
  59. static const uint8_t kTestSessionId3[] = {9, 0};
  60. class DrmSessionManagerTest : public ::testing::Test {
  61. public:
  62. DrmSessionManagerTest()
  63. : mDrmSessionManager(new DrmSessionManager(new FakeProcessInfo())),
  64. mTestDrm1(new FakeDrm()),
  65. mTestDrm2(new FakeDrm()) {
  66. GetSessionId(kTestSessionId1, ARRAY_SIZE(kTestSessionId1), &mSessionId1);
  67. GetSessionId(kTestSessionId2, ARRAY_SIZE(kTestSessionId2), &mSessionId2);
  68. GetSessionId(kTestSessionId3, ARRAY_SIZE(kTestSessionId3), &mSessionId3);
  69. }
  70. protected:
  71. static void GetSessionId(const uint8_t* ids, size_t num, Vector<uint8_t>* sessionId) {
  72. for (size_t i = 0; i < num; ++i) {
  73. sessionId->push_back(ids[i]);
  74. }
  75. }
  76. static void ExpectEqSessionInfo(const SessionInfo& info, sp<DrmSessionClientInterface> drm,
  77. const Vector<uint8_t>& sessionId, int64_t timeStamp) {
  78. EXPECT_EQ(drm, info.drm);
  79. EXPECT_TRUE(isEqualSessionId(sessionId, info.sessionId));
  80. EXPECT_EQ(timeStamp, info.timeStamp);
  81. }
  82. void addSession() {
  83. mDrmSessionManager->addSession(kTestPid1, mTestDrm1, mSessionId1);
  84. mDrmSessionManager->addSession(kTestPid2, mTestDrm2, mSessionId2);
  85. mDrmSessionManager->addSession(kTestPid2, mTestDrm2, mSessionId3);
  86. const PidSessionInfosMap& map = sessionMap();
  87. EXPECT_EQ(2u, map.size());
  88. ssize_t index1 = map.indexOfKey(kTestPid1);
  89. ASSERT_GE(index1, 0);
  90. const SessionInfos& infos1 = map[index1];
  91. EXPECT_EQ(1u, infos1.size());
  92. ExpectEqSessionInfo(infos1[0], mTestDrm1, mSessionId1, 0);
  93. ssize_t index2 = map.indexOfKey(kTestPid2);
  94. ASSERT_GE(index2, 0);
  95. const SessionInfos& infos2 = map[index2];
  96. EXPECT_EQ(2u, infos2.size());
  97. ExpectEqSessionInfo(infos2[0], mTestDrm2, mSessionId2, 1);
  98. ExpectEqSessionInfo(infos2[1], mTestDrm2, mSessionId3, 2);
  99. }
  100. const PidSessionInfosMap& sessionMap() {
  101. return mDrmSessionManager->mSessionMap;
  102. }
  103. void testGetLowestPriority() {
  104. int pid;
  105. int priority;
  106. EXPECT_FALSE(mDrmSessionManager->getLowestPriority_l(&pid, &priority));
  107. addSession();
  108. EXPECT_TRUE(mDrmSessionManager->getLowestPriority_l(&pid, &priority));
  109. EXPECT_EQ(kTestPid1, pid);
  110. FakeProcessInfo processInfo;
  111. int priority1;
  112. processInfo.getPriority(kTestPid1, &priority1);
  113. EXPECT_EQ(priority1, priority);
  114. }
  115. void testGetLeastUsedSession() {
  116. sp<DrmSessionClientInterface> drm;
  117. Vector<uint8_t> sessionId;
  118. EXPECT_FALSE(mDrmSessionManager->getLeastUsedSession_l(kTestPid1, &drm, &sessionId));
  119. addSession();
  120. EXPECT_TRUE(mDrmSessionManager->getLeastUsedSession_l(kTestPid1, &drm, &sessionId));
  121. EXPECT_EQ(mTestDrm1, drm);
  122. EXPECT_TRUE(isEqualSessionId(mSessionId1, sessionId));
  123. EXPECT_TRUE(mDrmSessionManager->getLeastUsedSession_l(kTestPid2, &drm, &sessionId));
  124. EXPECT_EQ(mTestDrm2, drm);
  125. EXPECT_TRUE(isEqualSessionId(mSessionId2, sessionId));
  126. // mSessionId2 is no longer the least used session.
  127. mDrmSessionManager->useSession(mSessionId2);
  128. EXPECT_TRUE(mDrmSessionManager->getLeastUsedSession_l(kTestPid2, &drm, &sessionId));
  129. EXPECT_EQ(mTestDrm2, drm);
  130. EXPECT_TRUE(isEqualSessionId(mSessionId3, sessionId));
  131. }
  132. sp<DrmSessionManager> mDrmSessionManager;
  133. sp<FakeDrm> mTestDrm1;
  134. sp<FakeDrm> mTestDrm2;
  135. Vector<uint8_t> mSessionId1;
  136. Vector<uint8_t> mSessionId2;
  137. Vector<uint8_t> mSessionId3;
  138. };
  139. TEST_F(DrmSessionManagerTest, addSession) {
  140. addSession();
  141. }
  142. TEST_F(DrmSessionManagerTest, useSession) {
  143. addSession();
  144. mDrmSessionManager->useSession(mSessionId1);
  145. mDrmSessionManager->useSession(mSessionId3);
  146. const PidSessionInfosMap& map = sessionMap();
  147. const SessionInfos& infos1 = map.valueFor(kTestPid1);
  148. const SessionInfos& infos2 = map.valueFor(kTestPid2);
  149. ExpectEqSessionInfo(infos1[0], mTestDrm1, mSessionId1, 3);
  150. ExpectEqSessionInfo(infos2[1], mTestDrm2, mSessionId3, 4);
  151. }
  152. TEST_F(DrmSessionManagerTest, removeSession) {
  153. addSession();
  154. mDrmSessionManager->removeSession(mSessionId2);
  155. const PidSessionInfosMap& map = sessionMap();
  156. EXPECT_EQ(2u, map.size());
  157. const SessionInfos& infos1 = map.valueFor(kTestPid1);
  158. const SessionInfos& infos2 = map.valueFor(kTestPid2);
  159. EXPECT_EQ(1u, infos1.size());
  160. EXPECT_EQ(1u, infos2.size());
  161. // mSessionId2 has been removed.
  162. ExpectEqSessionInfo(infos2[0], mTestDrm2, mSessionId3, 2);
  163. }
  164. TEST_F(DrmSessionManagerTest, removeDrm) {
  165. addSession();
  166. sp<FakeDrm> drm = new FakeDrm;
  167. const uint8_t ids[] = {123};
  168. Vector<uint8_t> sessionId;
  169. GetSessionId(ids, ARRAY_SIZE(ids), &sessionId);
  170. mDrmSessionManager->addSession(kTestPid2, drm, sessionId);
  171. mDrmSessionManager->removeDrm(mTestDrm2);
  172. const PidSessionInfosMap& map = sessionMap();
  173. const SessionInfos& infos2 = map.valueFor(kTestPid2);
  174. EXPECT_EQ(1u, infos2.size());
  175. // mTestDrm2 has been removed.
  176. ExpectEqSessionInfo(infos2[0], drm, sessionId, 3);
  177. }
  178. TEST_F(DrmSessionManagerTest, reclaimSession) {
  179. EXPECT_FALSE(mDrmSessionManager->reclaimSession(kTestPid1));
  180. addSession();
  181. // calling pid priority is too low
  182. EXPECT_FALSE(mDrmSessionManager->reclaimSession(50));
  183. EXPECT_TRUE(mDrmSessionManager->reclaimSession(10));
  184. EXPECT_EQ(1u, mTestDrm1->reclaimedSessions().size());
  185. EXPECT_TRUE(isEqualSessionId(mSessionId1, mTestDrm1->reclaimedSessions()[0]));
  186. mDrmSessionManager->removeSession(mSessionId1);
  187. // add a session from a higher priority process.
  188. sp<FakeDrm> drm = new FakeDrm;
  189. const uint8_t ids[] = {1, 3, 5};
  190. Vector<uint8_t> sessionId;
  191. GetSessionId(ids, ARRAY_SIZE(ids), &sessionId);
  192. mDrmSessionManager->addSession(15, drm, sessionId);
  193. EXPECT_TRUE(mDrmSessionManager->reclaimSession(18));
  194. EXPECT_EQ(1u, mTestDrm2->reclaimedSessions().size());
  195. // mSessionId2 is reclaimed.
  196. EXPECT_TRUE(isEqualSessionId(mSessionId2, mTestDrm2->reclaimedSessions()[0]));
  197. }
  198. TEST_F(DrmSessionManagerTest, getLowestPriority) {
  199. testGetLowestPriority();
  200. }
  201. TEST_F(DrmSessionManagerTest, getLeastUsedSession_l) {
  202. testGetLeastUsedSession();
  203. }
  204. } // namespace android