You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB

  1. package ringct
  2. import "bytes"
  3. import "testing"
  4. // this package needs to be verified for bug,
  5. // just in case, the top bit is set, it is impossible to do varint 64 bit number into 8 bytes, if the number is too big
  6. // in that case go needs 9 bytes, we should verify whether the number can ever reach there and thus place
  7. // suitable checks to avoid falling into the trap later on
  8. func TestVarInt(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. varInt []byte
  12. want uint64
  13. }{
  14. {
  15. name: "1 byte",
  16. varInt: []byte{0x01},
  17. want: 1,
  18. },
  19. {
  20. name: "3 bytes",
  21. varInt: []byte{0x8f, 0xd6, 0x17},
  22. want: 387855,
  23. },
  24. {
  25. name: "4 bytes",
  26. varInt: []byte{0x80, 0x92, 0xf4, 0x01},
  27. want: 4000000,
  28. },
  29. {
  30. name: "7 bytes",
  31. varInt: []byte{0x80, 0xc0, 0xca, 0xf3, 0x84, 0xa3, 0x02},
  32. want: 10000000000000,
  33. },
  34. }
  35. var got uint64
  36. var err error
  37. var gotVarInt []byte
  38. buf := new(bytes.Buffer)
  39. for _, test := range tests {
  40. gotVarInt = Uint64ToBytes(test.want)
  41. if bytes.Compare(gotVarInt, test.varInt) != 0 {
  42. t.Errorf("%s: varint want %x, got %x", test.name, test.varInt, gotVarInt)
  43. continue
  44. }
  45. buf.Reset()
  46. buf.Write(test.varInt)
  47. got, err = ReadVarInt(buf)
  48. if err != nil {
  49. t.Errorf("%s: %s", test.name, err)
  50. continue
  51. }
  52. if test.want != got {
  53. t.Errorf("%s: want %d, got %d", test.name, test.want, got)
  54. continue
  55. }
  56. }
  57. }