Skip to content

Commit f21b3db

Browse files
committed
Add convenience class for creating partial sttributes
1 parent 906826b commit f21b3db

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

Flexinets.Ldap.Core.Tests/LdapPacketTests.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NUnit.Framework;
22
using System;
3+
using System.Collections.Generic;
34
using System.IO;
45

56
namespace Flexinets.Ldap.Core.Tests
@@ -132,7 +133,7 @@ public void TestPacketStuff()
132133

133134
var partialAttributeList = new LdapAttribute(UniversalDataType.Sequence);
134135

135-
136+
136137

137138
var partialAttributeUid = new LdapAttribute(UniversalDataType.Sequence);
138139
partialAttributeUid.ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, "uid")); // type
@@ -159,8 +160,34 @@ public void TestPacketStuff()
159160
var packet = LdapPacket.ParsePacket(responsEntryBytes);
160161
RecurseAttributes(packet);
161162
Assert.AreEqual(expected, Utils.ByteArrayToString(packet.GetBytes()));
163+
}
164+
165+
166+
[TestCase]
167+
public void TestPacketPartialAttribute()
168+
{
169+
var expected = "3084000000800204000000016478042d636e3d62696e64557365722c636e3d55736572732c64633d6465762c64633d636f6d70616e792c64633d636f6d3047301804037569643111040f75736572756964676f657368657265302b040b6f626a656374436c617373311c040c616161616161616161616161040c626262626262626262626262";
170+
var responseEntryPacket = new LdapPacket(1);
171+
var searchResultEntry = new LdapAttribute(LdapOperation.SearchResultEntry);
172+
searchResultEntry.ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, "cn=bindUser,cn=Users,dc=dev,dc=company,dc=com")); // objectName
173+
174+
var partialAttributeList = new LdapAttribute(UniversalDataType.Sequence);
175+
162176

163177

178+
partialAttributeList.ChildAttributes.Add(new LdapPartialAttribute("uid", "useruidgoeshere"));
179+
partialAttributeList.ChildAttributes.Add(new LdapPartialAttribute("objectClass", new List<String> { "aaaaaaaaaaaa", "bbbbbbbbbbbb" }));
180+
181+
searchResultEntry.ChildAttributes.Add(partialAttributeList);
182+
responseEntryPacket.ChildAttributes.Add(searchResultEntry);
183+
var responsEntryBytes = responseEntryPacket.GetBytes();
184+
185+
Console.WriteLine(Utils.ByteArrayToString(responsEntryBytes));
186+
187+
188+
var packet = LdapPacket.ParsePacket(responsEntryBytes);
189+
RecurseAttributes(packet);
190+
Assert.AreEqual(expected, Utils.ByteArrayToString(packet.GetBytes()));
164191
}
165192

166193

@@ -205,7 +232,7 @@ public void TestPacketThunderbirdSearch()
205232

206233

207234

208-
235+
209236

210237

211238
private void RecurseAttributes(LdapAttribute attribute, Int32 depth = 1)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace Flexinets.Ldap.Core.Tests
7+
{
8+
public class LdapPartialAttributeTests
9+
{
10+
[TestCase]
11+
public void TestLdapPartialAttributeDescription()
12+
{
13+
var expected = "objectClass";
14+
var attribute = new LdapPartialAttribute(expected, "test");
15+
16+
Assert.AreEqual(expected, attribute.Description);
17+
}
18+
19+
20+
[TestCase]
21+
public void TestLdapPartialAttributeSingleValue()
22+
{
23+
var expected = "test";
24+
var attribute = new LdapPartialAttribute("objectClass", expected);
25+
26+
Assert.AreEqual(expected, attribute.Values.SingleOrDefault());
27+
}
28+
29+
30+
[TestCase]
31+
public void TestLdapPartialAttributeMultipleValues()
32+
{
33+
var expected = new List<String> { "test", "foo", "bar" };
34+
var attribute = new LdapPartialAttribute("objectClass", expected);
35+
36+
var output = attribute.Values;
37+
CollectionAssert.AreEqual(expected, output);
38+
}
39+
}
40+
}

Flexinets.Ldap.Core/Flexinets.Ldap.Core.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ For an example server implementation see https://github.com/vforteli/FlexinetsLd
1313
<PackageProjectUrl>https://github.com/vforteli/Flexinets.Ldap.Core</PackageProjectUrl>
1414
<PackageReleaseNotes>Minor performance improvements</PackageReleaseNotes>
1515
<PackageTags>LDAP packet parse assemble authentication</PackageTags>
16-
<AssemblyVersion>1.0.79.0</AssemblyVersion>
17-
<FileVersion>1.0.79.0</FileVersion>
16+
<AssemblyVersion>1.0.85.0</AssemblyVersion>
17+
<FileVersion>1.0.85.0</FileVersion>
1818
</PropertyGroup>
1919

2020
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard2.0|AnyCPU'">
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Flexinets.Ldap.Core
6+
{
7+
/// <summary>
8+
/// Convenience class for creating PartialAttributes
9+
/// </summary>
10+
public class LdapPartialAttribute : LdapAttribute
11+
{
12+
/// <summary>
13+
/// Partial attribute description
14+
/// </summary>
15+
public String Description => (String)ChildAttributes.FirstOrDefault().GetValue();
16+
17+
18+
/// <summary>
19+
/// Partial attribute values
20+
/// </summary>
21+
public List<String> Values => ChildAttributes[1].ChildAttributes.Select(o => (String)o.GetValue()).ToList();
22+
23+
24+
/// <summary>
25+
/// Create a partial Attribute from list of values
26+
/// </summary>
27+
/// <param name="attributeDescription"></param>
28+
/// <param name="attributeValues"></param>
29+
public LdapPartialAttribute(String attributeDescription, IEnumerable<String> attributeValues) : base(UniversalDataType.Sequence)
30+
{
31+
ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, attributeDescription));
32+
var values = new LdapAttribute(UniversalDataType.Set);
33+
foreach (var value in attributeValues)
34+
{
35+
values.ChildAttributes.Add(new LdapAttribute(UniversalDataType.OctetString, value));
36+
}
37+
ChildAttributes.Add(values);
38+
}
39+
40+
41+
/// <summary>
42+
/// Create a partial attribute with a single value
43+
/// </summary>
44+
/// <param name="attributeDescription"></param>
45+
/// <param name="attributeValue"></param>
46+
public LdapPartialAttribute(String attributeDescription, String attributeValue) : this(attributeDescription, new List<String> { attributeValue })
47+
{
48+
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)