lbcutil/gcs/builder/builder_test.go

106 lines
2.5 KiB
Go
Raw Normal View History

2017-01-18 02:20:28 +01:00
// Copyright (c) 2017 The btcsuite developers
// Copyright (c) 2017 The Lightning Network Developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package builder_test
import (
"encoding/hex"
"testing"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil/gcs/builder"
)
var (
// No need to allocate an err variable in every test
err error
// List of values for building a filter
contents = [][]byte{
[]byte("Alex"),
[]byte("Bob"),
[]byte("Charlie"),
[]byte("Dick"),
[]byte("Ed"),
[]byte("Frank"),
[]byte("George"),
[]byte("Harry"),
[]byte("Ilya"),
[]byte("John"),
[]byte("Kevin"),
[]byte("Larry"),
[]byte("Michael"),
[]byte("Nate"),
[]byte("Owen"),
[]byte("Paul"),
[]byte("Quentin"),
}
// List of values for querying a filter using MatchAny()
contents2 = [][]byte{
[]byte("Alice"),
[]byte("Betty"),
[]byte("Charmaine"),
[]byte("Donna"),
[]byte("Edith"),
[]byte("Faina"),
[]byte("Georgia"),
[]byte("Hannah"),
[]byte("Ilsbeth"),
[]byte("Jennifer"),
[]byte("Kayla"),
[]byte("Lena"),
[]byte("Michelle"),
[]byte("Natalie"),
[]byte("Ophelia"),
[]byte("Peggy"),
[]byte("Queenie"),
}
)
// TestUseBlockHash tests using a block hash as a filter key.
func TestUseBlockHash(t *testing.T) {
// Block hash #448710, pretty high difficulty.
hash, err := chainhash.NewHashFromStr("000000000000000000496d7ff9bd2c96154a8d64260e8b3b411e625712abb14c")
if err != nil {
t.Fatalf("Hash from string failed: %s", err.Error())
}
// Create a Builder with a key hash and check that the key is derived
// correctly.
b := builder.WithKeyHash(hash)
key, err := b.Key()
if err != nil {
t.Fatalf("Builder instantiation with key hash failed: %s",
err.Error())
}
testKey := [16]byte{0x4c, 0xb1, 0xab, 0x12, 0x57, 0x62, 0x1e, 0x41,
0x3b, 0x8b, 0x0e, 0x26, 0x64, 0x8d, 0x4a, 0x15}
if key != testKey {
t.Fatalf("Key not derived correctly from key hash:\n%s\n%s",
hex.EncodeToString(key[:]),
hex.EncodeToString(testKey[:]))
}
// Build a filter and test matches.
b.AddEntries(contents)
f, err := b.Build()
match, err := f.Match(key, []byte("Nate"))
if err != nil {
t.Fatalf("Filter match failed: %s", err)
}
if !match {
t.Fatal("Filter didn't match when it should have!")
}
match, err = f.Match(key, []byte("weks"))
if err != nil {
t.Fatalf("Filter match failed: %s", err)
}
if match {
t.Logf("False positive match, should be 1 in 2**%d!",
builder.DefaultP)
}
}