test_win_network.py 10 KB

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