digest_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package model
  2. import "testing"
  3. var testDigests = map[string]Digest{
  4. "": {},
  5. "sha256-1234": {s: "sha256-1234"},
  6. "sha256-5678": {s: "sha256-5678"},
  7. "blake2-9abc": {s: "blake2-9abc"},
  8. "-1234": {},
  9. "sha256-": {},
  10. "sha256-1234-5678": {},
  11. "sha256-P": {}, // invalid hex
  12. "sha256-1234P": {},
  13. "---": {},
  14. }
  15. func TestDigestParse(t *testing.T) {
  16. // Test cases.
  17. for s, want := range testDigests {
  18. got := ParseDigest(s)
  19. t.Logf("ParseDigest(%q) = %#v", s, got)
  20. if got != want {
  21. t.Errorf("ParseDigest(%q) = %q; want %q", s, got, want)
  22. }
  23. }
  24. }
  25. func TestDigestString(t *testing.T) {
  26. // Test cases.
  27. for s, d := range testDigests {
  28. want := s
  29. if !d.IsValid() {
  30. want = ""
  31. }
  32. got := d.String()
  33. if got != want {
  34. t.Errorf("ParseDigest(%q).String() = %q; want %q", s, got, want)
  35. }
  36. got = ParseDigest(s).String()
  37. if got != want {
  38. t.Errorf("roundtrip ParseDigest(%q).String() = %q; want %q", s, got, want)
  39. }
  40. }
  41. }