summaryrefslogtreecommitdiff
path: root/src/test/burn/WixTestTools/Firewall/Verifier.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/burn/WixTestTools/Firewall/Verifier.cs')
-rw-r--r--src/test/burn/WixTestTools/Firewall/Verifier.cs303
1 files changed, 303 insertions, 0 deletions
diff --git a/src/test/burn/WixTestTools/Firewall/Verifier.cs b/src/test/burn/WixTestTools/Firewall/Verifier.cs
new file mode 100644
index 00000000..d3f32c5c
--- /dev/null
+++ b/src/test/burn/WixTestTools/Firewall/Verifier.cs
@@ -0,0 +1,303 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3namespace WixTestTools.Firewall
4{
5 using System;
6 using System.Collections.Generic;
7 using NetFwTypeLib;
8 using Xunit;
9
10 public static class Verifier
11 {
12 static INetFwRules GetINetFwRules()
13 {
14 var policyType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2", true);
15 var policyInstance = Activator.CreateInstance(policyType);
16 var policy2 = policyInstance as INetFwPolicy2;
17 return policy2.Rules;
18 }
19
20 static INetFwRule3 GetINetFwRule3(string name, UniqueCheck unique)
21 {
22 var rules = GetINetFwRules();
23 INetFwRule3 rule3;
24
25 if (unique != null)
26 {
27 var enumerator = rules.GetEnumerator();
28 while (enumerator.MoveNext())
29 {
30 rule3 = enumerator.Current as INetFwRule3;
31 if (!unique.FirewallRuleIsUnique(rule3))
32 {
33 continue;
34 }
35
36 return rule3;
37 }
38 }
39
40 var rule1 = rules.Item(name);
41 rule3 = rule1 as INetFwRule3;
42 return rule3;
43 }
44
45 public static RuleDetails GetFirewallRule(string name, UniqueCheck unique)
46 {
47 var rule = GetINetFwRule3(name, unique);
48 var details = new RuleDetails(rule);
49 return details;
50 }
51
52 public static bool FirewallRuleExists(string name, UniqueCheck unique = null)
53 {
54 try
55 {
56 GetINetFwRule3(name, unique);
57 return true;
58 }
59 catch (System.IO.FileNotFoundException)
60 {
61 return false;
62 }
63 }
64
65 public static IEnumerable<RuleDetails> GetFirewallRules()
66 {
67 var rules = GetINetFwRules();
68 var enumerator = rules.GetEnumerator();
69 while (enumerator.MoveNext())
70 {
71 var rule3 = enumerator.Current as INetFwRule3;
72 yield return new RuleDetails(rule3);
73 }
74 }
75
76 public static void AddFirewallRule(RuleDetails information)
77 {
78 var rules = GetINetFwRules();
79 var rule1 = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
80 var rule3 = rule1 as INetFwRule3;
81
82 rule3.Name = information.Name;
83
84 if (!String.IsNullOrEmpty(information.Description))
85 {
86 rule3.Description = information.Description;
87 }
88
89 if (!String.IsNullOrEmpty(information.ApplicationName))
90 {
91 rule3.ApplicationName = information.ApplicationName;
92 }
93
94 if (!String.IsNullOrEmpty(information.ServiceName))
95 {
96 rule3.serviceName = information.ServiceName;
97 }
98
99 if (information.Protocol.HasValue)
100 {
101 rule3.Protocol = information.Protocol.Value;
102 }
103
104 if (!String.IsNullOrEmpty(information.LocalPorts))
105 {
106 rule3.LocalPorts = information.LocalPorts;
107 }
108
109 if (!String.IsNullOrEmpty(information.RemotePorts))
110 {
111 rule3.RemotePorts = information.RemotePorts;
112 }
113
114 if (!String.IsNullOrEmpty(information.LocalAddresses))
115 {
116 rule3.LocalAddresses = information.LocalAddresses;
117 }
118
119 if (!String.IsNullOrEmpty(information.RemoteAddresses))
120 {
121 rule3.RemoteAddresses = information.RemoteAddresses;
122 }
123
124 if (!String.IsNullOrEmpty(information.IcmpTypesAndCodes))
125 {
126 rule3.IcmpTypesAndCodes = information.IcmpTypesAndCodes;
127 }
128
129 if (information.Direction.HasValue)
130 {
131 rule3.Direction = information.Direction.Value;
132 }
133
134 if (information.Interfaces != null)
135 {
136 rule3.Interfaces = information.Interfaces;
137 }
138
139 if (!String.IsNullOrEmpty(information.InterfaceTypes))
140 {
141 rule3.InterfaceTypes = information.InterfaceTypes;
142 }
143
144 if (information.Enabled.HasValue)
145 {
146 rule3.Enabled = information.Enabled.Value;
147 }
148
149 if (!String.IsNullOrEmpty(information.Grouping))
150 {
151 rule3.Grouping = information.Grouping;
152 }
153
154 if (information.Profiles.HasValue)
155 {
156 rule3.Profiles = information.Profiles.Value;
157 }
158
159 if (information.EdgeTraversal.HasValue)
160 {
161 rule3.EdgeTraversal = information.EdgeTraversal.Value;
162 }
163
164 if (information.Action.HasValue)
165 {
166 rule3.Action = information.Action.Value;
167 }
168
169 if (information.EdgeTraversalOptions.HasValue)
170 {
171 rule3.EdgeTraversalOptions = information.EdgeTraversalOptions.Value;
172 }
173
174 if (!String.IsNullOrEmpty(information.LocalAppPackageId))
175 {
176 rule3.LocalAppPackageId = information.LocalAppPackageId;
177 }
178
179 if (!String.IsNullOrEmpty(information.LocalUserOwner))
180 {
181 rule3.LocalUserOwner = information.LocalUserOwner;
182 }
183
184 if (!String.IsNullOrEmpty(information.LocalUserAuthorizedList))
185 {
186 rule3.LocalUserAuthorizedList = information.LocalUserAuthorizedList;
187 }
188
189 if (!String.IsNullOrEmpty(information.RemoteUserAuthorizedList))
190 {
191 rule3.RemoteUserAuthorizedList = information.RemoteUserAuthorizedList;
192 }
193
194 if (!String.IsNullOrEmpty(information.RemoteMachineAuthorizedList))
195 {
196 rule3.RemoteMachineAuthorizedList = information.RemoteMachineAuthorizedList;
197 }
198
199 if (information.SecureFlags.HasValue)
200 {
201 rule3.SecureFlags = information.SecureFlags.Value;
202 }
203
204 rules.Add(rule3);
205 }
206
207 public static void UpdateFirewallRule(string name, RuleDetails information, UniqueCheck unique = null)
208 {
209 var rule = GetINetFwRule3(name, unique);
210
211 // remove ports so the Protocol can be changed, if required
212 if (information.Protocol.HasValue && rule.Protocol != information.Protocol.Value)
213 {
214 rule.LocalPorts = null;
215 rule.RemotePorts = null;
216 }
217
218 rule.Name = information.Name;
219 rule.Description = information.Description;
220 rule.Direction = information.Direction ?? NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
221 rule.ApplicationName = information.ApplicationName;
222 rule.serviceName = information.ServiceName;
223 rule.Protocol = information.Protocol ?? 256;
224 rule.LocalPorts = information.LocalPorts;
225 rule.RemotePorts = information.RemotePorts;
226 rule.LocalAddresses = information.LocalAddresses;
227 rule.RemoteAddresses = information.RemoteAddresses;
228 rule.IcmpTypesAndCodes = information.IcmpTypesAndCodes;
229 rule.Interfaces = information.Interfaces;
230 rule.InterfaceTypes = information.InterfaceTypes;
231 rule.Enabled = information.Enabled ?? false;
232 rule.Grouping = information.Grouping;
233 rule.Profiles = information.Profiles ?? 0x7fffffff;
234 rule.EdgeTraversal = information.EdgeTraversal ?? false;
235 rule.Action = information.Action ?? NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
236 rule.EdgeTraversalOptions = information.EdgeTraversalOptions ?? 0x0;
237 rule.LocalAppPackageId = information.LocalAppPackageId;
238 rule.LocalUserOwner = information.LocalUserOwner;
239 rule.LocalUserAuthorizedList = information.LocalUserAuthorizedList;
240 rule.RemoteUserAuthorizedList = information.RemoteUserAuthorizedList;
241 rule.RemoteMachineAuthorizedList = information.RemoteMachineAuthorizedList;
242 rule.SecureFlags = information.SecureFlags ?? 0;
243 }
244
245 public static void EnableFirewallRule(string name, UniqueCheck unique = null)
246 {
247 var rule = GetINetFwRule3(name, unique);
248 rule.Enabled = true;
249 }
250
251 public static void DisableFirewallRule(string name, UniqueCheck unique = null)
252 {
253 var rule = GetINetFwRule3(name, unique);
254 rule.Enabled = false;
255 }
256
257 public static void RemoveFirewallRulesByName(string name)
258 {
259 var rules = GetINetFwRules();
260 rules.Remove(name);
261 }
262
263 static string FormatErrorMessage(string name, string property, object expected, object actual, UniqueCheck unique)
264 {
265 return $"Assert Failure: {property} differ on rule: {name}" +
266 "\nExpected: " + expected +
267 "\nActual: " + actual +
268 "\n\nDirection: " + unique?.Direction +
269 "\nProfile: " + unique?.Profile +
270 "\nProtocol: " + unique?.Protocol +
271 "\nApplicationName: " + unique?.ApplicationName +
272 "\nLocalUserOwner: " + unique?.LocalUserOwner;
273 }
274
275 public static void VerifyFirewallRule(string name, RuleDetails expected, UniqueCheck unique = null)
276 {
277 var actual = GetFirewallRule(name, unique);
278 Assert.True(expected.Name == actual.Name, String.Format("Assert Failure: Names differ on rule: \nExpected: {0}\nActual: {1}", expected.Name, actual.Name));
279 Assert.True(expected.Description == actual.Description, FormatErrorMessage(name, "Descriptions", expected.Description, actual.Description, unique));
280 Assert.True(expected.ApplicationName == actual.ApplicationName, FormatErrorMessage(name, "ApplicationNames", expected.ApplicationName, actual.ApplicationName, unique));
281 Assert.True(expected.ServiceName == actual.ServiceName, FormatErrorMessage(name, "ServiceNames", expected.ServiceName, actual.ServiceName, unique));
282 Assert.True(expected.Protocol == actual.Protocol, FormatErrorMessage(name, "Protocols", expected.Protocol, actual.Protocol, unique));
283 Assert.True(expected.LocalPorts == actual.LocalPorts, FormatErrorMessage(name, "LocalPorts", expected.LocalPorts, actual.LocalPorts, unique));
284 Assert.True(expected.RemotePorts == actual.RemotePorts, FormatErrorMessage(name, "RemotePorts", expected.RemotePorts, actual.RemotePorts, unique));
285 Assert.True(expected.IcmpTypesAndCodes == actual.IcmpTypesAndCodes, FormatErrorMessage(name, "IcmpTypesAndCodes", expected.IcmpTypesAndCodes, actual.Description, unique));
286 Assert.True(expected.Direction == actual.Direction, FormatErrorMessage(name, "Directions", expected.Direction, actual.Direction, unique));
287 Assert.Equal<object>(expected.Interfaces, actual.Interfaces);
288 Assert.True(expected.InterfaceTypes == actual.InterfaceTypes, FormatErrorMessage(name, "InterfaceTypes", expected.InterfaceTypes, actual.InterfaceTypes, unique));
289 Assert.True(expected.Enabled == actual.Enabled, FormatErrorMessage(name, "Enabled flags", expected.Enabled, actual.Enabled, unique));
290 Assert.True(expected.Grouping == actual.Grouping, FormatErrorMessage(name, "Groupings", expected.Grouping, actual.Grouping, unique));
291 Assert.True(expected.Profiles == actual.Profiles, FormatErrorMessage(name, "Profiles", expected.Profiles, actual.Profiles, unique));
292 Assert.True(expected.EdgeTraversal == actual.EdgeTraversal, FormatErrorMessage(name, "EdgeTraversals", expected.EdgeTraversal, actual.EdgeTraversal, unique));
293 Assert.True(expected.Action == actual.Action, FormatErrorMessage(name, "Actions", expected.Action, actual.Action, unique));
294 Assert.True(expected.EdgeTraversalOptions == actual.EdgeTraversalOptions, FormatErrorMessage(name, "EdgeTraversalOptions", expected.EdgeTraversalOptions, actual.EdgeTraversalOptions, unique));
295 Assert.True(expected.LocalAppPackageId == actual.LocalAppPackageId, FormatErrorMessage(name, "LocalAppPackageIds", expected.LocalAppPackageId, actual.LocalAppPackageId, unique));
296 Assert.True(expected.LocalUserOwner == actual.LocalUserOwner, FormatErrorMessage(name, "LocalUserOwners", expected.LocalUserOwner, actual.LocalUserOwner, unique));
297 Assert.True(expected.LocalUserAuthorizedList == actual.LocalUserAuthorizedList, FormatErrorMessage(name, "LocalUserAuthorizedLists", expected.LocalUserAuthorizedList, actual.LocalUserAuthorizedList, unique));
298 Assert.True(expected.RemoteUserAuthorizedList == actual.RemoteUserAuthorizedList, FormatErrorMessage(name, "RemoteUserAuthorizedLists", expected.RemoteUserAuthorizedList, actual.RemoteUserAuthorizedList, unique));
299 Assert.True(expected.RemoteMachineAuthorizedList == actual.RemoteMachineAuthorizedList, FormatErrorMessage(name, "RemoteMachineAuthorizedLists", expected.RemoteMachineAuthorizedList, actual.RemoteMachineAuthorizedList, unique));
300 Assert.True(expected.SecureFlags == actual.SecureFlags, FormatErrorMessage(name, "SecureFlags", expected.SecureFlags, actual.SecureFlags, unique));
301 }
302 }
303}