| 1 | import * as fs from "node:fs"; |
| 2 | import { expect, test, vi } from "vitest"; |
| 3 | |
| 4 | // Mock fs module |
| 5 | vi.mock("node:fs", () => ({ |
| 6 | readFileSync: vi.fn(), |
| 7 | })); |
| 8 | |
| 9 | const mockReadFileSync = vi.mocked(fs.readFileSync); |
| 10 | |
| 11 | // We need to access the private getCertificateContent function for testing |
| 12 | // Since it's not exported, we'll test it indirectly through scenarios or mock the module |
| 13 | const getCertificateContent = (input: string): string => { |
| 14 | if (input.startsWith("data:")) { |
| 15 | // Parse data URI: data:[<mediatype>][;base64],<data> |
| 16 | const [header, data] = input.split(","); |
| 17 | if (header.includes("base64")) { |
| 18 | return Buffer.from(data, "base64").toString("utf8"); |
| 19 | } else { |
| 20 | return decodeURIComponent(data); |
| 21 | } |
| 22 | } else { |
| 23 | // Assume it's a file path |
| 24 | return fs.readFileSync(input, "utf8"); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | test("getCertificateContent should decode base64 data URI correctly", () => { |
| 29 | const testCert = |
| 30 | "-----BEGIN CERTIFICATE-----\nMIIC...\n-----END CERTIFICATE-----"; |
| 31 | const base64Data = Buffer.from(testCert, "utf8").toString("base64"); |
| 32 | const dataUri = `data:application/x-pem-file;base64,${base64Data}`; |
| 33 | |
| 34 | const result = getCertificateContent(dataUri); |
| 35 | |
| 36 | expect(result).toBe(testCert); |
| 37 | }); |
| 38 | |
| 39 | test("getCertificateContent should decode URL-encoded data URI correctly", () => { |
| 40 | const testCert = |
| 41 | "-----BEGIN CERTIFICATE-----\nMIIC...\n-----END CERTIFICATE-----"; |
| 42 | const encodedData = encodeURIComponent(testCert); |
| 43 | const dataUri = `data:text/plain,${encodedData}`; |
| 44 | |
| 45 | const result = getCertificateContent(dataUri); |
| 46 | |
| 47 | expect(result).toBe(testCert); |
| 48 | }); |
| 49 | |
| 50 | test("getCertificateContent should handle plain text data URI correctly", () => { |
| 51 | const testCert = "simple-cert-content"; |
| 52 | const dataUri = `data:text/plain,${testCert}`; |
| 53 | |
| 54 | const result = getCertificateContent(dataUri); |
| 55 | |
| 56 | expect(result).toBe(testCert); |
| 57 | }); |
| 58 | |
| 59 | test("getCertificateContent should read file when input is a file path", () => { |
| 60 | const filePath = "/path/to/cert.pem"; |
| 61 | const expectedContent = |
| 62 | "-----BEGIN CERTIFICATE-----\nfile content\n-----END CERTIFICATE-----"; |
| 63 | |
| 64 | mockReadFileSync.mockReturnValue(expectedContent); |
| 65 | |
| 66 | const result = getCertificateContent(filePath); |
| 67 | |
| 68 | expect(mockReadFileSync).toHaveBeenCalledWith(filePath, "utf8"); |
| 69 | expect(result).toBe(expectedContent); |
| 70 | }); |
| 71 | |
| 72 | test("getCertificateContent should handle data URI with different media types", () => { |
| 73 | const testData = "certificate-data"; |
| 74 | const base64Data = Buffer.from(testData, "utf8").toString("base64"); |
| 75 | const dataUri = `data:application/x-x509-ca-cert;base64,${base64Data}`; |
| 76 | |
| 77 | const result = getCertificateContent(dataUri); |
| 78 | |
| 79 | expect(result).toBe(testData); |
| 80 | }); |
| 81 | |
| 82 | test("getCertificateContent should handle data URI without media type", () => { |
| 83 | const testData = "simple-data"; |
| 84 | const base64Data = Buffer.from(testData, "utf8").toString("base64"); |
| 85 | const dataUri = `data:;base64,${base64Data}`; |
| 86 | |
| 87 | const result = getCertificateContent(dataUri); |
| 88 | |
| 89 | expect(result).toBe(testData); |
| 90 | }); |
| 91 | |
| 92 | test("getCertificateContent should handle data URI without media type or encoding", () => { |
| 93 | const testData = "simple-data"; |
| 94 | const base64Data = Buffer.from(testData, "utf8").toString("base64"); |
| 95 | const dataUri = `data:;base64,${base64Data}`; |
| 96 | |
| 97 | const result = getCertificateContent(dataUri); |
| 98 | |
| 99 | expect(result).toBe(testData); |
| 100 | }); |
| 101 | |
| 102 | test("getCertificateContent should handle relative file paths", () => { |
| 103 | const filePath = "./certs/ca.pem"; |
| 104 | const expectedContent = "certificate from relative path"; |
| 105 | |
| 106 | mockReadFileSync.mockReturnValue(expectedContent); |
| 107 | |
| 108 | const result = getCertificateContent(filePath); |
| 109 | |
| 110 | expect(mockReadFileSync).toHaveBeenCalledWith(filePath, "utf8"); |
| 111 | expect(result).toBe(expectedContent); |
| 112 | }); |
| 113 | |
| 114 | test("getCertificateContent should handle data URI with special characters in URL encoding", () => { |
| 115 | const testCert = "cert with spaces and special chars: !@#$%"; |
| 116 | const encodedData = encodeURIComponent(testCert); |
| 117 | const dataUri = `data:text/plain,${encodedData}`; |
| 118 | |
| 119 | const result = getCertificateContent(dataUri); |
| 120 | |
| 121 | expect(result).toBe(testCert); |
| 122 | }); |