summaryrefslogtreecommitdiff
path: root/src/test/burn/WixTestTools/Firewall/UniqueCheck.cs
blob: 7bc2e527e93f6def78cddce64fa393dd750e5704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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.

namespace WixTestTools.Firewall
{
    using NetFwTypeLib;

    /// <summary>
    /// A lot of firewall rules don't follow the Microsoft recommendation of using unique names.<br/>
    /// This class helps to disambiguate the rules based on Name, Direction, Profile, Protocol, ApplicationName, LocalUserOwner and RemoteAddresses.
    /// </summary>
    public class UniqueCheck
    {
        public UniqueCheck()
        {
        }

        public UniqueCheck(RuleDetails details)
        {
            this.Name = details.Name;
            this.Direction = details.Direction;
            this.Profile = details.Profiles;
            this.Protocol = details.Protocol;
            this.ApplicationName = details.ApplicationName;
            this.LocalUserOwner = details.LocalUserOwner;
            this.RemoteAddresses = details.RemoteAddresses;
            this.Grouping = details.Grouping;
        }


        public string Name { get; set; }

        public NET_FW_RULE_DIRECTION_? Direction { get; set; }

        public int? Profile { get; set; }

        public int? Protocol { get; set; }

        public string ApplicationName { get; set; }

        public string LocalUserOwner { get; set; }

        public string RemoteAddresses { get; set; }

        public string Grouping { get; set; }

        public bool FirewallRuleIsUnique(INetFwRule3 rule)
        {
            if (this.Name != null && rule.Name != this.Name)
            {
                return false;
            }

            if (this.Direction.HasValue && rule.Direction != this.Direction.Value)
            {
                return false;
            }

            if (this.Profile.HasValue && rule.Profiles != this.Profile.Value)
            {
                return false;
            }

            if (this.Protocol.HasValue && rule.Protocol != this.Protocol.Value)
            {
                return false;
            }

            if (this.ApplicationName != null && rule.ApplicationName != this.ApplicationName)
            {
                return false;
            }

            if (this.LocalUserOwner != null && rule.LocalUserOwner != this.LocalUserOwner)
            {
                return false;
            }

            if (this.RemoteAddresses != null && rule.RemoteAddresses != this.RemoteAddresses)
            {
                return false;
            }

            if (this.Grouping != null && rule.Grouping != this.Grouping)
            {
                return false;
            }

            return true;
        }
    }
}