|
| 1 | +import { parseResolvConf, detectHostDnsServers, getEffectiveDnsServers, DEFAULT_DNS_SERVERS } from './dns-resolver'; |
| 2 | +import * as fs from 'fs'; |
| 3 | + |
| 4 | +jest.mock('fs'); |
| 5 | +const mockedFs = fs as jest.Mocked<typeof fs>; |
| 6 | + |
| 7 | +const mockLogger = { |
| 8 | + debug: jest.fn(), |
| 9 | + info: jest.fn(), |
| 10 | + warn: jest.fn(), |
| 11 | + error: jest.fn(), |
| 12 | + success: jest.fn(), |
| 13 | + setLevel: jest.fn(), |
| 14 | +}; |
| 15 | + |
| 16 | +beforeEach(() => { |
| 17 | + jest.clearAllMocks(); |
| 18 | +}); |
| 19 | + |
| 20 | +describe('parseResolvConf', () => { |
| 21 | + it('extracts nameservers from standard content', () => { |
| 22 | + const content = `# Generated by systemd-resolved |
| 23 | +nameserver 1.1.1.1 |
| 24 | +nameserver 9.9.9.9 |
| 25 | +search example.com |
| 26 | +`; |
| 27 | + expect(parseResolvConf(content)).toEqual(['1.1.1.1', '9.9.9.9']); |
| 28 | + }); |
| 29 | + |
| 30 | + it('ignores comments and empty lines', () => { |
| 31 | + const content = ` |
| 32 | +# This is a comment |
| 33 | +; Another comment style |
| 34 | +
|
| 35 | +nameserver 1.1.1.1 |
| 36 | +
|
| 37 | +# nameserver 2.2.2.2 |
| 38 | +nameserver 8.8.8.8 |
| 39 | +`; |
| 40 | + expect(parseResolvConf(content)).toEqual(['1.1.1.1', '8.8.8.8']); |
| 41 | + }); |
| 42 | + |
| 43 | + it('skips invalid IPs', () => { |
| 44 | + const content = `nameserver 1.1.1.1 |
| 45 | +nameserver not-an-ip |
| 46 | +nameserver 8.8.8.8 |
| 47 | +`; |
| 48 | + expect(parseResolvConf(content)).toEqual(['1.1.1.1', '8.8.8.8']); |
| 49 | + }); |
| 50 | + |
| 51 | + it('handles IPv6 nameservers', () => { |
| 52 | + const content = `nameserver 2001:4860:4860::8888 |
| 53 | +nameserver 1.1.1.1 |
| 54 | +nameserver ::1 |
| 55 | +`; |
| 56 | + expect(parseResolvConf(content)).toEqual(['2001:4860:4860::8888', '1.1.1.1', '::1']); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('detectHostDnsServers', () => { |
| 61 | + it('filters out 127.0.0.11 (Docker embedded DNS)', () => { |
| 62 | + mockedFs.readFileSync.mockReturnValue( |
| 63 | + 'nameserver 127.0.0.11\nnameserver 1.1.1.1\nnameserver 8.8.8.8\n' |
| 64 | + ); |
| 65 | + const result = detectHostDnsServers(mockLogger as any); |
| 66 | + expect(result).toEqual(['1.1.1.1', '8.8.8.8']); |
| 67 | + }); |
| 68 | + |
| 69 | + it('filters out 127.0.0.53 and tries secondary file', () => { |
| 70 | + mockedFs.readFileSync.mockImplementation((filePath: any) => { |
| 71 | + if (filePath === '/run/systemd/resolve/resolv.conf') { |
| 72 | + throw new Error('ENOENT'); |
| 73 | + } |
| 74 | + if (filePath === '/etc/resolv.conf') { |
| 75 | + return 'nameserver 127.0.0.53\n'; |
| 76 | + } |
| 77 | + throw new Error('ENOENT'); |
| 78 | + }); |
| 79 | + const result = detectHostDnsServers(mockLogger as any); |
| 80 | + expect(result).toEqual(DEFAULT_DNS_SERVERS); |
| 81 | + expect(mockLogger.warn).toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns DEFAULT_DNS_SERVERS when no files are readable', () => { |
| 85 | + mockedFs.readFileSync.mockImplementation(() => { |
| 86 | + throw new Error('ENOENT'); |
| 87 | + }); |
| 88 | + const result = detectHostDnsServers(mockLogger as any); |
| 89 | + expect(result).toEqual(DEFAULT_DNS_SERVERS); |
| 90 | + expect(mockLogger.warn).toHaveBeenCalledWith( |
| 91 | + expect.stringContaining('falling back to') |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('uses first readable file with usable servers', () => { |
| 96 | + mockedFs.readFileSync.mockImplementation((filePath: any) => { |
| 97 | + if (filePath === '/run/systemd/resolve/resolv.conf') { |
| 98 | + return 'nameserver 9.9.9.9\nnameserver 1.1.1.1\n'; |
| 99 | + } |
| 100 | + return 'nameserver 8.8.8.8\n'; |
| 101 | + }); |
| 102 | + const result = detectHostDnsServers(mockLogger as any); |
| 103 | + expect(result).toEqual(['9.9.9.9', '1.1.1.1']); |
| 104 | + expect(mockLogger.info).toHaveBeenCalledWith( |
| 105 | + expect.stringContaining('/run/systemd/resolve/resolv.conf') |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('filters out ::1 IPv6 loopback', () => { |
| 110 | + mockedFs.readFileSync.mockReturnValue( |
| 111 | + 'nameserver ::1\nnameserver 2001:4860:4860::8888\n' |
| 112 | + ); |
| 113 | + const result = detectHostDnsServers(mockLogger as any); |
| 114 | + expect(result).toEqual(['2001:4860:4860::8888']); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe('getEffectiveDnsServers', () => { |
| 119 | + it('returns explicit servers when provided', () => { |
| 120 | + const result = getEffectiveDnsServers(['1.1.1.1', '9.9.9.9'], mockLogger as any); |
| 121 | + expect(result).toEqual(['1.1.1.1', '9.9.9.9']); |
| 122 | + }); |
| 123 | + |
| 124 | + it('calls auto-detect when explicit is undefined', () => { |
| 125 | + mockedFs.readFileSync.mockReturnValue('nameserver 9.9.9.9\n'); |
| 126 | + const result = getEffectiveDnsServers(undefined, mockLogger as any); |
| 127 | + expect(result).toEqual(['9.9.9.9']); |
| 128 | + }); |
| 129 | + |
| 130 | + it('calls auto-detect when explicit is empty array', () => { |
| 131 | + mockedFs.readFileSync.mockImplementation(() => { |
| 132 | + throw new Error('ENOENT'); |
| 133 | + }); |
| 134 | + const result = getEffectiveDnsServers([], mockLogger as any); |
| 135 | + expect(result).toEqual(DEFAULT_DNS_SERVERS); |
| 136 | + }); |
| 137 | +}); |
0 commit comments