keys_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. rpc "github.com/mikkeloscar/aur"
  13. gopkg "github.com/mikkeloscar/gopkgbuild"
  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 TestCheckPgpKeys(t *testing.T) {
  113. keyringDir, err := ioutil.TempDir("/tmp", "yay-test-keyring")
  114. if err != nil {
  115. t.Fatalf("Unable to init test keyring: %v\n", err)
  116. }
  117. defer os.RemoveAll(keyringDir)
  118. config.GpgBin = "gpg"
  119. config.GpgFlags = fmt.Sprintf("--homedir %s --keyserver 127.0.0.1", keyringDir)
  120. server := startPgpKeyServer()
  121. defer server.Shutdown(context.TODO())
  122. casetests := []struct {
  123. pkgs []*rpc.Pkg
  124. srcinfos map[string]*gopkg.PKGBUILD
  125. bases map[string][]*rpc.Pkg
  126. wantError bool
  127. }{
  128. // cower: single package, one valid key not yet in the keyring.
  129. // 487EACC08557AD082088DABA1EB2638FF56C0C53: Dave Reisner.
  130. {
  131. pkgs: []*rpc.Pkg{newPkg("cower")},
  132. srcinfos: map[string]*gopkg.PKGBUILD{"cower": {Pkgbase: "cower", Validpgpkeys: []string{"487EACC08557AD082088DABA1EB2638FF56C0C53"}}},
  133. bases: map[string][]*rpc.Pkg{"cower": {newPkg("cower")}},
  134. wantError: false,
  135. },
  136. // libc++: single package, two valid keys not yet in the keyring.
  137. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  138. // B6C8F98282B944E3B0D5C2530FC3042E345AD05D: Hans Wennborg.
  139. {
  140. pkgs: []*rpc.Pkg{newPkg("libc++")},
  141. srcinfos: map[string]*gopkg.PKGBUILD{"libc++": {Pkgbase: "libc++", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294", "B6C8F98282B944E3B0D5C2530FC3042E345AD05D"}}},
  142. bases: map[string][]*rpc.Pkg{"libc++": {newPkg("libc++")}},
  143. wantError: false,
  144. },
  145. // Two dummy packages requiring the same key.
  146. // ABAF11C65A2970B130ABE3C479BE3E4300411886: Linus Torvalds.
  147. {
  148. pkgs: []*rpc.Pkg{newPkg("dummy-1"), newPkg("dummy-2")},
  149. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-1": {Pkgbase: "dummy-1", Validpgpkeys: []string{"ABAF11C65A2970B130ABE3C479BE3E4300411886"}}, "dummy-2": {Pkgbase: "dummy-2", Validpgpkeys: []string{"ABAF11C65A2970B130ABE3C479BE3E4300411886"}}},
  150. bases: map[string][]*rpc.Pkg{"dummy-1": {newPkg("dummy-1")}, "dummy-2": {newPkg("dummy-2")}},
  151. wantError: false,
  152. },
  153. // dummy package: single package, two valid keys, one of them already
  154. // in the keyring.
  155. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  156. // C52048C0C0748FEE227D47A2702353E0F7E48EDB: Thomas Dickey.
  157. {
  158. pkgs: []*rpc.Pkg{newPkg("dummy-3")},
  159. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-3": {Pkgbase: "dummy-3", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294", "C52048C0C0748FEE227D47A2702353E0F7E48EDB"}}},
  160. bases: map[string][]*rpc.Pkg{"dummy-3": {newPkg("dummy-3")}},
  161. wantError: false,
  162. },
  163. // Two dummy packages with existing keys.
  164. {
  165. pkgs: []*rpc.Pkg{newPkg("dummy-4"), newPkg("dummy-5")},
  166. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-4": {Pkgbase: "dummy-4", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294"}}, "dummy-5": {Pkgbase: "dummy-5", Validpgpkeys: []string{"C52048C0C0748FEE227D47A2702353E0F7E48EDB"}}},
  167. bases: map[string][]*rpc.Pkg{"dummy-4": {newPkg("dummy-4")}, "dummy-5": {newPkg("dummy-5")}},
  168. wantError: false,
  169. },
  170. // Dummy package with invalid key, should fail.
  171. {
  172. pkgs: []*rpc.Pkg{newPkg("dummy-7")},
  173. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-7": {Pkgbase: "dummy-7", Validpgpkeys: []string{"THIS-SHOULD-FAIL"}}},
  174. bases: map[string][]*rpc.Pkg{"dummy-7": {newPkg("dummy-7")}},
  175. wantError: true,
  176. },
  177. // Dummy package with both an invalid an another valid key, should fail.
  178. // A314827C4E4250A204CE6E13284FC34C8E4B1A25: Thomas Bächler.
  179. {
  180. pkgs: []*rpc.Pkg{newPkg("dummy-8")},
  181. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-8": {Pkgbase: "dummy-8", Validpgpkeys: []string{"A314827C4E4250A204CE6E13284FC34C8E4B1A25", "THIS-SHOULD-FAIL"}}},
  182. bases: map[string][]*rpc.Pkg{"dummy-8": {newPkg("dummy-8")}},
  183. wantError: true,
  184. },
  185. }
  186. for _, tt := range casetests {
  187. err := checkPgpKeys(tt.pkgs, tt.bases, tt.srcinfos)
  188. if !tt.wantError {
  189. if err != nil {
  190. t.Fatalf("Got error %q, want no error", err)
  191. }
  192. continue
  193. }
  194. // Here, we want to see the error.
  195. if err == nil {
  196. t.Fatalf("Got no error; want error")
  197. }
  198. }
  199. }