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