module engine import os struct DownloadEvents { mut: events []DownloadEvent handle &Handle = unsafe { nil } interrupt bool } fn collect_download_event(context voidptr, event DownloadEvent) { mut data := unsafe { &DownloadEvents(context) } data.events << event if data.interrupt && event.kind == .init { data.handle.interrupt_downloads_async() } } fn test_download_file_transport_resume_signatures_and_time_conditions() ! { dir := os.join_path(os.vtmp_dir(), 'native-download-file-${os.getpid()}') source := os.join_path(dir, 'source') cache := os.join_path(dir, 'cache') os.mkdir_all(source)! os.mkdir_all(cache)! defer { os.rmdir_all(dir) or { eprintln(err) } } path := os.join_path(source, 'app.pkg.tar') os.write_file(path, 'package content\n')! os.write_file(path + '.sig', 'signature content\n')! mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut events := &DownloadEvents{ handle: handle } handle.set_download_callback(collect_download_event, events)! request := DownloadRequest{ url: 'file://' + path, filename: 'app.pkg.tar', allow_resume: true, download_signature: true } first := handle.download_files([request], cache)! assert first.updated assert first.files.map(it.result) == [0, 0] assert os.read_file(os.join_path(cache, 'app.pkg.tar'))! == 'package content\n' assert os.read_file(os.join_path(cache, 'app.pkg.tar.sig'))! == 'signature content\n' assert events.events.filter(it.kind == .init).map(it.filename) == ['app.pkg.tar', 'app.pkg.tar.sig'] assert events.events.filter(it.kind == .completed).map(it.total) == [i64(16), 18] second := handle.download_files([request], cache)! assert second.updated assert second.files.map(it.result) == [1, 0] assert !os.exists(os.join_path(cache, 'app.pkg.tar.part')) os.write_file(os.join_path(cache, 'resumed.pkg.tar.part'), 'package ')! resumed := handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'resumed.pkg.tar', allow_resume: true }], cache)! assert resumed.updated assert os.read_file(os.join_path(cache, 'resumed.pkg.tar'))! == 'package content\n' assert events.events.filter(it.kind == .completed).last().total == 8 assert !os.exists(os.join_path(cache, 'resumed.pkg.tar.part')) os.write_file(os.join_path(cache, 'complete.pkg.tar.part'), 'package content\n')! assert handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'complete.pkg.tar', allow_resume: true }], cache)!.updated assert os.read_file(os.join_path(cache, 'complete.pkg.tar'))! == 'package content\n' } fn test_download_mirrors_optional_failures_limits_and_interrupt_recovery() ! { dir := os.join_path(os.vtmp_dir(), 'native-download-errors-${os.getpid()}') source := os.join_path(dir, 'source') cache := os.join_path(dir, 'cache') os.mkdir_all(source)! os.mkdir_all(cache)! defer { os.rmdir_all(dir) or { eprintln(err) } } path := os.join_path(source, 'app.pkg.tar') os.write_file(path, 'content')! mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } mut events := &DownloadEvents{ handle: handle } handle.set_download_callback(collect_download_event, events)! retried := handle.download_files([DownloadRequest{ filepath: 'app.pkg.tar' cache_servers: ['file://' + os.join_path(dir, 'missing')] servers: ['file://' + source] filename: 'app.pkg.tar' allow_resume: true download_signature: true signature_optional: true }], cache)! assert retried.updated assert retried.files.map(it.result) == [0, -1] assert events.events.filter(it.kind == .retry).len == 1 assert os.read_file(os.join_path(cache, 'app.pkg.tar'))! == 'content' if _ := handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'small', max_size: 3 }], cache) { assert false } assert handle.errno() == .libcurl assert !os.exists(os.join_path(cache, 'small')) assert !os.exists(os.join_path(cache, 'small.part')) events.interrupt = true if _ := handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'interrupted', allow_resume: true }], cache) { assert false } assert handle.errno() == .trans_abort events.interrupt = false assert handle.download_files([DownloadRequest{ url: 'file://' + path, filename: 'interrupted', allow_resume: true }], cache)!.updated assert os.read_file(os.join_path(cache, 'interrupted'))! == 'content' events.events.clear() assert handle.download_files([ DownloadRequest{ url: 'file://' + path, filename: 'smallest', max_size: 8 }, DownloadRequest{ url: 'file://' + path, filename: 'largest', max_size: 16 }, DownloadRequest{ url: 'file://' + path, filename: 'equal-largest', max_size: 16 }, ], cache)!.updated assert events.events.filter(it.kind == .init).map(it.filename) == ['largest', 'equal-largest', 'smallest'] } fn C.link(&char, &char) i32 fn test_download_refuses_cache_links_before_truncating_them() ! { dir := os.join_path(os.vtmp_dir(), 'native-download-links-${os.getpid()}') os.mkdir_all(dir)! defer { os.rmdir_all(dir) or { eprintln(err) } } source := os.join_path(dir, 'source') victim := os.join_path(dir, 'victim') partial := os.join_path(dir, 'target.part') os.write_file(source, 'download')! os.write_file(victim, 'preserve')! mut handle := initialize('/', dir)! defer { handle.release() or { eprintln(err) } } os.symlink(victim, partial)! request := DownloadRequest{ url: 'file://' + source, filename: 'target' } if _ := handle.download_files([request], dir) { assert false } assert os.read_file(victim)! == 'preserve' os.rm(partial)! assert C.link(unsafe { &char(victim.str) }, unsafe { &char(partial.str) }) == 0 if _ := handle.download_files([request], dir) { assert false } assert os.read_file(victim)! == 'preserve' for requests in [ [DownloadRequest{ url: 'file://' + source, filename: '../victim' }], [DownloadRequest{ url: 'file://' + source, filename: 'a' }, DownloadRequest{ url: 'file://' + source, filename: 'a.part' }], [DownloadRequest{ url: 'file://' + source, filename: 'a', download_signature: true }, DownloadRequest{ url: 'file://' + source, filename: 'a.sig' }], ] { if _ := handle.download_files(requests, dir) { assert false } assert handle.errno() == .wrong_args } }