1234module update56import crypto.blake37import json28import os910fn scratch(name string) !string {11 dir := os.join_path(os.temp_dir(), 'koh3_update_${os.getpid()}_${name}')12 os.rmdir_all(dir) or {}13 os.mkdir_all(dir, os.MkdirParams{})!14 return dir15}1617fn test_versions_compare_as_numbers_component_by_component() {18 assert compare_versions('2.10.0', '2.9.9') == 119 assert compare_versions('2.9.9', '2.10.0') == -120 assert compare_versions('3.0.82', '3.0.82') == 021 assert compare_versions('1.0', '1.0.0') == 022 assert compare_versions('1.01', '1.1') == 023 assert compare_versions('1.0.1', '1.0') == 124 assert compare_versions('10.0.0', '9.9.9') == 125 assert compare_versions('0.9', '1.0') == -126}2728fn test_a_release_document_is_read_with_and_without_notes() {29 body := '{"version":"3.0.84","platforms":{"macos-aarch64":{"url":"https://x/koh","blake3":"ab"}}}'30 document := json2.decode[Document](body)!31 assert document.version == '3.0.84'32 assert document.notes or { 'absent' } == 'absent'33 assert document.platforms['macos-aarch64'] or { PlatformFile{} }.url == 'https://x/koh'34 with_notes := '{"version":"3.0.84","notes":"hello","platforms":{}}'35 assert json2.decode[Document](with_notes)!.notes or { '' } == 'hello'36}3738fn test_a_download_that_is_not_the_published_file_is_refused() {39 body := 'the new binary'.bytes()40 41 42 good := '0e86a96ebd4c3d861a2e85c07544478573af7ab3745c1d4e7b5750fdbe7cc397'43 assert blake3.sum256(body).hex() == good44 verify(body, good)!45 if _ := verify(body, 'ff'.repeat(32)) {46 assert false, 'a download that does not match its checksum must be refused'47 } else {48 assert err.msg().contains('not the published file')49 }50 if _ := verify([]u8{}, good) {51 assert false, 'an empty download must be refused'52 }53 if _ := verify(body, '') {54 assert false, 'a document without a checksum must be refused'55 }56}5758fn test_write_beside_writes_beside_the_target_and_refuses_a_locked_folder() {59 dir := scratch('write_beside')!60 defer {61 os.rmdir_all(dir) or {}62 }63 fresh := write_beside(dir, 'binary'.bytes())!64 assert os.dir(fresh) == dir65 assert os.read_file(fresh)! == 'binary'66 67 assert (os.stat(fresh) or { panic(err) }).mode & 0o777 == 0o7556869 locked := os.join_path(dir, 'locked')70 os.mkdir_all(locked, os.MkdirParams{})!71 os.chmod(locked, 0o500)!72 defer {73 os.chmod(locked, 0o700) or {}74 }75 if _ := write_beside(locked, 'binary'.bytes()) {76 assert false, 'a folder that cannot be written must be refused'77 } else {78 assert err.msg().contains('cannot write beside')79 }80}8182fn test_the_source_can_be_named_by_the_environment() {83 os.setenv(source_env, 'http://127.0.0.1:8080/koh/releases/latest.json', true)84 assert source() == 'http://127.0.0.1:8080/koh/releases/latest.json'85 os.unsetenv(source_env)86 assert source() == default_source87}888990fn test_a_file_url_is_read_into_the_same_bytes() {91 dir := scratch('download')!92 defer {93 os.rmdir_all(dir) or {}94 }95 path := os.join_path(dir, 'koh3')96 os.write_file_array(path, 'the new binary'.bytes())!97 body := download('file://${path}')!98 assert body.bytestr() == 'the new binary'99 assert blake3.sum256(body).hex() == '0e86a96ebd4c3d861a2e85c07544478573af7ab3745c1d4e7b5750fdbe7cc397'100 if _ := download('file://${dir}/missing') {101 assert false, 'a file URL that is not there must be refused'102 } else {103 assert err.msg().contains('could not read')104 }105}