update_test.v

3.0 KB · raw

1//! Unit tests for `koh update`: ordering versions, decoding a release2//! document, and the two checks that stand between a download and the binary.34module 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	// The Blake3 of this exact text, computed elsewhere: a real check, not the41	// implementation checking itself.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_staging_writes_beside_the_target_and_refuses_a_locked_folder() {59	dir := scratch('stage')!60	defer {61		os.rmdir_all(dir) or {}62	}63	staged := stage(dir, 'binary'.bytes())!64	assert os.dir(staged) == dir65	assert os.read_file(staged)! == 'binary'66	// 0755: the file is about to become the koh binary.67	assert (os.stat(staged) 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 _ := stage(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}