HmacTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
  4. *
  5. * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
  6. */
  7. namespace Lcobucci\JWT\Signer;
  8. /**
  9. * @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
  10. * @since 0.1.0
  11. */
  12. class HmacTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @var Hmac|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $signer;
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp()
  22. {
  23. $this->signer = $this->getMockForAbstractClass(Hmac::class);
  24. $this->signer->expects($this->any())
  25. ->method('getAlgorithmId')
  26. ->willReturn('TEST123');
  27. $this->signer->expects($this->any())
  28. ->method('getAlgorithm')
  29. ->willReturn('sha256');
  30. }
  31. /**
  32. * @test
  33. *
  34. * @uses Lcobucci\JWT\Signer\Key
  35. *
  36. * @covers Lcobucci\JWT\Signer\Hmac::createHash
  37. */
  38. public function createHashMustReturnAHashAccordingWithTheAlgorithm()
  39. {
  40. $hash = hash_hmac('sha256', 'test', '123', true);
  41. $this->assertEquals($hash, $this->signer->createHash('test', new Key('123')));
  42. return $hash;
  43. }
  44. /**
  45. * @test
  46. *
  47. * @depends createHashMustReturnAHashAccordingWithTheAlgorithm
  48. *
  49. * @uses Lcobucci\JWT\Signer\Hmac::createHash
  50. * @uses Lcobucci\JWT\Signer\Key
  51. *
  52. * @covers Lcobucci\JWT\Signer\Hmac::doVerify
  53. */
  54. public function doVerifyShouldReturnTrueWhenExpectedHashWasCreatedWithSameInformation($expected)
  55. {
  56. $this->assertTrue($this->signer->doVerify($expected, 'test', new Key('123')));
  57. }
  58. /**
  59. * @test
  60. *
  61. * @depends createHashMustReturnAHashAccordingWithTheAlgorithm
  62. *
  63. * @uses Lcobucci\JWT\Signer\Hmac::createHash
  64. * @uses Lcobucci\JWT\Signer\Key
  65. *
  66. * @covers Lcobucci\JWT\Signer\Hmac::doVerify
  67. */
  68. public function doVerifyShouldReturnFalseWhenExpectedHashWasNotCreatedWithSameInformation($expected)
  69. {
  70. $this->assertFalse($this->signer->doVerify($expected, 'test', new Key('1234')));
  71. }
  72. /**
  73. * @test
  74. *
  75. * @uses Lcobucci\JWT\Signer\Key
  76. *
  77. * @covers Lcobucci\JWT\Signer\Hmac::doVerify
  78. */
  79. public function doVerifyShouldReturnFalseWhenExpectedHashIsNotString()
  80. {
  81. $this->assertFalse($this->signer->doVerify(false, 'test', new Key('1234')));
  82. }
  83. /**
  84. * @test
  85. *
  86. * @covers Lcobucci\JWT\Signer\Hmac::hashEquals
  87. */
  88. public function hashEqualsShouldReturnFalseWhenExpectedHashHasDifferentLengthThanGenerated()
  89. {
  90. $this->assertFalse($this->signer->hashEquals('123', '1234'));
  91. }
  92. /**
  93. * @test
  94. *
  95. * @depends createHashMustReturnAHashAccordingWithTheAlgorithm
  96. *
  97. * @uses Lcobucci\JWT\Signer\Hmac::createHash
  98. * @uses Lcobucci\JWT\Signer\Key
  99. *
  100. * @covers Lcobucci\JWT\Signer\Hmac::hashEquals
  101. */
  102. public function hashEqualsShouldReturnFalseWhenExpectedHashIsDifferentThanGenerated($expected)
  103. {
  104. $this->assertFalse($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('1234'))));
  105. }
  106. /**
  107. * @test
  108. *
  109. * @depends createHashMustReturnAHashAccordingWithTheAlgorithm
  110. *
  111. * @uses Lcobucci\JWT\Signer\Hmac::createHash
  112. * @uses Lcobucci\JWT\Signer\Key
  113. *
  114. * @covers Lcobucci\JWT\Signer\Hmac::hashEquals
  115. */
  116. public function hashEqualsShouldReturnTrueWhenExpectedHashIsEqualsThanGenerated($expected)
  117. {
  118. $this->assertTrue($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('123'))));
  119. }
  120. }