1
0
mirror of https://github.com/ssh-vault/ssh-vault.git synced 2025-04-19 07:42:18 +03:00
ssh-vault/cache_test.go
nbari 83692b27d1
modified: cache_test.go
modified:   getkey.go
	modified:   getkey_test.go
	modified:   vault.go
2016-10-24 12:23:58 +02:00

66 lines
1.2 KiB
Go

package sshvault
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestCacheIsFile(t *testing.T) {
cache := &cache{}
if cache.IsFile("/") {
t.Errorf("Expecting false")
}
if !cache.IsFile("cache_test.go") {
t.Errorf("Expecting true")
}
}
type mockSchlosser struct{}
func (m mockSchlosser) GetKey(u string) ([]string, error) {
switch u {
case "alice":
return []string{"ssh-rsa ABC"}, nil
case "bob":
return nil, nil
default:
return nil, fmt.Errorf("Error")
}
}
func TestCacheGet(t *testing.T) {
dir, err := ioutil.TempDir("", "cache")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(dir) // clean up
var testTable = []struct {
user string
key int
out string
err bool
}{
{"alice", 0, "alice.key-1", false},
{"alice", 1, "alice.key-1", false},
{"alice", 2, "", true},
{"bob", 1, "", true},
}
cache := &cache{dir}
gk := mockSchlosser{}
for _, tt := range testTable {
out, err := cache.Get(gk, tt.user, tt.key)
if tt.err {
if err == nil {
t.Error("Expecting error")
}
} else if strings.HasPrefix(out, tt.out) {
t.Errorf("%q != %q", tt.out, out)
}
//fmt.Printf("out = %+v\n", out)
//fmt.Printf("err = %+v\n", err)
}
}