|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + chmodSync, |
| 4 | + existsSync, |
| 5 | + mkdtempSync, |
| 6 | + rmSync, |
| 7 | + statSync, |
| 8 | + symlinkSync, |
| 9 | + writeFileSync, |
| 10 | +} from 'node:fs'; |
| 11 | +import { tmpdir } from 'node:os'; |
| 12 | +import * as path from 'node:path'; |
| 13 | +import { ensureSocketDir } from '../socket-path.ts'; |
| 14 | + |
| 15 | +let tempDir: string; |
| 16 | + |
| 17 | +describe('ensureSocketDir', () => { |
| 18 | + beforeEach(() => { |
| 19 | + tempDir = mkdtempSync(path.join(tmpdir(), 'xcodebuildmcp-socket-path-')); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + rmSync(tempDir, { recursive: true, force: true }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('creates a private socket directory', () => { |
| 27 | + const socketPath = path.join(tempDir, 'daemon', 'd.sock'); |
| 28 | + |
| 29 | + ensureSocketDir(socketPath); |
| 30 | + |
| 31 | + expect(existsSync(path.dirname(socketPath))).toBe(true); |
| 32 | + expect(statSync(path.dirname(socketPath)).mode & 0o777).toBe(0o700); |
| 33 | + }); |
| 34 | + |
| 35 | + it('tightens permissions on an existing socket directory owned by the current user', () => { |
| 36 | + const dir = path.join(tempDir, 'daemon'); |
| 37 | + const socketPath = path.join(dir, 'd.sock'); |
| 38 | + ensureSocketDir(socketPath); |
| 39 | + chmodSync(dir, 0o755); |
| 40 | + |
| 41 | + ensureSocketDir(socketPath); |
| 42 | + |
| 43 | + expect(statSync(dir).mode & 0o777).toBe(0o700); |
| 44 | + }); |
| 45 | + |
| 46 | + it('rejects symlink socket directories', () => { |
| 47 | + const targetDir = path.join(tempDir, 'target'); |
| 48 | + const linkDir = path.join(tempDir, 'daemon'); |
| 49 | + ensureSocketDir(path.join(targetDir, 'placeholder.sock')); |
| 50 | + symlinkSync(targetDir, linkDir); |
| 51 | + |
| 52 | + expect(() => ensureSocketDir(path.join(linkDir, 'd.sock'))).toThrow(/cannot be a symlink/u); |
| 53 | + }); |
| 54 | + |
| 55 | + it('rejects non-directory socket path parents', () => { |
| 56 | + const filePath = path.join(tempDir, 'daemon'); |
| 57 | + writeFileSync(filePath, 'not a directory'); |
| 58 | + |
| 59 | + expect(() => ensureSocketDir(path.join(filePath, 'd.sock'))).toThrow(/not a directory/u); |
| 60 | + }); |
| 61 | +}); |
0 commit comments