github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/crypto/ringct/varint_test.go (about) 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 17 package ringct 18 19 import "bytes" 20 import "testing" 21 22 // this package needs to be verified for bug, 23 // 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 24 // in that case go needs 9 bytes, we should verify whether the number can ever reach there and thus place 25 // suitable checks to avoid falling into the trap later on 26 func TestVarInt(t *testing.T) { 27 tests := []struct { 28 name string 29 varInt []byte 30 want uint64 31 }{ 32 { 33 name: "1 byte", 34 varInt: []byte{0x01}, 35 want: 1, 36 }, 37 { 38 name: "3 bytes", 39 varInt: []byte{0x8f, 0xd6, 0x17}, 40 want: 387855, 41 }, 42 { 43 name: "4 bytes", 44 varInt: []byte{0x80, 0x92, 0xf4, 0x01}, 45 want: 4000000, 46 }, 47 { 48 name: "7 bytes", 49 varInt: []byte{0x80, 0xc0, 0xca, 0xf3, 0x84, 0xa3, 0x02}, 50 want: 10000000000000, 51 }, 52 } 53 var got uint64 54 var err error 55 var gotVarInt []byte 56 buf := new(bytes.Buffer) 57 for _, test := range tests { 58 gotVarInt = Uint64ToBytes(test.want) 59 if bytes.Compare(gotVarInt, test.varInt) != 0 { 60 t.Errorf("%s: varint want %x, got %x", test.name, test.varInt, gotVarInt) 61 continue 62 } 63 buf.Reset() 64 buf.Write(test.varInt) 65 got, err = ReadVarInt(buf) 66 if err != nil { 67 t.Errorf("%s: %s", test.name, err) 68 continue 69 } 70 if test.want != got { 71 t.Errorf("%s: want %d, got %d", test.name, test.want, got) 72 continue 73 } 74 } 75 }