Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Amco\Security\Akamai;
- /**
- * @author Jorge Matricali <jorgematricali@amco.mx>
- *
- * @group PLAYER
- * @group Security
- */
- class TokenAuthenticationTest extends \PHPUnit_Framework_TestCase
- {
- public function testGeneration()
- {
- $generator = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $token = $generator->generateToken();
- $this->assertNotEmpty($token);
- }
- /**
- * @expectedException Amco\Security\Akamai\Exceptions\ParameterException
- */
- public function testInvalidAlgorithm()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setAlgorithm('inVaLid');
- }
- public function testValidIPv4()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('', $auth->getIp());
- $this->assertEquals('', $auth->getIpField());
- // Valid IPv4
- $auth->setIp('127.0.0.1');
- $this->assertEquals('127.0.0.1', $auth->getIp());
- $this->assertEquals('ip=127.0.0.1'.$auth->getFieldDelimiter(), $auth->getIpField());
- }
- /**
- * @expectedException Amco\Security\Akamai\Exceptions\ParameterException
- */
- public function testInvalidIPv4()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setIp('127.0.0.300');
- }
- public function testValidIPv6()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('', $auth->getIp());
- $this->assertEquals('', $auth->getIpField());
- // Valid IPv4
- $auth->setIp('2001:0db8:85a3:08d3:1319:8a2e:0370:7334');
- $this->assertEquals('2001:0db8:85a3:08d3:1319:8a2e:0370:7334', $auth->getIp());
- $this->assertEquals('ip=2001:0db8:85a3:08d3:1319:8a2e:0370:7334'.$auth->getFieldDelimiter(), $auth->getIpField());
- }
- /**
- * @expectedException Amco\Security\Akamai\Exceptions\ParameterException
- */
- public function testInvalidIPv6()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setIp('2001:0db8:85a3:08d3:xxxx:8a2e:0370:7334');
- }
- public function testStartTime()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $gstv = new \ReflectionMethod('Amco\Security\Akamai\TokenAuthentication', 'getStartTimeValue');
- $gstv->setAccessible(true);
- $this->assertEquals(0, $auth->getStartTime());
- $this->assertEquals(time(), $gstv->invoke($auth));
- $this->assertEquals('', $auth->getStartTimeField());
- $auth->setStartTime('now');
- $this->assertEquals(time(), $auth->getStartTime());
- $this->assertEquals(time(), $gstv->invoke($auth));
- $this->assertEquals('st='.time().$auth->getFieldDelimiter(), $auth->getStartTimeField());
- $auth->setStartTime(12345);
- $this->assertEquals(12345, $auth->getStartTime());
- $this->assertEquals(12345, $gstv->invoke($auth));
- $this->assertEquals('st='. 12345 .$auth->getFieldDelimiter(), $auth->getStartTimeField());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- try {
- $auth->setStartTime('');
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- $this->assertEquals(0, $auth->getStartTime());
- $this->assertEquals(time(), $gstv->invoke($auth));
- $this->assertEquals('', $auth->getStartTimeField());
- }
- public function testWindow()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- // Default window time
- $this->assertEquals(300, $auth->getWindow());
- $auth->setWindow(500);
- $this->assertEquals(500, $auth->getWindow());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- try {
- $auth->setWindow('abc');
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- $this->assertEquals(300, $auth->getWindow());
- try {
- $auth->setWindow(0);
- } catch (\Exception $e2) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
- $this->assertEquals(300, $auth->getWindow());
- }
- public function testAcl()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('', $auth->getAcl());
- $this->assertEquals('acl=/*'.$auth->getFieldDelimiter(), $auth->getAclField());
- $auth->setAcl('test');
- $this->assertEquals('test', $auth->getAcl());
- $this->assertEquals('acl=test'.$auth->getFieldDelimiter(), $auth->getAclField());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setUrl('https://example.com/protected/resource');
- $this->assertEquals('', $auth->getAclField()); // If we have an URL we shouldn't have an ACL
- try {
- $auth->setAcl('test');
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- }
- public function testUrl()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('', $auth->getUrl());
- $this->assertEquals('', $auth->getUrlField());
- $auth->setUrl('https://example.com/protected/resource');
- $this->assertEquals('https://example.com/protected/resource', $auth->getUrl());
- $this->assertEquals('url=https://example.com/protected/resource'.$auth->getFieldDelimiter(), $auth->getUrlField());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setAcl('test');
- $this->assertEquals('', $auth->getUrlField()); // If we have an URL we shouldn't have an ACL
- try {
- $auth->setUrl('https://example.com/');
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- }
- public function testSession()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('', $auth->getSessionId());
- $this->assertEquals('', $auth->getSessionIdField());
- $auth->setSessionId('e10adc3949ba59abbe56e057f20f883e');
- $this->assertEquals('e10adc3949ba59abbe56e057f20f883e', $auth->getSessionId());
- $this->assertEquals('id=e10adc3949ba59abbe56e057f20f883e'.$auth->getFieldDelimiter(), $auth->getSessionIdField());
- $auth->setSessionId(123456778);
- $this->assertEquals('123456778', $auth->getSessionId());
- $this->assertEquals('id=123456778'.$auth->getFieldDelimiter(), $auth->getSessionIdField());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- try {
- $auth->setSessionId(array());
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- $this->assertEquals('', $auth->getSessionId());
- $this->assertEquals('', $auth->getSessionIdField());
- try {
- $auth->setSessionId(new \StdClass());
- } catch (\Exception $e2) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
- $this->assertEquals('', $auth->getSessionId());
- $this->assertEquals('', $auth->getSessionIdField());
- }
- public function testData()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('', $auth->getData());
- $this->assertEquals('', $auth->getDataField());
- $auth->setData('e10adc3949ba59abbe56e057f20f883e');
- $this->assertEquals('e10adc3949ba59abbe56e057f20f883e', $auth->getData());
- $this->assertEquals('data=e10adc3949ba59abbe56e057f20f883e'.$auth->getFieldDelimiter(), $auth->getDataField());
- $auth->setData(123456778);
- $this->assertEquals('123456778', $auth->getData());
- $this->assertEquals('data=123456778'.$auth->getFieldDelimiter(), $auth->getDataField());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- try {
- $auth->setData(array());
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- $this->assertEquals('', $auth->getData());
- $this->assertEquals('', $auth->getDataField());
- try {
- $auth->setData(new \StdClass());
- } catch (\Exception $e2) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
- $this->assertEquals('', $auth->getData());
- $this->assertEquals('', $auth->getDataField());
- }
- public function testSalt()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('', $auth->getSalt());
- $this->assertEquals('', $auth->getSaltField());
- $auth->setSalt('s4lt');
- $this->assertEquals('s4lt', $auth->getSalt());
- $this->assertEquals('salt=s4lt'.$auth->getFieldDelimiter(), $auth->getSaltField());
- $auth->setSalt(123456778);
- $this->assertEquals('123456778', $auth->getSalt());
- $this->assertEquals('salt=123456778'.$auth->getFieldDelimiter(), $auth->getSaltField());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- try {
- $auth->setSalt(array());
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- $this->assertEquals('', $auth->getSalt());
- $this->assertEquals('', $auth->getSaltField());
- try {
- $auth->setSalt(new \StdClass());
- } catch (\Exception $e2) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
- $this->assertEquals('', $auth->getSalt());
- $this->assertEquals('', $auth->getSaltField());
- }
- public function testKey()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('aabbccddeeff00112233445566778899', $auth->getKey());
- $auth->setKey('abcd1234');
- $this->assertEquals('abcd1234', $auth->getKey());
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- try {
- $auth->setKey('zxvft');
- } catch (\Exception $e1) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e1);
- $this->assertEquals('aabbccddeeff00112233445566778899', $auth->getKey());
- try {
- $auth->setKey('abcd1234f'); // Must be an even number of characters
- } catch (\Exception $e2) {
- }
- $this->assertInstanceOf('Amco\Security\Akamai\Exceptions\ParameterException', $e2);
- $this->assertEquals('aabbccddeeff00112233445566778899', $auth->getKey());
- }
- public function testFieldDelimiter()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertEquals('~', $auth->getFieldDelimiter());
- $auth->setFieldDelimiter('|');
- $this->assertEquals('|', $auth->getFieldDelimiter());
- }
- public function testEarlyUrlEncoding()
- {
- $encode = new \ReflectionMethod('Amco\Security\Akamai\TokenAuthentication', 'encode');
- $encode->setAccessible(true);
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $this->assertFalse($auth->getEarlyUrlEncoding());
- $this->assertEquals('test ', $encode->invoke($auth, 'test '));
- $auth->setEarlyUrlEncoding(true);
- $this->assertTrue($auth->getEarlyUrlEncoding());
- $this->assertEquals('test%20', $encode->invoke($auth, 'test '));
- }
- public function testSha256()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA256);
- $token = $auth->generateToken();
- $this->assertNotEmpty($token);
- }
- public function testSha1()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA1);
- $token = $auth->generateToken();
- $this->assertNotEmpty($token);
- }
- public function testMd5()
- {
- $auth = new TokenAuthentication('aabbccddeeff00112233445566778899');
- $auth->setAlgorithm(TokenAuthentication::ALGORITHM_MD5);
- $token = $auth->generateToken();
- $this->assertNotEmpty($token);
- }
- public function testLegacySha256()
- {
- $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
- $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA256);
- $auth->setFieldDelimiter('&');
- $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
- $token = $auth->generateCustomToken();
- $this->assertNotEmpty($token);
- }
- public function testLegacySha1()
- {
- $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
- $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA1);
- $auth->setFieldDelimiter('&');
- $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
- $token = $auth->generateCustomToken();
- $this->assertNotEmpty($token);
- }
- public function testLegacyMd5()
- {
- $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
- $auth->setAlgorithm(TokenAuthentication::ALGORITHM_MD5);
- $auth->setFieldDelimiter('&');
- $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
- $token = $auth->generateCustomToken();
- $this->assertNotEmpty($token);
- }
- public function testLegacySha1IP()
- {
- $auth = new TokenAuthentication('ae834f5dde69cd965f674606be7cd683');
- $auth->setAlgorithm(TokenAuthentication::ALGORITHM_SHA1);
- $auth->setFieldDelimiter('&');
- $auth->setIp('200.80.149.68');
- $auth->setUrl('/multimediav81/plataforma_vod/MP4/201301/WMP4H00525MTDS_full/WMP4H00525MTDS_full_HLS.ism/WMP4H00525MTDS_full_HLS-audio=128000-video=2524000.m3u8');
- $token = $auth->generateCustomToken();
- $this->assertNotEmpty($token);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement