|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Collections.Generic; |
4 | 5 | using System.Diagnostics;
|
5 | 6 | using System.DirectoryServices.Tests;
|
6 | 7 | using System.Globalization;
|
7 | 8 | using System.Net;
|
| 9 | +using System.Text; |
8 | 10 | using System.Threading;
|
9 | 11 | using Xunit;
|
10 | 12 |
|
@@ -474,6 +476,67 @@ private static void RunAsyncSearch(IAsyncResult asyncResult)
|
474 | 476 | }
|
475 | 477 | }
|
476 | 478 |
|
| 479 | + public static IEnumerable<object[]> TestCompareRequestTheory_TestData() |
| 480 | + { |
| 481 | + yield return new object[] { "input", "input", ResultCode.CompareTrue }; |
| 482 | + yield return new object[] { "input", Encoding.UTF8.GetBytes("input"), ResultCode.CompareTrue }; |
| 483 | + |
| 484 | + yield return new object[] { "input", "false", ResultCode.CompareFalse }; |
| 485 | + yield return new object[] { "input", new byte[] { 1, 2, 3, 4, 5 }, ResultCode.CompareFalse }; |
| 486 | + |
| 487 | + yield return new object[] { "http://example.com/", "http://example.com/", ResultCode.CompareTrue }; |
| 488 | + yield return new object[] { "http://example.com/", new Uri("http://example.com/"), ResultCode.CompareTrue }; |
| 489 | + yield return new object[] { "http://example.com/", Encoding.UTF8.GetBytes("http://example.com/"), ResultCode.CompareTrue }; |
| 490 | + |
| 491 | + yield return new object[] { "http://example.com/", "http://false/", ResultCode.CompareFalse }; |
| 492 | + yield return new object[] { "http://example.com/", new Uri("http://false/"), ResultCode.CompareFalse }; |
| 493 | + yield return new object[] { "http://example.com/", Encoding.UTF8.GetBytes("http://false/"), ResultCode.CompareFalse }; |
| 494 | + } |
| 495 | + |
| 496 | + [ConditionalTheory(nameof(IsLdapConfigurationExist))] |
| 497 | + [MemberData(nameof(TestCompareRequestTheory_TestData))] |
| 498 | + public void TestCompareRequestTheory(object value, object assertion, ResultCode compareResult) |
| 499 | + { |
| 500 | + using (LdapConnection connection = GetConnection()) |
| 501 | + { |
| 502 | + string ouName = "ProtocolsGroup10"; |
| 503 | + string rdn = "ou=" + ouName; |
| 504 | + |
| 505 | + DeleteEntry(connection, rdn); |
| 506 | + AddOrganizationalUnit(connection, rdn); |
| 507 | + |
| 508 | + string dn = rdn + "," + LdapConfiguration.Configuration.SearchDn; |
| 509 | + |
| 510 | + // set description to value |
| 511 | + var mod = new ModifyRequest(dn, DirectoryAttributeOperation.Replace, "description", value); |
| 512 | + var response = connection.SendRequest(mod); |
| 513 | + Assert.Equal(ResultCode.Success, response.ResultCode); |
| 514 | + |
| 515 | + // compare description to assertion |
| 516 | + var cmp = new CompareRequest(dn, new DirectoryAttribute("description", assertion)); |
| 517 | + response = connection.SendRequest(cmp); |
| 518 | + // assert compare result |
| 519 | + Assert.Equal(compareResult, response.ResultCode); |
| 520 | + |
| 521 | + // compare description to value |
| 522 | + cmp = new CompareRequest(dn, new DirectoryAttribute("description", value)); |
| 523 | + response = connection.SendRequest(cmp); |
| 524 | + // compare result always true |
| 525 | + Assert.Equal(ResultCode.CompareTrue, response.ResultCode); |
| 526 | + } |
| 527 | + } |
| 528 | + |
| 529 | + [ConditionalFact(nameof(IsLdapConfigurationExist))] |
| 530 | + public void TestCompareRequest() |
| 531 | + { |
| 532 | + using (LdapConnection connection = GetConnection()) |
| 533 | + { |
| 534 | + // negative case: ou=NotFound does not exist |
| 535 | + var cmp = new CompareRequest("ou=NotFound," + LdapConfiguration.Configuration.SearchDn, "ou", "NotFound"); |
| 536 | + Assert.Throws<DirectoryOperationException>(() => connection.SendRequest(cmp)); |
| 537 | + } |
| 538 | + } |
| 539 | + |
477 | 540 | [ConditionalFact(nameof(IsActiveDirectoryServer))]
|
478 | 541 | public void TestPageRequests()
|
479 | 542 | {
|
@@ -586,7 +649,7 @@ public void TestSortedSearch()
|
586 | 649 | }
|
587 | 650 | }
|
588 | 651 | }
|
589 |
| - |
| 652 | + |
590 | 653 | private void DeleteAttribute(LdapConnection connection, string entryDn, string attributeName)
|
591 | 654 | {
|
592 | 655 | string dn = entryDn + "," + LdapConfiguration.Configuration.SearchDn;
|
|
0 commit comments