Add a route to 127.0.0.1 and ::1 to simplify unit tests on Windows
(routes=None, # type: Optional[List[Any]]
ipv6=False, # type: bool
iflist=None, # type: Optional[List[str]]
)
| 951 | |
| 952 | |
| 953 | def _route_add_loopback(routes=None, # type: Optional[List[Any]] |
| 954 | ipv6=False, # type: bool |
| 955 | iflist=None, # type: Optional[List[str]] |
| 956 | ): |
| 957 | # type: (...) -> None |
| 958 | """Add a route to 127.0.0.1 and ::1 to simplify unit tests on Windows""" |
| 959 | if not WINDOWS: |
| 960 | warning("Calling _route_add_loopback is only valid on Windows") |
| 961 | return |
| 962 | warning("This will completely mess up the routes. Testing purpose only !") |
| 963 | # Add only if some adapters already exist |
| 964 | if ipv6: |
| 965 | if not conf.route6.routes: |
| 966 | return |
| 967 | else: |
| 968 | if not conf.route.routes: |
| 969 | return |
| 970 | conf.ifaces._add_fake_iface(conf.loopback_name) |
| 971 | adapter = conf.ifaces.dev_from_name(conf.loopback_name) |
| 972 | if iflist: |
| 973 | iflist.append(adapter.network_name) |
| 974 | return |
| 975 | # Remove all conf.loopback_name routes |
| 976 | for route in list(conf.route.routes): |
| 977 | iface = route[3] |
| 978 | if iface == conf.loopback_name: |
| 979 | conf.route.routes.remove(route) |
| 980 | # Remove conf.loopback_name interface |
| 981 | for devname, ifname in list(conf.ifaces.items()): |
| 982 | if ifname == conf.loopback_name: |
| 983 | conf.ifaces.pop(devname) |
| 984 | # Inject interface |
| 985 | conf.ifaces[r"\Device\NPF_{0XX00000-X000-0X0X-X00X-00XXXX000XXX}"] = adapter |
| 986 | conf.loopback_name = adapter.network_name |
| 987 | if isinstance(conf.iface, NetworkInterface): |
| 988 | if conf.iface.network_name == conf.loopback_name: |
| 989 | conf.iface = adapter |
| 990 | conf.netcache.arp_cache["127.0.0.1"] = "ff:ff:ff:ff:ff:ff" # type: ignore |
| 991 | conf.netcache.in6_neighbor["::1"] = "ff:ff:ff:ff:ff:ff" # type: ignore |
| 992 | # Build the packed network addresses |
| 993 | loop_net = struct.unpack("!I", socket.inet_aton("127.0.0.0"))[0] |
| 994 | loop_mask = struct.unpack("!I", socket.inet_aton("255.0.0.0"))[0] |
| 995 | # Build the fake routes |
| 996 | loopback_route = ( |
| 997 | loop_net, |
| 998 | loop_mask, |
| 999 | "0.0.0.0", |
| 1000 | adapter.network_name, |
| 1001 | "127.0.0.1", |
| 1002 | 1 |
| 1003 | ) |
| 1004 | loopback_route6 = ('::1', 128, '::', adapter.network_name, ["::1"], 1) |
| 1005 | loopback_route6_custom = ("fe80::", 128, "::", adapter.network_name, ["::1"], 1) |
| 1006 | if routes is None: |
| 1007 | # Injection |
| 1008 | conf.route6.routes.append(loopback_route6) |
| 1009 | conf.route6.routes.append(loopback_route6_custom) |
| 1010 | conf.route.routes.append(loopback_route) |
no test coverage detected