2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-08-03 17:20:05 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ldb
|
|
|
|
|
|
|
|
import (
|
2014-12-25 00:55:14 +01:00
|
|
|
"bytes"
|
2014-07-03 02:47:24 +02:00
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcutil"
|
2015-03-04 04:25:38 +01:00
|
|
|
"github.com/btcsuite/golangcrypto/ripemd160"
|
2013-08-03 17:20:05 +02:00
|
|
|
)
|
|
|
|
|
2014-12-25 00:55:14 +01:00
|
|
|
func TestAddrIndexKeySerialization(t *testing.T) {
|
|
|
|
var hash160Bytes [ripemd160.Size]byte
|
2015-03-15 03:09:51 +01:00
|
|
|
var packedIndex [12]byte
|
2014-12-25 00:55:14 +01:00
|
|
|
|
|
|
|
fakeHash160 := btcutil.Hash160([]byte("testing"))
|
|
|
|
copy(fakeHash160, hash160Bytes[:])
|
|
|
|
|
|
|
|
fakeIndex := txAddrIndex{
|
|
|
|
hash160: hash160Bytes,
|
|
|
|
blkHeight: 1,
|
|
|
|
txoffset: 5,
|
|
|
|
txlen: 360,
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedKey := addrIndexToKey(&fakeIndex)
|
2015-03-19 14:56:06 +01:00
|
|
|
copy(packedIndex[:], serializedKey[23:35])
|
2015-03-15 03:09:51 +01:00
|
|
|
unpackedIndex := unpackTxIndex(packedIndex)
|
2014-12-25 00:55:14 +01:00
|
|
|
|
|
|
|
if unpackedIndex.blkHeight != fakeIndex.blkHeight {
|
|
|
|
t.Errorf("Incorrect block height. Unpack addr index key"+
|
|
|
|
"serialization failed. Expected %d, received %d",
|
|
|
|
1, unpackedIndex.blkHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
if unpackedIndex.txoffset != fakeIndex.txoffset {
|
|
|
|
t.Errorf("Incorrect tx offset. Unpack addr index key"+
|
|
|
|
"serialization failed. Expected %d, received %d",
|
|
|
|
5, unpackedIndex.txoffset)
|
|
|
|
}
|
|
|
|
|
|
|
|
if unpackedIndex.txlen != fakeIndex.txlen {
|
|
|
|
t.Errorf("Incorrect tx len. Unpack addr index key"+
|
|
|
|
"serialization failed. Expected %d, received %d",
|
|
|
|
360, unpackedIndex.txlen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBytesPrefix(t *testing.T) {
|
|
|
|
testKey := []byte("a")
|
|
|
|
|
|
|
|
prefixRange := bytesPrefix(testKey)
|
|
|
|
if !bytes.Equal(prefixRange.Start, []byte("a")) {
|
|
|
|
t.Errorf("Wrong prefix start, got %d, expected %d", prefixRange.Start,
|
|
|
|
[]byte("a"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(prefixRange.Limit, []byte("b")) {
|
|
|
|
t.Errorf("Wrong prefix end, got %d, expected %d", prefixRange.Limit,
|
|
|
|
[]byte("b"))
|
2013-08-03 17:20:05 +02:00
|
|
|
}
|
|
|
|
}
|