|
| 1 | +// Tests for the native VPN capability gate. |
| 2 | +// |
| 3 | +// The point of this file is the invariant in `refuses to route traffic over the |
| 4 | +// placeholder data path`: linking the native module must NOT be sufficient to |
| 5 | +// carry user traffic, because the native forwarders are raw-UDP placeholders. |
| 6 | +// The gate is a code path, not a comment, so it is testable -- and these |
| 7 | +// assertions fail the moment NATIVE_DATA_PATH_IS_SECURE is flipped, which is |
| 8 | +// the deliberate acknowledgement that a real tunnel has landed. |
| 9 | + |
| 10 | +import { describe, it, mock, beforeEach } from 'node:test' |
| 11 | +import assert from 'node:assert/strict' |
| 12 | + |
| 13 | +/** Mutable stand-in for react-native's NativeModules registry. */ |
| 14 | +const nativeModules: Record<string, unknown> = {} |
| 15 | + |
| 16 | +/** Records what the "native" side was actually asked to do. */ |
| 17 | +interface NativeCalls { |
| 18 | + start: unknown[] |
| 19 | + stop: number |
| 20 | + requestPermission: number |
| 21 | +} |
| 22 | + |
| 23 | +const calls: NativeCalls = { start: [], stop: 0, requestPermission: 0 } |
| 24 | + |
| 25 | +const platform = { OS: 'ios' as string } |
| 26 | + |
| 27 | +class FakeNativeEventEmitter { |
| 28 | + addListener(): { remove(): void } { |
| 29 | + return { remove() {} } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// `namedExports` is deprecated in favour of `exports` at runtime, but the |
| 34 | +// pinned @types/node does not declare `exports` on MockModuleOptions yet, and |
| 35 | +// `npm run typecheck` is a gate. Switch when the types catch up. |
| 36 | +mock.module('react-native', { |
| 37 | + namedExports: { |
| 38 | + NativeModules: nativeModules, |
| 39 | + NativeEventEmitter: FakeNativeEventEmitter, |
| 40 | + Platform: platform, |
| 41 | + }, |
| 42 | +}) |
| 43 | + |
| 44 | +const { BifrostVpn, isNativeVpnAvailable, isNativeVpnUsable, NATIVE_DATA_PATH_IS_SECURE } = |
| 45 | + await import('./BifrostVpn.ts') |
| 46 | +const { selectVpnMode } = await import('./index.ts') |
| 47 | + |
| 48 | +/** Install a fake linked native module, as a config plugin would. */ |
| 49 | +function linkNativeModule(): void { |
| 50 | + nativeModules.BifrostVpn = { |
| 51 | + async requestPermission() { |
| 52 | + calls.requestPermission += 1 |
| 53 | + return true |
| 54 | + }, |
| 55 | + async start(config: unknown) { |
| 56 | + calls.start.push(config) |
| 57 | + }, |
| 58 | + async stop() { |
| 59 | + calls.stop += 1 |
| 60 | + }, |
| 61 | + async getStatus() { |
| 62 | + return { |
| 63 | + connected: true, |
| 64 | + serverAddress: 'vpn.example', |
| 65 | + tunnelAddress: '10.0.0.2', |
| 66 | + bytesIn: 1, |
| 67 | + bytesOut: 2, |
| 68 | + } |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function unlinkNativeModule(): void { |
| 74 | + delete nativeModules.BifrostVpn |
| 75 | +} |
| 76 | + |
| 77 | +beforeEach(() => { |
| 78 | + unlinkNativeModule() |
| 79 | + platform.OS = 'ios' |
| 80 | + calls.start = [] |
| 81 | + calls.stop = 0 |
| 82 | + calls.requestPermission = 0 |
| 83 | +}) |
| 84 | + |
| 85 | +describe('native VPN capability gate', () => { |
| 86 | + it('declares the placeholder data path insecure', () => { |
| 87 | + assert.equal( |
| 88 | + NATIVE_DATA_PATH_IS_SECURE, |
| 89 | + false, |
| 90 | + 'NATIVE_DATA_PATH_IS_SECURE must stay false while the native forwarders ' + |
| 91 | + 'are raw-UDP placeholders. If a real, on-device-validated ' + |
| 92 | + 'WireGuard/OpenVPN data path has landed, update this file together ' + |
| 93 | + 'with the flag -- do not flip the flag alone.' |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + it('refuses to route traffic over the placeholder data path', async () => { |
| 98 | + linkNativeModule() |
| 99 | + |
| 100 | + // The module is linked, so "is it available?" is now true... |
| 101 | + assert.equal(isNativeVpnAvailable(), true) |
| 102 | + |
| 103 | + // ...but that must not be enough to use it. |
| 104 | + assert.equal(isNativeVpnUsable(), false) |
| 105 | + assert.equal(selectVpnMode(), 'server') |
| 106 | + |
| 107 | + await assert.rejects( |
| 108 | + () => BifrostVpn.start({ serverAddress: 'vpn.example' }), |
| 109 | + /raw-UDP placeholder/ |
| 110 | + ) |
| 111 | + assert.deepEqual(calls.start, [], 'the native side must never be asked to start') |
| 112 | + }) |
| 113 | + |
| 114 | + it('does not prompt for OS tunnel permission it cannot honour', async () => { |
| 115 | + linkNativeModule() |
| 116 | + |
| 117 | + assert.equal(await BifrostVpn.requestPermission(), false) |
| 118 | + assert.equal( |
| 119 | + calls.requestPermission, |
| 120 | + 0, |
| 121 | + 'no OS permission prompt for a tunnel that will refuse to start' |
| 122 | + ) |
| 123 | + }) |
| 124 | + |
| 125 | + it('reports the module unavailable when it is not linked', async () => { |
| 126 | + assert.equal(isNativeVpnAvailable(), false) |
| 127 | + assert.equal(isNativeVpnUsable(), false) |
| 128 | + assert.equal(selectVpnMode(), 'server') |
| 129 | + |
| 130 | + await assert.rejects( |
| 131 | + () => BifrostVpn.start({ serverAddress: 'vpn.example' }), |
| 132 | + /not linked/ |
| 133 | + ) |
| 134 | + }) |
| 135 | + |
| 136 | + it('reports the module unavailable on web even when linked', () => { |
| 137 | + linkNativeModule() |
| 138 | + platform.OS = 'web' |
| 139 | + |
| 140 | + assert.equal(isNativeVpnAvailable(), false) |
| 141 | + assert.equal(selectVpnMode(), 'server') |
| 142 | + }) |
| 143 | + |
| 144 | + it('keeps stop and getStatus safe no-ops when unavailable', async () => { |
| 145 | + await BifrostVpn.stop() |
| 146 | + assert.equal(calls.stop, 0) |
| 147 | + |
| 148 | + const status = await BifrostVpn.getStatus() |
| 149 | + assert.equal(status.connected, false) |
| 150 | + assert.equal(status.bytesIn, 0) |
| 151 | + }) |
| 152 | + |
| 153 | + it('still forwards stop to a linked module so a tunnel can always be torn down', async () => { |
| 154 | + linkNativeModule() |
| 155 | + |
| 156 | + await BifrostVpn.stop() |
| 157 | + assert.equal(calls.stop, 1, 'stop must reach the native side regardless of the gate') |
| 158 | + }) |
| 159 | + |
| 160 | + it('resolves the native module lazily, not at import time', () => { |
| 161 | + // The module was absent when this file first imported the bridge; linking it |
| 162 | + // afterwards must still be observed. |
| 163 | + assert.equal(isNativeVpnAvailable(), false) |
| 164 | + linkNativeModule() |
| 165 | + assert.equal(isNativeVpnAvailable(), true) |
| 166 | + }) |
| 167 | +}) |
0 commit comments