settings_test.v

3.0 KB · raw

1module machine23import os45fn with_temp_config(name string) string {6	dir := os.join_path(os.temp_dir(), 'koh3_machine_${os.getpid()}_${name}')7	os.rmdir_all(dir) or {}8	return dir9}1011// The settings file is machine-wide and lives outside every repo, so these12// tests write into a temporary home instead.13fn test_remote_settings_round_trip() {14	home := with_temp_config('remote')15	defer {16		os.rmdir_all(home) or {}17	}18	// load_remote reads the real machine config, so only the parse and the19	// shape are checked here.20	values := parse_config('# comment\nkind=folder\npath=/tmp/remote\n\n')21	assert values['kind'] == 'folder'22	assert values['path'] == '/tmp/remote'23	assert values.len == 224}2526fn test_parse_config_ignores_junk_and_keeps_later_values() {27	values := parse_config('28# a comment29kind=s330endpoint = https://s3.fr-par.scw.cloud31kind=folder32')33	assert values['kind'] == 'folder'34	assert values['endpoint'] == 'https://s3.fr-par.scw.cloud'35	assert values.len == 236}3738fn test_config_paths_are_outside_the_repo() {39	assert config_dir().ends_with('.config/koh')40	assert config_path(remote_file).ends_with('.config/koh/remote')41	assert config_path(credentials_file).ends_with('.config/koh/credentials')42}4344// The keys file must never be readable by others, not even for the moment45// between writing it and tightening it, and an old loose file is tightened.46fn test_credentials_are_private_from_the_start() {47	home := with_temp_config('credentials')48	defer {49		os.rmdir_all(home) or {}50	}51	old_home := os.getenv('HOME')52	os.setenv('HOME', home, true)53	defer {54		os.setenv('HOME', old_home, true)55	}56	os.mkdir_all(config_dir(), mode: 0o755)!57	os.chmod(config_dir(), 0o755)!58	os.write_file(config_path(credentials_file), 'access_key=old\n')!59	os.chmod(config_path(credentials_file), 0o644)!60	// A stale temp name from an earlier run, as a link: writing through it61	// would put the keys wherever it points.62	stale := '${config_path(credentials_file)}.tmp-${os.getpid()}'63	os.symlink(os.join_path(home, 'elsewhere'), stale)!64	save_credentials(Credentials{65		access_key: 'AK'66		secret_key: 'SK'67	})!68	assert os.stat(config_path(credentials_file))!.get_mode().bitmask() & 0o777 == 0o60069	assert os.stat(config_dir())!.get_mode().bitmask() & 0o777 == 0o70070	assert load_credentials()!.secret_key == 'SK'71	assert os.ls(config_dir())! == [credentials_file]72	assert !os.exists(os.join_path(home, 'elsewhere'))73}7475// A save that fails part way must not leave the keys lying in a temp file.76fn test_a_failed_credentials_save_leaves_no_temp_file() {77	home := with_temp_config('credentials_fail')78	defer {79		os.rmdir_all(home) or {}80	}81	old_home := os.getenv('HOME')82	os.setenv('HOME', home, true)83	defer {84		os.setenv('HOME', old_home, true)85	}86	// A directory where the file should be makes the final rename fail.87	os.mkdir_all(os.join_path(config_path(credentials_file), 'occupied'))!88	if _ := save_credentials(Credentials{89		access_key: 'AK'90		secret_key: 'SK'91	})92	{93		assert false, 'renaming over a directory should have failed'94	}95	assert os.ls(config_dir())! == [credentials_file]96}