AccessToken2Test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. require_once "../src/AccessToken2.php";
  3. class AccessToken2Test
  4. {
  5. public $appId = "970CA35de60c44645bbae8a215061b33";
  6. public $appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
  7. public $channelName = "7d72365eb983485397e3e3f9d460bdda";
  8. public $expire = 600;
  9. public $issueTs = 1111111;
  10. public $salt = 1;
  11. public $uid = 2882341273;
  12. public $uidStr = "2882341273";
  13. public $userId = "test_user";
  14. public $chatUserId = "2882341273";
  15. public function run()
  16. {
  17. $this->test_build_ServiceRtc();
  18. $this->test_build_ServiceRtc_uid_0();
  19. $this->test_build_ServiceRtc_account();
  20. $this->test_build_ServiceChat_user();
  21. $this->test_build_ServiceChat_app();
  22. $this->test_build_multi_service();
  23. $this->test_parse_TokenRtc();
  24. $this->test_parse_TokenRtc_Rtm_Chat_MultiService();
  25. $this->test_parse_TokenRtm();
  26. $this->test_parse_TokenChat_user();
  27. $this->test_parse_TokenChat_app();
  28. }
  29. public function test_build_ServiceRtc()
  30. {
  31. $accessToken = new AccessToken2($this->appId, $this->appCertificate, $this->expire);
  32. $accessToken->issueTs = $this->issueTs;
  33. $accessToken->salt = $this->salt;
  34. $serviceRtc = new ServiceRtc($this->channelName, $this->uid);
  35. $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_JOIN_CHANNEL, $this->expire);
  36. $accessToken->addService($serviceRtc);
  37. $token = $accessToken->build();
  38. Util::assertEqual("007eJxTYBBbsMMnKq7p9Hf/HcIX5kce9b518kCiQgSr5Zrp4X1Tu6UUGCzNDZwdjU1TUs0Mkk1MzExMk5ISUy0SjQxNDcwMk4yN3b8IMEQwMTAwMoAwBIL4CgzmKeZGxmamqUmWFsYmFqbGluapxqnGaZYpJmYGSSkpiVwMRhYWRsYmhkbmxgDCaiTj", $token);
  39. }
  40. public function test_build_ServiceRtc_uid_0()
  41. {
  42. $accessToken = new AccessToken2($this->appId, $this->appCertificate, $this->expire);
  43. $accessToken->issueTs = $this->issueTs;
  44. $accessToken->salt = $this->salt;
  45. $serviceRtc = new ServiceRtc($this->channelName, "");
  46. $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_JOIN_CHANNEL, $this->expire);
  47. $accessToken->addService($serviceRtc);
  48. $token = $accessToken->build();
  49. Util::assertEqual("007eJxTYLhzZP08Lxa1Pg57+TcXb/3cZ3wi4V6kbpbOog0G2dOYk20UGCzNDZwdjU1TUs0Mkk1MzExMk5ISUy0SjQxNDcwMk4yN3b8IMEQwMTAwMoAwBIL4CgzmKeZGxmamqUmWFsYmFqbGluapxqnGaZYpJmYGSSkpiQwMADacImo=", $token);
  50. }
  51. public function test_build_ServiceRtc_account()
  52. {
  53. $accessToken = new AccessToken2($this->appId, $this->appCertificate, $this->expire);
  54. $accessToken->issueTs = $this->issueTs;
  55. $accessToken->salt = $this->salt;
  56. $serviceRtc = new ServiceRtc($this->channelName, $this->uidStr);
  57. $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_JOIN_CHANNEL, $this->expire);
  58. $accessToken->addService($serviceRtc);
  59. $token = $accessToken->build();
  60. Util::assertEqual("007eJxTYBBbsMMnKq7p9Hf/HcIX5kce9b518kCiQgSr5Zrp4X1Tu6UUGCzNDZwdjU1TUs0Mkk1MzExMk5ISUy0SjQxNDcwMk4yN3b8IMEQwMTAwMoAwBIL4CgzmKeZGxmamqUmWFsYmFqbGluapxqnGaZYpJmYGSSkpiVwMRhYWRsYmhkbmxgDCaiTj", $token);
  61. }
  62. public function test_build_ServiceChat_user() {
  63. $accessToken = new AccessToken2($this->appId, $this->appCertificate, $this->expire);
  64. $accessToken->issueTs = $this->issueTs;
  65. $accessToken->salt = $this->salt;
  66. $serviceChat = new ServiceChat($this->chatUserId);
  67. $serviceChat->addPrivilege($serviceChat::PRIVILEGE_USER, $this->expire);
  68. $accessToken->addService($serviceChat);
  69. $token = $accessToken->build();
  70. Util::assertEqual("007eJxTYNAIsnbS3v/A5t2TC6feR15r+6cq8bqAvfaW+tk/Vzz+p6xTYLA0N3B2NDZNSTUzSDYxMTMxTUpKTLVINDI0NTAzTDI2dv8iwBDBxMDAyADCrEDMCOZzMRhZWBgZmxgamRsDAB+lHrg=", $token);
  71. }
  72. public function test_build_ServiceChat_app() {
  73. $accessToken = new AccessToken2($this->appId, $this->appCertificate, $this->expire);
  74. $accessToken->issueTs = $this->issueTs;
  75. $accessToken->salt = $this->salt;
  76. $serviceChat = new ServiceChat();
  77. $serviceChat->addPrivilege($serviceChat::PRIVILEGE_APP, $this->expire);
  78. $accessToken->addService($serviceChat);
  79. $token = $accessToken->build();
  80. Util::assertEqual("007eJxTYNDNaz3snC8huEfHWdz6s98qltq4zqy9fl99Uh0FDvy6F6DAYGlu4OxobJqSamaQbGJiZmKalJSYapFoZGhqYGaYZGzs/kWAIYKJgYGRAYRZgZgJzGdgAACt8hhr", $token);
  81. }
  82. public function test_build_multi_service()
  83. {
  84. $accessToken = new AccessToken2($this->appId, $this->appCertificate, $this->expire);
  85. $accessToken->issueTs = $this->issueTs;
  86. $accessToken->salt = $this->salt;
  87. $serviceRtc = new ServiceRtc($this->channelName, $this->uidStr);
  88. $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_JOIN_CHANNEL, $this->expire);
  89. $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_PUBLISH_AUDIO_STREAM, $this->expire);
  90. $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_PUBLISH_VIDEO_STREAM, $this->expire);
  91. $serviceRtc->addPrivilege($serviceRtc::PRIVILEGE_PUBLISH_DATA_STREAM, $this->expire);
  92. $accessToken->addService($serviceRtc);
  93. $serviceRtm = new ServiceRtm($this->userId);
  94. $serviceRtm->addPrivilege($serviceRtm::PRIVILEGE_LOGIN, $this->expire);
  95. $accessToken->addService($serviceRtm);
  96. $serviceChat = new ServiceChat($this->chatUserId);
  97. $serviceChat->addPrivilege($serviceChat::PRIVILEGE_USER, $this->expire);
  98. $accessToken->addService($serviceChat);
  99. $token = $accessToken->build();
  100. Util::assertEqual("007eJxTYPg19dsX8xO2Nys/bpSeoH/0j9CvSs1JWib9291PKC53l85UYLA0N3B2NDZNSTUzSDYxMTMxTUpKTLVINDI0NTAzTDI2dv8iwBDBxMDAyMDAwAwkWYAYxGcCk8xgkgVMKjCYp5gbGZuZpiZZWhibWJgaW5qnGqcap1mmmJgZJKWkJHIxGFlYGBmbGBqZGzMBzYGYxMlQklpcEl9anFrEChdEVgoAw6ct/Q==", $token);
  101. }
  102. public function test_parse_TokenRtc()
  103. {
  104. $accessToken = new AccessToken2();
  105. $res = $accessToken->parse("007eJxTYBBbsMMnKq7p9Hf/HcIX5kce9b518kCiQgSr5Zrp4X1Tu6UUGCzNDZwdjU1TUs0Mkk1MzExMk5ISUy0SjQxNDcwMk4yN3b8IMEQwMTAwMoAwBIL4CgzmKeZGxmamqUmWFsYmFqbGluapxqnGaZYpJmYGSSkpiVwMRhYWRsYmhkbmxgDCaiTj");
  106. Util::assertEqual(true, $res);
  107. Util::assertEqual($this->appId, $accessToken->appId);
  108. Util::assertEqual($this->expire, $accessToken->expire);
  109. Util::assertEqual($this->issueTs, $accessToken->issueTs);
  110. Util::assertEqual($this->salt, $accessToken->salt);
  111. Util::assertEqual(1, count($accessToken->services));
  112. Util::assertEqual($this->channelName, $accessToken->services[ServiceRtc::SERVICE_TYPE]->channelName);
  113. Util::assertEqual($this->uidStr, $accessToken->services[ServiceRtc::SERVICE_TYPE]->uid);
  114. Util::assertEqual(ServiceRtc::SERVICE_TYPE, $accessToken->services[ServiceRtc::SERVICE_TYPE]->type);
  115. Util::assertEqual($this->expire, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_JOIN_CHANNEL]);
  116. Util::assertEqual(0, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_PUBLISH_AUDIO_STREAM]);
  117. Util::assertEqual(0, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_PUBLISH_VIDEO_STREAM]);
  118. Util::assertEqual(0, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_PUBLISH_DATA_STREAM]);
  119. }
  120. public function test_parse_TokenRtc_Rtm_Chat_MultiService()
  121. {
  122. $accessToken = new AccessToken2();
  123. $res = $accessToken->parse("007eJxTYPg19dsX8xO2Nys/bpSeoH/0j9CvSs1JWib9291PKC53l85UYLA0N3B2NDZNSTUzSDYxMTMxTUpKTLVINDI0NTAzTDI2dv8iwBDBxMDAyMDAwAwkWYAYxGcCk8xgkgVMKjCYp5gbGZuZpiZZWhibWJgaW5qnGqcap1mmmJgZJKWkJHIxGFlYGBmbGBqZGzMBzYGYxMlQklpcEl9anFrEChdEVgoAw6ct/Q==");
  124. Util::assertEqual(true, $res);
  125. Util::assertEqual($this->appId, $accessToken->appId);
  126. Util::assertEqual($this->expire, $accessToken->expire);
  127. Util::assertEqual($this->issueTs, $accessToken->issueTs);
  128. Util::assertEqual($this->salt, $accessToken->salt);
  129. Util::assertEqual(3, count($accessToken->services));
  130. Util::assertEqual($this->channelName, $accessToken->services[ServiceRtc::SERVICE_TYPE]->channelName);
  131. Util::assertEqual($this->uidStr, $accessToken->services[ServiceRtc::SERVICE_TYPE]->uid);
  132. Util::assertEqual(ServiceRtc::SERVICE_TYPE, $accessToken->services[ServiceRtc::SERVICE_TYPE]->type);
  133. Util::assertEqual($this->userId, $accessToken->services[ServiceRtm::SERVICE_TYPE]->userId);
  134. Util::assertEqual($this->expire, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_JOIN_CHANNEL]);
  135. Util::assertEqual($this->expire, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_PUBLISH_AUDIO_STREAM]);
  136. Util::assertEqual($this->expire, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_PUBLISH_VIDEO_STREAM]);
  137. Util::assertEqual($this->expire, $accessToken->services[ServiceRtc::SERVICE_TYPE]->privileges[ServiceRtc::PRIVILEGE_PUBLISH_DATA_STREAM]);
  138. Util::assertEqual($this->expire, $accessToken->services[ServiceRtm::SERVICE_TYPE]->privileges[ServiceRtm::PRIVILEGE_LOGIN]);
  139. // CHAT
  140. Util::assertEqual(ServiceChat::SERVICE_TYPE, $accessToken->services[ServiceChat::SERVICE_TYPE]->type);
  141. Util::assertEqual($this->chatUserId, $accessToken->services[ServiceChat::SERVICE_TYPE]->userId);
  142. Util::assertEqual($this->expire, $accessToken->services[ServiceChat::SERVICE_TYPE]->privileges[ServiceChat::PRIVILEGE_USER]);
  143. }
  144. public function test_parse_TokenRtm()
  145. {
  146. $accessToken = new AccessToken2();
  147. $res = $accessToken->parse("007eJxSYOCdJftjyTM2zxW6Xhm/5T0j5LdcUt/xYVt48fb5Mp3PX9coMFiaGzg7GpumpJoZJJuYmJmYJiUlplokGhmaGpgZJhkbu38RYIhgYmBgZABhJgZGBkYwn5OhJLW4JL60OLUIEAAA//9ZVh6A");
  148. Util::assertEqual(true, $res);
  149. Util::assertEqual($this->appId, $accessToken->appId);
  150. Util::assertEqual($this->expire, $accessToken->expire);
  151. Util::assertEqual($this->issueTs, $accessToken->issueTs);
  152. Util::assertEqual($this->salt, $accessToken->salt);
  153. Util::assertEqual(1, count($accessToken->services));
  154. Util::assertEqual(ServiceRtm::SERVICE_TYPE, $accessToken->services[ServiceRtm::SERVICE_TYPE]->type);
  155. Util::assertEqual($this->expire, $accessToken->services[ServiceRtm::SERVICE_TYPE]->privileges[ServiceRtm::PRIVILEGE_LOGIN]);
  156. }
  157. public function test_parse_TokenChat_user()
  158. {
  159. $accessToken = new AccessToken2();
  160. $res = $accessToken->parse("007eJxTYNAIsnbS3v/A5t2TC6feR15r+6cq8bqAvfaW+tk/Vzz+p6xTYLA0N3B2NDZNSTUzSDYxMTMxTUpKTLVINDI0NTAzTDI2dv8iwBDBxMDAyADCrEDMCOZzMRhZWBgZmxgamRsDAB+lHrg=");
  161. Util::assertEqual(true, $res);
  162. Util::assertEqual($this->appId, $accessToken->appId);
  163. Util::assertEqual($this->expire, $accessToken->expire);
  164. Util::assertEqual($this->issueTs, $accessToken->issueTs);
  165. Util::assertEqual($this->salt, $accessToken->salt);
  166. Util::assertEqual(1, count($accessToken->services));
  167. Util::assertEqual(ServiceChat::SERVICE_TYPE, $accessToken->services[ServiceChat::SERVICE_TYPE]->type);
  168. Util::assertEqual($this->chatUserId, $accessToken->services[ServiceChat::SERVICE_TYPE]->userId);
  169. Util::assertEqual($this->expire, $accessToken->services[ServiceChat::SERVICE_TYPE]->privileges[ServiceChat::PRIVILEGE_USER]);
  170. }
  171. public function test_parse_TokenChat_app()
  172. {
  173. $accessToken = new AccessToken2();
  174. $res = $accessToken->parse("007eJxTYNDNaz3snC8huEfHWdz6s98qltq4zqy9fl99Uh0FDvy6F6DAYGlu4OxobJqSamaQbGJiZmKalJSYapFoZGhqYGaYZGzs/kWAIYKJgYGRAYRZgZgJzGdgAACt8hhr");
  175. Util::assertEqual(true, $res);
  176. Util::assertEqual($this->appId, $accessToken->appId);
  177. Util::assertEqual($this->expire, $accessToken->expire);
  178. Util::assertEqual($this->issueTs, $accessToken->issueTs);
  179. Util::assertEqual($this->salt, $accessToken->salt);
  180. Util::assertEqual(1, count($accessToken->services));
  181. Util::assertEqual(ServiceChat::SERVICE_TYPE, $accessToken->services[ServiceChat::SERVICE_TYPE]->type);
  182. Util::assertEqual($this->expire, $accessToken->services[ServiceChat::SERVICE_TYPE]->privileges[ServiceChat::PRIVILEGE_APP]);
  183. }
  184. }
  185. $accessToken2Test = new AccessToken2Test();
  186. $accessToken2Test->run();