keys_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package main
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "os"
  9. "path"
  10. "regexp"
  11. "testing"
  12. gosrc "github.com/Morganamilo/go-srcinfo"
  13. rpc "github.com/mikkeloscar/aur"
  14. )
  15. const (
  16. // The default port used by the PGP key server.
  17. gpgServerPort = 11371
  18. )
  19. func init() {
  20. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  21. regex := regexp.MustCompile(`search=0[xX]([a-fA-F0-9]+)`)
  22. matches := regex.FindStringSubmatch(r.RequestURI)
  23. data := ""
  24. if matches != nil {
  25. data = getPgpKey(matches[1])
  26. }
  27. w.Header().Set("Content-Type", "application/pgp-keys")
  28. w.Write([]byte(data))
  29. })
  30. }
  31. func newPkg(basename string) *rpc.Pkg {
  32. return &rpc.Pkg{Name: basename, PackageBase: basename}
  33. }
  34. func getPgpKey(key string) string {
  35. var buffer bytes.Buffer
  36. if contents, err := ioutil.ReadFile(path.Join("testdata", "keys", key)); err == nil {
  37. buffer.WriteString("-----BEGIN PGP PUBLIC KEY BLOCK-----\n")
  38. buffer.WriteString("Version: SKS 1.1.6\n")
  39. buffer.WriteString("Comment: Hostname: yay\n\n")
  40. buffer.Write(contents)
  41. buffer.WriteString("\n-----END PGP PUBLIC KEY BLOCK-----\n")
  42. }
  43. return buffer.String()
  44. }
  45. func startPgpKeyServer() *http.Server {
  46. srv := &http.Server{Addr: fmt.Sprintf("127.0.0.1:%d", gpgServerPort)}
  47. go func() {
  48. srv.ListenAndServe()
  49. }()
  50. return srv
  51. }
  52. func TestImportKeys(t *testing.T) {
  53. keyringDir, err := ioutil.TempDir("/tmp", "yay-test-keyring")
  54. if err != nil {
  55. t.Fatalf("Unable to init test keyring %q: %v\n", keyringDir, err)
  56. }
  57. defer os.RemoveAll(keyringDir)
  58. config.GpgBin = "gpg"
  59. config.GpgFlags = fmt.Sprintf("--homedir %s --keyserver 127.0.0.1", keyringDir)
  60. server := startPgpKeyServer()
  61. defer server.Shutdown(context.TODO())
  62. casetests := []struct {
  63. keys []string
  64. wantError bool
  65. }{
  66. // Single key, should succeed.
  67. // C52048C0C0748FEE227D47A2702353E0F7E48EDB: Thomas Dickey.
  68. {
  69. keys: []string{"C52048C0C0748FEE227D47A2702353E0F7E48EDB"},
  70. wantError: false,
  71. },
  72. // Two keys, should succeed as well.
  73. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  74. // B6C8F98282B944E3B0D5C2530FC3042E345AD05D: Hans Wennborg.
  75. {
  76. keys: []string{"11E521D646982372EB577A1F8F0871F202119294",
  77. "B6C8F98282B944E3B0D5C2530FC3042E345AD05D"},
  78. wantError: false,
  79. },
  80. // Single invalid key, should fail.
  81. {
  82. keys: []string{"THIS-SHOULD-FAIL"},
  83. wantError: true,
  84. },
  85. // Two invalid keys, should fail.
  86. {
  87. keys: []string{"THIS-SHOULD-FAIL", "THIS-ONE-SHOULD-FAIL-TOO"},
  88. wantError: true,
  89. },
  90. // Invalid + valid key. Should fail as well.
  91. // 647F28654894E3BD457199BE38DBBDC86092693E: Greg Kroah-Hartman.
  92. {
  93. keys: []string{"THIS-SHOULD-FAIL",
  94. "647F28654894E3BD457199BE38DBBDC86092693E"},
  95. wantError: true,
  96. },
  97. }
  98. for _, tt := range casetests {
  99. err := importKeys(tt.keys)
  100. if !tt.wantError {
  101. if err != nil {
  102. t.Fatalf("Got error %q, want no error", err)
  103. }
  104. continue
  105. }
  106. // Here, we want to see the error.
  107. if err == nil {
  108. t.Fatalf("Got no error; want error")
  109. }
  110. }
  111. }
  112. func makeSrcinfo(pkgbase string, pgpkeys ...string) *gosrc.Srcinfo {
  113. srcinfo := gosrc.Srcinfo{}
  114. srcinfo.Pkgbase = pkgbase
  115. srcinfo.ValidPGPKeys = pgpkeys
  116. return &srcinfo
  117. }
  118. func TestCheckPgpKeys(t *testing.T) {
  119. keyringDir, err := ioutil.TempDir("/tmp", "yay-test-keyring")
  120. if err != nil {
  121. t.Fatalf("Unable to init test keyring: %v\n", err)
  122. }
  123. defer os.RemoveAll(keyringDir)
  124. config.GpgBin = "gpg"
  125. config.GpgFlags = fmt.Sprintf("--homedir %s --keyserver 127.0.0.1", keyringDir)
  126. server := startPgpKeyServer()
  127. defer server.Shutdown(context.TODO())
  128. casetests := []struct {
  129. pkgs []*rpc.Pkg
  130. srcinfos map[string]*gosrc.Srcinfo
  131. bases map[string][]*rpc.Pkg
  132. wantError bool
  133. }{
  134. // cower: single package, one valid key not yet in the keyring.
  135. // 487EACC08557AD082088DABA1EB2638FF56C0C53: Dave Reisner.
  136. {
  137. pkgs: []*rpc.Pkg{newPkg("cower")},
  138. srcinfos: map[string]*gosrc.Srcinfo{"cower": makeSrcinfo("cower", "487EACC08557AD082088DABA1EB2638FF56C0C53")},
  139. bases: map[string][]*rpc.Pkg{"cower": {newPkg("cower")}},
  140. wantError: false,
  141. },
  142. // libc++: single package, two valid keys not yet in the keyring.
  143. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  144. // B6C8F98282B944E3B0D5C2530FC3042E345AD05D: Hans Wennborg.
  145. {
  146. pkgs: []*rpc.Pkg{newPkg("libc++")},
  147. srcinfos: map[string]*gosrc.Srcinfo{"libc++": makeSrcinfo("libc++", "11E521D646982372EB577A1F8F0871F202119294", "B6C8F98282B944E3B0D5C2530FC3042E345AD05D")},
  148. bases: map[string][]*rpc.Pkg{"libc++": {newPkg("libc++")}},
  149. wantError: false,
  150. },
  151. // Two dummy packages requiring the same key.
  152. // ABAF11C65A2970B130ABE3C479BE3E4300411886: Linus Torvalds.
  153. {
  154. pkgs: []*rpc.Pkg{newPkg("dummy-1"), newPkg("dummy-2")},
  155. srcinfos: map[string]*gosrc.Srcinfo{"dummy-1": makeSrcinfo("dummy-1", "ABAF11C65A2970B130ABE3C479BE3E4300411886"), "dummy-2": makeSrcinfo("dummy-2", "ABAF11C65A2970B130ABE3C479BE3E4300411886")},
  156. bases: map[string][]*rpc.Pkg{"dummy-1": {newPkg("dummy-1")}, "dummy-2": {newPkg("dummy-2")}},
  157. wantError: false,
  158. },
  159. // dummy package: single package, two valid keys, one of them already
  160. // in the keyring.
  161. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  162. // C52048C0C0748FEE227D47A2702353E0F7E48EDB: Thomas Dickey.
  163. {
  164. pkgs: []*rpc.Pkg{newPkg("dummy-3")},
  165. srcinfos: map[string]*gosrc.Srcinfo{"dummy-3": makeSrcinfo("dummy-3", "11E521D646982372EB577A1F8F0871F202119294", "C52048C0C0748FEE227D47A2702353E0F7E48EDB")},
  166. bases: map[string][]*rpc.Pkg{"dummy-3": {newPkg("dummy-3")}},
  167. wantError: false,
  168. },
  169. // Two dummy packages with existing keys.
  170. {
  171. pkgs: []*rpc.Pkg{newPkg("dummy-4"), newPkg("dummy-5")},
  172. srcinfos: map[string]*gosrc.Srcinfo{"dummy-4": makeSrcinfo("dummy-4", "11E521D646982372EB577A1F8F0871F202119294"), "dummy-5": makeSrcinfo("dummy-5", "C52048C0C0748FEE227D47A2702353E0F7E48EDB")},
  173. bases: map[string][]*rpc.Pkg{"dummy-4": {newPkg("dummy-4")}, "dummy-5": {newPkg("dummy-5")}},
  174. wantError: false,
  175. },
  176. // Dummy package with invalid key, should fail.
  177. {
  178. pkgs: []*rpc.Pkg{newPkg("dummy-7")},
  179. srcinfos: map[string]*gosrc.Srcinfo{"dummy-7": makeSrcinfo("dummy-7", "THIS-SHOULD-FAIL")},
  180. bases: map[string][]*rpc.Pkg{"dummy-7": {newPkg("dummy-7")}},
  181. wantError: true,
  182. },
  183. // Dummy package with both an invalid an another valid key, should fail.
  184. // A314827C4E4250A204CE6E13284FC34C8E4B1A25: Thomas Bächler.
  185. {
  186. pkgs: []*rpc.Pkg{newPkg("dummy-8")},
  187. srcinfos: map[string]*gosrc.Srcinfo{"dummy-8": makeSrcinfo("dummy-8", "A314827C4E4250A204CE6E13284FC34C8E4B1A25", "THIS-SHOULD-FAIL")},
  188. bases: map[string][]*rpc.Pkg{"dummy-8": {newPkg("dummy-8")}},
  189. wantError: true,
  190. },
  191. }
  192. for _, tt := range casetests {
  193. err := checkPgpKeys(tt.pkgs, tt.bases, tt.srcinfos)
  194. if !tt.wantError {
  195. if err != nil {
  196. t.Fatalf("Got error %q, want no error", err)
  197. }
  198. continue
  199. }
  200. // Here, we want to see the error.
  201. if err == nil {
  202. t.Fatalf("Got no error; want error")
  203. }
  204. }
  205. }