| 1 | module engine |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | struct DownloadEvents { |
| 6 | mut: |
| 7 | events []DownloadEvent |
| 8 | handle &Handle = unsafe { nil } |
| 9 | interrupt bool |
| 10 | } |
| 11 | |
| 12 | fn collect_download_event(context voidptr, event DownloadEvent) { |
| 13 | mut data := unsafe { &DownloadEvents(context) } |
| 14 | data.events << event |
| 15 | if data.interrupt && event.kind == .init { data.handle.interrupt_downloads_async() } |
| 16 | } |
| 17 | |
| 18 | fn test_download_file_transport_resume_signatures_and_time_conditions() ! { |
| 19 | dir := os.join_path(os.vtmp_dir(), 'native-download-file-${os.getpid()}') |
| 20 | source := os.join_path(dir, 'source') |
| 21 | cache := os.join_path(dir, 'cache') |
| 22 | os.mkdir_all(source)! |
| 23 | os.mkdir_all(cache)! |
| 24 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 25 | path := os.join_path(source, 'app.pkg.tar') |
| 26 | os.write_file(path, 'package content\n')! |
| 27 | os.write_file(path + '.sig', 'signature content\n')! |
| 28 | mut handle := initialize('/', dir)! |
| 29 | defer { handle.release() or { eprintln(err) } } |
| 30 | mut events := &DownloadEvents{ handle: handle } |
| 31 | handle.set_download_callback(collect_download_event, events)! |
| 32 | request := DownloadRequest{ url: 'file://' + path, filename: 'app.pkg.tar', allow_resume: true, download_signature: true } |
| 33 | first := handle.download_files([request], cache)! |
| 34 | assert first.updated |
| 35 | assert first.files.map(it.result) == [0, 0] |
| 36 | assert os.read_file(os.join_path(cache, 'app.pkg.tar'))! == 'package content\n' |
| 37 | assert os.read_file(os.join_path(cache, 'app.pkg.tar.sig'))! == 'signature content\n' |
| 38 | assert events.events.filter(it.kind == .init).map(it.filename) == ['app.pkg.tar', 'app.pkg.tar.sig'] |
| 39 | assert events.events.filter(it.kind == .completed).map(it.total) == [i64(16), 18] |
| 40 | second := handle.download_files([request], cache)! |
| 41 | assert second.updated |
| 42 | assert second.files.map(it.result) == [1, 0] |
| 43 | assert !os.exists(os.join_path(cache, 'app.pkg.tar.part')) |
| 44 | os.write_file(os.join_path(cache, 'resumed.pkg.tar.part'), 'package ')! |
| 45 | resumed := handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'resumed.pkg.tar', allow_resume: true }], cache)! |
| 46 | assert resumed.updated |
| 47 | assert os.read_file(os.join_path(cache, 'resumed.pkg.tar'))! == 'package content\n' |
| 48 | assert events.events.filter(it.kind == .completed).last().total == 8 |
| 49 | assert !os.exists(os.join_path(cache, 'resumed.pkg.tar.part')) |
| 50 | os.write_file(os.join_path(cache, 'complete.pkg.tar.part'), 'package content\n')! |
| 51 | assert handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'complete.pkg.tar', allow_resume: true }], cache)!.updated |
| 52 | assert os.read_file(os.join_path(cache, 'complete.pkg.tar'))! == 'package content\n' |
| 53 | } |
| 54 | |
| 55 | fn test_download_mirrors_optional_failures_limits_and_interrupt_recovery() ! { |
| 56 | dir := os.join_path(os.vtmp_dir(), 'native-download-errors-${os.getpid()}') |
| 57 | source := os.join_path(dir, 'source') |
| 58 | cache := os.join_path(dir, 'cache') |
| 59 | os.mkdir_all(source)! |
| 60 | os.mkdir_all(cache)! |
| 61 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 62 | path := os.join_path(source, 'app.pkg.tar') |
| 63 | os.write_file(path, 'content')! |
| 64 | mut handle := initialize('/', dir)! |
| 65 | defer { handle.release() or { eprintln(err) } } |
| 66 | mut events := &DownloadEvents{ handle: handle } |
| 67 | handle.set_download_callback(collect_download_event, events)! |
| 68 | retried := handle.download_files([DownloadRequest{ |
| 69 | filepath: 'app.pkg.tar' |
| 70 | cache_servers: ['file://' + os.join_path(dir, 'missing')] |
| 71 | servers: ['file://' + source] |
| 72 | filename: 'app.pkg.tar' |
| 73 | allow_resume: true |
| 74 | download_signature: true |
| 75 | signature_optional: true |
| 76 | }], cache)! |
| 77 | assert retried.updated |
| 78 | assert retried.files.map(it.result) == [0, -1] |
| 79 | assert events.events.filter(it.kind == .retry).len == 1 |
| 80 | assert os.read_file(os.join_path(cache, 'app.pkg.tar'))! == 'content' |
| 81 | if _ := handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'small', max_size: 3 }], cache) { |
| 82 | assert false |
| 83 | } |
| 84 | assert handle.errno() == .libcurl |
| 85 | assert !os.exists(os.join_path(cache, 'small')) |
| 86 | assert !os.exists(os.join_path(cache, 'small.part')) |
| 87 | events.interrupt = true |
| 88 | if _ := handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'interrupted', allow_resume: true }], cache) { |
| 89 | assert false |
| 90 | } |
| 91 | assert handle.errno() == .trans_abort |
| 92 | events.interrupt = false |
| 93 | assert handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'interrupted', allow_resume: true }], cache)!.updated |
| 94 | assert os.read_file(os.join_path(cache, 'interrupted'))! == 'content' |
| 95 | events.events.clear() |
| 96 | assert handle.download_files([ |
| 97 | DownloadRequest{ url: 'file://' + path, filename: 'smallest', max_size: 8 }, |
| 98 | DownloadRequest{ url: 'file://' + path, filename: 'largest', max_size: 16 }, |
| 99 | DownloadRequest{ url: 'file://' + path, filename: 'equal-largest', max_size: 16 }, |
| 100 | ], cache)!.updated |
| 101 | assert events.events.filter(it.kind == .init).map(it.filename) == ['largest', 'equal-largest', |
| 102 | 'smallest'] |
| 103 | } |
| 104 | |
| 105 | fn C.link(&char, &char) i32 |
| 106 | |
| 107 | fn test_download_refuses_cache_links_before_truncating_them() ! { |
| 108 | dir := os.join_path(os.vtmp_dir(), 'native-download-links-${os.getpid()}') |
| 109 | os.mkdir_all(dir)! |
| 110 | defer { os.rmdir_all(dir) or { eprintln(err) } } |
| 111 | source := os.join_path(dir, 'source') |
| 112 | victim := os.join_path(dir, 'victim') |
| 113 | partial := os.join_path(dir, 'target.part') |
| 114 | os.write_file(source, 'download')! |
| 115 | os.write_file(victim, 'preserve')! |
| 116 | mut handle := initialize('/', dir)! |
| 117 | defer { handle.release() or { eprintln(err) } } |
| 118 | os.symlink(victim, partial)! |
| 119 | request := DownloadRequest{ url: 'file://' + source, filename: 'target' } |
| 120 | if _ := handle.download_files([request], dir) { |
| 121 | assert false |
| 122 | } |
| 123 | assert os.read_file(victim)! == 'preserve' |
| 124 | os.rm(partial)! |
| 125 | assert C.link(unsafe { &char(victim.str) }, unsafe { &char(partial.str) }) == 0 |
| 126 | if _ := handle.download_files([request], dir) { |
| 127 | assert false |
| 128 | } |
| 129 | assert os.read_file(victim)! == 'preserve' |
| 130 | for requests in [ |
| 131 | [DownloadRequest{ url: 'file://' + source, filename: '../victim' }], |
| 132 | [DownloadRequest{ url: 'file://' + source, filename: 'a' }, |
| 133 | DownloadRequest{ url: 'file://' + source, filename: 'a.part' }], |
| 134 | [DownloadRequest{ url: 'file://' + source, filename: 'a', download_signature: true }, |
| 135 | DownloadRequest{ url: 'file://' + source, filename: 'a.sig' }], |
| 136 | ] { |
| 137 | if _ := handle.download_files(requests, dir) { |
| 138 | assert false |
| 139 | } |
| 140 | assert handle.errno() == .wrong_args |
| 141 | } |
| 142 | } |
| 143 | |