Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PrincipalTest.cs « tests « System.DirectoryServices.AccountManagement « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 22b54d58d5035df997cd066f8c32a0620c1ceca9 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Security.Principal;
using Xunit;

namespace System.DirectoryServices.AccountManagement.Tests
{
    public abstract class PrincipalTest : IDisposable
    {
        public PrincipalContext DomainContext { get; private set; }

        public PrincipalTest() => RefreshContext();

        private void RefreshContext()
        {
            string username = "Administrator";
            string password = "PLACEHOLDER";

            string OU = "Tests";
            string baseDomain = WindowsIdentity.GetCurrent().Name.Split(new char[] { '\\' })[1] + "-TEST";
            string domain = $"{baseDomain}.nttest.microsoft.com";
            string container = $"ou={OU},dc={baseDomain},dc=nttest,dc=microsoft,dc=com";
            DomainContext?.Dispose();
            try
            {
                DomainContext = new PrincipalContext(ContextType.Domain, domain, container, username, password);
            }
            catch
            {
            }
        }

        public void Dispose() => DomainContext?.Dispose();

        [Fact]
        public void AddExistingPrincipal()
        {
            if (DomainContext == null)
            {
                return;
            }
            
            // use new GUID for the user name so we be sure this user does not exist yet
            string name = Guid.NewGuid().ToString();
            using (Principal principal = CreatePrincipal(DomainContext, name))
            {
                principal.Save();
            }

            Assert.NotNull(Principal.FindByIdentity(DomainContext, name));

            // this previously caused the user to be deleted. it is still expected to throw an exception, but not delete the user
            bool exceptionThrown = false;
            try
            {
                using (Principal principal = CreatePrincipal(DomainContext, name))
                {
                    principal.Save();
                }
            }
            catch (PrincipalExistsException)
            {
                exceptionThrown = true;
            }

            // validate that we correctly throw an exception when trying to add an existing principal
            Assert.True(exceptionThrown);

            // validate that we did not delete incorrectly delete the first principal
            using (Principal principal2 = Principal.FindByIdentity(DomainContext, name))
            {
                Assert.NotNull(principal2);

                // explicitly delete the user and check it was really deleted
                principal2.Delete();
            }

            // ensure we cleaned up the test principal
            Assert.Null(Principal.FindByIdentity(DomainContext, name));
        }

        [Fact]
        public void TestExtendedPrincipal()
        {
            if (DomainContext == null)
            {
                return;
            }

            string name = Guid.NewGuid().ToString();
            byte[] writtenArray = new byte[] { 10, 20, 30 };
            using (Principal principal = CreateExtendedPrincipal(DomainContext, name))
            {
                IExtendedPrincipalTest extendedPrincipal = (IExtendedPrincipalTest)principal;
                extendedPrincipal.ByteArrayExtension = writtenArray;
                principal.Save();
            }

            RefreshContext();
            using (Principal principal = FindExtendedPrincipal(DomainContext, name))
            {
                IExtendedPrincipalTest extendedPrincipal = (IExtendedPrincipalTest)principal;
                byte[] readArray = extendedPrincipal.ByteArrayExtension;
                principal.Delete();
            }
        }

        public abstract Principal CreatePrincipal(PrincipalContext context, string name);

        public abstract Principal CreateExtendedPrincipal(PrincipalContext context, string name);

        public abstract Principal FindExtendedPrincipal(PrincipalContext context, string name);
    }

    public interface IExtendedPrincipalTest
    {
        object ObjectExtension { get; set; }
        byte[] ByteArrayExtension { get; set; }
        object[] ObjectArrayExtension { get; set; }
    }
}