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.

75 lines
2.4 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package ringct
  17. import "bytes"
  18. import "testing"
  19. // this package needs to be verified for bug,
  20. // 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
  21. // in that case go needs 9 bytes, we should verify whether the number can ever reach there and thus place
  22. // suitable checks to avoid falling into the trap later on
  23. func TestVarInt(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. varInt []byte
  27. want uint64
  28. }{
  29. {
  30. name: "1 byte",
  31. varInt: []byte{0x01},
  32. want: 1,
  33. },
  34. {
  35. name: "3 bytes",
  36. varInt: []byte{0x8f, 0xd6, 0x17},
  37. want: 387855,
  38. },
  39. {
  40. name: "4 bytes",
  41. varInt: []byte{0x80, 0x92, 0xf4, 0x01},
  42. want: 4000000,
  43. },
  44. {
  45. name: "7 bytes",
  46. varInt: []byte{0x80, 0xc0, 0xca, 0xf3, 0x84, 0xa3, 0x02},
  47. want: 10000000000000,
  48. },
  49. }
  50. var got uint64
  51. var err error
  52. var gotVarInt []byte
  53. buf := new(bytes.Buffer)
  54. for _, test := range tests {
  55. gotVarInt = Uint64ToBytes(test.want)
  56. if bytes.Compare(gotVarInt, test.varInt) != 0 {
  57. t.Errorf("%s: varint want %x, got %x", test.name, test.varInt, gotVarInt)
  58. continue
  59. }
  60. buf.Reset()
  61. buf.Write(test.varInt)
  62. got, err = ReadVarInt(buf)
  63. if err != nil {
  64. t.Errorf("%s: %s", test.name, err)
  65. continue
  66. }
  67. if test.want != got {
  68. t.Errorf("%s: want %d, got %d", test.name, test.want, got)
  69. continue
  70. }
  71. }
  72. }