PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/php/net/xp_framework/unittest/security/checksum/AbstractDigestTest.class.php

http://github.com/xp-framework/xp-framework
PHP | 110 lines | 48 code | 12 blank | 50 comment | 0 complexity | 2f0e75eee9fc498938d400f4911826c0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php namespace net\xp_framework\unittest\security\checksum;
  2. use unittest\TestCase;
  3. use security\checksum\MessageDigest;
  4. /**
  5. * TestCase for MD5 digest
  6. *
  7. * @see xp://security.checksum.MD5Digest
  8. */
  9. abstract class AbstractDigestTest extends TestCase {
  10. protected $fixture;
  11. /**
  12. * Creates a new message digest object
  13. *
  14. * @return security.checksum.MessageDigest
  15. */
  16. protected abstract function newDigest();
  17. /**
  18. * Returns a checksum for a given input string
  19. *
  20. * @param string data
  21. * @return security.checksum.Checksum
  22. */
  23. protected abstract function checksumOf($data);
  24. /**
  25. * Sets up test case
  26. *
  27. */
  28. public function setUp() {
  29. $this->fixture= $this->newDigest();
  30. }
  31. /**
  32. * Test calling update once
  33. *
  34. */
  35. #[@test]
  36. public function singleUpdate() {
  37. $this->fixture->update('Hello');
  38. $this->assertEquals(
  39. $this->checksumOf('Hello'),
  40. $this->fixture->digest()
  41. );
  42. }
  43. /**
  44. * Test calling update() multiple times
  45. *
  46. */
  47. #[@test]
  48. public function multipleUpdates() {
  49. $this->fixture->update('Hello');
  50. $this->fixture->update('World');
  51. $this->assertEquals(
  52. $this->checksumOf('HelloWorld'),
  53. $this->fixture->digest()
  54. );
  55. }
  56. /**
  57. * Test not calling update() results in the MD5 of an empty string
  58. *
  59. */
  60. #[@test]
  61. public function noUpdate() {
  62. $this->assertEquals(
  63. $this->checksumOf(''),
  64. $this->fixture->digest()
  65. );
  66. }
  67. /**
  68. * Test not calling update() but instead digest with optional parameter
  69. *
  70. */
  71. #[@test]
  72. public function digestOnly() {
  73. $this->assertEquals(
  74. $this->checksumOf('Hello'),
  75. $this->fixture->digest('Hello')
  76. );
  77. }
  78. /**
  79. * Test not calling update() results in the MD5 of an empty string
  80. *
  81. */
  82. #[@test, @expect('lang.IllegalStateException')]
  83. public function callingUpdateAfterFinalization() {
  84. $this->fixture->update('...');
  85. $this->fixture->digest();
  86. $this->fixture->update('...');
  87. }
  88. /**
  89. * Test not calling update() results in the MD5 of an empty string
  90. *
  91. */
  92. #[@test, @expect('lang.IllegalStateException')]
  93. public function callingDigestAfterFinalization() {
  94. $this->fixture->update('...');
  95. $this->fixture->digest();
  96. $this->fixture->digest();
  97. }
  98. }