sigcache_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. package txscript
  5. import (
  6. "crypto/rand"
  7. "testing"
  8. "github.com/pkt-cash/pktd/btcutil/er"
  9. "github.com/pkt-cash/pktd/btcec"
  10. "github.com/pkt-cash/pktd/chaincfg/chainhash"
  11. )
  12. // genRandomSig returns a random message, a signature of the message under the
  13. // public key and the public key. This function is used to generate randomized
  14. // test data.
  15. func genRandomSig() (*chainhash.Hash, *btcec.Signature, *btcec.PublicKey, er.R) {
  16. privKey, err := btcec.NewPrivateKey(btcec.S256())
  17. if err != nil {
  18. return nil, nil, nil, err
  19. }
  20. var msgHash chainhash.Hash
  21. if _, err := rand.Read(msgHash[:]); err != nil {
  22. return nil, nil, nil, er.E(err)
  23. }
  24. sig, err := privKey.Sign(msgHash[:])
  25. if err != nil {
  26. return nil, nil, nil, err
  27. }
  28. return &msgHash, sig, privKey.PubKey(), nil
  29. }
  30. // TestSigCacheAddExists tests the ability to add, and later check the
  31. // existence of a signature triplet in the signature cache.
  32. func TestSigCacheAddExists(t *testing.T) {
  33. sigCache := NewSigCache(200)
  34. // Generate a random sigCache entry triplet.
  35. msg1, sig1, key1, err := genRandomSig()
  36. if err != nil {
  37. t.Errorf("unable to generate random signature test data")
  38. }
  39. // Add the triplet to the signature cache.
  40. sigCache.Add(*msg1, sig1, key1)
  41. // The previously added triplet should now be found within the sigcache.
  42. sig1Copy, _ := btcec.ParseSignature(sig1.Serialize(), btcec.S256())
  43. key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed(), btcec.S256())
  44. if !sigCache.Exists(*msg1, sig1Copy, key1Copy) {
  45. t.Errorf("previously added item not found in signature cache")
  46. }
  47. }
  48. // TestSigCacheAddEvictEntry tests the eviction case where a new signature
  49. // triplet is added to a full signature cache which should trigger randomized
  50. // eviction, followed by adding the new element to the cache.
  51. func TestSigCacheAddEvictEntry(t *testing.T) {
  52. // Create a sigcache that can hold up to 100 entries.
  53. sigCacheSize := uint(100)
  54. sigCache := NewSigCache(sigCacheSize)
  55. // Fill the sigcache up with some random sig triplets.
  56. for i := uint(0); i < sigCacheSize; i++ {
  57. msg, sig, key, err := genRandomSig()
  58. if err != nil {
  59. t.Fatalf("unable to generate random signature test data")
  60. }
  61. sigCache.Add(*msg, sig, key)
  62. sigCopy, _ := btcec.ParseSignature(sig.Serialize(), btcec.S256())
  63. keyCopy, _ := btcec.ParsePubKey(key.SerializeCompressed(), btcec.S256())
  64. if !sigCache.Exists(*msg, sigCopy, keyCopy) {
  65. t.Errorf("previously added item not found in signature" +
  66. "cache")
  67. }
  68. }
  69. // The sigcache should now have sigCacheSize entries within it.
  70. if uint(len(sigCache.validSigs)) != sigCacheSize {
  71. t.Fatalf("sigcache should now have %v entries, instead it has %v",
  72. sigCacheSize, len(sigCache.validSigs))
  73. }
  74. // Add a new entry, this should cause eviction of a randomly chosen
  75. // previous entry.
  76. msgNew, sigNew, keyNew, err := genRandomSig()
  77. if err != nil {
  78. t.Fatalf("unable to generate random signature test data")
  79. }
  80. sigCache.Add(*msgNew, sigNew, keyNew)
  81. // The sigcache should still have sigCache entries.
  82. if uint(len(sigCache.validSigs)) != sigCacheSize {
  83. t.Fatalf("sigcache should now have %v entries, instead it has %v",
  84. sigCacheSize, len(sigCache.validSigs))
  85. }
  86. // The entry added above should be found within the sigcache.
  87. sigNewCopy, _ := btcec.ParseSignature(sigNew.Serialize(), btcec.S256())
  88. keyNewCopy, _ := btcec.ParsePubKey(keyNew.SerializeCompressed(), btcec.S256())
  89. if !sigCache.Exists(*msgNew, sigNewCopy, keyNewCopy) {
  90. t.Fatalf("previously added item not found in signature cache")
  91. }
  92. }
  93. // TestSigCacheAddMaxEntriesZeroOrNegative tests that if a sigCache is created
  94. // with a max size <= 0, then no entries are added to the sigcache at all.
  95. func TestSigCacheAddMaxEntriesZeroOrNegative(t *testing.T) {
  96. // Create a sigcache that can hold up to 0 entries.
  97. sigCache := NewSigCache(0)
  98. // Generate a random sigCache entry triplet.
  99. msg1, sig1, key1, err := genRandomSig()
  100. if err != nil {
  101. t.Errorf("unable to generate random signature test data")
  102. }
  103. // Add the triplet to the signature cache.
  104. sigCache.Add(*msg1, sig1, key1)
  105. // The generated triplet should not be found.
  106. sig1Copy, _ := btcec.ParseSignature(sig1.Serialize(), btcec.S256())
  107. key1Copy, _ := btcec.ParsePubKey(key1.SerializeCompressed(), btcec.S256())
  108. if sigCache.Exists(*msg1, sig1Copy, key1Copy) {
  109. t.Errorf("previously added signature found in sigcache, but" +
  110. "shouldn't have been")
  111. }
  112. // There shouldn't be any entries in the sigCache.
  113. if len(sigCache.validSigs) != 0 {
  114. t.Errorf("%v items found in sigcache, no items should have"+
  115. "been added", len(sigCache.validSigs))
  116. }
  117. }