|
| 1 | +package testcerts |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestGeneratingCerts(t *testing.T) { |
| 10 | + _, _, err := GenerateCerts() |
| 11 | + if err != nil { |
| 12 | + t.Errorf("Error while generating certificates - %s", err) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +func TestGeneratingCertsToFile(t *testing.T) { |
| 17 | + t.Run("Test the happy path", func(t *testing.T) { |
| 18 | + tempDir, err := os.MkdirTemp("", "") |
| 19 | + if err != nil { |
| 20 | + t.Errorf("Error creating temporary directory: %s", err) |
| 21 | + } |
| 22 | + defer os.RemoveAll(tempDir) |
| 23 | + |
| 24 | + certPath := filepath.Join(tempDir, "cert") |
| 25 | + keyPath := filepath.Join(tempDir, "key") |
| 26 | + |
| 27 | + err = GenerateCertsToFile(certPath, keyPath) |
| 28 | + if err != nil { |
| 29 | + t.Errorf("Error while generating certificates to files - %s", err) |
| 30 | + } |
| 31 | + |
| 32 | + // Check if Cert file exists |
| 33 | + _, err = os.Stat(certPath) |
| 34 | + if err != nil { |
| 35 | + t.Errorf("Error while generating certificates to files file error - %s", err) |
| 36 | + } |
| 37 | + |
| 38 | + // Check if Key file exists |
| 39 | + _, err = os.Stat(keyPath) |
| 40 | + if err != nil { |
| 41 | + t.Errorf("Error while generating certificates to files file error - %s", err) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("Testing the unhappy path for cert files", func(t *testing.T) { |
| 46 | + tempDir, err := os.MkdirTemp("", "") |
| 47 | + if err != nil { |
| 48 | + t.Errorf("Error creating temporary directory: %s", err) |
| 49 | + } |
| 50 | + defer os.RemoveAll(tempDir) |
| 51 | + |
| 52 | + certPath := filepath.Join(tempDir, "doesntexist", "cert") |
| 53 | + keyPath := filepath.Join(tempDir, "key") |
| 54 | + |
| 55 | + err = GenerateCertsToFile(certPath, keyPath) |
| 56 | + if err == nil { |
| 57 | + t.Errorf("Expected error when generating a certificate with a bad path got nil") |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("Testing the unhappy path for key files", func(t *testing.T) { |
| 62 | + tempDir, err := os.MkdirTemp("", "") |
| 63 | + if err != nil { |
| 64 | + t.Errorf("Error creating temporary directory: %s", err) |
| 65 | + } |
| 66 | + defer os.RemoveAll(tempDir) |
| 67 | + |
| 68 | + certPath := filepath.Join(tempDir, "cert") |
| 69 | + keyPath := filepath.Join(tempDir, "doesntexist", "key") |
| 70 | + |
| 71 | + err = GenerateCertsToFile(certPath, keyPath) |
| 72 | + if err == nil { |
| 73 | + t.Errorf("Expected error when generating a key with a bad path got nil") |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("Testing the unhappy path for insufficient permissions", func(t *testing.T) { |
| 78 | + dir, err := os.MkdirTemp("", "permission-test") |
| 79 | + if err != nil { |
| 80 | + t.Errorf("Error creating temp directory - %s", err) |
| 81 | + } |
| 82 | + defer os.RemoveAll(dir) |
| 83 | + |
| 84 | + // Change permissions of the temp directory so that it can't be written to |
| 85 | + err = os.Chmod(dir, 0444) |
| 86 | + if err != nil { |
| 87 | + t.Errorf("Error changing permissions of temp directory - %s", err) |
| 88 | + } |
| 89 | + |
| 90 | + certPath := filepath.Join(dir, "cert") |
| 91 | + keyPath := filepath.Join(dir, "key") |
| 92 | + |
| 93 | + err = GenerateCertsToFile(certPath, keyPath) |
| 94 | + if err == nil { |
| 95 | + t.Errorf("Expected error when generating certificate with insufficient permissions, got nil") |
| 96 | + } |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +func TestGenerateCertsToTempFile(t *testing.T) { |
| 101 | + t.Run("Test the happy path", func(t *testing.T) { |
| 102 | + certFile, keyFile, err := GenerateCertsToTempFile("/tmp") |
| 103 | + if err != nil { |
| 104 | + t.Errorf("Error while generating certificates to temp files - %s", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Check if Cert file exists |
| 108 | + _, err = os.Stat(certFile) |
| 109 | + if err != nil { |
| 110 | + t.Errorf("Error while generating certificates to temp files file error - %s", err) |
| 111 | + } |
| 112 | + _ = os.Remove(certFile) |
| 113 | + |
| 114 | + // Check if Key file exists |
| 115 | + _, err = os.Stat(keyFile) |
| 116 | + if err != nil { |
| 117 | + t.Errorf("Error while generating certificates to temp files file error - %s", err) |
| 118 | + } |
| 119 | + _ = os.Remove(keyFile) |
| 120 | + }) |
| 121 | + |
| 122 | + t.Run("Testing the unhappy path when creating cert temp file", func(t *testing.T) { |
| 123 | + _, _, err := GenerateCertsToTempFile("/doesnotexist") |
| 124 | + if err == nil { |
| 125 | + t.Errorf("Expected error when generating a certificate with a bad directory path got nil") |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + t.Run("Testing the unhappy path for insufficient permissions when creating temp file", func(t *testing.T) { |
| 130 | + dir, err := os.MkdirTemp("", "permission-test") |
| 131 | + if err != nil { |
| 132 | + t.Errorf("Error creating temp directory - %s", err) |
| 133 | + } |
| 134 | + defer os.RemoveAll(dir) |
| 135 | + |
| 136 | + // Change permissions of the temp directory so that it can't be written to |
| 137 | + err = os.Chmod(dir, 0444) |
| 138 | + if err != nil { |
| 139 | + t.Errorf("Error changing permissions of temp directory - %s", err) |
| 140 | + } |
| 141 | + |
| 142 | + _, _, err = GenerateCertsToTempFile(dir) |
| 143 | + if err == nil { |
| 144 | + t.Errorf("Expected error when generating a key with a bad directory path got nil") |
| 145 | + } |
| 146 | + }) |
| 147 | +} |
0 commit comments