substitute.scm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. (parameterize ((guix-warning-port (current-error-port)))
  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 %narinfo
  122. ;; Skeleton of the narinfo used below.
  123. (string-append "StorePath: " (%store-prefix)
  124. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  125. URL: example.nar
  126. Compression: none
  127. NarHash: sha256:" (bytevector->nix-base32-string
  128. (sha256 (string->utf8 "Substitutable data."))) "
  129. NarSize: 42
  130. References: bar baz
  131. Deriver: " (%store-prefix) "/foo.drv
  132. System: mips64el-linux\n"))
  133. (define* (call-with-narinfo narinfo thunk
  134. #:optional
  135. (narinfo-directory %main-substitute-directory))
  136. "Call THUNK in a context where the directory at URL is populated with
  137. a file for NARINFO."
  138. (mkdir-p narinfo-directory)
  139. (let ((cache-directory (string-append (getenv "XDG_CACHE_HOME")
  140. "/guix/substitute/")))
  141. (dynamic-wind
  142. (lambda ()
  143. (when (file-exists? cache-directory)
  144. (delete-file-recursively cache-directory))
  145. (call-with-output-file (string-append narinfo-directory
  146. "/nix-cache-info")
  147. (lambda (port)
  148. (format port "StoreDir: ~a\nWantMassQuery: 0\n"
  149. (%store-prefix))))
  150. (call-with-output-file (string-append narinfo-directory "/"
  151. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  152. ".narinfo")
  153. (cut display narinfo <>))
  154. ;; Prepare the nar.
  155. (call-with-output-file
  156. (string-append narinfo-directory "/example.out")
  157. (cut display "Substitutable data." <>))
  158. (call-with-output-file
  159. (string-append narinfo-directory "/example.nar")
  160. (cute write-file
  161. (string-append narinfo-directory "/example.out") <>))
  162. (%allow-unauthenticated-substitutes? #f))
  163. thunk
  164. (lambda ()
  165. (when (file-exists? cache-directory)
  166. (delete-file-recursively cache-directory))))))
  167. (define-syntax-rule (with-narinfo narinfo body ...)
  168. (call-with-narinfo narinfo (lambda () body ...)))
  169. (define-syntax-rule (with-narinfo* narinfo directory body ...)
  170. (call-with-narinfo narinfo (lambda () body ...) directory))
  171. ;; Transmit these options to 'guix substitute'.
  172. (substitute-urls (list (getenv "GUIX_BINARY_SUBSTITUTE_URL")))
  173. ;; Never use file descriptor 4, unlike what happens when invoked by the
  174. ;; daemon.
  175. (%reply-file-descriptor #f)
  176. (test-equal "query narinfo without signature"
  177. "" ; not substitutable
  178. (with-narinfo %narinfo
  179. (string-trim-both
  180. (with-output-to-string
  181. (lambda ()
  182. (with-input-from-string (string-append "have " (%store-prefix)
  183. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  184. (lambda ()
  185. (guix-substitute "--query"))))))))
  186. (test-equal "query narinfo with invalid hash"
  187. ;; The hash in the signature differs from the hash of %NARINFO.
  188. ""
  189. (with-narinfo (string-append %narinfo "Signature: "
  190. (signature-field "different body")
  191. "\n")
  192. (string-trim-both
  193. (with-output-to-string
  194. (lambda ()
  195. (with-input-from-string (string-append "have " (%store-prefix)
  196. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  197. (lambda ()
  198. (guix-substitute "--query"))))))))
  199. (test-equal "query narinfo with signature over nothing"
  200. ;; The signature is computed over the empty string, not over the important
  201. ;; parts, so the narinfo must be ignored.
  202. ""
  203. (with-narinfo (string-append "Signature: " (signature-field "") "\n"
  204. %narinfo "\n")
  205. (string-trim-both
  206. (with-output-to-string
  207. (lambda ()
  208. (with-input-from-string (string-append "have " (%store-prefix)
  209. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  210. (lambda ()
  211. (guix-substitute "--query"))))))))
  212. (test-equal "query narinfo with signature over irrelevant bits"
  213. ;; The signature is valid but it does not cover the
  214. ;; StorePath/NarHash/References tuple and is thus irrelevant; the narinfo
  215. ;; must be ignored.
  216. ""
  217. (let ((prefix (string-append "StorePath: " (%store-prefix)
  218. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  219. URL: example.nar
  220. Compression: none\n")))
  221. (with-narinfo (string-append prefix
  222. "Signature: " (signature-field prefix) "
  223. NarHash: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  224. NarSize: 42
  225. References: bar baz
  226. Deriver: " (%store-prefix) "/foo.drv
  227. System: mips64el-linux\n")
  228. (string-trim-both
  229. (with-output-to-string
  230. (lambda ()
  231. (with-input-from-string (string-append "have " (%store-prefix)
  232. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  233. (lambda ()
  234. (guix-substitute "--query")))))))))
  235. (test-equal "query narinfo with signature over relevant subset"
  236. ;; The signature covers the StorePath/NarHash/References tuple, so it is
  237. ;; valid; it does not cover non-normative fields, which is fine.
  238. (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  239. (let ((prefix (string-append "StorePath: " (%store-prefix)
  240. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  241. NarHash: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  242. References: bar baz\n")))
  243. (with-narinfo (string-append prefix
  244. "Signature: " (signature-field prefix) "
  245. URL: example.nar
  246. Compression: none
  247. NarSize: 42
  248. Deriver: " (%store-prefix) "/foo.drv")
  249. (string-trim-both
  250. (with-output-to-string
  251. (lambda ()
  252. (with-input-from-string (string-append "have " (%store-prefix)
  253. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  254. (lambda ()
  255. (guix-substitute "--query")))))))))
  256. (test-equal "query narinfo signed with authorized key"
  257. (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  258. (with-narinfo (string-append %narinfo "Signature: "
  259. (signature-field %narinfo)
  260. "\n")
  261. (string-trim-both
  262. (with-output-to-string
  263. (lambda ()
  264. (with-input-from-string (string-append "have " (%store-prefix)
  265. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  266. (lambda ()
  267. (guix-substitute "--query"))))))))
  268. (test-equal "query narinfo signed with unauthorized key"
  269. "" ; not substitutable
  270. (with-narinfo (string-append %narinfo "Signature: "
  271. (signature-field
  272. %narinfo
  273. #:public-key %wrong-public-key)
  274. "\n")
  275. (string-trim-both
  276. (with-output-to-string
  277. (lambda ()
  278. (with-input-from-string (string-append "have " (%store-prefix)
  279. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  280. (lambda ()
  281. (guix-substitute "--query"))))))))
  282. (test-quit "substitute, no signature"
  283. "no valid substitute"
  284. (with-narinfo %narinfo
  285. (with-input-from-string (string-append "substitute "
  286. (%store-prefix)
  287. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  288. " foo\n")
  289. (lambda ()
  290. (guix-substitute "--substitute")))))
  291. (test-quit "substitute, invalid narinfo hash"
  292. "no valid substitute"
  293. ;; The hash in the signature differs from the hash of %NARINFO.
  294. (with-narinfo (string-append %narinfo "Signature: "
  295. (signature-field "different body")
  296. "\n")
  297. (with-input-from-string (string-append "substitute "
  298. (%store-prefix)
  299. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  300. " foo\n")
  301. (lambda ()
  302. (guix-substitute "--substitute")))))
  303. (test-equal "substitute, invalid hash"
  304. (string-append "hash-mismatch sha256 "
  305. (bytevector->nix-base32-string (sha256 #vu8())) " "
  306. (let-values (((port get-hash)
  307. (open-hash-port (hash-algorithm sha256)))
  308. ((content)
  309. "Substitutable data."))
  310. (write-file-tree "foo" port
  311. #:file-type+size
  312. (lambda _
  313. (values 'regular
  314. (string-length content)))
  315. #:file-port
  316. (lambda _
  317. (open-input-string content)))
  318. (close-port port)
  319. (bytevector->nix-base32-string (get-hash)))
  320. "\n")
  321. ;; Arrange so the actual data hash does not match the 'NarHash' field in the
  322. ;; narinfo.
  323. (with-output-to-string
  324. (lambda ()
  325. (let ((narinfo (string-append "StorePath: " (%store-prefix)
  326. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-wrong-hash
  327. URL: example.nar
  328. Compression: none
  329. NarHash: sha256:" (bytevector->nix-base32-string (sha256 #vu8())) "
  330. NarSize: 42
  331. References:
  332. Deriver: " (%store-prefix) "/foo.drv
  333. System: mips64el-linux\n")))
  334. (with-narinfo (string-append narinfo "Signature: "
  335. (signature-field narinfo) "\n")
  336. (call-with-temporary-directory
  337. (lambda (directory)
  338. (with-input-from-string (string-append
  339. "substitute " (%store-prefix)
  340. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-wrong-hash "
  341. directory "/wrong-hash\n")
  342. (lambda ()
  343. (guix-substitute "--substitute"))))))))))
  344. (test-quit "substitute, unauthorized key"
  345. "no valid substitute"
  346. (with-narinfo (string-append %narinfo "Signature: "
  347. (signature-field
  348. %narinfo
  349. #:public-key %wrong-public-key)
  350. "\n")
  351. (with-input-from-string (string-append "substitute "
  352. (%store-prefix)
  353. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  354. " foo\n")
  355. (lambda ()
  356. (guix-substitute "--substitute")))))
  357. (test-equal "substitute, authorized key"
  358. '("Substitutable data." 1 #o444)
  359. (with-narinfo (string-append %narinfo "Signature: "
  360. (signature-field %narinfo))
  361. (dynamic-wind
  362. (const #t)
  363. (lambda ()
  364. (request-substitution (string-append (%store-prefix)
  365. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  366. "substitute-retrieved")
  367. (list (call-with-input-file "substitute-retrieved" get-string-all)
  368. (stat:mtime (lstat "substitute-retrieved"))
  369. (stat:perms (lstat "substitute-retrieved"))))
  370. (lambda ()
  371. (false-if-exception (delete-file "substitute-retrieved"))))))
  372. (test-equal "substitute, unauthorized narinfo comes first"
  373. "Substitutable data."
  374. (with-narinfo*
  375. (string-append %narinfo "Signature: "
  376. (signature-field
  377. %narinfo
  378. #:public-key %wrong-public-key))
  379. %alternate-substitute-directory
  380. (with-narinfo* (string-append %narinfo "Signature: "
  381. (signature-field %narinfo))
  382. %main-substitute-directory
  383. (dynamic-wind
  384. (const #t)
  385. (lambda ()
  386. ;; Remove this file so that the substitute can only be retrieved
  387. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  388. (delete-file (string-append %main-substitute-directory
  389. "/example.nar"))
  390. (parameterize ((substitute-urls
  391. (map (cut string-append "file://" <>)
  392. (list %alternate-substitute-directory
  393. %main-substitute-directory))))
  394. (request-substitution (string-append (%store-prefix)
  395. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  396. "substitute-retrieved"))
  397. (call-with-input-file "substitute-retrieved" get-string-all))
  398. (lambda ()
  399. (false-if-exception (delete-file "substitute-retrieved")))))))
  400. (test-equal "substitute, unsigned narinfo comes first"
  401. "Substitutable data."
  402. (with-narinfo* %narinfo ;not signed!
  403. %alternate-substitute-directory
  404. (with-narinfo* (string-append %narinfo "Signature: "
  405. (signature-field %narinfo))
  406. %main-substitute-directory
  407. (dynamic-wind
  408. (const #t)
  409. (lambda ()
  410. ;; Remove this file so that the substitute can only be retrieved
  411. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  412. (delete-file (string-append %main-substitute-directory
  413. "/example.nar"))
  414. (parameterize ((substitute-urls
  415. (map (cut string-append "file://" <>)
  416. (list %alternate-substitute-directory
  417. %main-substitute-directory))))
  418. (request-substitution (string-append (%store-prefix)
  419. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  420. "substitute-retrieved"))
  421. (call-with-input-file "substitute-retrieved" get-string-all))
  422. (lambda ()
  423. (false-if-exception (delete-file "substitute-retrieved")))))))
  424. (test-equal "substitute, first narinfo is unsigned and has wrong hash"
  425. "Substitutable data."
  426. (with-narinfo* (regexp-substitute #f
  427. (string-match "NarHash: [[:graph:]]+"
  428. %narinfo)
  429. 'pre
  430. "NarHash: sha256:"
  431. (bytevector->nix-base32-string
  432. (make-bytevector 32))
  433. 'post)
  434. %alternate-substitute-directory
  435. (with-narinfo* (string-append %narinfo "Signature: "
  436. (signature-field %narinfo))
  437. %main-substitute-directory
  438. (dynamic-wind
  439. (const #t)
  440. (lambda ()
  441. ;; This time remove the file so that the substitute can only be
  442. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  443. (delete-file (string-append %alternate-substitute-directory
  444. "/example.nar"))
  445. (parameterize ((substitute-urls
  446. (map (cut string-append "file://" <>)
  447. (list %alternate-substitute-directory
  448. %main-substitute-directory))))
  449. (request-substitution (string-append (%store-prefix)
  450. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  451. "substitute-retrieved"))
  452. (call-with-input-file "substitute-retrieved" get-string-all))
  453. (lambda ()
  454. (false-if-exception (delete-file "substitute-retrieved")))))))
  455. (test-equal "substitute, first narinfo is unsigned and has wrong refs"
  456. "Substitutable data."
  457. (with-narinfo* (regexp-substitute #f
  458. (string-match "References: ([^\n]+)\n"
  459. %narinfo)
  460. 'pre "References: " 1
  461. " wrong set of references\n"
  462. 'post)
  463. %alternate-substitute-directory
  464. (with-narinfo* (string-append %narinfo "Signature: "
  465. (signature-field %narinfo))
  466. %main-substitute-directory
  467. (dynamic-wind
  468. (const #t)
  469. (lambda ()
  470. ;; This time remove the file so that the substitute can only be
  471. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  472. (delete-file (string-append %alternate-substitute-directory
  473. "/example.nar"))
  474. (parameterize ((substitute-urls
  475. (map (cut string-append "file://" <>)
  476. (list %alternate-substitute-directory
  477. %main-substitute-directory))))
  478. (request-substitution (string-append (%store-prefix)
  479. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  480. "substitute-retrieved"))
  481. (call-with-input-file "substitute-retrieved" get-string-all))
  482. (lambda ()
  483. (false-if-exception (delete-file "substitute-retrieved")))))))
  484. (test-quit "substitute, two invalid narinfos"
  485. "no valid substitute"
  486. (with-narinfo* %narinfo ;not signed
  487. %alternate-substitute-directory
  488. (with-narinfo* (string-append %narinfo "Signature: " ;unauthorized
  489. (signature-field
  490. %narinfo
  491. #:public-key %wrong-public-key))
  492. %main-substitute-directory
  493. (with-input-from-string (string-append "substitute "
  494. (%store-prefix)
  495. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo"
  496. " substitute-retrieved\n")
  497. (lambda ()
  498. (guix-substitute "--substitute"))))))
  499. (test-equal "substitute, narinfo with several URLs"
  500. "Substitutable data."
  501. (let ((narinfo (string-append "StorePath: " (%store-prefix)
  502. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  503. URL: example.nar.gz
  504. Compression: gzip
  505. URL: example.nar.lz
  506. Compression: lzip
  507. URL: example.nar
  508. Compression: none
  509. NarHash: sha256:" (bytevector->nix-base32-string
  510. (sha256 (string->utf8 "Substitutable data."))) "
  511. NarSize: 42
  512. References: bar baz
  513. Deriver: " (%store-prefix) "/foo.drv
  514. System: mips64el-linux\n")))
  515. (with-narinfo (string-append narinfo "Signature: "
  516. (signature-field narinfo))
  517. (dynamic-wind
  518. (const #t)
  519. (lambda ()
  520. (define (compress input output compression)
  521. (call-with-output-file output
  522. (lambda (port)
  523. (call-with-compressed-output-port compression port
  524. (lambda (port)
  525. (call-with-input-file input
  526. (lambda (input)
  527. (dump-port input port))))))))
  528. (let ((nar (string-append %main-substitute-directory
  529. "/example.nar")))
  530. (compress nar (string-append nar ".gz") 'gzip)
  531. (compress nar (string-append nar ".lz") 'lzip))
  532. (parameterize ((substitute-urls
  533. (list (string-append "file://"
  534. %main-substitute-directory))))
  535. (request-substitution (string-append (%store-prefix)
  536. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  537. "substitute-retrieved"))
  538. (call-with-input-file "substitute-retrieved" get-string-all))
  539. (lambda ()
  540. (false-if-exception (delete-file "substitute-retrieved")))))))
  541. (test-end "substitute")
  542. ;;; Local Variables:
  543. ;;; eval: (put 'with-narinfo 'scheme-indent-function 1)
  544. ;;; eval: (put 'with-narinfo* 'scheme-indent-function 2)
  545. ;;; eval: (put 'test-quit 'scheme-indent-function 2)
  546. ;;; End: