keys_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "path"
  9. "regexp"
  10. "testing"
  11. rpc "github.com/mikkeloscar/aur"
  12. gopkg "github.com/mikkeloscar/gopkgbuild"
  13. )
  14. const (
  15. // The default port used by the PGP key server.
  16. gpgServerPort = 11371
  17. )
  18. func init() {
  19. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  20. regex := regexp.MustCompile(`search=0[xX]([a-fA-F0-9]+)`)
  21. matches := regex.FindStringSubmatch(r.RequestURI)
  22. data := ""
  23. if matches != nil {
  24. data = getPgpKey(matches[1])
  25. }
  26. w.Header().Set("Content-Type", "application/pgp-keys")
  27. w.Write([]byte(data))
  28. })
  29. }
  30. func newPkg(basename string) *rpc.Pkg {
  31. return &rpc.Pkg{Name: basename, PackageBase: basename}
  32. }
  33. func newSplitPkg(basename, name string) *rpc.Pkg {
  34. return &rpc.Pkg{Name: name, PackageBase: basename}
  35. }
  36. func getPgpKey(key string) string {
  37. var buffer bytes.Buffer
  38. if contents, err := ioutil.ReadFile(path.Join("testdata", "keys", key)); err == nil {
  39. buffer.WriteString("-----BEGIN PGP PUBLIC KEY BLOCK-----\n")
  40. buffer.WriteString("Version: SKS 1.1.6\n")
  41. buffer.WriteString("Comment: Hostname: yay\n\n")
  42. buffer.Write(contents)
  43. buffer.WriteString("\n-----END PGP PUBLIC KEY BLOCK-----\n")
  44. }
  45. return buffer.String()
  46. }
  47. func startPgpKeyServer() *http.Server {
  48. srv := &http.Server{Addr: fmt.Sprintf("127.0.0.1:%d", gpgServerPort)}
  49. go func() {
  50. srv.ListenAndServe()
  51. }()
  52. return srv
  53. }
  54. func TestFormatKeysToImport(t *testing.T) {
  55. casetests := []struct {
  56. keySet pgpKeySet
  57. bases map[string][]*rpc.Pkg
  58. expected string
  59. alternate string
  60. wantError bool
  61. }{
  62. // Single key, required by single package.
  63. {
  64. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}},
  65. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo\n%s Import?", arrow),
  66. wantError: false,
  67. },
  68. // Single key, required by two packages.
  69. {
  70. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo"), newPkg("PKG-bar")}},
  71. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo PKG-bar\n%s Import?", arrow),
  72. wantError: false,
  73. },
  74. // Two keys, each required by a single package. Since iterating the map
  75. // does not force any particular order, we cannot really predict the
  76. // order in which the elements will appear. As we have only two cases,
  77. // let's add the second possibility to the alternate variable, to check
  78. // if there are any errors.
  79. {
  80. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}, "KEY-2": []*rpc.Pkg{newPkg("PKG-bar")}},
  81. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo\n\tKEY-2, required by: PKG-bar\n%s Import?", arrow),
  82. alternate: fmt.Sprintf("GPG keys need importing:\n\tKEY-2, required by: PKG-bar\n\tKEY-1, required by: PKG-foo\n%s Import?", arrow),
  83. wantError: false,
  84. },
  85. // Two keys required by single package.
  86. {
  87. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}, "KEY-2": []*rpc.Pkg{newPkg("PKG-foo")}},
  88. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo\n\tKEY-2, required by: PKG-foo\n%s Import?", arrow),
  89. alternate: fmt.Sprintf("GPG keys need importing:\n\tKEY-2, required by: PKG-foo\n\tKEY-1, required by: PKG-foo\n%s Import?", arrow),
  90. wantError: false,
  91. },
  92. // Two keys, one of them required by two packages.
  93. {
  94. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo"), newPkg("PKG-bar")}, "KEY-2": []*rpc.Pkg{newPkg("PKG-bar")}},
  95. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo PKG-bar\n\tKEY-2, required by: PKG-bar\n%s Import?", arrow),
  96. alternate: fmt.Sprintf("GPG keys need importing:\n\tKEY-2, required by: PKG-bar\n\tKEY-1, required by: PKG-foo PKG-bar\n%s Import?", arrow),
  97. wantError: false,
  98. },
  99. // Two keys, split package (linux-ck/linux-ck-headers).
  100. {
  101. keySet: pgpKeySet{"ABAF11C65A2970B130ABE3C479BE3E4300411886": []*rpc.Pkg{newPkg("linux-ck")}, "647F28654894E3BD457199BE38DBBDC86092693E": []*rpc.Pkg{newPkg("linux-ck")}},
  102. bases: map[string][]*rpc.Pkg{"linux-ck": {newSplitPkg("linux-ck", "linux-ck-headers"), newPkg("linux-ck")}},
  103. expected: fmt.Sprintf("GPG keys need importing:\n\tABAF11C65A2970B130ABE3C479BE3E4300411886, required by: linux-ck (linux-ck-headers linux-ck)\n\t647F28654894E3BD457199BE38DBBDC86092693E, required by: linux-ck (linux-ck-headers linux-ck)\n%s Import?", arrow),
  104. alternate: fmt.Sprintf("GPG keys need importing:\n\t647F28654894E3BD457199BE38DBBDC86092693E, required by: linux-ck (linux-ck-headers linux-ck)\n\tABAF11C65A2970B130ABE3C479BE3E4300411886, required by: linux-ck (linux-ck-headers linux-ck)\n%s Import?", arrow),
  105. wantError: false,
  106. },
  107. // One key, three split packages.
  108. {
  109. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}},
  110. bases: map[string][]*rpc.Pkg{"PKG-foo": {newPkg("PKG-foo"), newSplitPkg("PKG-foo", "PKG-foo-1"), newSplitPkg("PKG-foo", "PKG-foo-2")}},
  111. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo (PKG-foo PKG-foo-1 PKG-foo-2)\n%s Import?", arrow),
  112. wantError: false,
  113. },
  114. // No keys, should fail.
  115. {
  116. keySet: pgpKeySet{},
  117. expected: "",
  118. wantError: true,
  119. },
  120. }
  121. for _, tt := range casetests {
  122. question, err := formatKeysToImport(tt.keySet, tt.bases)
  123. if !tt.wantError {
  124. if err != nil {
  125. t.Fatalf("Got error %q, want no error", err)
  126. }
  127. if question != tt.expected && question != tt.alternate {
  128. t.Fatalf("Got %q\n, expected: %q", question, tt.expected)
  129. }
  130. continue
  131. }
  132. // Here, we want to see the error.
  133. if err == nil {
  134. t.Fatalf("Got no error; want error")
  135. }
  136. }
  137. }
  138. func TestImportKeys(t *testing.T) {
  139. keyringDir, err := ioutil.TempDir("/tmp", "yay-test-keyring")
  140. if err != nil {
  141. t.Fatalf("Unable to init test keyring %q: %v\n", keyringDir, err)
  142. }
  143. defer os.RemoveAll(keyringDir)
  144. config.GpgBin = "gpg"
  145. config.GpgFlags = fmt.Sprintf("--homedir %s --keyserver 127.0.0.1", keyringDir)
  146. server := startPgpKeyServer()
  147. defer server.Shutdown(nil)
  148. casetests := []struct {
  149. keys []string
  150. wantError bool
  151. }{
  152. // Single key, should succeed.
  153. // C52048C0C0748FEE227D47A2702353E0F7E48EDB: Thomas Dickey.
  154. {
  155. keys: []string{"C52048C0C0748FEE227D47A2702353E0F7E48EDB"},
  156. wantError: false,
  157. },
  158. // Two keys, should succeed as well.
  159. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  160. // B6C8F98282B944E3B0D5C2530FC3042E345AD05D: Hans Wennborg.
  161. {
  162. keys: []string{"11E521D646982372EB577A1F8F0871F202119294",
  163. "B6C8F98282B944E3B0D5C2530FC3042E345AD05D"},
  164. wantError: false,
  165. },
  166. // Single invalid key, should fail.
  167. {
  168. keys: []string{"THIS-SHOULD-FAIL"},
  169. wantError: true,
  170. },
  171. // Two invalid keys, should fail.
  172. {
  173. keys: []string{"THIS-SHOULD-FAIL", "THIS-ONE-SHOULD-FAIL-TOO"},
  174. wantError: true,
  175. },
  176. // Invalid + valid key. Should fail as well.
  177. // 647F28654894E3BD457199BE38DBBDC86092693E: Greg Kroah-Hartman.
  178. {
  179. keys: []string{"THIS-SHOULD-FAIL",
  180. "647F28654894E3BD457199BE38DBBDC86092693E"},
  181. wantError: true,
  182. },
  183. }
  184. for _, tt := range casetests {
  185. err := importKeys(tt.keys)
  186. if !tt.wantError {
  187. if err != nil {
  188. t.Fatalf("Got error %q, want no error", err)
  189. }
  190. continue
  191. }
  192. // Here, we want to see the error.
  193. if err == nil {
  194. t.Fatalf("Got no error; want error")
  195. }
  196. }
  197. }
  198. func TestCheckPgpKeys(t *testing.T) {
  199. keyringDir, err := ioutil.TempDir("/tmp", "yay-test-keyring")
  200. if err != nil {
  201. t.Fatalf("Unable to init test keyring: %v\n", err)
  202. }
  203. defer os.RemoveAll(keyringDir)
  204. config.GpgBin = "gpg"
  205. config.GpgFlags = fmt.Sprintf("--homedir %s --keyserver 127.0.0.1", keyringDir)
  206. server := startPgpKeyServer()
  207. defer server.Shutdown(nil)
  208. casetests := []struct {
  209. pkgs []*rpc.Pkg
  210. srcinfos map[string]*gopkg.PKGBUILD
  211. bases map[string][]*rpc.Pkg
  212. wantError bool
  213. }{
  214. // cower: single package, one valid key not yet in the keyring.
  215. // 487EACC08557AD082088DABA1EB2638FF56C0C53: Dave Reisner.
  216. {
  217. pkgs: []*rpc.Pkg{newPkg("cower")},
  218. srcinfos: map[string]*gopkg.PKGBUILD{"cower": &gopkg.PKGBUILD{Pkgbase: "cower", Validpgpkeys: []string{"487EACC08557AD082088DABA1EB2638FF56C0C53"}}},
  219. bases: map[string][]*rpc.Pkg{"cower": {newPkg("cower")}},
  220. wantError: false,
  221. },
  222. // libc++: single package, two valid keys not yet in the keyring.
  223. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  224. // B6C8F98282B944E3B0D5C2530FC3042E345AD05D: Hans Wennborg.
  225. {
  226. pkgs: []*rpc.Pkg{newPkg("libc++")},
  227. srcinfos: map[string]*gopkg.PKGBUILD{"libc++": &gopkg.PKGBUILD{Pkgbase: "libc++", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294", "B6C8F98282B944E3B0D5C2530FC3042E345AD05D"}}},
  228. bases: map[string][]*rpc.Pkg{"libc++": {newPkg("libc++")}},
  229. wantError: false,
  230. },
  231. // Two dummy packages requiring the same key.
  232. // ABAF11C65A2970B130ABE3C479BE3E4300411886: Linus Torvalds.
  233. {
  234. pkgs: []*rpc.Pkg{newPkg("dummy-1"), newPkg("dummy-2")},
  235. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-1": &gopkg.PKGBUILD{Pkgbase: "dummy-1", Validpgpkeys: []string{"ABAF11C65A2970B130ABE3C479BE3E4300411886"}}, "dummy-2": &gopkg.PKGBUILD{Pkgbase: "dummy-2", Validpgpkeys: []string{"ABAF11C65A2970B130ABE3C479BE3E4300411886"}}},
  236. bases: map[string][]*rpc.Pkg{"dummy-1": {newPkg("dummy-1")}, "dummy-2": {newPkg("dummy-2")}},
  237. wantError: false,
  238. },
  239. // dummy package: single package, two valid keys, one of them already
  240. // in the keyring.
  241. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  242. // C52048C0C0748FEE227D47A2702353E0F7E48EDB: Thomas Dickey.
  243. {
  244. pkgs: []*rpc.Pkg{newPkg("dummy-3")},
  245. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-3": &gopkg.PKGBUILD{Pkgbase: "dummy-3", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294", "C52048C0C0748FEE227D47A2702353E0F7E48EDB"}}},
  246. bases: map[string][]*rpc.Pkg{"dummy-3": {newPkg("dummy-3")}},
  247. wantError: false,
  248. },
  249. // Two dummy packages with existing keys.
  250. {
  251. pkgs: []*rpc.Pkg{newPkg("dummy-4"), newPkg("dummy-5")},
  252. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-4": &gopkg.PKGBUILD{Pkgbase: "dummy-4", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294"}}, "dummy-5": &gopkg.PKGBUILD{Pkgbase: "dummy-5", Validpgpkeys: []string{"C52048C0C0748FEE227D47A2702353E0F7E48EDB"}}},
  253. bases: map[string][]*rpc.Pkg{"dummy-4": {newPkg("dummy-4")}, "dummy-5": {newPkg("dummy-5")}},
  254. wantError: false,
  255. },
  256. // Dummy package with invalid key, should fail.
  257. {
  258. pkgs: []*rpc.Pkg{newPkg("dummy-7")},
  259. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-7": &gopkg.PKGBUILD{Pkgbase: "dummy-7", Validpgpkeys: []string{"THIS-SHOULD-FAIL"}}},
  260. bases: map[string][]*rpc.Pkg{"dummy-7": {newPkg("dummy-7")}},
  261. wantError: true,
  262. },
  263. // Dummy package with both an invalid an another valid key, should fail.
  264. // A314827C4E4250A204CE6E13284FC34C8E4B1A25: Thomas Bächler.
  265. {
  266. pkgs: []*rpc.Pkg{newPkg("dummy-8")},
  267. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-8": &gopkg.PKGBUILD{Pkgbase: "dummy-8", Validpgpkeys: []string{"A314827C4E4250A204CE6E13284FC34C8E4B1A25", "THIS-SHOULD-FAIL"}}},
  268. bases: map[string][]*rpc.Pkg{"dummy-8": {newPkg("dummy-8")}},
  269. wantError: true,
  270. },
  271. }
  272. for _, tt := range casetests {
  273. err := checkPgpKeys(tt.pkgs, tt.bases, tt.srcinfos)
  274. if !tt.wantError {
  275. if err != nil {
  276. t.Fatalf("Got error %q, want no error", err)
  277. }
  278. continue
  279. }
  280. // Here, we want to see the error.
  281. if err == nil {
  282. t.Fatalf("Got no error; want error")
  283. }
  284. }
  285. }