AccessTokenTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. include "../src/RtcTokenBuilder.php";
  3. include "../src/RtmTokenBuilder.php";
  4. include "TestTool.php";
  5. $appID = "970CA35de60c44645bbae8a215061b33";
  6. $appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
  7. $channelName = "7d72365eb983485397e3e3f9d460bdda";
  8. $ts = 1111111;
  9. $salt = 1;
  10. $uid = "2882341273";
  11. $expiredTs = 1446455471;
  12. $expected = "006970CA35de60c44645bbae8a215061b33IACV0fZUBw+72cVoL9eyGGh3Q6Poi8bgjwVLnyKSJyOXR7dIfRBXoFHlEAABAAAAR/QQAAEAAQCvKDdW";
  13. $builder = AccessToken::init($appID, $appCertificate, $channelName, $uid);
  14. $builder->message->salt = $salt;
  15. $builder->message->ts = $ts;
  16. $builder->addPrivilege(AccessToken::Privileges["kJoinChannel"], $expiredTs);
  17. $result = $builder->build();
  18. assertEqual($expected, $result);
  19. $builder2 = AccessToken::initWithToken($expected, $appCertificate, $channelName, $uid);
  20. $result2 = $builder2->build();
  21. assertEqual($expected, $result2);
  22. //test 2 uid 0 case
  23. $appID = "970CA35de60c44645bbae8a215061b33";
  24. $appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
  25. $channelName = "7d72365eb983485397e3e3f9d460bdda";
  26. $ts = 1111111;
  27. $salt = 1;
  28. $uid = "0";
  29. $expiredTs = 1446455471;
  30. $expected = "006970CA35de60c44645bbae8a215061b33IABNRUO/126HmzFc+J8lQFfnkssUdUXqiePeE2WNZ7lyubdIfRAh39v0EAABAAAAR/QQAAEAAQCvKDdW";
  31. $builder = AccessToken::init($appID, $appCertificate, $channelName, $uid);
  32. $builder->message->salt = $salt;
  33. $builder->message->ts = $ts;
  34. $builder->addPrivilege(AccessToken::Privileges["kJoinChannel"], $expiredTs);
  35. $result = $builder->build();
  36. assertEqual($expected, $result);
  37. $builder2 = AccessToken::initWithToken($expected, $appCertificate, $channelName, $uid);
  38. $result2 = $builder2->build();
  39. assertEqual($expected, $result2);
  40. //test 2 uid 0 number case
  41. $appID = "970CA35de60c44645bbae8a215061b33";
  42. $appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
  43. $channelName = "7d72365eb983485397e3e3f9d460bdda";
  44. $ts = 1111111;
  45. $salt = 1;
  46. $uid = 0;
  47. $expiredTs = 1446455471;
  48. $expected = "006970CA35de60c44645bbae8a215061b33IACw1o7htY6ISdNRtku3p9tjTPi0jCKf9t49UHJhzCmL6bdIfRAAAAAAEAABAAAAR/QQAAEAAQCvKDdW";
  49. $builder = AccessToken::init($appID, $appCertificate, $channelName, $uid);
  50. $builder->message->salt = $salt;
  51. $builder->message->ts = $ts;
  52. $builder->addPrivilege(AccessToken::Privileges["kJoinChannel"], $expiredTs);
  53. $result = $builder->build();
  54. assertEqual($expected, $result);
  55. $builder2 = AccessToken::initWithToken($expected, $appCertificate, $channelName, $uid);
  56. $result2 = $builder2->build();
  57. assertEqual($expected, $result2);
  58. $appID = "3d76392019ca47599548a67ad27c9699";
  59. $appCertificate = "386668c56db0499791d32264810c2a29";
  60. $channelName = "7d72365eb983485397e3e3f9d460bdda";
  61. $uid = 2882341273;
  62. $token = RtcTokenBuilder::buildTokenWithUid($appID, $appCertificate, $channelName, $uid, RtcTokenBuilder::RoleAttendee, $expiredTs);
  63. $parser = AccessToken::initWithToken($token, $appCertificate, $channelName, $uid);
  64. $privilegeKey = AccessToken::Privileges["kJoinChannel"];
  65. assertEqual($parser->message->privileges[$privilegeKey], $expiredTs);
  66. $userAccount = "test_user";
  67. $token = RtmTokenBuilder::buildToken($appID, $appCertificate, $userAccount, RtmTokenBuilder::RoleRtmUser, $expiredTs);
  68. $parser = AccessToken::initWithToken($token, $appCertificate, $channelName, $userAccount);
  69. $privilegeKey = AccessToken::Privileges["kRtmLogin"];
  70. assertEqual($parser->message->privileges[$privilegeKey], $expiredTs);
  71. $appID = "";
  72. //invalid values
  73. $expected = "006970CA35de60c44645bbae8a215061b33IACw1o7htY6ISdNRtku3p9tjTPi0jCKf9t49UHJhzCmL6bdIfRAAAAAAEAABAAAAR/QQAAEAAQCvKDdW";
  74. $builder = AccessToken::init($appID, $appCertificate, $channelName, $uid);
  75. assertEqual($builder, NULL);
  76. ?>