| 1 | module engine |
| 2 | |
| 3 | import nspkg.tests.retrievalfixtures |
| 4 | import os |
| 5 | |
| 6 | struct FetchEvents { |
| 7 | mut: |
| 8 | items []RetrievalEvent |
| 9 | } |
| 10 | |
| 11 | fn fetch_event(context voidptr, event RetrievalEvent) { |
| 12 | mut state := unsafe { &FetchEvents(context) } |
| 13 | state.items << event |
| 14 | } |
| 15 | |
| 16 | fn planned_fetch(context voidptr, request FetchRequest) int { |
| 17 | mut plan := unsafe { &retrievalfixtures.Plan(context) } |
| 18 | return plan.fetch(request.url, request.directory, request.filename, request.force) |
| 19 | } |
| 20 | |
| 21 | fn incorrect_fetch(context voidptr, request FetchRequest) int { |
| 22 | mode := unsafe { *(&int(context)) } |
| 23 | if mode == 2 { return 2 } |
| 24 | if mode == 1 { |
| 25 | os.mkdir(request.directory + request.filename) or { |
| 26 | eprintln(err) |
| 27 | return -1 |
| 28 | } |
| 29 | } |
| 30 | return 0 |
| 31 | } |
| 32 | |
| 33 | fn test_fetch_callback_cannot_report_success_without_a_regular_output_file() ! { |
| 34 | dir := os.join_path(os.vtmp_dir(), 'native-fetch-invalid-callback-${os.getpid()}') |
| 35 | os.mkdir_all(dir)! |
| 36 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 37 | mut handle := initialize('/', dir)! |
| 38 | defer { handle.release() or { eprintln(err) } } |
| 39 | handle.add_string_list(.cache_dirs, dir)! |
| 40 | for mode in 0 .. 3 { |
| 41 | handle.set_fetch_callback(incorrect_fetch, &mode)! |
| 42 | if _ := handle.fetch_package_urls(['custom://host/claimed-${mode}.pkg.tar']) { |
| 43 | assert false |
| 44 | } |
| 45 | assert handle.errno() == .retrieve |
| 46 | assert handle.error().msg().contains(['did not supply', 'non-regular', 'invalid status'][mode]) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn test_cache_selection_fetch_order_signatures_and_temporary_names() ! { |
| 51 | dir := os.join_path(os.vtmp_dir(), 'native-package-fetch-${os.getpid()}') |
| 52 | source := os.join_path(dir, 'source') |
| 53 | readonly_cache := os.join_path(dir, 'readonly') |
| 54 | cache := os.join_path(dir, 'new', 'cache') |
| 55 | os.mkdir_all(source)! |
| 56 | os.mkdir_all(readonly_cache)! |
| 57 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 58 | defer { os.chmod(readonly_cache, 0o755) or { eprintln(err) } } |
| 59 | os.write_file(os.join_path(source, 'new.pkg.tar'), 'new')! |
| 60 | os.write_file(os.join_path(source, 'new.pkg.tar.sig'), 'signature')! |
| 61 | os.write_file(os.join_path(source, 'payload'), 'generic')! |
| 62 | os.write_file(os.join_path(readonly_cache, 'cached.pkg.tar'), 'cached')! |
| 63 | os.chmod(readonly_cache, 0o555)! |
| 64 | mut handle := initialize('/', dir)! |
| 65 | defer { handle.release() or { eprintln(err) } } |
| 66 | handle.set_string_list(.cache_dirs, [readonly_cache, cache])! |
| 67 | mut events := &FetchEvents{} |
| 68 | handle.set_retrieval_callback(fetch_event, events)! |
| 69 | old_mask := C.umask(0o077) |
| 70 | defer { C.umask(old_mask) } |
| 71 | assert handle.cache_directory()! == cache + '/' |
| 72 | assert os.stat(cache)!.mode & 0o777 == 0o755 |
| 73 | url := 'file://' + os.join_path(source, 'new.pkg.tar') |
| 74 | paths := handle.fetch_package_urls([url, 'file:///missing/cached.pkg.tar'])! |
| 75 | assert paths == [os.join_path(readonly_cache, 'cached.pkg.tar'), |
| 76 | os.join_path(cache, 'new.pkg.tar')] |
| 77 | assert events.items.map(it.kind) == [RetrievalEventKind.pkg_retrieve_start, .pkg_retrieve_done] |
| 78 | assert events.items[0].packages == 1 |
| 79 | assert os.read_file(paths[1])! == 'new' |
| 80 | temporary := handle.fetch_package_urls(['file://' + os.join_path(source, 'payload')])![0] |
| 81 | assert os.base(temporary).starts_with('alpmtmp.') |
| 82 | assert os.read_file(temporary)! == 'generic' |
| 83 | assert os.stat(temporary)!.mode & 0o777 == 0o600 |
| 84 | handle.set_integer_option(.default_siglevel, 3)! |
| 85 | handle.set_integer_option(.remote_file_siglevel, i64(sig_use_default))! |
| 86 | assert handle.fetch_package_urls([url])! == [os.join_path(cache, 'new.pkg.tar')] |
| 87 | assert os.read_file(os.join_path(cache, 'new.pkg.tar.sig'))! == 'signature' |
| 88 | events.items.clear() |
| 89 | assert handle.fetch_package_urls(['file:///missing/new.pkg.tar'])! == [os.join_path(cache, 'new.pkg.tar')] |
| 90 | assert events.items.len == 0 |
| 91 | os.rm(os.join_path(source, 'new.pkg.tar.sig'))! |
| 92 | os.rm(os.join_path(cache, 'new.pkg.tar.sig'))! |
| 93 | assert handle.fetch_package_urls([url])!.len == 1 |
| 94 | assert events.items.map(it.kind) == [RetrievalEventKind.pkg_retrieve_start, .pkg_retrieve_done] |
| 95 | before := os.ls(cache)!.sorted() |
| 96 | if _ := handle.fetch_package_urls(['file://' + os.join_path(source, 'payload'), 'invalid-url']) { |
| 97 | assert false |
| 98 | } |
| 99 | assert handle.errno() == .retrieve |
| 100 | assert os.ls(cache)!.sorted() == before |
| 101 | } |
| 102 | |
| 103 | fn test_cache_tmpdir_fallback_and_external_fetch_destination_contract() ! { |
| 104 | dir := os.join_path(os.vtmp_dir(), 'native-fetch-callback-${os.getpid()}') |
| 105 | os.mkdir_all(dir)! |
| 106 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 107 | saved := os.getenv_opt('TMPDIR') |
| 108 | assert os.setenv('TMPDIR', dir, true) == 0 |
| 109 | defer { |
| 110 | if value := saved { |
| 111 | assert os.setenv('TMPDIR', value, true) == 0 |
| 112 | } else { |
| 113 | assert os.unsetenv('TMPDIR') == 0 |
| 114 | } |
| 115 | } |
| 116 | mut handle := initialize('/', dir)! |
| 117 | defer { handle.release() or { eprintln(err) } } |
| 118 | assert handle.cache_directory()! == dir + '/' |
| 119 | assert handle.string_list(.cache_dirs)! == [dir + '/'] |
| 120 | mut plan := &retrievalfixtures.Plan{ |
| 121 | replies: { |
| 122 | 'custom://host/download': retrievalfixtures.Reply{ status: 0, data: 'body'.bytes() } |
| 123 | } |
| 124 | } |
| 125 | handle.set_fetch_callback(planned_fetch, plan)! |
| 126 | path := handle.fetch_package_urls(['custom://host/download'])![0] |
| 127 | assert os.read_file(path)! == 'body' |
| 128 | assert os.base(path).starts_with('alpmtmp.') |
| 129 | assert plan.calls[0].filename == os.base(path) |
| 130 | assert plan.calls[0].trailing_slash |
| 131 | assert handle.curl_multi == unsafe { nil } |
| 132 | plan.replies['custom://host/app.pkg.tar'] = retrievalfixtures.Reply{ status: 0, data: 'app'.bytes() } |
| 133 | paths := handle.fetch_package_urls(['custom://host/app.pkg.tar', 'custom://host/app.pkg.tar'])! |
| 134 | assert paths.len == 2 |
| 135 | assert paths[0] == paths[1] |
| 136 | assert plan.calls.len == 2 |
| 137 | if _ := handle.fetch_package_urls(['custom://one/conflict.pkg.tar', 'custom://two/conflict.pkg.tar']) { |
| 138 | assert false |
| 139 | } |
| 140 | assert handle.errno() == .wrong_args |
| 141 | } |
| 142 | |