github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/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 "math/rand" 24 "os" 25 "testing" 26 "time" 27 28 "github.com/jedisct1/go-minisign" 29 ) 30 31 var ( 32 testSecKey = "RWRCSwAAAABVN5lr2JViGBN8DhX3/Qb/0g0wBdsNAR/APRW2qy9Fjsfr12sK2cd3URUFis1jgzQzaoayK8x4syT4G3Gvlt9RwGIwUYIQW/0mTeI+ECHu1lv5U4Wa2YHEPIesVPyRm5M=" 33 testPubKey = "RWTAPRW2qy9FjsBiMFGCEFv9Jk3iPhAh7tZb+VOFmtmBxDyHrFT8kZuT" 34 ) 35 36 func TestSignify(t *testing.T) { 37 tmpFile, err := os.CreateTemp("", "") 38 if err != nil { 39 t.Fatal(err) 40 } 41 defer os.Remove(tmpFile.Name()) 42 defer tmpFile.Close() 43 44 rand.Seed(time.Now().UnixNano()) 45 46 data := make([]byte, 1024) 47 rand.Read(data) 48 tmpFile.Write(data) 49 50 if err = tmpFile.Close(); err != nil { 51 t.Fatal(err) 52 } 53 54 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "clé", "croissants") 55 if err != nil { 56 t.Fatal(err) 57 } 58 defer os.Remove(tmpFile.Name() + ".sig") 59 60 // Verify the signature using a golang library 61 sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig") 62 if err != nil { 63 t.Fatal(err) 64 } 65 66 pKey, err := minisign.NewPublicKey(testPubKey) 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 valid, err := pKey.VerifyFromFile(tmpFile.Name(), sig) 72 if err != nil { 73 t.Fatal(err) 74 } 75 if !valid { 76 t.Fatal("invalid signature") 77 } 78 } 79 80 func TestSignifyTrustedCommentTooManyLines(t *testing.T) { 81 tmpFile, err := os.CreateTemp("", "") 82 if err != nil { 83 t.Fatal(err) 84 } 85 defer os.Remove(tmpFile.Name()) 86 defer tmpFile.Close() 87 88 rand.Seed(time.Now().UnixNano()) 89 90 data := make([]byte, 1024) 91 rand.Read(data) 92 tmpFile.Write(data) 93 94 if err = tmpFile.Close(); err != nil { 95 t.Fatal(err) 96 } 97 98 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "", "crois\nsants") 99 if err == nil || err.Error() == "" { 100 t.Fatalf("should have errored on a multi-line trusted comment, got %v", err) 101 } 102 defer os.Remove(tmpFile.Name() + ".sig") 103 } 104 105 func TestSignifyTrustedCommentTooManyLinesLF(t *testing.T) { 106 tmpFile, err := os.CreateTemp("", "") 107 if err != nil { 108 t.Fatal(err) 109 } 110 defer os.Remove(tmpFile.Name()) 111 defer tmpFile.Close() 112 113 rand.Seed(time.Now().UnixNano()) 114 115 data := make([]byte, 1024) 116 rand.Read(data) 117 tmpFile.Write(data) 118 119 if err = tmpFile.Close(); err != nil { 120 t.Fatal(err) 121 } 122 123 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "crois\rsants", "") 124 if err != nil { 125 t.Fatal(err) 126 } 127 defer os.Remove(tmpFile.Name() + ".sig") 128 } 129 130 func TestSignifyTrustedCommentEmpty(t *testing.T) { 131 tmpFile, err := os.CreateTemp("", "") 132 if err != nil { 133 t.Fatal(err) 134 } 135 defer os.Remove(tmpFile.Name()) 136 defer tmpFile.Close() 137 138 rand.Seed(time.Now().UnixNano()) 139 140 data := make([]byte, 1024) 141 rand.Read(data) 142 tmpFile.Write(data) 143 144 if err = tmpFile.Close(); err != nil { 145 t.Fatal(err) 146 } 147 148 err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "", "") 149 if err != nil { 150 t.Fatal(err) 151 } 152 defer os.Remove(tmpFile.Name() + ".sig") 153 }