test_win_network.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.utils.platform
  6. import salt.utils.win_network as win_network
  7. # Import Salt Testing Libs
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase, skipIf
  10. mock_ip_base = MagicMock(
  11. return_value={"dns_enabled": False, "dns_suffix": "", "dynamic_dns_enabled": False}
  12. )
  13. mock_unicast = MagicMock(
  14. return_value={
  15. "ip_addresses": [
  16. {
  17. "address": "172.18.87.49",
  18. "broadcast": "172.18.87.63",
  19. "loopback": "127.0.0.1",
  20. "netmask": "255.255.255.240",
  21. "prefix_length": 28,
  22. "prefix_origin": "Manual",
  23. "suffix_origin": "Manual",
  24. }
  25. ],
  26. "ipv6_addresses": [
  27. {
  28. "address": "fe80::e8a4:1224:5548:2b81",
  29. "interface_index": 32,
  30. "prefix_length": 64,
  31. "prefix_origin": "WellKnown",
  32. "suffix_origin": "Router",
  33. }
  34. ],
  35. }
  36. )
  37. mock_gateway = MagicMock(
  38. return_value={
  39. "ip_gateways": ["192.168.0.1"],
  40. "ipv6_gateways": ["fe80::208:a2ff:fe0b:de70"],
  41. }
  42. )
  43. mock_dns = MagicMock(
  44. return_value={
  45. "ip_dns": ["10.4.0.1", "10.1.0.1", "8.8.8.8"],
  46. "ipv6_dns": ["2600:740a:1:304::1"],
  47. }
  48. )
  49. mock_multicast = MagicMock(
  50. return_value={
  51. "ip_multicast": [
  52. "224.0.0.1",
  53. "224.0.0.251",
  54. "224.0.0.252",
  55. "230.230.230.230",
  56. "239.0.0.250",
  57. "239.255.255.250",
  58. ],
  59. "ipv6_multicast": [
  60. "ff01::1",
  61. "ff02::1",
  62. "ff02::c",
  63. "ff02::fb",
  64. "ff02::1:3",
  65. "ff02::1:ff0f:4c48",
  66. "ff02::1:ffa6:f6e6",
  67. ],
  68. }
  69. )
  70. mock_anycast = MagicMock(return_value={"ip_anycast": [], "ipv6_anycast": []})
  71. mock_wins = MagicMock(return_value={"ip_wins": []})
  72. class PhysicalAddress(object):
  73. def __init__(self, address):
  74. self.address = address
  75. def ToString(self):
  76. return str(self.address)
  77. class Interface(object):
  78. """
  79. Mocked interface object
  80. """
  81. def __init__(
  82. self,
  83. i_address="02D5F1DD31E0",
  84. i_description="Dell GigabitEthernet",
  85. i_id="{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}",
  86. i_name="Ethernet",
  87. i_receive_only=False,
  88. i_status=1,
  89. i_type=6,
  90. ):
  91. self.PhysicalAddress = PhysicalAddress(i_address)
  92. self.Description = i_description
  93. self.Id = i_id
  94. self.Name = i_name
  95. self.NetworkInterfaceType = i_type
  96. self.IsReceiveOnly = i_receive_only
  97. self.OperationalStatus = i_status
  98. def GetPhysicalAddress(self):
  99. return self.PhysicalAddress
  100. @skipIf(not salt.utils.platform.is_windows(), "System is not Windows")
  101. class WinNetworkTestCase(TestCase):
  102. def test_get_interface_info_dot_net(self):
  103. expected = {
  104. "Ethernet": {
  105. "alias": "Ethernet",
  106. "description": "Dell GigabitEthernet",
  107. "dns_enabled": False,
  108. "dns_suffix": "",
  109. "dynamic_dns_enabled": False,
  110. "id": "{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}",
  111. "ip_addresses": [
  112. {
  113. "address": "172.18.87.49",
  114. "broadcast": "172.18.87.63",
  115. "loopback": "127.0.0.1",
  116. "netmask": "255.255.255.240",
  117. "prefix_length": 28,
  118. "prefix_origin": "Manual",
  119. "suffix_origin": "Manual",
  120. }
  121. ],
  122. "ip_anycast": [],
  123. "ip_dns": ["10.4.0.1", "10.1.0.1", "8.8.8.8"],
  124. "ip_gateways": ["192.168.0.1"],
  125. "ip_multicast": [
  126. "224.0.0.1",
  127. "224.0.0.251",
  128. "224.0.0.252",
  129. "230.230.230.230",
  130. "239.0.0.250",
  131. "239.255.255.250",
  132. ],
  133. "ip_wins": [],
  134. "ipv6_addresses": [
  135. {
  136. "address": "fe80::e8a4:1224:5548:2b81",
  137. "interface_index": 32,
  138. "prefix_length": 64,
  139. "prefix_origin": "WellKnown",
  140. "suffix_origin": "Router",
  141. }
  142. ],
  143. "ipv6_anycast": [],
  144. "ipv6_dns": ["2600:740a:1:304::1"],
  145. "ipv6_gateways": ["fe80::208:a2ff:fe0b:de70"],
  146. "ipv6_multicast": [
  147. "ff01::1",
  148. "ff02::1",
  149. "ff02::c",
  150. "ff02::fb",
  151. "ff02::1:3",
  152. "ff02::1:ff0f:4c48",
  153. "ff02::1:ffa6:f6e6",
  154. ],
  155. "physical_address": "02:D5:F1:DD:31:E0",
  156. "receive_only": False,
  157. "status": "Up",
  158. "type": "Ethernet",
  159. }
  160. }
  161. mock_int = MagicMock(return_value=[Interface()])
  162. with patch.object(
  163. win_network, "_get_network_interfaces", mock_int
  164. ), patch.object(
  165. win_network, "_get_ip_base_properties", mock_ip_base
  166. ), patch.object(
  167. win_network, "_get_ip_unicast_info", mock_unicast
  168. ), patch.object(
  169. win_network, "_get_ip_gateway_info", mock_gateway
  170. ), patch.object(
  171. win_network, "_get_ip_dns_info", mock_dns
  172. ), patch.object(
  173. win_network, "_get_ip_multicast_info", mock_multicast
  174. ), patch.object(
  175. win_network, "_get_ip_anycast_info", mock_anycast
  176. ), patch.object(
  177. win_network, "_get_ip_wins_info", mock_wins
  178. ):
  179. # ret = win_network._get_base_properties()
  180. results = win_network.get_interface_info_dot_net()
  181. self.assertDictEqual(expected, results)
  182. def test_get_network_info(self):
  183. expected = {
  184. "Dell GigabitEthernet": {
  185. "hwaddr": "02:D5:F1:DD:31:E0",
  186. "inet": [
  187. {
  188. "address": "172.18.87.49",
  189. "broadcast": "172.18.87.63",
  190. "gateway": "192.168.0.1",
  191. "label": "Dell GigabitEthernet",
  192. "netmask": "255.255.255.240",
  193. }
  194. ],
  195. "inet6": [
  196. {
  197. "address": "fe80::e8a4:1224:5548:2b81",
  198. "gateway": "fe80::208:a2ff:fe0b:de70",
  199. }
  200. ],
  201. "up": True,
  202. }
  203. }
  204. mock_int = MagicMock(return_value=[Interface()])
  205. with patch.object(
  206. win_network, "_get_network_interfaces", mock_int
  207. ), patch.object(
  208. win_network, "_get_ip_base_properties", mock_ip_base
  209. ), patch.object(
  210. win_network, "_get_ip_unicast_info", mock_unicast
  211. ), patch.object(
  212. win_network, "_get_ip_gateway_info", mock_gateway
  213. ), patch.object(
  214. win_network, "_get_ip_dns_info", mock_dns
  215. ), patch.object(
  216. win_network, "_get_ip_multicast_info", mock_multicast
  217. ), patch.object(
  218. win_network, "_get_ip_anycast_info", mock_anycast
  219. ), patch.object(
  220. win_network, "_get_ip_wins_info", mock_wins
  221. ):
  222. # ret = win_network._get_base_properties()
  223. results = win_network.get_interface_info()
  224. self.assertDictEqual(expected, results)
  225. def test__get_base_properties_tap_adapter(self):
  226. """
  227. Adapter Type 53 is apparently an undocumented type corresponding to
  228. OpenVPN TAP Adapters and possibly other TAP Adapters. This test makes
  229. sure the win_network util will catch that.
  230. https://github.com/saltstack/salt/issues/56196
  231. https://github.com/saltstack/salt/issues/56275
  232. """
  233. i_face = Interface(
  234. i_address="03DE4D0713FA",
  235. i_description="Windows TAP Adapter",
  236. i_id="{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}",
  237. i_name="Windows TAP Adapter",
  238. i_receive_only=False,
  239. i_status=1,
  240. i_type=53,
  241. )
  242. expected = {
  243. "alias": "Windows TAP Adapter",
  244. "description": "Windows TAP Adapter",
  245. "id": "{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}",
  246. "receive_only": False,
  247. "physical_address": "03:DE:4D:07:13:FA",
  248. "status": "Up",
  249. "type": "TAPAdapter",
  250. }
  251. results = win_network._get_base_properties(i_face=i_face)
  252. self.assertDictEqual(expected, results)
  253. def test__get_base_properties_undefined_adapter(self):
  254. """
  255. The Adapter Type 53 may be an arbitrary number assigned by OpenVPN.
  256. This will test the ability to avoid stack tracing on an undefined
  257. adapter type. If one is encountered, just use the description.
  258. """
  259. i_face = Interface(
  260. i_address="03DE4D0713FA",
  261. i_description="Undefined Adapter",
  262. i_id="{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}",
  263. i_name="Undefined",
  264. i_receive_only=False,
  265. i_status=1,
  266. i_type=50,
  267. )
  268. expected = {
  269. "alias": "Undefined",
  270. "description": "Undefined Adapter",
  271. "id": "{C5F468C0-DD5F-4C2B-939F-A411DCB5DE16}",
  272. "receive_only": False,
  273. "physical_address": "03:DE:4D:07:13:FA",
  274. "status": "Up",
  275. "type": "Undefined Adapter",
  276. }
  277. results = win_network._get_base_properties(i_face=i_face)
  278. self.assertDictEqual(expected, results)