diff --git a/internal/config/config_validation_internal_test.go b/internal/config/config_validation_internal_test.go index 8399aba..1800ab3 100644 --- a/internal/config/config_validation_internal_test.go +++ b/internal/config/config_validation_internal_test.go @@ -618,6 +618,62 @@ func TestConfigFileInDirectoryPixaMayNotEnterAbortsStartup(t *testing.T) { } } +// TestConfigFileLinkingToItselfAbortsStartup checks that a config file +// pixa cannot read for a reason other than not existing aborts startup, +// as root too: a symbolic link to itself fails with "too many levels of +// symbolic links". +func TestConfigFileLinkingToItselfAbortsStartup(t *testing.T) { + workDir := t.TempDir() + + err := os.Symlink("config.yml", filepath.Join(workDir, "config.yml")) + if err != nil { + t.Fatalf("failed to create symbolic link: %v", err) + } + + // Only the working directory's config.yml is there: the appname rules + // out /etc, and HOME is empty. + t.Setenv("PIXA_CONFIG_PATH", "") + t.Setenv("HOME", t.TempDir()) + t.Chdir(workDir) + + log := slog.New(slog.DiscardHandler) + + sc, err := loadConfigFile(log, "pixa-test-nonexistent-app") + if err == nil { + t.Fatalf("config file pixa cannot read must abort startup, got config: %v", + sc) + } + + t.Logf("got expected error: %v", err) + + if !strings.Contains(err.Error(), "config.yml") { + t.Errorf("error %q does not name the config file config.yml", err.Error()) + } +} + +// TestConfigPathThroughFileIsPassedOver checks that a config file path +// that runs through a file, such as one under a HOME of /dev/null, is +// passed over like one that does not exist, since no file can be there. +func TestConfigPathThroughFileIsPassedOver(t *testing.T) { + // No config file is there: the appname rules out /etc, HOME is + // /dev/null, and the working directory is empty. + t.Setenv("PIXA_CONFIG_PATH", "") + t.Setenv("HOME", os.DevNull) + t.Chdir(t.TempDir()) + + log := slog.New(slog.DiscardHandler) + + sc, err := loadConfigFile(log, "pixa-test-nonexistent-app") + if err != nil { + t.Fatalf("a config path through a file must be passed over, got error: %v", + err) + } + + if sc != nil { + t.Errorf("expected no config file, got config: %v", sc) + } +} + func TestEnsureStateDirCreatesDirectory(t *testing.T) { t.Parallel()