set-sshkeys/main_test.go

214 lines
6.4 KiB
Go

package main
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestAdd(t *testing.T) {
require := require.New(t)
sshKeys := addSSHKeys([]*sshKey{}, []*sshKey{{algorithm: "ssh-ed25519", pubKey: "abcdefg"}})
require.Len(sshKeys, 1)
sshKeys = addSSHKeys(sshKeys, []*sshKey{{algorithm: "ssh-ed25519", pubKey: "abcdefg"}})
require.Len(sshKeys, 1)
sshKeys = addSSHKeys(sshKeys, []*sshKey{{algorithm: "ssh-ed25519", pubKey: "abcdefg", alias: "buxdehude"}})
require.Len(sshKeys, 1)
require.Equal("buxdehude", sshKeys[0].alias)
sshKeys = addSSHKeys(sshKeys, []*sshKey{{algorithm: "ssh-ed25519", pubKey: "asdsadasd", alias: "hello@world"}})
require.Len(sshKeys, 2)
sshKeys = addSSHKeys(sshKeys, []*sshKey{{algorithm: "ssh-ed25519", pubKey: "asdsadasd", alias: "world@hello"}})
require.Len(sshKeys, 2)
require.Equal("hello@world", sshKeys[1].alias)
sshKeys = addSSHKeys(sshKeys, []*sshKey{{algorithm: "ssh-ed25519", pubKey: "", alias: "world@hello"}})
require.Len(sshKeys, 2)
sshKeys = addSSHKeys(sshKeys, []*sshKey{{algorithm: "", pubKey: "asdsadasd", alias: "world@hello"}})
require.Len(sshKeys, 2)
}
func TestRead(t *testing.T) {
require := require.New(t)
testCases := []struct {
expectedSSHKeys []*sshKey
b []byte
}{
{
expectedSSHKeys: []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "abcdefg", alias: "hello@world"},
},
b: []byte(`ssh-ed25519 abcdefg hello@world`),
},
{
expectedSSHKeys: []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "abcdefg", alias: "hello@world"},
},
b: []byte(`ssh-ed25519 abcdefg hello@world
ssh-ed25519 abcdefg`),
},
{
expectedSSHKeys: []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "abcdefg", alias: "hello@world"},
},
b: []byte(`ssh-ed25519 abcdefg
ssh-ed25519 abcdefg hello@world`),
},
{
expectedSSHKeys: []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "abcdefg", alias: "hello@world"},
},
b: []byte(`ssh-ed25519 abcdefg hello@world
ssh-ed25519 abcdefg world@hello`),
},
{
expectedSSHKeys: []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "abcdefg", alias: "hello@world"},
},
b: []byte(`ssh-ed25519 abcdefg hello@world
ssh-ed25519
abcdefg test`),
},
}
for i := range testCases {
sshKeys, err := readSSHKeys(bytes.NewReader(testCases[i].b))
require.NoError(err)
require.ElementsMatch(testCases[i].expectedSSHKeys, sshKeys)
}
}
func TestRemove(t *testing.T) {
require := require.New(t)
s := []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
{algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"},
}
r := []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
}
result := removeSSHKeys(s, r)
require.Len(result, 1)
r = []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
{algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"},
}
result = removeSSHKeys(s, r)
require.Len(result, 0)
r = []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf"},
}
result = removeSSHKeys(s, r)
require.Len(result, 1)
}
func TestSSHKeyCompare(t *testing.T) {
require := require.New(t)
testCases := []struct {
s *sshKey
r *sshKey
expectedValue bool
}{
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, &sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, true},
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, &sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh"}, true},
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh"}, &sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, true},
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, &sshKey{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"}, false},
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, &sshKey{algorithm: "ssh-rsa", pubKey: "sdfsdf", alias: "hello@world"}, false},
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, &sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "hello@world"}, true},
}
for i := range testCases {
require.Equal(testCases[i].expectedValue, testCases[i].s.Compare(testCases[i].r), "TestCase %v does not match the expected value", i)
}
}
func TestSSHKeyString(t *testing.T) {
require := require.New(t)
s := []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
{algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"},
{algorithm: "ssh-rsa", pubKey: "asdfasdsdfsfadsad"},
}
b := []string{
"ssh-ed25519 sdfsdf hello@world",
"ssh-rsa asdfasdadsad world@hello",
"ssh-rsa asdfasdsdfsfadsad",
}
for i := range s {
require.Equal(b[i], s[i].String())
}
}
func TestSSHKeyValidate(t *testing.T) {
require := require.New(t)
testCases := []struct {
s *sshKey
expectedValue error
}{
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh", alias: "world@hello"}, nil},
{&sshKey{algorithm: "ssh-rsa", pubKey: "abcdefgh"}, nil},
{&sshKey{algorithm: "ssh-rsa", pubKey: ""}, fmt.Errorf("Missing attribute pubKey")},
{&sshKey{algorithm: "ssh-rsa"}, fmt.Errorf("Missing attribute pubKey")},
{&sshKey{algorithm: "", pubKey: "abcdefgh"}, fmt.Errorf("Missing attribute algorithm")},
{&sshKey{pubKey: "abcdefgh"}, fmt.Errorf("Missing attribute algorithm")},
{&sshKey{algorithm: "ssh-rsa", alias: "sdfsdf"}, fmt.Errorf("Missing attribute pubKey")},
{&sshKey{pubKey: "abcdefgh", alias: "sdfsdf"}, fmt.Errorf("Missing attribute algorithm")},
}
for i := range testCases {
if testCases[i].expectedValue == nil {
require.NoError(testCases[i].s.Validate())
continue
}
require.EqualError(testCases[i].expectedValue, testCases[i].s.Validate().Error())
}
}
func TestWrite(t *testing.T) {
require := require.New(t)
testCases := []struct {
expectedSSHKeys []*sshKey
b []byte
}{
{
expectedSSHKeys: []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "abcdefg", alias: "hello@world"},
{algorithm: "ssh-rsa", pubKey: "gfedcba"},
{algorithm: "ssh-rsa", pubKey: "1234567", alias: "world@hello"},
},
b: []byte(`ssh-ed25519 abcdefg hello@world
ssh-rsa gfedcba
ssh-rsa 1234567 world@hello
`),
},
}
for i := range testCases {
b := bytes.NewBuffer([]byte{})
err := writeSSHKeys(b, testCases[i].expectedSSHKeys)
require.NoError(err)
require.Equal(testCases[i].b, b.Bytes())
}
}