hash_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package fingerprint
  4. import (
  5. "crypto"
  6. "errors"
  7. "testing"
  8. )
  9. func TestHashFromString(t *testing.T) {
  10. t.Run("InvalidHashAlgorithm", func(t *testing.T) {
  11. _, err := HashFromString("invalid-hash-algorithm")
  12. if !errors.Is(err, errInvalidHashAlgorithm) {
  13. t.Errorf("Expected error '%v' for invalid hash name, got '%v'", errInvalidHashAlgorithm, err)
  14. }
  15. })
  16. t.Run("ValidHashAlgorithm", func(t *testing.T) {
  17. h, err := HashFromString("sha-512")
  18. if err != nil {
  19. t.Fatalf("Unexpected error for valid hash name, got '%v'", err)
  20. }
  21. if h != crypto.SHA512 {
  22. t.Errorf("Expected hash ID of %d, got %d", int(crypto.SHA512), int(h))
  23. }
  24. })
  25. t.Run("ValidCaseInsensitiveHashAlgorithm", func(t *testing.T) {
  26. h, err := HashFromString("SHA-512")
  27. if err != nil {
  28. t.Fatalf("Unexpected error for valid hash name, got '%v'", err)
  29. }
  30. if h != crypto.SHA512 {
  31. t.Errorf("Expected hash ID of %d, got %d", int(crypto.SHA512), int(h))
  32. }
  33. })
  34. }
  35. func TestStringFromHash_Roundtrip(t *testing.T) {
  36. for _, h := range nameToHash() {
  37. s, err := StringFromHash(h)
  38. if err != nil {
  39. t.Fatalf("Unexpected error for valid hash algorithm, got '%v'", err)
  40. }
  41. h2, err := HashFromString(s)
  42. if err != nil {
  43. t.Fatalf("Unexpected error for valid hash name, got '%v'", err)
  44. }
  45. if h != h2 {
  46. t.Errorf("Hash value doesn't match, expected: 0x%x, got 0x%x", h, h2)
  47. }
  48. }
  49. }