substitute.scm 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  3. ;;; Copyright © 2014-2015, 2017-2019, 2021-2022 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-substitute)
  20. #:use-module (guix scripts substitute)
  21. #:use-module (guix narinfo)
  22. #:use-module (guix base64)
  23. #:use-module (gcrypt hash)
  24. #:use-module (guix serialization)
  25. #:use-module (gcrypt pk-crypto)
  26. #:use-module (guix pki)
  27. #:use-module (guix config)
  28. #:use-module (guix base32)
  29. #:use-module ((guix store) #:select (%store-prefix))
  30. #:use-module ((guix ui) #:select (guix-warning-port))
  31. #:use-module ((guix utils)
  32. #:select (call-with-temporary-directory
  33. call-with-compressed-output-port))
  34. #:use-module ((guix build utils)
  35. #:select (mkdir-p delete-file-recursively dump-port))
  36. #:use-module (guix tests http)
  37. #:use-module (rnrs bytevectors)
  38. #:use-module (rnrs io ports)
  39. #:use-module (web uri)
  40. #:use-module (ice-9 regex)
  41. #:use-module (srfi srfi-11)
  42. #:use-module (srfi srfi-26)
  43. #:use-module (srfi srfi-34)
  44. #:use-module (srfi srfi-35)
  45. #:use-module ((srfi srfi-64) #:hide (test-error)))
  46. (define-syntax-rule (test-quit name error-rx exp)
  47. "Emit a test that passes when EXP throws to 'quit' with value 1, and when
  48. it writes to GUIX-WARNING-PORT a messages that matches ERROR-RX."
  49. (test-equal name
  50. '(1 #t)
  51. (let ((error-output (open-output-string)))
  52. (parameterize ((current-error-port error-output)
  53. (guix-warning-port error-output))
  54. (catch 'quit
  55. (lambda ()
  56. exp
  57. #f)
  58. (lambda (key value)
  59. (list value
  60. (let ((message (get-output-string error-output)))
  61. (->bool (string-match error-rx message))))))))))
  62. (define (request-substitution item destination)
  63. "Run 'guix substitute --substitute' to fetch ITEM to DESTINATION."
  64. (false-if-exception (delete-file destination))
  65. (with-input-from-string (string-append "substitute " item " "
  66. destination "\n")
  67. (lambda ()
  68. (guix-substitute "--substitute"))))
  69. (define %public-key
  70. ;; This key is known to be in the ACL by default.
  71. (call-with-input-file (string-append %config-directory "/signing-key.pub")
  72. (compose string->canonical-sexp get-string-all)))
  73. (define %private-key
  74. (call-with-input-file (string-append %config-directory "/signing-key.sec")
  75. (compose string->canonical-sexp get-string-all)))
  76. (define* (signature-body bv #:key (public-key %public-key))
  77. "Return the signature of BV as the base64-encoded body of a narinfo's
  78. 'Signature' field."
  79. (base64-encode
  80. (string->utf8
  81. (canonical-sexp->string
  82. (signature-sexp (bytevector->hash-data (sha256 bv)
  83. #:key-type 'rsa)
  84. %private-key
  85. public-key)))))
  86. (define %wrong-public-key
  87. (string->canonical-sexp "(public-key
  88. (rsa
  89. (n #00E05873AC2B168760343145918E954EE9AB73C026355693B192E01EE835261AA689E9EF46642E895BCD65C648524059FC450E4BA77A68F4C52D0E39EF0CC9359709AB6AAB153B63782201871325B0FDA19CB401CD99FD0C31A91CA9000AA90A77E82B89E036FB63BC1D3961207469B3B12468977148D376F8012BB12A4B11A8F1#)
  90. (e #010001#)
  91. )
  92. )"))
  93. (define* (signature-field bv-or-str
  94. #:key (version "1") (public-key %public-key))
  95. "Return the 'Signature' field value of bytevector/string BV-OR-STR, using
  96. PUBLIC-KEY as the signature's principal, and using VERSION as the signature
  97. version identifier.."
  98. (string-append version ";example.gnu.org;"
  99. (signature-body (if (string? bv-or-str)
  100. (string->utf8 bv-or-str)
  101. bv-or-str)
  102. #:public-key public-key)))
  103. (test-begin "substitute")
  104. (test-quit "not a number"
  105. "signature version"
  106. (narinfo-signature->canonical-sexp
  107. (signature-field "foo" #:version "not a number")))
  108. (test-quit "wrong version number"
  109. "unsupported.*version"
  110. (narinfo-signature->canonical-sexp
  111. (signature-field "foo" #:version "2")))
  112. (test-assert "valid narinfo-signature->canonical-sexp"
  113. (canonical-sexp? (narinfo-signature->canonical-sexp (signature-field "foo"))))
  114. (define %main-substitute-directory
  115. ;; The place where 'call-with-narinfo' stores its data by default.
  116. (uri-path (string->uri (getenv "GUIX_BINARY_SUBSTITUTE_URL"))))
  117. (define %alternate-substitute-directory
  118. ;; Another place.
  119. (string-append (dirname %main-substitute-directory)
  120. "/substituter-alt-data"))
  121. (define %unroutable-substitute-url
  122. ;; Substitute URL with an unroutable server address, as per
  123. ;; <https://www.rfc-editor.org/rfc/rfc5737>.
  124. "http://203.0.113.1")
  125. (define %narinfo
  126. ;; Skeleton of the narinfo used below.
  127. (string-append "StorePath: " (%store-prefix)
  128. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  129. URL: example.nar
  130. Compression: none
  131. NarHash: sha256:" (bytevector->nix-base32-string
  132. (sha256 (string->utf8 "Substitutable data."))) "
  133. NarSize: 42
  134. References: bar baz
  135. Deriver: " (%store-prefix) "/foo.drv
  136. System: mips64el-linux\n"))
  137. (define* (call-with-narinfo narinfo thunk
  138. #:optional
  139. (narinfo-directory %main-substitute-directory))
  140. "Call THUNK in a context where the directory at URL is populated with
  141. a file for NARINFO."
  142. (mkdir-p narinfo-directory)
  143. (let ((cache-directory (string-append (getenv "XDG_CACHE_HOME")
  144. "/guix/substitute/")))
  145. (dynamic-wind
  146. (lambda ()
  147. (when (file-exists? cache-directory)
  148. (delete-file-recursively cache-directory))
  149. (call-with-output-file (string-append narinfo-directory
  150. "/nix-cache-info")
  151. (lambda (port)
  152. (format port "StoreDir: ~a\nWantMassQuery: 0\n"
  153. (%store-prefix))))
  154. (call-with-output-file (string-append narinfo-directory "/"
  155. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  156. ".narinfo")
  157. (cut display narinfo <>))
  158. ;; Prepare the nar.
  159. (call-with-output-file
  160. (string-append narinfo-directory "/example.out")
  161. (cut display "Substitutable data." <>))
  162. (call-with-output-file
  163. (string-append narinfo-directory "/example.nar")
  164. (cute write-file
  165. (string-append narinfo-directory "/example.out") <>))
  166. (%allow-unauthenticated-substitutes? #f))
  167. thunk
  168. (lambda ()
  169. (when (file-exists? cache-directory)
  170. (delete-file-recursively cache-directory))))))
  171. (define-syntax-rule (with-narinfo narinfo body ...)
  172. (call-with-narinfo narinfo (lambda () body ...)))
  173. (define-syntax-rule (with-narinfo* narinfo directory body ...)
  174. (call-with-narinfo narinfo (lambda () body ...) directory))
  175. ;; Transmit these options to 'guix substitute'.
  176. (substitute-urls (list (getenv "GUIX_BINARY_SUBSTITUTE_URL")))
  177. ;; Never use file descriptor 4, unlike what happens when invoked by the
  178. ;; daemon.
  179. (%reply-file-descriptor #f)
  180. (test-equal "query narinfo without signature"
  181. "" ; not substitutable
  182. (with-narinfo %narinfo
  183. (string-trim-both
  184. (with-output-to-string
  185. (lambda ()
  186. (with-input-from-string (string-append "have " (%store-prefix)
  187. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  188. (lambda ()
  189. (guix-substitute "--query"))))))))
  190. (test-equal "query narinfo with invalid hash"
  191. ;; The hash in the signature differs from the hash of %NARINFO.
  192. ""
  193. (with-narinfo (string-append %narinfo "Signature: "
  194. (signature-field "different body")
  195. "\n")
  196. (string-trim-both
  197. (with-output-to-string
  198. (lambda ()
  199. (with-input-from-string (string-append "have " (%store-prefix)
  200. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  201. (lambda ()
  202. (guix-substitute "--query"))))))))
  203. (test-equal "query narinfo with signature over nothing"
  204. ;; The signature is computed over the empty string, not over the important
  205. ;; parts, so the narinfo must be ignored.
  206. ""
  207. (with-narinfo (string-append "Signature: " (signature-field "") "\n"
  208. %narinfo "\n")
  209. (string-trim-both
  210. (with-output-to-string
  211. (lambda ()
  212. (with-input-from-string (string-append "have " (%store-prefix)
  213. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  214. (lambda ()
  215. (guix-substitute "--query"))))))))
  216. (test-equal "query narinfo with signature over irrelevant bits"
  217. ;; The signature is valid but it does not cover the
  218. ;; StorePath/NarHash/References tuple and is thus irrelevant; the narinfo
  219. ;; must be ignored.
  220. ""
  221. (let ((prefix (string-append "StorePath: " (%store-prefix)
  222. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  223. URL: example.nar
  224. Compression: none\n")))
  225. (with-narinfo (string-append prefix
  226. "Signature: " (signature-field prefix) "
  227. NarHash: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  228. NarSize: 42
  229. References: bar baz
  230. Deriver: " (%store-prefix) "/foo.drv
  231. System: mips64el-linux\n")
  232. (string-trim-both
  233. (with-output-to-string
  234. (lambda ()
  235. (with-input-from-string (string-append "have " (%store-prefix)
  236. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  237. (lambda ()
  238. (guix-substitute "--query")))))))))
  239. (test-equal "query narinfo with signature over relevant subset"
  240. ;; The signature covers the StorePath/NarHash/References tuple, so it is
  241. ;; valid; it does not cover non-normative fields, which is fine.
  242. (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  243. (let ((prefix (string-append "StorePath: " (%store-prefix)
  244. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  245. NarHash: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  246. References: bar baz\n")))
  247. (with-narinfo (string-append prefix
  248. "Signature: " (signature-field prefix) "
  249. URL: example.nar
  250. Compression: none
  251. NarSize: 42
  252. Deriver: " (%store-prefix) "/foo.drv")
  253. (string-trim-both
  254. (with-output-to-string
  255. (lambda ()
  256. (with-input-from-string (string-append "have " (%store-prefix)
  257. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  258. (lambda ()
  259. (guix-substitute "--query")))))))))
  260. (test-equal "query narinfo signed with authorized key"
  261. (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  262. (with-narinfo (string-append %narinfo "Signature: "
  263. (signature-field %narinfo)
  264. "\n")
  265. (string-trim-both
  266. (with-output-to-string
  267. (lambda ()
  268. (with-input-from-string (string-append "have " (%store-prefix)
  269. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  270. (lambda ()
  271. (guix-substitute "--query"))))))))
  272. (test-equal "query narinfo signed with authorized key, unroutable URL first"
  273. (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  274. (with-narinfo (string-append %narinfo "Signature: "
  275. (signature-field %narinfo)
  276. "\n")
  277. (string-trim-both
  278. (with-output-to-string
  279. (lambda ()
  280. (with-input-from-string (string-append "have " (%store-prefix)
  281. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  282. (lambda ()
  283. (parameterize ((substitute-urls
  284. (list %unroutable-substitute-url
  285. (string-append "file://"
  286. %main-substitute-directory))))
  287. (guix-substitute "--query")))))))))
  288. (test-equal "query narinfo signed with unauthorized key"
  289. "" ; not substitutable
  290. (with-narinfo (string-append %narinfo "Signature: "
  291. (signature-field
  292. %narinfo
  293. #:public-key %wrong-public-key)
  294. "\n")
  295. (string-trim-both
  296. (with-output-to-string
  297. (lambda ()
  298. (with-input-from-string (string-append "have " (%store-prefix)
  299. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  300. (lambda ()
  301. (guix-substitute "--query"))))))))
  302. (test-quit "substitute, no signature"
  303. "no valid substitute"
  304. (with-narinfo %narinfo
  305. (with-input-from-string (string-append "substitute "
  306. (%store-prefix)
  307. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  308. " foo\n")
  309. (lambda ()
  310. (guix-substitute "--substitute")))))
  311. (test-quit "substitute, invalid narinfo hash"
  312. "no valid substitute"
  313. ;; The hash in the signature differs from the hash of %NARINFO.
  314. (with-narinfo (string-append %narinfo "Signature: "
  315. (signature-field "different body")
  316. "\n")
  317. (with-input-from-string (string-append "substitute "
  318. (%store-prefix)
  319. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  320. " foo\n")
  321. (lambda ()
  322. (guix-substitute "--substitute")))))
  323. (test-equal "substitute, invalid hash"
  324. (string-append "hash-mismatch sha256 "
  325. (bytevector->nix-base32-string (sha256 #vu8())) " "
  326. (let-values (((port get-hash)
  327. (open-hash-port (hash-algorithm sha256)))
  328. ((content)
  329. "Substitutable data."))
  330. (write-file-tree "foo" port
  331. #:file-type+size
  332. (lambda _
  333. (values 'regular
  334. (string-length content)))
  335. #:file-port
  336. (lambda _
  337. (open-input-string content)))
  338. (close-port port)
  339. (bytevector->nix-base32-string (get-hash)))
  340. "\n")
  341. ;; Arrange so the actual data hash does not match the 'NarHash' field in the
  342. ;; narinfo.
  343. (with-output-to-string
  344. (lambda ()
  345. (let ((narinfo (string-append "StorePath: " (%store-prefix)
  346. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-wrong-hash
  347. URL: example.nar
  348. Compression: none
  349. NarHash: sha256:" (bytevector->nix-base32-string (sha256 #vu8())) "
  350. NarSize: 42
  351. References:
  352. Deriver: " (%store-prefix) "/foo.drv
  353. System: mips64el-linux\n")))
  354. (with-narinfo (string-append narinfo "Signature: "
  355. (signature-field narinfo) "\n")
  356. (call-with-temporary-directory
  357. (lambda (directory)
  358. (with-input-from-string (string-append
  359. "substitute " (%store-prefix)
  360. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-wrong-hash "
  361. directory "/wrong-hash\n")
  362. (lambda ()
  363. (guix-substitute "--substitute"))))))))))
  364. (test-quit "substitute, unauthorized key"
  365. "no valid substitute"
  366. (with-narinfo (string-append %narinfo "Signature: "
  367. (signature-field
  368. %narinfo
  369. #:public-key %wrong-public-key)
  370. "\n")
  371. (with-input-from-string (string-append "substitute "
  372. (%store-prefix)
  373. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  374. " foo\n")
  375. (lambda ()
  376. (guix-substitute "--substitute")))))
  377. (test-equal "substitute, authorized key"
  378. '("Substitutable data." 1 #o444)
  379. (with-narinfo (string-append %narinfo "Signature: "
  380. (signature-field %narinfo))
  381. (dynamic-wind
  382. (const #t)
  383. (lambda ()
  384. (request-substitution (string-append (%store-prefix)
  385. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  386. "substitute-retrieved")
  387. (list (call-with-input-file "substitute-retrieved" get-string-all)
  388. (stat:mtime (lstat "substitute-retrieved"))
  389. (stat:perms (lstat "substitute-retrieved"))))
  390. (lambda ()
  391. (false-if-exception (delete-file "substitute-retrieved"))))))
  392. (test-equal "substitute, authorized key, first substitute URL is unroutable"
  393. '("Substitutable data." 1 #o444)
  394. (with-narinfo (string-append %narinfo "Signature: "
  395. (signature-field %narinfo))
  396. (dynamic-wind
  397. (const #t)
  398. (lambda ()
  399. ;; Pick an unroutable URL as the first one. This shouldn't be a
  400. ;; problem.
  401. (parameterize ((substitute-urls
  402. (list %unroutable-substitute-url
  403. (string-append "file://"
  404. %main-substitute-directory))))
  405. (request-substitution (string-append (%store-prefix)
  406. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  407. "substitute-retrieved")
  408. (list (call-with-input-file "substitute-retrieved" get-string-all)
  409. (stat:mtime (lstat "substitute-retrieved"))
  410. (stat:perms (lstat "substitute-retrieved")))))
  411. (lambda ()
  412. (false-if-exception (delete-file "substitute-retrieved"))))))
  413. (test-equal "substitute, unauthorized narinfo comes first"
  414. "Substitutable data."
  415. (with-narinfo*
  416. (string-append %narinfo "Signature: "
  417. (signature-field
  418. %narinfo
  419. #:public-key %wrong-public-key))
  420. %alternate-substitute-directory
  421. (with-narinfo* (string-append %narinfo "Signature: "
  422. (signature-field %narinfo))
  423. %main-substitute-directory
  424. (dynamic-wind
  425. (const #t)
  426. (lambda ()
  427. ;; Remove this file so that the substitute can only be retrieved
  428. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  429. (delete-file (string-append %main-substitute-directory
  430. "/example.nar"))
  431. (parameterize ((substitute-urls
  432. (map (cut string-append "file://" <>)
  433. (list %alternate-substitute-directory
  434. %main-substitute-directory))))
  435. (request-substitution (string-append (%store-prefix)
  436. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  437. "substitute-retrieved"))
  438. (call-with-input-file "substitute-retrieved" get-string-all))
  439. (lambda ()
  440. (false-if-exception (delete-file "substitute-retrieved")))))))
  441. (test-equal "substitute, unsigned narinfo comes first"
  442. "Substitutable data."
  443. (with-narinfo* %narinfo ;not signed!
  444. %alternate-substitute-directory
  445. (with-narinfo* (string-append %narinfo "Signature: "
  446. (signature-field %narinfo))
  447. %main-substitute-directory
  448. (dynamic-wind
  449. (const #t)
  450. (lambda ()
  451. ;; Remove this file so that the substitute can only be retrieved
  452. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  453. (delete-file (string-append %main-substitute-directory
  454. "/example.nar"))
  455. (parameterize ((substitute-urls
  456. (map (cut string-append "file://" <>)
  457. (list %alternate-substitute-directory
  458. %main-substitute-directory))))
  459. (request-substitution (string-append (%store-prefix)
  460. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  461. "substitute-retrieved"))
  462. (call-with-input-file "substitute-retrieved" get-string-all))
  463. (lambda ()
  464. (false-if-exception (delete-file "substitute-retrieved")))))))
  465. (test-equal "substitute, first URL has narinfo but lacks nar, second URL unauthorized"
  466. "Substitutable data."
  467. (with-narinfo*
  468. (string-append %narinfo "Signature: "
  469. (signature-field
  470. %narinfo
  471. #:public-key %wrong-public-key))
  472. %alternate-substitute-directory
  473. (with-narinfo* (string-append %narinfo "Signature: "
  474. (signature-field %narinfo))
  475. %main-substitute-directory
  476. (dynamic-wind
  477. (const #t)
  478. (lambda ()
  479. ;; Remove this file so that the substitute can only be retrieved
  480. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  481. (delete-file (string-append %main-substitute-directory
  482. "/example.nar"))
  483. (parameterize ((substitute-urls
  484. (map (cut string-append "file://" <>)
  485. (list %main-substitute-directory
  486. %alternate-substitute-directory))))
  487. (request-substitution (string-append (%store-prefix)
  488. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  489. "substitute-retrieved"))
  490. (call-with-input-file "substitute-retrieved" get-string-all))
  491. (lambda ()
  492. (false-if-exception (delete-file "substitute-retrieved")))))))
  493. (test-equal "substitute, first URL has narinfo but nar is 404, both URLs authorized"
  494. "Substitutable data."
  495. (with-narinfo*
  496. (string-append %narinfo "Signature: "
  497. (signature-field %narinfo))
  498. %main-substitute-directory
  499. (with-http-server `((200 ,(string-append %narinfo "Signature: "
  500. (signature-field %narinfo)))
  501. (404 "Sorry, nar is missing!"))
  502. (dynamic-wind
  503. (const #t)
  504. (lambda ()
  505. (parameterize ((substitute-urls
  506. (list (%local-url)
  507. (string-append "file://"
  508. %main-substitute-directory))))
  509. (request-substitution (string-append (%store-prefix)
  510. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  511. "substitute-retrieved"))
  512. (call-with-input-file "substitute-retrieved" get-string-all))
  513. (lambda ()
  514. (false-if-exception (delete-file "substitute-retrieved")))))))
  515. (test-equal "substitute, first URL has narinfo but nar is 404, one URL authorized"
  516. "Substitutable data."
  517. (with-narinfo*
  518. (string-append %narinfo "Signature: "
  519. (signature-field
  520. %narinfo
  521. #:public-key %wrong-public-key))
  522. %main-substitute-directory
  523. (with-http-server `((200 ,(string-append %narinfo "Signature: "
  524. (signature-field
  525. %narinfo
  526. #:public-key %wrong-public-key)))
  527. (404 "Sorry, nar is missing!"))
  528. (let ((url1 (%local-url)))
  529. (parameterize ((%http-server-port 0))
  530. (with-http-server `((200 ,(string-append %narinfo "Signature: "
  531. (signature-field %narinfo)))
  532. (404 "Sorry, nar is missing!"))
  533. (let ((url2 (%local-url)))
  534. (dynamic-wind
  535. (const #t)
  536. (lambda ()
  537. (parameterize ((substitute-urls
  538. (list url1 url2
  539. (string-append "file://"
  540. %main-substitute-directory))))
  541. (request-substitution (string-append (%store-prefix)
  542. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  543. "substitute-retrieved"))
  544. (call-with-input-file "substitute-retrieved" get-string-all))
  545. (lambda ()
  546. (false-if-exception (delete-file "substitute-retrieved")))))))))))
  547. (test-equal "substitute, preferred nar URL is 404, other is 200"
  548. "Substitutable data."
  549. (with-narinfo* (string-append %narinfo "Signature: " (signature-field %narinfo))
  550. %main-substitute-directory
  551. (with-http-server `((200 ,(string-append %narinfo "Signature: "
  552. (signature-field %narinfo)
  553. "\n"
  554. "URL: example.nar.lz\n"
  555. "Compression: lzip\n"))
  556. (404 "Sorry, nar.lz is missing!")
  557. (200 ,(call-with-input-file
  558. (string-append %main-substitute-directory
  559. "/example.nar")
  560. get-bytevector-all)))
  561. (dynamic-wind
  562. (const #t)
  563. (lambda ()
  564. (parameterize ((substitute-urls (list (%local-url))))
  565. (request-substitution (string-append (%store-prefix)
  566. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  567. "substitute-retrieved"))
  568. (call-with-input-file "substitute-retrieved" get-string-all))
  569. (lambda ()
  570. (false-if-exception (delete-file "substitute-retrieved")))))))
  571. (test-quit "substitute, narinfo is available but nar is missing"
  572. "failed to find alternative substitute"
  573. (with-narinfo*
  574. (string-append %narinfo "Signature: "
  575. (signature-field
  576. %narinfo
  577. #:public-key %wrong-public-key))
  578. %main-substitute-directory
  579. (with-http-server `((200 ,(string-append %narinfo "Signature: "
  580. (signature-field %narinfo)))
  581. (404 "Sorry, nar is missing!"))
  582. (parameterize ((substitute-urls
  583. (list (%local-url)
  584. (string-append "file://"
  585. %main-substitute-directory))))
  586. (delete-file (string-append %main-substitute-directory
  587. "/example.nar"))
  588. (request-substitution (string-append (%store-prefix)
  589. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  590. "substitute-retrieved")
  591. (not (file-exists? "substitute-retrieved"))))))
  592. (test-equal "substitute, first narinfo is unsigned and has wrong hash"
  593. "Substitutable data."
  594. (with-narinfo* (regexp-substitute #f
  595. (string-match "NarHash: [[:graph:]]+"
  596. %narinfo)
  597. 'pre
  598. "NarHash: sha256:"
  599. (bytevector->nix-base32-string
  600. (make-bytevector 32))
  601. 'post)
  602. %alternate-substitute-directory
  603. (with-narinfo* (string-append %narinfo "Signature: "
  604. (signature-field %narinfo))
  605. %main-substitute-directory
  606. (dynamic-wind
  607. (const #t)
  608. (lambda ()
  609. ;; This time remove the file so that the substitute can only be
  610. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  611. (delete-file (string-append %alternate-substitute-directory
  612. "/example.nar"))
  613. (parameterize ((substitute-urls
  614. (map (cut string-append "file://" <>)
  615. (list %alternate-substitute-directory
  616. %main-substitute-directory))))
  617. (request-substitution (string-append (%store-prefix)
  618. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  619. "substitute-retrieved"))
  620. (call-with-input-file "substitute-retrieved" get-string-all))
  621. (lambda ()
  622. (false-if-exception (delete-file "substitute-retrieved")))))))
  623. (test-equal "substitute, first narinfo is unsigned and has wrong refs"
  624. "Substitutable data."
  625. (with-narinfo* (regexp-substitute #f
  626. (string-match "References: ([^\n]+)\n"
  627. %narinfo)
  628. 'pre "References: " 1
  629. " wrong set of references\n"
  630. 'post)
  631. %alternate-substitute-directory
  632. (with-narinfo* (string-append %narinfo "Signature: "
  633. (signature-field %narinfo))
  634. %main-substitute-directory
  635. (dynamic-wind
  636. (const #t)
  637. (lambda ()
  638. ;; This time remove the file so that the substitute can only be
  639. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  640. (delete-file (string-append %alternate-substitute-directory
  641. "/example.nar"))
  642. (parameterize ((substitute-urls
  643. (map (cut string-append "file://" <>)
  644. (list %alternate-substitute-directory
  645. %main-substitute-directory))))
  646. (request-substitution (string-append (%store-prefix)
  647. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  648. "substitute-retrieved"))
  649. (call-with-input-file "substitute-retrieved" get-string-all))
  650. (lambda ()
  651. (false-if-exception (delete-file "substitute-retrieved")))))))
  652. (test-quit "substitute, two invalid narinfos"
  653. "no valid substitute"
  654. (with-narinfo* %narinfo ;not signed
  655. %alternate-substitute-directory
  656. (with-narinfo* (string-append %narinfo "Signature: " ;unauthorized
  657. (signature-field
  658. %narinfo
  659. #:public-key %wrong-public-key))
  660. %main-substitute-directory
  661. (with-input-from-string (string-append "substitute "
  662. (%store-prefix)
  663. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  664. " substitute-retrieved\n")
  665. (lambda ()
  666. (guix-substitute "--substitute"))))))
  667. (test-equal "substitute, narinfo with several URLs"
  668. "Substitutable data."
  669. (let ((narinfo (string-append "StorePath: " (%store-prefix)
  670. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  671. URL: example.nar.gz
  672. Compression: gzip
  673. URL: example.nar.lz
  674. Compression: lzip
  675. URL: example.nar
  676. Compression: none
  677. NarHash: sha256:" (bytevector->nix-base32-string
  678. (sha256 (string->utf8 "Substitutable data."))) "
  679. NarSize: 42
  680. References: bar baz
  681. Deriver: " (%store-prefix) "/foo.drv
  682. System: mips64el-linux\n")))
  683. (with-narinfo (string-append narinfo "Signature: "
  684. (signature-field narinfo))
  685. (dynamic-wind
  686. (const #t)
  687. (lambda ()
  688. (define (compress input output compression)
  689. (call-with-output-file output
  690. (lambda (port)
  691. (call-with-compressed-output-port compression port
  692. (lambda (port)
  693. (call-with-input-file input
  694. (lambda (input)
  695. (dump-port input port))))))))
  696. (let ((nar (string-append %main-substitute-directory
  697. "/example.nar")))
  698. (compress nar (string-append nar ".gz") 'gzip)
  699. (compress nar (string-append nar ".lz") 'lzip))
  700. (parameterize ((substitute-urls
  701. (list (string-append "file://"
  702. %main-substitute-directory))))
  703. (request-substitution (string-append (%store-prefix)
  704. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  705. "substitute-retrieved"))
  706. (call-with-input-file "substitute-retrieved" get-string-all))
  707. (lambda ()
  708. (false-if-exception (delete-file "substitute-retrieved")))))))
  709. (test-end "substitute")
  710. ;;; Local Variables:
  711. ;;; eval: (put 'with-narinfo 'scheme-indent-function 1)
  712. ;;; eval: (put 'with-narinfo* 'scheme-indent-function 2)
  713. ;;; eval: (put 'test-quit 'scheme-indent-function 2)
  714. ;;; End: