UNPKG

spanwright

Version:

CLI tool to generate Cloud Spanner E2E testing framework projects with Go database tools and Playwright browser automation

230 lines 11.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); const secure_temp_1 = require("../secure-temp"); (0, vitest_1.describe)('secure-temp', () => { (0, vitest_1.describe)('createSecureTempDir', () => { (0, vitest_1.it)('should create a unique temporary directory', async () => { const tempDir = await (0, secure_temp_1.createSecureTempDir)(); (0, vitest_1.expect)(tempDir).toBeTruthy(); (0, vitest_1.expect)(path.isAbsolute(tempDir)).toBe(true); (0, vitest_1.expect)(tempDir.includes('spanwright-')).toBe(true); (0, vitest_1.expect)(fs.existsSync(tempDir)).toBe(true); // Check that it's a directory const stats = fs.statSync(tempDir); (0, vitest_1.expect)(stats.isDirectory()).toBe(true); }); (0, vitest_1.it)('should create directory with custom prefix', async () => { const tempDir = await (0, secure_temp_1.createSecureTempDir)('custom-prefix-'); (0, vitest_1.expect)(tempDir.includes('custom-prefix-')).toBe(true); (0, vitest_1.expect)(fs.existsSync(tempDir)).toBe(true); }); (0, vitest_1.it)('should create unique directories on multiple calls', async () => { const tempDir1 = await (0, secure_temp_1.createSecureTempDir)(); const tempDir2 = await (0, secure_temp_1.createSecureTempDir)(); (0, vitest_1.expect)(tempDir1).not.toBe(tempDir2); (0, vitest_1.expect)(fs.existsSync(tempDir1)).toBe(true); (0, vitest_1.expect)(fs.existsSync(tempDir2)).toBe(true); }); }); (0, vitest_1.describe)('createSecureTempDirSync', () => { (0, vitest_1.it)('should create a unique temporary directory synchronously', () => { const tempDir = (0, secure_temp_1.createSecureTempDirSync)(); (0, vitest_1.expect)(tempDir).toBeTruthy(); (0, vitest_1.expect)(path.isAbsolute(tempDir)).toBe(true); (0, vitest_1.expect)(tempDir.includes('spanwright-')).toBe(true); (0, vitest_1.expect)(fs.existsSync(tempDir)).toBe(true); // Check that it's a directory const stats = fs.statSync(tempDir); (0, vitest_1.expect)(stats.isDirectory()).toBe(true); }); (0, vitest_1.it)('should create directory with custom prefix', () => { const tempDir = (0, secure_temp_1.createSecureTempDirSync)('sync-test-'); (0, vitest_1.expect)(tempDir.includes('sync-test-')).toBe(true); (0, vitest_1.expect)(fs.existsSync(tempDir)).toBe(true); }); }); (0, vitest_1.describe)('withTempDir', () => { (0, vitest_1.it)('should execute function with temp directory and clean up', async () => { let capturedTempDir = ''; const result = await (0, secure_temp_1.withTempDir)(async (tempDir) => { capturedTempDir = tempDir; (0, vitest_1.expect)(fs.existsSync(tempDir)).toBe(true); // Create a test file const testFile = path.join(tempDir, 'test.txt'); fs.writeFileSync(testFile, 'test content'); (0, vitest_1.expect)(fs.existsSync(testFile)).toBe(true); return 'success'; }); (0, vitest_1.expect)(result).toBe('success'); (0, vitest_1.expect)(capturedTempDir).toBeTruthy(); // Directory should be cleaned up (0, vitest_1.expect)(fs.existsSync(capturedTempDir)).toBe(false); }); (0, vitest_1.it)('should clean up even if function throws', async () => { let capturedTempDir = ''; try { await (0, secure_temp_1.withTempDir)(async (tempDir) => { capturedTempDir = tempDir; (0, vitest_1.expect)(fs.existsSync(tempDir)).toBe(true); throw new Error('Test error'); }); } catch (error) { (0, vitest_1.expect)(error.message).toBe('Test error'); } (0, vitest_1.expect)(capturedTempDir).toBeTruthy(); // Directory should still be cleaned up (0, vitest_1.expect)(fs.existsSync(capturedTempDir)).toBe(false); }); (0, vitest_1.it)('should work with custom prefix', async () => { let capturedTempDir = ''; await (0, secure_temp_1.withTempDir)(async (tempDir) => { capturedTempDir = tempDir; (0, vitest_1.expect)(tempDir.includes('custom-')).toBe(true); }, 'custom-'); (0, vitest_1.expect)(fs.existsSync(capturedTempDir)).toBe(false); }); }); (0, vitest_1.describe)('createSecureTempFile', () => { (0, vitest_1.it)('should create a secure temporary file', async () => { const tempDir = await (0, secure_temp_1.createSecureTempDir)(); try { const { fd, path: filePath } = await (0, secure_temp_1.createSecureTempFile)(tempDir); (0, vitest_1.expect)(filePath).toBeTruthy(); (0, vitest_1.expect)(path.isAbsolute(filePath)).toBe(true); (0, vitest_1.expect)(filePath.includes('tmp-')).toBe(true); (0, vitest_1.expect)(fs.existsSync(filePath)).toBe(true); // Check file stats const stats = fs.statSync(filePath); (0, vitest_1.expect)(stats.isFile()).toBe(true); (0, vitest_1.expect)(stats.mode & 0o777).toBe(0o600); // Check permissions // Close the file descriptor await fd.close(); } finally { // Clean up fs.rmSync(tempDir, { recursive: true, force: true }); } }); (0, vitest_1.it)('should create file with custom prefix', async () => { const tempDir = await (0, secure_temp_1.createSecureTempDir)(); try { const { fd, path: filePath } = await (0, secure_temp_1.createSecureTempFile)(tempDir, 'custom-'); (0, vitest_1.expect)(filePath.includes('custom-')).toBe(true); (0, vitest_1.expect)(fs.existsSync(filePath)).toBe(true); await fd.close(); } finally { // Clean up fs.rmSync(tempDir, { recursive: true, force: true }); } }); (0, vitest_1.it)('should create unique files on multiple calls', async () => { const tempDir = await (0, secure_temp_1.createSecureTempDir)(); try { const { fd: fd1, path: filePath1 } = await (0, secure_temp_1.createSecureTempFile)(tempDir); const { fd: fd2, path: filePath2 } = await (0, secure_temp_1.createSecureTempFile)(tempDir); (0, vitest_1.expect)(filePath1).not.toBe(filePath2); (0, vitest_1.expect)(fs.existsSync(filePath1)).toBe(true); (0, vitest_1.expect)(fs.existsSync(filePath2)).toBe(true); await fd1.close(); await fd2.close(); } finally { // Clean up fs.rmSync(tempDir, { recursive: true, force: true }); } }); }); (0, vitest_1.describe)('security properties', () => { (0, vitest_1.it)('should create directories in system temp directory', async () => { const tempDir = await (0, secure_temp_1.createSecureTempDir)(); const systemTempDir = os.tmpdir(); // Resolve both paths to handle symlinks const resolvedTempDir = fs.realpathSync(tempDir); const resolvedSystemTempDir = fs.realpathSync(systemTempDir); (0, vitest_1.expect)(resolvedTempDir.startsWith(resolvedSystemTempDir)).toBe(true); }); (0, vitest_1.it)('should have proper directory permissions', async () => { const tempDir = await (0, secure_temp_1.createSecureTempDir)(); try { const stats = fs.statSync(tempDir); // Check that it's a directory (0, vitest_1.expect)(stats.isDirectory()).toBe(true); // On Unix systems, check permissions if (process.platform !== 'win32') { const mode = stats.mode & 0o777; (0, vitest_1.expect)(mode).toBe(0o700); // Should be rwx------ } } finally { // Clean up fs.rmSync(tempDir, { recursive: true, force: true }); } }); (0, vitest_1.it)('should generate sufficiently random directory names', async () => { const tempDirs = await Promise.all([ (0, secure_temp_1.createSecureTempDir)(), (0, secure_temp_1.createSecureTempDir)(), (0, secure_temp_1.createSecureTempDir)(), (0, secure_temp_1.createSecureTempDir)(), (0, secure_temp_1.createSecureTempDir)(), ]); try { // Extract the random parts const randomParts = tempDirs.map(dir => path.basename(dir)); // All should be different const uniqueParts = new Set(randomParts); (0, vitest_1.expect)(uniqueParts.size).toBe(randomParts.length); // Each should have sufficient length (prefix + random chars) randomParts.forEach(part => { (0, vitest_1.expect)(part.length).toBeGreaterThan(10); }); } finally { // Clean up all directories tempDirs.forEach(dir => { fs.rmSync(dir, { recursive: true, force: true }); }); } }); }); }); //# sourceMappingURL=secure-temp.test.js.map