OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / txscript / sigcache_test.go
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.
4
5 package txscript
6
7 import (
8         "crypto/rand"
9         "testing"
10
11         "github.com/btcsuite/btcd/btcec"
12         "github.com/btcsuite/btcd/chaincfg/chainhash"
13 )
14
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
17 // test data.
18 func genRandomSig() (*chainhash.Hash, *btcec.Signature, *btcec.PublicKey, error) {
19         privKey, err := btcec.NewPrivateKey(btcec.S256())
20         if err != nil {
21                 return nil, nil, nil, err
22         }
23
24         var msgHash chainhash.Hash
25         if _, err := rand.Read(msgHash[:]); err != nil {
26                 return nil, nil, nil, err
27         }
28
29         sig, err := privKey.Sign(msgHash[:])
30         if err != nil {
31                 return nil, nil, nil, err
32         }
33
34         return &msgHash, sig, privKey.PubKey(), nil
35 }
36
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)
41
42         // Generate a random sigCache entry triplet.
43         msg1, sig1, key1, err := genRandomSig()
44         if err != nil {
45                 t.Errorf("unable to generate random signature test data")
46         }
47
48         // Add the triplet to the signature cache.
49         sigCache.Add(*msg1, sig1, key1)
50
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")
56         }
57 }
58
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)
66
67         // Fill the sigcache up with some random sig triplets.
68         for i := uint(0); i < sigCacheSize; i++ {
69                 msg, sig, key, err := genRandomSig()
70                 if err != nil {
71                         t.Fatalf("unable to generate random signature test data")
72                 }
73
74                 sigCache.Add(*msg, sig, key)
75
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" +
80                                 "cache")
81                 }
82         }
83
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))
88         }
89
90         // Add a new entry, this should cause eviction of a randomly chosen
91         // previous entry.
92         msgNew, sigNew, keyNew, err := genRandomSig()
93         if err != nil {
94                 t.Fatalf("unable to generate random signature test data")
95         }
96         sigCache.Add(*msgNew, sigNew, keyNew)
97
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))
102         }
103
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")
109         }
110 }
111
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)
117
118         // Generate a random sigCache entry triplet.
119         msg1, sig1, key1, err := genRandomSig()
120         if err != nil {
121                 t.Errorf("unable to generate random signature test data")
122         }
123
124         // Add the triplet to the signature cache.
125         sigCache.Add(*msg1, sig1, key1)
126
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")
133         }
134
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))
139         }
140 }