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