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.

90 lines
2.8 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 p2p
  17. import "testing"
  18. /* this func decode boost variant
  19. *
  20. * 0 0a 60 == (60 >> 2 ) = 24 bytes
  21. * 1 0a c0 == (c0 >> 2 ) = 48 bytes
  22. * 2 0a 21 01 == (121 >> 2 ) = 72
  23. * 3 0a 81 01 == (181 >> 2 ) = 96 bytes
  24. * 4 0a e1 01
  25. * 5 0a 41 02
  26. * 7 0a 01 03
  27. * 8 0a 61 03
  28. * 9 0a c1 03
  29. *
  30. *
  31. *
  32. * */
  33. func Test_Boost_Varint_Serdes(t *testing.T) {
  34. buf := make([]byte, 8, 8)
  35. bytes_required := Encode_Boost_Varint(buf, 24)
  36. if bytes_required != 1 || buf[0] != 0x60 {
  37. t.Errorf("Single bytes boost varint encode test failed %d", buf[0])
  38. }
  39. // decode and test
  40. value, bytes_required := Decode_Boost_Varint(buf)
  41. if value != 24 || bytes_required != 1 {
  42. t.Errorf("Single bytes boost varint decode test failed value %d bytes %d", value, bytes_required)
  43. }
  44. // 2 bytes test
  45. bytes_required = Encode_Boost_Varint(buf, 72)
  46. if bytes_required != 2 || buf[0] != 0x21 || buf[1] != 1 {
  47. t.Errorf("2 bytes boost varint encode test failed")
  48. }
  49. // decode and test
  50. value, bytes_required = Decode_Boost_Varint(buf)
  51. if value != 72 || bytes_required != 2 {
  52. t.Errorf("2 bytes boost varint decode test failed")
  53. }
  54. bytes_required = Encode_Boost_Varint(buf, 6000)
  55. if bytes_required != 2 || buf[0] != 0xc1 || buf[1] != 0x5d {
  56. t.Errorf("2 bytes boost varint encode test failed")
  57. }
  58. // decode and test
  59. value, bytes_required = Decode_Boost_Varint(buf)
  60. if value != 6000 || bytes_required != 2 {
  61. t.Errorf("2 bytes boost varint decode test failed")
  62. }
  63. // 3 bytes test
  64. bytes_required = Encode_Boost_Varint(buf, 40096)
  65. if bytes_required != 4 || buf[0] != 0x82 || buf[1] == 0x72 && buf[2] != 0x2 && buf[3] == 0 {
  66. t.Errorf("3 bytes boost varint encode test failed")
  67. }
  68. // decode and test
  69. value, bytes_required = Decode_Boost_Varint(buf)
  70. if value != 40096 || bytes_required != 4 {
  71. t.Errorf("3 bytes boost varint decode test failed")
  72. }
  73. }