|
| 1 | +// Copyright 2024 The Lynx Authors. All rights reserved. |
| 2 | +// Licensed under the Apache License Version 2.0 that can be found in the |
| 3 | +// LICENSE file in the root directory of this source tree. |
| 4 | + |
| 5 | +import { describe, expect, test, vi } from 'vitest' |
| 6 | + |
| 7 | +vi.mock('node:os') |
| 8 | + |
| 9 | +describe('findIp', () => { |
| 10 | + test('v4', async () => { |
| 11 | + const { default: os } = await import('node:os') |
| 12 | + |
| 13 | + vi.mocked(os.networkInterfaces).mockReturnValue({ |
| 14 | + eth0: [ |
| 15 | + { |
| 16 | + address: '192.168.1.1', |
| 17 | + family: 'IPv4', |
| 18 | + internal: false, |
| 19 | + netmask: '255.255.255.0', |
| 20 | + mac: '00:00:00:00:00:00', |
| 21 | + cidr: '192.168.1.1/24', |
| 22 | + }, |
| 23 | + ], |
| 24 | + }) |
| 25 | + |
| 26 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 27 | + |
| 28 | + const ip = await findIp('v4') |
| 29 | + expect(ip).toBe('192.168.1.1') |
| 30 | + }) |
| 31 | + |
| 32 | + test('v6', async () => { |
| 33 | + const { default: os } = await import('node:os') |
| 34 | + |
| 35 | + vi.mocked(os.networkInterfaces).mockReturnValue({ |
| 36 | + eth0: [ |
| 37 | + { |
| 38 | + address: 'fd00::1', |
| 39 | + family: 'IPv6', |
| 40 | + internal: false, |
| 41 | + netmask: 'ffff:ffff:ffff:ffff::', |
| 42 | + mac: '00:00:00:00:00:00', |
| 43 | + cidr: 'fd00::1/64', |
| 44 | + scopeid: 0, |
| 45 | + }, |
| 46 | + ], |
| 47 | + }) |
| 48 | + |
| 49 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 50 | + |
| 51 | + const ip = await findIp('v6') |
| 52 | + expect(ip).toBe('[fd00::1]') |
| 53 | + }) |
| 54 | + |
| 55 | + test('multiple ips (should use the first ip)', async () => { |
| 56 | + const { default: os } = await import('node:os') |
| 57 | + |
| 58 | + vi.mocked(os.networkInterfaces).mockReturnValue({ |
| 59 | + eth0: [ |
| 60 | + { |
| 61 | + address: '192.168.1.1', |
| 62 | + family: 'IPv4', |
| 63 | + internal: false, |
| 64 | + netmask: '255.255.255.0', |
| 65 | + mac: '00:00:00:00:00:00', |
| 66 | + cidr: '192.168.1.1/24', |
| 67 | + }, |
| 68 | + { |
| 69 | + address: '192.168.2.1', |
| 70 | + family: 'IPv4', |
| 71 | + internal: false, |
| 72 | + netmask: '255.255.255.0', |
| 73 | + mac: '00:00:00:00:00:00', |
| 74 | + cidr: '192.168.2.1/24', |
| 75 | + }, |
| 76 | + ], |
| 77 | + }) |
| 78 | + |
| 79 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 80 | + |
| 81 | + const ip = await findIp('v4') |
| 82 | + expect(ip).toBe('192.168.1.1') // should use the first ip |
| 83 | + }) |
| 84 | + |
| 85 | + test('multiple ips (should ignore internal ips)', async () => { |
| 86 | + const { default: os } = await import('node:os') |
| 87 | + |
| 88 | + vi.mocked(os.networkInterfaces).mockReturnValue({ |
| 89 | + eth0: [ |
| 90 | + { |
| 91 | + address: '192.168.2.1', |
| 92 | + family: 'IPv4', |
| 93 | + internal: true, |
| 94 | + netmask: '255.255.255.0', |
| 95 | + mac: '00:00:00:00:00:00', |
| 96 | + cidr: '192.168.2.1/24', |
| 97 | + }, |
| 98 | + { |
| 99 | + address: '192.168.1.1', |
| 100 | + family: 'IPv4', |
| 101 | + internal: false, |
| 102 | + netmask: '255.255.255.0', |
| 103 | + mac: '00:00:00:00:00:00', |
| 104 | + cidr: '192.168.1.1/24', |
| 105 | + }, |
| 106 | + ], |
| 107 | + }) |
| 108 | + |
| 109 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 110 | + |
| 111 | + const ip = await findIp('v4') |
| 112 | + expect(ip).toBe('192.168.1.1') // should ignore internal ips |
| 113 | + }) |
| 114 | + |
| 115 | + test('no v4 ips', async () => { |
| 116 | + const { default: os } = await import('node:os') |
| 117 | + |
| 118 | + vi.mocked(os.networkInterfaces).mockReturnValue({ |
| 119 | + eth0: [ |
| 120 | + { |
| 121 | + address: 'fd00::1', |
| 122 | + family: 'IPv6', |
| 123 | + internal: false, |
| 124 | + netmask: 'ffff:ffff:ffff:ffff::', |
| 125 | + mac: '00:00:00:00:00:00', |
| 126 | + cidr: 'fd00::1/64', |
| 127 | + scopeid: 0, |
| 128 | + }, |
| 129 | + ], |
| 130 | + }) |
| 131 | + |
| 132 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 133 | + |
| 134 | + await expect(findIp('v4')).rejects.toThrow( |
| 135 | + 'No valid IP found', |
| 136 | + ) |
| 137 | + }) |
| 138 | + |
| 139 | + test('no ips', async () => { |
| 140 | + const { default: os } = await import('node:os') |
| 141 | + |
| 142 | + vi.mocked(os.networkInterfaces).mockReturnValue({}) |
| 143 | + |
| 144 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 145 | + |
| 146 | + await expect(findIp('v4')).rejects.toThrow( |
| 147 | + 'No valid IP found', |
| 148 | + ) |
| 149 | + }) |
| 150 | + |
| 151 | + test('invalid network interfaces', async () => { |
| 152 | + const { default: os } = await import('node:os') |
| 153 | + |
| 154 | + vi.mocked(os.networkInterfaces).mockReturnValue({ |
| 155 | + // @ts-expect-error mocked invalid network interfaces |
| 156 | + eth0: null, |
| 157 | + }) |
| 158 | + |
| 159 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 160 | + |
| 161 | + await expect(findIp('v4')).rejects.toThrow( |
| 162 | + 'No valid IP found', |
| 163 | + ) |
| 164 | + }) |
| 165 | + |
| 166 | + test('invalid ip address', async () => { |
| 167 | + const { default: os } = await import('node:os') |
| 168 | + |
| 169 | + vi.mocked(os.networkInterfaces).mockReturnValue({ |
| 170 | + eth0: [ |
| 171 | + { |
| 172 | + // @ts-expect-error invalid ip address |
| 173 | + address: null, |
| 174 | + }, |
| 175 | + ], |
| 176 | + }) |
| 177 | + |
| 178 | + const { findIp } = await import('../../src/plugins/dev.plugin.js') |
| 179 | + |
| 180 | + await expect(findIp('v4')).rejects.toThrow( |
| 181 | + 'No valid IP found', |
| 182 | + ) |
| 183 | + }) |
| 184 | +}) |
0 commit comments