Skip to content

Commit 3ff74ea

Browse files
committed
Setup loopback aliases for p2p tests properly
1 parent 3060bfd commit 3ff74ea

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

.github/workflows/build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ jobs:
7474
run: |
7575
sudo -E bash .github/install.sh
7676
sudo -E bash .github/install-post.sh
77+
- name: Setup mock IPs
78+
run: sudo python3 tests/test_p2p.py --setup-mock-ips
7779
- name: Build and test (RelWithDebInfo)
7880
run: |
7981
set -euo pipefail
@@ -186,6 +188,10 @@ jobs:
186188
steps:
187189
- uses: actions/checkout@main
188190

191+
- name: Setup mock IPs
192+
run: py -3 tests/test_p2p.py --setup-mock-ips
193+
shell: cmd
194+
189195
# Mark all directories as safe so checkouts performed in CMakeLists.txt don't cause "unsafe repository" errors.
190196
# See https://github.com/actions/checkout/issues/766
191197
- name: Configure Git

.github/workflows/macos.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v4
1616

17+
- name: Setup mock IPs
18+
run: sudo python3 tests/test_p2p.py --setup-mock-ips
19+
shell: bash
20+
1721
- name: Ensure vcpkg cache directories exist
1822
run: |
1923
mkdir -p "${VCPKG_DOWNLOADS}"
@@ -61,21 +65,11 @@ jobs:
6165
run: ./test_connection suite-quick
6266
shell: bash
6367

64-
- name: Setup mock IPs
65-
working-directory: ${{github.workspace}}/build/bin
66-
run: sudo python3 test_p2p.py --setup-mock-ips
67-
shell: bash
68-
6968
- name: Test p2p
7069
working-directory: ${{github.workspace}}/build/bin
7170
run: python3 test_p2p.py --spewlevel=debug --loglevel-p2prendezvous=debug
7271
shell: bash
7372

74-
- name: Cleanup mock IPs
75-
working-directory: ${{github.workspace}}/build/bin
76-
run: sudo python3 test_p2p.py --cleanup-mock-ips
77-
shell: bash
78-
7973
- name: Configure CMake (Release)
8074
run: cmake -B ${{github.workspace}}/build-release
8175
-DCMAKE_BUILD_TYPE=Release

tests/test_p2p.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ def _IsAddressBindable( addr ):
208208
except OSError:
209209
return False
210210

211+
_WINDOWS_LOOPBACK_IFACE = 'Loopback Pseudo-Interface 1'
212+
211213
def _AddLoopbackAddr( addr ):
212214
is_ipv6 = ':' in addr
213215
sys_name = platform.system()
@@ -223,7 +225,12 @@ def _AddLoopbackAddr( addr ):
223225
print( "Running 'ip -6 addr add %s/112 dev lo'" % addr )
224226
subprocess.run( [ 'ip', '-6', 'addr', 'add', addr + '/112', 'dev', 'lo' ], check=True )
225227
# IPv4 on Linux: the entire 127/8 block is routable on lo, nothing to do.
226-
# Windows: TBD when needed
228+
elif sys_name == 'Windows':
229+
if is_ipv6:
230+
print( "Running 'netsh interface ipv6 add address \"%s\" %s'" % ( _WINDOWS_LOOPBACK_IFACE, addr ) )
231+
subprocess.run( [ 'netsh', 'interface', 'ipv6', 'add', 'address',
232+
_WINDOWS_LOOPBACK_IFACE, addr ], check=True )
233+
# IPv4 on Windows: the entire 127/8 block is routable on the loopback adapter, nothing to do.
227234

228235
def _RemoveLoopbackAddr( addr ):
229236
is_ipv6 = ':' in addr
@@ -240,7 +247,12 @@ def _RemoveLoopbackAddr( addr ):
240247
print( "Running 'ip -6 addr del %s/112 dev lo'" % addr )
241248
subprocess.run( [ 'ip', '-6', 'addr', 'del', addr + '/112', 'dev', 'lo' ], check=False )
242249
# IPv4 on Linux: nothing was added, nothing to remove.
243-
# Windows: TBD when needed
250+
elif sys_name == 'Windows':
251+
if is_ipv6:
252+
print( "Running 'netsh interface ipv6 delete address \"%s\" %s'" % ( _WINDOWS_LOOPBACK_IFACE, addr ) )
253+
subprocess.run( [ 'netsh', 'interface', 'ipv6', 'delete', 'address',
254+
_WINDOWS_LOOPBACK_IFACE, addr ], check=False )
255+
# IPv4 on Windows: nothing was added, nothing to remove.
244256

245257
def SetupMockIPs():
246258
"""Add loopback aliases for every mock address that is not already bindable."""
@@ -264,7 +276,10 @@ def CheckMockIPsBindable():
264276
print( "ERROR: the following addresses required by the mock network are not bindable:" )
265277
for addr in missing:
266278
print( " " + addr )
267-
print( "Run 'sudo %s --setup-mock-ips' to add the required loopback aliases." % sys.argv[0] )
279+
if platform.system() == 'Windows':
280+
print( "Run '%s --setup-mock-ips' from an elevated (Administrator) prompt." % sys.argv[0] )
281+
else:
282+
print( "Run 'sudo %s --setup-mock-ips' to add the required loopback aliases." % sys.argv[0] )
268283
sys.exit(1)
269284

270285
def _nat( internal, gateway, nat_type ):

0 commit comments

Comments
 (0)