substitute.scm 32 KB

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