main_test.go (1953B)
1 // These are all property based tests. 2 3 // These don't validate the implementation against a lamport signature spec, but 4 // they do validate expected invariants for signatures, messages, and keys using 5 // property based tests. 6 7 package main 8 9 import ( 10 "crypto/rand" 11 "sync" 12 "testing" 13 ) 14 15 func signAndValidate(t *testing.T, wg *sync.WaitGroup) { 16 17 defer wg.Done() 18 19 publicKey, privateKey := GenerateKeys() 20 21 message := rand.Text() 22 23 signature := Sign(privateKey, message) 24 25 valid := Verify(publicKey, message, signature) 26 27 if valid != true { 28 t.Errorf("Signature generation and verification mismatch.") 29 } 30 } 31 32 func signChangeAndValidate(t *testing.T, wg *sync.WaitGroup) { 33 34 defer wg.Done() 35 36 publicKey, privateKey := GenerateKeys() 37 38 message := rand.Text() 39 40 signature := Sign(privateKey, message) 41 42 signature[0][0] += 1 43 44 valid := Verify(publicKey, message, signature) 45 46 if valid != false { 47 t.Errorf("Signature changed, but still verified") 48 } 49 } 50 51 func signDifferentMessagesAndValidate(t *testing.T, wg *sync.WaitGroup) { 52 53 defer wg.Done() 54 55 publicKey, privateKey := GenerateKeys() 56 57 message1 := rand.Text() 58 message2 := rand.Text() 59 60 signature := Sign(privateKey, message1) 61 62 valid := Verify(publicKey, message1, signature) 63 64 if valid != true { 65 t.Errorf("Unable to validate signature for message it was generated with.") 66 } 67 68 valid = Verify(publicKey, message2, signature) 69 70 if valid != false { 71 t.Errorf("Signature valid for multiple messages") 72 } 73 } 74 75 func TestSignatureIsValid(t *testing.T) { 76 77 var wg sync.WaitGroup 78 79 for _ = range 100000 { 80 wg.Add(1) 81 go signAndValidate(t, &wg) 82 } 83 84 wg.Wait() 85 86 } 87 88 func TestSignatureChangeInvalidation(t *testing.T) { 89 90 var wg sync.WaitGroup 91 92 for _ = range 100000 { 93 wg.Add(1) 94 go signChangeAndValidate(t, &wg) 95 } 96 97 wg.Wait() 98 99 } 100 101 func TestMessageChangeInvalidation(t *testing.T) { 102 103 var wg sync.WaitGroup 104 105 for _ = range 100000 { 106 wg.Add(1) 107 go signDifferentMessagesAndValidate(t, &wg) 108 } 109 110 wg.Wait() 111 112 }