blob: d1c3252f9100bb9835ad0b1aa6e11ade03c881f1 [file] [log] [blame]
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package secret
import (
"os"
"path/filepath"
"testing"
"golang.org/x/oscar/internal/testutil"
)
func TestNetrc(t *testing.T) {
file := filepath.Join(t.TempDir(), "netrc")
if err := os.WriteFile(file, []byte(testNetrc), 0666); err != nil {
t.Fatal(err)
}
t.Setenv("NETRC", file)
db := Netrc()
if secret, ok := db.Get("missing"); secret != "" || ok != false {
t.Errorf("Get(missing) = %q, %v, want %q, %v", secret, ok, "", false)
}
if secret, ok := db.Get("example.com"); secret != "u2:p2" || ok != true {
t.Errorf("Get(example.com) = %q, %v, want %q, %v", secret, ok, "u2:p2", true)
}
testutil.StopPanic(func() {
db.Set("name", "value")
t.Errorf("Set did not panic")
})
}
var testNetrc = `
machine example.com login u1 password p1
machine missing login u password p and more
machine example.com login u2 password p2
`
func TestEmpty(t *testing.T) {
db := Empty()
if secret, ok := db.Get("missing"); secret != "" || ok != false {
t.Errorf("Get(missing) = %q, %v, want %q, %v", secret, ok, "", false)
}
testutil.StopPanic(func() {
db.Set("name", "value")
t.Errorf("Set did not panic")
})
}