master
 1package generate
 2
 3import (
 4	"os"
 5	"strings"
 6	"testing"
 7)
 8
 9func TestGenerateSSHKeyEDSA(t *testing.T) {
10	publicKey, privateKey, err := generateSSHKeyEDSA()
11	if err != nil {
12		t.Fatalf("generateSSHKeyEDSA() returned error: %v", err)
13	}
14
15	if len(publicKey) == 0 {
16		t.Error("generateSSHKeyEDSA() returned empty public key")
17	}
18
19	if len(privateKey) == 0 {
20		t.Error("generateSSHKeyEDSA() returned empty private key")
21	}
22
23	// Check public key format
24	if !strings.HasPrefix(publicKey, "ssh-ed25519 ") {
25		t.Error("generateSSHKeyEDSA() public key should start with 'ssh-ed25519 '")
26	}
27
28	// Check private key format
29	if !strings.Contains(privateKey, "BEGIN OPENSSH PRIVATE KEY") {
30		t.Error("generateSSHKeyEDSA() private key should contain PEM header")
31	}
32}
33
34func TestReturnKeyPath(t *testing.T) {
35	path, err := returnKeyPath("test_key")
36	if err != nil {
37		t.Fatalf("returnKeyPath() returned error: %v", err)
38	}
39
40	if len(path) == 0 {
41		t.Error("returnKeyPath() returned empty path")
42	}
43
44	if !strings.Contains(path, "test_key") {
45		t.Error("returnKeyPath() should contain the filename")
46	}
47}
48
49func TestWriteStringToFile(t *testing.T) {
50	tmpFile := "/tmp/test_ssh_key_write"
51	defer func(name string) {
52		err := os.Remove(name)
53		if err != nil {
54			t.Error("error removing tmp file")
55		}
56	}(tmpFile)
57
58	err := writeStringToFile(tmpFile, "test content", 0600)
59	if err != nil {
60		t.Fatalf("writeStringToFile() returned error: %v", err)
61	}
62
63	// Verify file exists
64	if _, err := os.Stat(tmpFile); os.IsNotExist(err) {
65		t.Error("writeStringToFile() did not create file")
66	}
67
68	// Verify content
69	content, err := os.ReadFile(tmpFile)
70	if err != nil {
71		t.Fatalf("Failed to read test file: %v", err)
72	}
73
74	if string(content) != "test content" {
75		t.Errorf("writeStringToFile() wrote %q, want %q", string(content), "test content")
76	}
77}
78
79func TestSSHKey(t *testing.T) {
80	// Test with no args (should return error)
81	err := SSHKey([]string{})
82	if err == nil {
83		t.Error("SSHKey() with no args should return error")
84	}
85}