protocol_test.go 643 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package protocol
  2. import (
  3. "testing"
  4. "testing/quick"
  5. )
  6. func TestHeaderFunctions(t *testing.T) {
  7. f := func(ver, id, typ int) bool {
  8. ver = int(uint(ver) % 16)
  9. id = int(uint(id) % 4096)
  10. typ = int(uint(typ) % 256)
  11. h0 := header{ver, id, typ}
  12. h1 := decodeHeader(encodeHeader(h0))
  13. return h0 == h1
  14. }
  15. if err := quick.Check(f, nil); err != nil {
  16. t.Error(err)
  17. }
  18. }
  19. func TestPad(t *testing.T) {
  20. tests := [][]int{
  21. {0, 0},
  22. {1, 3},
  23. {2, 2},
  24. {3, 1},
  25. {4, 0},
  26. {32, 0},
  27. {33, 3},
  28. }
  29. for _, tc := range tests {
  30. if p := pad(tc[0]); p != tc[1] {
  31. t.Errorf("Incorrect padding for %d bytes, %d != %d", tc[0], p, tc[1])
  32. }
  33. }
  34. }