keys_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. rpc "github.com/mikkeloscar/aur"
  8. gopkg "github.com/mikkeloscar/gopkgbuild"
  9. )
  10. func newPkg(basename string) *rpc.Pkg {
  11. return &rpc.Pkg{Name: basename, PackageBase: basename}
  12. }
  13. func newSplitPkg(basename, name string) *rpc.Pkg {
  14. return &rpc.Pkg{Name: name, PackageBase: basename}
  15. }
  16. func initTestKeyring() (string, error) {
  17. config.GpgBin = "gpg"
  18. tmpdir, err := ioutil.TempDir("/tmp", "yay-test-keyring")
  19. if err != nil {
  20. return "", err
  21. }
  22. return tmpdir, nil
  23. }
  24. func TestFormatKeysToImport(t *testing.T) {
  25. casetests := []struct {
  26. keySet pgpKeySet
  27. bases map[string][]*rpc.Pkg
  28. expected string
  29. alternate string
  30. wantError bool
  31. }{
  32. // Single key, required by single package.
  33. {
  34. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}},
  35. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo\n%s Import?", arrow),
  36. wantError: false,
  37. },
  38. // Single key, required by two packages.
  39. {
  40. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo"), newPkg("PKG-bar")}},
  41. expected: fmt.Sprintf("GPG keys need importing:\n\tKEY-1, required by: PKG-foo PKG-bar\n%s Import?", arrow),
  42. wantError: false,
  43. },
  44. // Two keys, each required by a single package. Since iterating the map
  45. // does not force any particular order, we cannot really predict the
  46. // order in which the elements will appear. As we have only two cases,
  47. // let's add the second possibility to the alternate variable, to check
  48. // if there are any errors.
  49. {
  50. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}, "KEY-2": []*rpc.Pkg{newPkg("PKG-bar")}},
  51. 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),
  52. 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),
  53. wantError: false,
  54. },
  55. // Two keys required by single package.
  56. {
  57. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}, "KEY-2": []*rpc.Pkg{newPkg("PKG-foo")}},
  58. 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),
  59. 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),
  60. wantError: false,
  61. },
  62. // Two keys, one of them required by two packages.
  63. {
  64. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo"), newPkg("PKG-bar")}, "KEY-2": []*rpc.Pkg{newPkg("PKG-bar")}},
  65. 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),
  66. 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),
  67. wantError: false,
  68. },
  69. // Two keys, split package (linux-ck/linux-ck-headers).
  70. {
  71. keySet: pgpKeySet{"ABAF11C65A2970B130ABE3C479BE3E4300411886": []*rpc.Pkg{newPkg("linux-ck")}, "647F28654894E3BD457199BE38DBBDC86092693E": []*rpc.Pkg{newPkg("linux-ck")}},
  72. bases: map[string][]*rpc.Pkg{"linux-ck": {newSplitPkg("linux-ck", "linux-ck-headers"), newPkg("linux-ck")}},
  73. 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),
  74. 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),
  75. wantError: false,
  76. },
  77. // One key, three split packages.
  78. {
  79. keySet: pgpKeySet{"KEY-1": []*rpc.Pkg{newPkg("PKG-foo")}},
  80. bases: map[string][]*rpc.Pkg{"PKG-foo": {newPkg("PKG-foo"), newSplitPkg("PKG-foo", "PKG-foo-1"), newSplitPkg("PKG-foo", "PKG-foo-2")}},
  81. 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),
  82. wantError: false,
  83. },
  84. // No keys, should fail.
  85. {
  86. keySet: pgpKeySet{},
  87. expected: "",
  88. wantError: true,
  89. },
  90. }
  91. for _, tt := range casetests {
  92. question, err := formatKeysToImport(tt.keySet, tt.bases)
  93. if !tt.wantError {
  94. if err != nil {
  95. t.Fatalf("Got error %q, want no error", err)
  96. }
  97. if question != tt.expected && question != tt.alternate {
  98. t.Fatalf("Got %q\n, expected: %q", question, tt.expected)
  99. }
  100. continue
  101. }
  102. // Here, we want to see the error.
  103. if err == nil {
  104. t.Fatalf("Got no error; want error")
  105. }
  106. }
  107. }
  108. func TestImportKeys(t *testing.T) {
  109. keyring, err := initTestKeyring()
  110. if err != nil {
  111. t.Fatalf("Unable to init test keyring %q: %v\n", keyring, err)
  112. }
  113. // Removing the leftovers.
  114. defer os.RemoveAll(keyring)
  115. keyringArgs := []string{"--homedir", keyring}
  116. casetests := []struct {
  117. keys []string
  118. wantError bool
  119. }{
  120. // Single key, should succeed.
  121. // C52048C0C0748FEE227D47A2702353E0F7E48EDB: Thomas Dickey.
  122. {
  123. keys: []string{"C52048C0C0748FEE227D47A2702353E0F7E48EDB"},
  124. wantError: false,
  125. },
  126. // Two keys, should succeed as well.
  127. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  128. // B6C8F98282B944E3B0D5C2530FC3042E345AD05D: Hans Wennborg.
  129. {
  130. keys: []string{"11E521D646982372EB577A1F8F0871F202119294",
  131. "B6C8F98282B944E3B0D5C2530FC3042E345AD05D"},
  132. wantError: false,
  133. },
  134. // Single invalid key, should fail.
  135. {
  136. keys: []string{"THIS-SHOULD-FAIL"},
  137. wantError: true,
  138. },
  139. // Two invalid keys, should fail.
  140. {
  141. keys: []string{"THIS-SHOULD-FAIL", "THIS-ONE-SHOULD-FAIL-TOO"},
  142. wantError: true,
  143. },
  144. // Invalid + valid key. Should fail as well.
  145. // 647F28654894E3BD457199BE38DBBDC86092693E: Greg Kroah-Hartman.
  146. {
  147. keys: []string{"THIS-SHOULD-FAIL",
  148. "647F28654894E3BD457199BE38DBBDC86092693E"},
  149. wantError: true,
  150. },
  151. }
  152. for _, tt := range casetests {
  153. err := importKeys(keyringArgs, tt.keys)
  154. if !tt.wantError {
  155. if err != nil {
  156. t.Fatalf("Got error %q, want no error", err)
  157. }
  158. continue
  159. }
  160. // Here, we want to see the error.
  161. if err == nil {
  162. t.Fatalf("Got no error; want error")
  163. }
  164. }
  165. }
  166. func TestCheckPgpKeys(t *testing.T) {
  167. keyring, err := initTestKeyring()
  168. if err != nil {
  169. t.Fatalf("Unable to init test keyring %q: %v\n", keyring, err)
  170. }
  171. // Removing the leftovers.
  172. defer os.RemoveAll(keyring)
  173. keyringArgs := []string{"--homedir", keyring}
  174. casetests := []struct {
  175. pkgs []*rpc.Pkg
  176. srcinfos map[string]*gopkg.PKGBUILD
  177. bases map[string][]*rpc.Pkg
  178. wantError bool
  179. }{
  180. // cower: single package, one valid key not yet in the keyring.
  181. // 487EACC08557AD082088DABA1EB2638FF56C0C53: Dave Reisner.
  182. {
  183. pkgs: []*rpc.Pkg{newPkg("cower")},
  184. srcinfos: map[string]*gopkg.PKGBUILD{"cower": &gopkg.PKGBUILD{Pkgbase: "cower", Validpgpkeys: []string{"487EACC08557AD082088DABA1EB2638FF56C0C53"}}},
  185. bases: map[string][]*rpc.Pkg{"cower": {newPkg("cower")}},
  186. wantError: false,
  187. },
  188. // libc++: single package, two valid keys not yet in the keyring.
  189. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  190. // B6C8F98282B944E3B0D5C2530FC3042E345AD05D: Hans Wennborg.
  191. {
  192. pkgs: []*rpc.Pkg{newPkg("libc++")},
  193. srcinfos: map[string]*gopkg.PKGBUILD{"libc++": &gopkg.PKGBUILD{Pkgbase: "libc++", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294", "B6C8F98282B944E3B0D5C2530FC3042E345AD05D"}}},
  194. bases: map[string][]*rpc.Pkg{"libc++": {newPkg("libc++")}},
  195. wantError: false,
  196. },
  197. // Two dummy packages requiring the same key.
  198. // ABAF11C65A2970B130ABE3C479BE3E4300411886: Linus Torvalds.
  199. {
  200. pkgs: []*rpc.Pkg{newPkg("dummy-1"), newPkg("dummy-2")},
  201. 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"}}},
  202. bases: map[string][]*rpc.Pkg{"dummy-1": {newPkg("dummy-1")}, "dummy-2": {newPkg("dummy-2")}},
  203. wantError: false,
  204. },
  205. // dummy package: single package, two valid keys, one of them already
  206. // in the keyring.
  207. // 11E521D646982372EB577A1F8F0871F202119294: Tom Stellard.
  208. // C52048C0C0748FEE227D47A2702353E0F7E48EDB: Thomas Dickey.
  209. {
  210. pkgs: []*rpc.Pkg{newPkg("dummy-3")},
  211. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-3": &gopkg.PKGBUILD{Pkgbase: "dummy-3", Validpgpkeys: []string{"11E521D646982372EB577A1F8F0871F202119294", "C52048C0C0748FEE227D47A2702353E0F7E48EDB"}}},
  212. bases: map[string][]*rpc.Pkg{"dummy-3": {newPkg("dummy-3")}},
  213. wantError: false,
  214. },
  215. // Two dummy packages with existing keys.
  216. {
  217. pkgs: []*rpc.Pkg{newPkg("dummy-4"), newPkg("dummy-5")},
  218. 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"}}},
  219. bases: map[string][]*rpc.Pkg{"dummy-4": {newPkg("dummy-4")}, "dummy-5": {newPkg("dummy-5")}},
  220. wantError: false,
  221. },
  222. // Dummy package with invalid key, should fail.
  223. {
  224. pkgs: []*rpc.Pkg{newPkg("dummy-7")},
  225. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-7": &gopkg.PKGBUILD{Pkgbase: "dummy-7", Validpgpkeys: []string{"THIS-SHOULD-FAIL"}}},
  226. bases: map[string][]*rpc.Pkg{"dummy-7": {newPkg("dummy-7")}},
  227. wantError: true,
  228. },
  229. // Dummy package with both an invalid an another valid key, should fail.
  230. // A314827C4E4250A204CE6E13284FC34C8E4B1A25: Thomas Bächler.
  231. {
  232. pkgs: []*rpc.Pkg{newPkg("dummy-8")},
  233. srcinfos: map[string]*gopkg.PKGBUILD{"dummy-8": &gopkg.PKGBUILD{Pkgbase: "dummy-8", Validpgpkeys: []string{"A314827C4E4250A204CE6E13284FC34C8E4B1A25", "THIS-SHOULD-FAIL"}}},
  234. bases: map[string][]*rpc.Pkg{"dummy-8": {newPkg("dummy-8")}},
  235. wantError: true,
  236. },
  237. }
  238. for _, tt := range casetests {
  239. err := checkPgpKeys(tt.pkgs, tt.srcinfos, tt.bases, keyringArgs)
  240. if !tt.wantError {
  241. if err != nil {
  242. t.Fatalf("Got error %q, want no error", err)
  243. }
  244. continue
  245. }
  246. // Here, we want to see the error.
  247. if err == nil {
  248. t.Fatalf("Got no error; want error")
  249. }
  250. }
  251. }