Skip to content

Commit

Permalink
refactor: add more test for ssh.go
Browse files Browse the repository at this point in the history
Signed-off-by: Suraj Shirvankar <[email protected]>
  • Loading branch information
h0lyalg0rithm authored and glimchb committed Aug 16, 2024
1 parent aa72379 commit 8a96a2a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion sztp-agent/pkg/secureagent/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) {
file string
content string
Algorithm string
KeyOnly bool
}
tests := []struct {
name string
Expand All @@ -24,6 +25,7 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) {
file: "/tmp/test.pub",
content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR",
Algorithm: "ssh-ed25519",
KeyOnly: false,
},
want: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR",
},
Expand All @@ -33,6 +35,7 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) {
file: "/tmp/test.pub",
content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment",
Algorithm: "ssh-ed25519",
KeyOnly: false,
},
want: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR",
},
Expand All @@ -42,6 +45,7 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) {
file: "/tmp/test.pub",
content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment error",
Algorithm: "ssh-ed25519",
KeyOnly: false,
},
want: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR",
},
Expand All @@ -50,6 +54,7 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) {
args: args{
file: "/tmp/test.pub",
content: "ssh-ed25519",
KeyOnly: false,
},
want: "ssh-ed25519",
},
Expand All @@ -58,17 +63,28 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) {
args: args{
file: "/tmp/test.pub",
content: "",
KeyOnly: false,
},
want: "",
},
{
name: "Test file only key",
args: args{
file: "/tmp/test.pub",
content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR",
Algorithm: "ssh-ed25519",
KeyOnly: true,
},
want: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.content != "" {
createTempTestFile(tt.args.file, tt.args.content, true)
}
for _, key := range readSSHHostKeyPublicFiles(tt.args.file) {
if got := getSSHHostKeyString(key, true); !reflect.DeepEqual(got, tt.want) {
if got := getSSHHostKeyString(key, !tt.args.KeyOnly); !reflect.DeepEqual(got, tt.want) {
t.Errorf("readSSHHostKeyPublicFiles() - got: %v, want %v", got, tt.want)
}
}
Expand Down

0 comments on commit 8a96a2a

Please sign in to comment.