Fix netrc one-line parsing and enforce dummy login writes
All checks were successful
buildbot/nix-eval Build done.
buildbot/nix-build Build done.
buildbot/nix-effects Build done.

This commit is contained in:
Abel Luck 2026-03-03 08:52:31 +01:00
parent c24af42fc0
commit aa4732af7b
4 changed files with 89 additions and 22 deletions

View file

@ -3,6 +3,7 @@ package netrc
import (
"os"
"path/filepath"
"strings"
"testing"
)
@ -55,6 +56,28 @@ func TestUpsertUpdateExisting(t *testing.T) {
}
}
func TestUpsertUpdateExistingOneLineEntry(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "netrc")
initial := "machine other.host login dummy password otherpass\nmachine cache.example.com login dummy password oldtoken\n"
if err := os.WriteFile(path, []byte(initial), 0600); err != nil {
t.Fatal(err)
}
if err := Upsert(path, "cache.example.com", "newtoken"); err != nil {
t.Fatalf("unexpected error: %v", err)
}
pw, err := GetPassword(path, "cache.example.com")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if pw != "newtoken" {
t.Errorf("password = %q, want %q", pw, "newtoken")
}
}
func TestUpsertAppend(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "netrc")
@ -197,3 +220,29 @@ func TestFilePermissionsCorrected(t *testing.T) {
t.Errorf("file permissions = %o, want 0600", perm)
}
}
func TestWriteUsesOneLineFormatAndDummyLoginFallback(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "netrc")
// Existing one-line entry with missing login should be normalized.
if err := os.WriteFile(path, []byte("machine cache.example.com password oldtoken\n"), 0600); err != nil {
t.Fatal(err)
}
if err := Upsert(path, "cache.example.com", "newtoken"); err != nil {
t.Fatalf("unexpected error: %v", err)
}
content, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read error: %v", err)
}
text := string(content)
if !strings.Contains(text, "machine cache.example.com login dummy password newtoken\n") {
t.Fatalf("unexpected netrc content: %q", text)
}
if strings.Contains(text, "\nlogin ") || strings.Contains(text, "\npassword ") {
t.Fatalf("netrc entry should be written on one line: %q", text)
}
}