github.com/arjunbeliever/ignite@v0.0.0-20220406110515-46bbbbec2587/crypto/signify/signify_test.go (about) 1 // Copyright 2020 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // signFile reads the contents of an input file and signs it (in armored format) 18 // with the key provided, placing the signature into the output file. 19 20 package signify 21 22 import ( 23 "io/ioutil" 24 "math/rand" 25 "os" 26 "testing" 27 "time" 28 29 "github.com/jedisct1/go-minisign" 30 ) 31 32 var ( 33 testSecKey = "RWRCSwAAAABVN5lr2JViGBN8DhX3/Qb/0g0wBdsNAR/APRW2qy9Fjsfr12sK2cd3URUFis1jgzQzaoayK8x4syT4G3Gvlt9RwGIwUYIQW/0mTeI+ECHu1lv5U4Wa2YHEPIesVPyRm5M=" 34 testPubKey = "RWTAPRW2qy9FjsBiMFGCEFv9Jk3iPhAh7tZb+VOFmtmBxDyHrFT8kZuT" 35 ) 36 37 func TestSignify(t *testing.T) { 38 tmpFile, err := ioutil.TempFile("", "") 39 if err != nil { 40 t.Fatal(err) 41 } 42 defer os.Remove(tmpFile.Name()) 43 defer tmpFile.Close() 44 45 rand.Seed(time.Now().UnixNano()) 46 47 data := make([]byte, 1024) 48 rand.Read(data) 49 tmpFile.Write(data) 50 51 if err = tmpFile.Close(); err != nil { 52 t.Fatal(err) 53 } 54 55 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "clé", "croissants") 56 if err != nil { 57 t.Fatal(err) 58 } 59 defer os.Remove(tmpFile.Name() + ".sig") 60 61 // Verify the signature using a golang library 62 sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig") 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 pKey, err := minisign.NewPublicKey(testPubKey) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 valid, err := pKey.VerifyFromFile(tmpFile.Name(), sig) 73 if err != nil { 74 t.Fatal(err) 75 } 76 if !valid { 77 t.Fatal("invalid signature") 78 } 79 } 80 81 func TestSignifyTrustedCommentTooManyLines(t *testing.T) { 82 tmpFile, err := ioutil.TempFile("", "") 83 if err != nil { 84 t.Fatal(err) 85 } 86 defer os.Remove(tmpFile.Name()) 87 defer tmpFile.Close() 88 89 rand.Seed(time.Now().UnixNano()) 90 91 data := make([]byte, 1024) 92 rand.Read(data) 93 tmpFile.Write(data) 94 95 if err = tmpFile.Close(); err != nil { 96 t.Fatal(err) 97 } 98 99 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "", "crois\nsants") 100 if err == nil || err.Error() == "" { 101 t.Fatalf("should have errored on a multi-line trusted comment, got %v", err) 102 } 103 defer os.Remove(tmpFile.Name() + ".sig") 104 } 105 106 func TestSignifyTrustedCommentTooManyLinesLF(t *testing.T) { 107 tmpFile, err := ioutil.TempFile("", "") 108 if err != nil { 109 t.Fatal(err) 110 } 111 defer os.Remove(tmpFile.Name()) 112 defer tmpFile.Close() 113 114 rand.Seed(time.Now().UnixNano()) 115 116 data := make([]byte, 1024) 117 rand.Read(data) 118 tmpFile.Write(data) 119 120 if err = tmpFile.Close(); err != nil { 121 t.Fatal(err) 122 } 123 124 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "crois\rsants", "") 125 if err != nil { 126 t.Fatal(err) 127 } 128 defer os.Remove(tmpFile.Name() + ".sig") 129 } 130 131 func TestSignifyTrustedCommentEmpty(t *testing.T) { 132 tmpFile, err := ioutil.TempFile("", "") 133 if err != nil { 134 t.Fatal(err) 135 } 136 defer os.Remove(tmpFile.Name()) 137 defer tmpFile.Close() 138 139 rand.Seed(time.Now().UnixNano()) 140 141 data := make([]byte, 1024) 142 rand.Read(data) 143 tmpFile.Write(data) 144 145 if err = tmpFile.Close(); err != nil { 146 t.Fatal(err) 147 } 148 149 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "", "") 150 if err != nil { 151 t.Fatal(err) 152 } 153 defer os.Remove(tmpFile.Name() + ".sig") 154 }