hashalgorithm_test.go 966 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (C) 2016 The Protocol Authors.
  2. package protocol
  3. import "testing"
  4. /*
  5. 0 1 2 3
  6. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  7. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  8. | Reserved | Hash |D|P|R|
  9. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  10. */
  11. func TestHashAlgorithmFromFlagBits(t *testing.T) {
  12. // SHA256 is algorithm zero, shifted three bits to the left (for clarity,
  13. // I know it doesn't actually do anything).
  14. sha256 := uint32(0 << 3)
  15. h, err := HashAlgorithmFromFlagBits(sha256)
  16. if err != nil {
  17. t.Error(err)
  18. }
  19. if h != SHA256 {
  20. t.Error("Zero should have unmarshalled as SHA256")
  21. }
  22. // Any other algorithm is unknown
  23. unknown := uint32(1 << 3)
  24. _, err = HashAlgorithmFromFlagBits(unknown)
  25. if err == nil {
  26. t.Error("Unknown algo should not have unmarshalled")
  27. }
  28. }