distinguished_name 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/distinguished_name.rb +1 -0
- data/lib/distinguished_name/transform.rb +29 -0
- data/lib/distinguished_name/version.rb +1 -1
- data/test/test_helper.rb +1 -1
- data/test/transform_test.rb +62 -0
- metadata +4 -1
data/lib/distinguished_name.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
3
|
+
class DistinguishedName::Transform
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
def slashify(dn_string)
|
8
|
+
x509_parsed_dn = parse_dn(dn_string)
|
9
|
+
x509_parsed_dn.to_a.map {|piece| "/#{piece[0]}=#{piece[1]}" }.reverse.join
|
10
|
+
end
|
11
|
+
|
12
|
+
def ldapify(dn_string)
|
13
|
+
x509_parsed_dn = parse_dn(dn_string)
|
14
|
+
x509_parsed_dn.to_a.map {|piece| "#{piece[0]}=#{piece[1]}" }.reverse.join(",")
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def parse_dn(dn_string)
|
20
|
+
begin
|
21
|
+
OpenSSL::X509::Name.parse(dn_string)
|
22
|
+
rescue Java::JavaLang::NullPointerException #MRI can throw a TypeError
|
23
|
+
raise OpenSSL::X509::NameError
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
|
2
|
+
require_relative "../lib/distinguished_name"
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class TransformTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@ldap_dn = "CN=Ward.Hines.86,OU=people,OU=Steelers,O=NFL,C=US"
|
8
|
+
@slash_dn = "/C=US/O=NFL/OU=Steelers/OU=people/CN=Ward.Hines.86"
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
=begin
|
13
|
+
def test_ldapify_doesnt_modify_ldap_dn
|
14
|
+
assert_equal(@ldap_dn, DistinguishedName::Transform.ldapify(@ldap_dn))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_slashify_doesnt_modify_slash_dn
|
18
|
+
assert_equal(@slash_dn, DistinguishedName::Transform.slashify(@slash_dn))
|
19
|
+
end
|
20
|
+
=end
|
21
|
+
|
22
|
+
def test_slashify_accepts_a_comma_separated_version_and_returns_a_slash_separated_version_of_the_dn
|
23
|
+
assert_equal(@slash_dn, DistinguishedName::Transform.slashify(@ldap_dn))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_ldapify_accepts_a_slash_separated_version_and_returns_a_comma_separated_version_of_the_dn
|
27
|
+
assert_equal(@ldap_dn, DistinguishedName::Transform.ldapify(@slash_dn))
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_slashify_method_fails_gracefully_with_invalid_dn_string_that_has_no_equal_sign
|
31
|
+
bad_dn = 'foo'
|
32
|
+
assert_raise OpenSSL::X509::NameError do
|
33
|
+
DistinguishedName::Transform.slashify(bad_dn)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_dn_parsing_raises_when_there_is_an_invalid_keyword
|
38
|
+
bad_dn = 'INVALID_KEYWORD=YoMama'
|
39
|
+
assert_raise OpenSSL::X509::NameError do
|
40
|
+
DistinguishedName::Transform.ldapify(bad_dn)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_ldapify_method_fails_gracefully_with_invalid_dn_string_that_has_no_equal_sign
|
45
|
+
bad_dn = 'foo'
|
46
|
+
assert_raise OpenSSL::X509::NameError do
|
47
|
+
DistinguishedName::Transform.ldapify(bad_dn)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_arbitrarily_ordered_keywords_slashifies_correctly
|
52
|
+
arbitrary_comma_dn = "ST=New Jersey,L=What is L,OU=Organizational Units,OU=Oklahoma University,CN=Ja Rule,C=CAN"
|
53
|
+
expected_dn = "/C=CAN/CN=Ja Rule/OU=Oklahoma University/OU=Organizational Units/L=What is L/ST=New Jersey"
|
54
|
+
assert_equal(expected_dn, DistinguishedName::Transform.slashify(arbitrary_comma_dn))
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_arbitrarily_ordered_keywords_ldapifies_correctly
|
58
|
+
arbitrary_slash_dn = "/C=CAN/CN=Ja Rule/OU=Oklahoma University/OU=Organizational Units/L=What is L/ST=New Jersey"
|
59
|
+
expected_dn = "ST=New Jersey,L=What is L,OU=Organizational Units,OU=Oklahoma University,CN=Ja Rule,C=CAN"
|
60
|
+
assert_equal(expected_dn, DistinguishedName::Transform.ldapify(arbitrary_slash_dn))
|
61
|
+
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: distinguished_name
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -26,9 +26,11 @@ files:
|
|
26
26
|
- distinguished_name.gemspec
|
27
27
|
- lib/distinguished_name.rb
|
28
28
|
- lib/distinguished_name/canonicalize.rb
|
29
|
+
- lib/distinguished_name/transform.rb
|
29
30
|
- lib/distinguished_name/version.rb
|
30
31
|
- test/canonicalize_test.rb
|
31
32
|
- test/test_helper.rb
|
33
|
+
- test/transform_test.rb
|
32
34
|
homepage: ''
|
33
35
|
licenses: []
|
34
36
|
post_install_message:
|
@@ -56,4 +58,5 @@ summary: Methods for interacting with distinguished name strings
|
|
56
58
|
test_files:
|
57
59
|
- test/canonicalize_test.rb
|
58
60
|
- test/test_helper.rb
|
61
|
+
- test/transform_test.rb
|
59
62
|
...
|