1 // Copyright (c) 2015-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
11 "github.com/btcsuite/btcd/btcec"
12 "github.com/btcsuite/btcd/chaincfg/chainhash"
15 // genRandomSig returns a random message, a signature of the message under the
16 // public key and the public key. This function is used to generate randomized
18 func genRandomSig() (*chainhash.Hash, *btcec.Signature, *btcec.PublicKey, error) {
19 privKey, err := btcec.NewPrivateKey(btcec.S256())
21 return nil, nil, nil, err
24 var msgHash chainhash.Hash
25 if _, err := rand.Read(msgHash[:]); err != nil {
26 return nil, nil, nil, err
29 sig, err := privKey.Sign(msgHash[:])
31 return nil, nil, nil, err
34 return &msgHash, sig, privKey.PubKey(), nil
37 // TestSigCacheAddExists tests the ability to add, and later check the
38 // existence of a signature triplet in the signature cache.
39 func TestSigCacheAddExists(t *testing.T) {
40 sigCache := NewSigCache(200)
42 // Generate a random sigCache entry triplet.
43 msg1, sig1, key1, err := genRandomSig()
45 t.Errorf("unable to generate random signature test data")
48 // Add the triplet to the signature cache.
49 sigCache.Add(*msg1, sig1, key1)
51 // The previously added triplet should now be found within the sigcache.
52 sig1Copy, _ := btcec.ParseSignature(sig1.Serialize(), btcec.S256())
53 key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed(), btcec.S256())
54 if !sigCache.Exists(*msg1, sig1Copy, key1Copy) {
55 t.Errorf("previously added item not found in signature cache")
59 // TestSigCacheAddEvictEntry tests the eviction case where a new signature
60 // triplet is added to a full signature cache which should trigger randomized
61 // eviction, followed by adding the new element to the cache.
62 func TestSigCacheAddEvictEntry(t *testing.T) {
63 // Create a sigcache that can hold up to 100 entries.
64 sigCacheSize := uint(100)
65 sigCache := NewSigCache(sigCacheSize)
67 // Fill the sigcache up with some random sig triplets.
68 for i := uint(0); i < sigCacheSize; i++ {
69 msg, sig, key, err := genRandomSig()
71 t.Fatalf("unable to generate random signature test data")
74 sigCache.Add(*msg, sig, key)
76 sigCopy, _ := btcec.ParseSignature(sig.Serialize(), btcec.S256())
77 keyCopy, _ := btcec.ParsePubKey(key.SerializeCompressed(), btcec.S256())
78 if !sigCache.Exists(*msg, sigCopy, keyCopy) {
79 t.Errorf("previously added item not found in signature" +
84 // The sigcache should now have sigCacheSize entries within it.
85 if uint(len(sigCache.validSigs)) != sigCacheSize {
86 t.Fatalf("sigcache should now have %v entries, instead it has %v",
87 sigCacheSize, len(sigCache.validSigs))
90 // Add a new entry, this should cause eviction of a randomly chosen
92 msgNew, sigNew, keyNew, err := genRandomSig()
94 t.Fatalf("unable to generate random signature test data")
96 sigCache.Add(*msgNew, sigNew, keyNew)
98 // The sigcache should still have sigCache entries.
99 if uint(len(sigCache.validSigs)) != sigCacheSize {
100 t.Fatalf("sigcache should now have %v entries, instead it has %v",
101 sigCacheSize, len(sigCache.validSigs))
104 // The entry added above should be found within the sigcache.
105 sigNewCopy, _ := btcec.ParseSignature(sigNew.Serialize(), btcec.S256())
106 keyNewCopy, _ := btcec.ParsePubKey(keyNew.SerializeCompressed(), btcec.S256())
107 if !sigCache.Exists(*msgNew, sigNewCopy, keyNewCopy) {
108 t.Fatalf("previously added item not found in signature cache")
112 // TestSigCacheAddMaxEntriesZeroOrNegative tests that if a sigCache is created
113 // with a max size <= 0, then no entries are added to the sigcache at all.
114 func TestSigCacheAddMaxEntriesZeroOrNegative(t *testing.T) {
115 // Create a sigcache that can hold up to 0 entries.
116 sigCache := NewSigCache(0)
118 // Generate a random sigCache entry triplet.
119 msg1, sig1, key1, err := genRandomSig()
121 t.Errorf("unable to generate random signature test data")
124 // Add the triplet to the signature cache.
125 sigCache.Add(*msg1, sig1, key1)
127 // The generated triplet should not be found.
128 sig1Copy, _ := btcec.ParseSignature(sig1.Serialize(), btcec.S256())
129 key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed(), btcec.S256())
130 if sigCache.Exists(*msg1, sig1Copy, key1Copy) {
131 t.Errorf("previously added signature found in sigcache, but" +
132 "shouldn't have been")
135 // There shouldn't be any entries in the sigCache.
136 if len(sigCache.validSigs) != 0 {
137 t.Errorf("%v items found in sigcache, no items should have"+
138 "been added", len(sigCache.validSigs))