Crc32Tests.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Masuit.Tools.Security;
  2. using Xunit;
  3. namespace Masuit.Tools.Abstractions.Test.Security;
  4. public class Crc32Tests
  5. {
  6. [Fact]
  7. public void Crc32_DefaultConstructor_ShouldInitializeWithDefaultValues()
  8. {
  9. // Arrange & Act
  10. var crc32 = new Crc32();
  11. // Assert
  12. Assert.Equal(32, crc32.HashSize);
  13. }
  14. [Fact]
  15. public void Crc32_ParameterizedConstructor_ShouldInitializeWithGivenValues()
  16. {
  17. // Arrange
  18. uint polynomial = 0x04C11DB7;
  19. uint seed = 0xFFFFFFFF;
  20. // Act
  21. var crc32 = new Crc32(polynomial, seed);
  22. // Assert
  23. Assert.Equal(32, crc32.HashSize);
  24. }
  25. [Fact]
  26. public void HashFinal_ShouldReturnCorrectHash()
  27. {
  28. // Arrange
  29. var crc32 = new Crc32();
  30. var data = new byte[] { 1, 2, 3 };
  31. // Act
  32. crc32.ComputeHash(data);
  33. var hash = crc32.Hash;
  34. // Assert
  35. Assert.NotNull(hash);
  36. Assert.Equal(4, hash.Length);
  37. }
  38. [Fact]
  39. public void Compute_WithBuffer_ShouldReturnCorrectHash()
  40. {
  41. // Arrange
  42. var data = new byte[] { 1, 2, 3 };
  43. // Act
  44. var hash = Crc32.Compute(data);
  45. // Assert
  46. Assert.Equal((uint)1438416925, hash);
  47. }
  48. [Fact]
  49. public void Compute_WithSeedAndBuffer_ShouldReturnCorrectHash()
  50. {
  51. // Arrange
  52. var data = new byte[] { 1, 2, 3 };
  53. uint seed = 0xFFFFFFFF;
  54. // Act
  55. var hash = Crc32.Compute(seed, data);
  56. // Assert
  57. Assert.Equal((uint)1438416925, hash);
  58. }
  59. [Fact]
  60. public void Compute_WithPolynomialSeedAndBuffer_ShouldReturnCorrectHash()
  61. {
  62. // Arrange
  63. var data = new byte[] { 1, 2, 3 };
  64. uint polynomial = 0x04C11DB7;
  65. uint seed = 0xFFFFFFFF;
  66. // Act
  67. var hash = Crc32.Compute(polynomial, seed, data);
  68. // Assert
  69. Assert.Equal((uint)4185564129, hash);
  70. }
  71. }