signify_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // signFile reads the contents of an input file and signs it (in armored format)
  17. // with the key provided, placing the signature into the output file.
  18. package signify
  19. import (
  20. "io/ioutil"
  21. "math/rand"
  22. "os"
  23. "testing"
  24. "time"
  25. "github.com/jedisct1/go-minisign"
  26. )
  27. var (
  28. testSecKey = "RWRCSwAAAABVN5lr2JViGBN8DhX3/Qb/0g0wBdsNAR/APRW2qy9Fjsfr12sK2cd3URUFis1jgzQzaoayK8x4syT4G3Gvlt9RwGIwUYIQW/0mTeI+ECHu1lv5U4Wa2YHEPIesVPyRm5M="
  29. testPubKey = "RWTAPRW2qy9FjsBiMFGCEFv9Jk3iPhAh7tZb+VOFmtmBxDyHrFT8kZuT"
  30. )
  31. func TestSignify(t *testing.T) {
  32. tmpFile, err := ioutil.TempFile("", "")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. defer os.Remove(tmpFile.Name())
  37. defer tmpFile.Close()
  38. rand.Seed(time.Now().UnixNano())
  39. data := make([]byte, 1024)
  40. rand.Read(data)
  41. tmpFile.Write(data)
  42. if err = tmpFile.Close(); err != nil {
  43. t.Fatal(err)
  44. }
  45. err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "clé", "croissants")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. defer os.Remove(tmpFile.Name() + ".sig")
  50. // Verify the signature using a golang library
  51. sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. pKey, err := minisign.NewPublicKey(testPubKey)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. valid, err := pKey.VerifyFromFile(tmpFile.Name(), sig)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if !valid {
  64. t.Fatal("invalid signature")
  65. }
  66. }
  67. func TestSignifyTrustedCommentTooManyLines(t *testing.T) {
  68. tmpFile, err := ioutil.TempFile("", "")
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. defer os.Remove(tmpFile.Name())
  73. defer tmpFile.Close()
  74. rand.Seed(time.Now().UnixNano())
  75. data := make([]byte, 1024)
  76. rand.Read(data)
  77. tmpFile.Write(data)
  78. if err = tmpFile.Close(); err != nil {
  79. t.Fatal(err)
  80. }
  81. err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "", "crois\nsants")
  82. if err == nil || err.Error() == "" {
  83. t.Fatalf("should have errored on a multi-line trusted comment, got %v", err)
  84. }
  85. defer os.Remove(tmpFile.Name() + ".sig")
  86. }
  87. func TestSignifyTrustedCommentTooManyLinesLF(t *testing.T) {
  88. tmpFile, err := ioutil.TempFile("", "")
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. defer os.Remove(tmpFile.Name())
  93. defer tmpFile.Close()
  94. rand.Seed(time.Now().UnixNano())
  95. data := make([]byte, 1024)
  96. rand.Read(data)
  97. tmpFile.Write(data)
  98. if err = tmpFile.Close(); err != nil {
  99. t.Fatal(err)
  100. }
  101. err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "crois\rsants", "")
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. defer os.Remove(tmpFile.Name() + ".sig")
  106. }
  107. func TestSignifyTrustedCommentEmpty(t *testing.T) {
  108. tmpFile, err := ioutil.TempFile("", "")
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. defer os.Remove(tmpFile.Name())
  113. defer tmpFile.Close()
  114. rand.Seed(time.Now().UnixNano())
  115. data := make([]byte, 1024)
  116. rand.Read(data)
  117. tmpFile.Write(data)
  118. if err = tmpFile.Close(); err != nil {
  119. t.Fatal(err)
  120. }
  121. err = SignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, "", "")
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. defer os.Remove(tmpFile.Name() + ".sig")
  126. }