aboutsummaryrefslogtreecommitdiff
path: root/src/test/burn
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/burn')
-rw-r--r--src/test/burn/WixTestTools/UserGroupVerifier.cs198
1 files changed, 198 insertions, 0 deletions
diff --git a/src/test/burn/WixTestTools/UserGroupVerifier.cs b/src/test/burn/WixTestTools/UserGroupVerifier.cs
new file mode 100644
index 00000000..2f874057
--- /dev/null
+++ b/src/test/burn/WixTestTools/UserGroupVerifier.cs
@@ -0,0 +1,198 @@
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
4{
5 using System;
6 using System.Text;
7 using System.DirectoryServices;
8 using System.DirectoryServices.AccountManagement;
9 using System.Security.Principal;
10 using Xunit;
11
12 /// <summary>
13 /// Contains methods for User Group verification
14 /// </summary>
15 public static class UserGroupVerifier
16 {
17 /// <summary>
18 /// Create a local group on the machine
19 /// </summary>
20 /// <param name="groupName"></param>
21 /// <remarks>Has to be run as an Admin</remarks>
22 public static void CreateLocalGroup(string groupName)
23 {
24 DeleteLocalGroup(groupName);
25 GroupPrincipal newGroup = new GroupPrincipal(new PrincipalContext(ContextType.Machine));
26 newGroup.Name = groupName;
27 newGroup.Description = String.Empty;
28 newGroup.Save();
29 }
30
31 /// <summary>
32 /// Deletes a local gorup from the machine
33 /// </summary>
34 /// <param name="groupName">group name to delete</param>
35 /// <remarks>Has to be run as an Admin</remarks>
36 public static void DeleteLocalGroup(string groupName)
37 {
38 GroupPrincipal newGroup = GetGroup(String.Empty, groupName);
39 if (null != newGroup)
40 {
41 newGroup.Delete();
42 }
43 }
44
45 /// <summary>
46 /// Verifies that a group exists or not
47 /// </summary>
48 /// <param name="domainName">domain name for the group, empty for local groups</param>
49 /// <param name="groupName">the group name</param>
50 public static bool GroupExists(string domainName, string groupName)
51 {
52 GroupPrincipal group = GetGroup(domainName, groupName);
53
54 return null != group;
55 }
56
57 /// <summary>
58 /// Sets the group comment for a given group
59 /// </summary>
60 /// <param name="domainName">domain name for the group, empty for local users</param>
61 /// <param name="groupName">the group name</param>
62 /// <param name="comment">comment to be set for the group</param>
63 public static void SetGroupComment(string domainName, string groupName, string comment)
64 {
65 GroupPrincipal group = GetGroup(domainName, groupName);
66
67 Assert.False(null == group, String.Format("Group '{0}' was not found under domain '{1}'.", groupName, domainName));
68
69 var directoryEntry = group.GetUnderlyingObject() as DirectoryEntry;
70 Assert.False(null == directoryEntry);
71 directoryEntry.Properties["Description"].Value = comment;
72 group.Save();
73 }
74
75 /// <summary>
76 /// Adds the specified group to the specified local group
77 /// </summary>
78 /// <param name="memberName">Member to add</param>
79 /// <param name="groupName">Group to add too</param>
80 public static void AddGroupToGroup(string memberName, string groupName)
81 {
82 DirectoryEntry localMachine;
83 DirectoryEntry localGroup;
84
85 localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
86 localGroup = localMachine.Children.Find(groupName, "group");
87 Assert.False(null == localGroup, String.Format("Group '{0}' was not found.", groupName));
88 DirectoryEntry group = FindActiveDirectoryGroup(memberName);
89 localGroup.Invoke("Add", new object[] { group.Path.ToString() });
90 }
91
92 /// <summary>
93 /// Find the specified group in AD
94 /// </summary>
95 /// <param name="groupName">group name to lookup</param>
96 /// <returns>DirectoryEntry of the group</returns>
97 private static DirectoryEntry FindActiveDirectoryGroup(string groupName)
98 {
99 var mLocalMachine = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
100 var mLocalEntries = mLocalMachine.Children;
101
102 var theGroup = mLocalEntries.Find(groupName);
103 return theGroup;
104 }
105
106 /// <summary>
107 /// Verifies the group comment for a given group
108 /// </summary>
109 /// <param name="domainName">domain name for the group, empty for local users</param>
110 /// <param name="groupName">the group name</param>
111 /// <param name="comment">the comment to be verified</param>
112 public static void VerifyGroupComment(string domainName, string groupName, string comment)
113 {
114 GroupPrincipal group = GetGroup(domainName, groupName);
115
116 Assert.False(null == group, String.Format("Group '{0}' was not found under domain '{1}'.", groupName, domainName));
117
118 var directoryEntry = group.GetUnderlyingObject() as DirectoryEntry;
119 Assert.False(null == directoryEntry);
120 Assert.True(comment == (string)(directoryEntry.Properties["Description"].Value));
121 }
122
123 /// <summary>
124 /// Verify that a given group is member of a local group
125 /// </summary>
126 /// <param name="domainName">domain name for the group, empty for local groups</param>
127 /// <param name="memberName">the member name</param>
128 /// <param name="groupNames">list of groups to check for membership</param>
129 public static void VerifyIsMemberOf(string domainName, string memberName, params string[] groupNames)
130 {
131 IsMemberOf(domainName, memberName, true, groupNames);
132 }
133
134 /// <summary>
135 /// Verify that a given group is NOT member of a local group
136 /// </summary>
137 /// <param name="domainName">domain name for the group, empty for local groups</param>
138 /// <param name="memberName">the member name</param>
139 /// <param name="groupNames">list of groups to check for membership</param>
140 public static void VerifyIsNotMemberOf(string domainName, string memberName, params string[] groupNames)
141 {
142 IsMemberOf(domainName, memberName, false, groupNames);
143 }
144
145 /// <summary>
146 /// Verify that a given user is member of a local group
147 /// </summary>
148 /// <param name="domainName">domain name for the group, empty for local groups</param>
149 /// <param name="memberName">the member name</param>
150 /// <param name="shouldBeMember">whether the group is expected to be a member of the groups or not</param>
151 /// <param name="groupNames">list of groups to check for membership</param>
152 private static void IsMemberOf(string domainName, string memberName, bool shouldBeMember, params string[] groupNames)
153 {
154 GroupPrincipal group = GetGroup(domainName, memberName);
155 Assert.False(null == group, String.Format("Group '{0}' was not found under domain '{1}'.", memberName, domainName));
156
157 bool missedAGroup = false;
158 string message = String.Empty;
159 foreach (string groupName in groupNames)
160 {
161 try
162 {
163 bool found = group.IsMemberOf(new PrincipalContext(ContextType.Machine), IdentityType.Name, groupName);
164 if (found != shouldBeMember)
165 {
166 missedAGroup = true;
167 message += String.Format("Group '{0}/{1}' is {2} a member of local group '{3}'. \r\n", domainName, memberName, found ? String.Empty : "NOT", groupName);
168 }
169 }
170 catch (System.DirectoryServices.AccountManagement.PrincipalOperationException)
171 {
172 missedAGroup = true;
173 message += String.Format("Local group '{0}' was not found. \r\n", groupName);
174 }
175 }
176 Assert.False(missedAGroup, message);
177 }
178
179 /// <summary>
180 /// Returns the GroupPrincipal object for a given group
181 /// </summary>
182 /// <param name="domainName">Domain name to look under, if Empty the LocalMachine is assumed as the domain</param>
183 /// <param name="groupName"></param>
184 /// <returns>UserPrincipal Object for the group if found, or null other wise</returns>
185 private static GroupPrincipal GetGroup(string domainName, string groupName)
186 {
187 if (String.IsNullOrEmpty(domainName))
188 {
189 return GroupPrincipal.FindByIdentity(new PrincipalContext(ContextType.Machine), IdentityType.Name, groupName);
190 }
191 else
192 {
193 return GroupPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain,domainName), IdentityType.Name, groupName);
194 }
195 }
196 }
197}
198