File Coverage

blib/lib/X500/DN/Marpa/DN.pm
Criterion Covered Total %
statement 38 38 100.0
branch 3 4 75.0
condition n/a
subroutine 12 12 100.0
pod 6 6 100.0
total 59 60 98.3


line stmt bran cond sub pod time code
1             package X500::DN::Marpa::DN;
2              
3 1     1   22338 use parent 'X500::DN::Marpa';
  1         310  
  1         5  
4 1     1   57 use strict;
  1         2  
  1         22  
5 1     1   4 use warnings;
  1         2  
  1         31  
6 1     1   5 use warnings qw(FATAL utf8); # Fatalize encoding glitches.
  1         2  
  1         39  
7              
8 1     1   4 use Moo;
  1         2  
  1         8  
9              
10 1     1   997 use X500::DN::Marpa::RDN;
  1         2  
  1         415  
11              
12             our $VERSION = '1.00';
13              
14             # ------------------------------------------------
15              
16             sub getRFC2253String
17             {
18 3     3 1 1920 my($self) = @_;
19              
20 3         17 return $self -> dn;
21              
22             } # End of getRFC2253String.
23              
24             # ------------------------------------------------
25              
26             sub getRDN
27             {
28 3     3 1 390 my($self, $n) = @_;
29 3         18 my($temp) = $self -> rdn($n + 1);
30              
31 3 50       14 return $temp if (length($temp) == 0);
32              
33 3         76 my($rdn) = X500::DN::Marpa::RDN -> new;
34              
35 3         338701 $rdn -> parse($temp);
36              
37 3         15 return $rdn;
38              
39             } # End of getRDN.
40              
41             # ------------------------------------------------
42              
43             sub getRDNs
44             {
45 3     3 1 16 my($self) = @_;
46              
47 3         23 return $self -> rdn_number;
48              
49             } # End of getRDNs.
50              
51             # ------------------------------------------------
52              
53             sub getX500String
54             {
55 3     3 1 414 my($self) = @_;
56              
57 3         17 return '{' . $self -> openssl_dn . '}';
58              
59             } # End of getX500String.
60              
61             # ------------------------------------------------
62              
63             sub hasMultivaluedRDNs
64             {
65 2     2 1 1275 my($self) = @_;
66 2         5 my($result) = 0;
67              
68 2         53 for my $rdn ($self -> stack -> print)
69             {
70 3 100       38 $result = 1 if ($$rdn{count} > 1);
71             }
72              
73 2         7 return $result;
74              
75             } # End of hasMultivaluedRDNs.
76              
77             # ------------------------------------------------
78              
79             sub ParseRFC2253
80             {
81 3     3 1 236001 my($self, $dn) = @_;
82              
83 3         19 $self -> parse($dn);
84              
85 3         10 return $self; # Sic. See docs.
86              
87             } # End of ParseRFC2253.
88              
89             # ------------------------------------------------
90              
91             1;
92              
93             =pod
94              
95             =encoding utf8
96              
97             =head1 NAME
98              
99             C - Backcompat module to emulate the DN part of C
100              
101             =head1 Synopsis
102              
103             This is scripts/back.compat.pl:
104              
105             #!/usr/bin/env perl
106              
107             use strict;
108             use warnings;
109              
110             use X500::DN::Marpa::DN;
111             use X500::DN::Marpa::RDN;
112              
113             # -----------------------
114              
115             print "Part 1:\n";
116              
117             my($dn) = X500::DN::Marpa::DN -> new;
118             my($text) = 'foo=FOO + bar=BAR + frob=FROB, baz=BAZ';
119              
120             $dn -> ParseRFC2253($text);
121              
122             print "Parsing: $text\n";
123             print 'RDN count: ', $dn -> getRDNs, " (Expected: 2)\n";
124             print 'DN: ', $dn -> getRFC2253String, " (Expected: baz=BAZ,foo=FOO+bar=BAR+frob=FROB)\n";
125             print 'X500 string: ', $dn -> getX500String, " (Expected: {foo=FOO+bar=BAR+frob=FROB+baz=BAZ})\n";
126             print '-' x 50, "\n";
127             print "Part 2:\n";
128              
129             my($rdn) = $dn -> getRDN(0);
130             my $type_count = $rdn -> getAttributeTypes;
131             my(@types) = $rdn -> getAttributeTypes;
132              
133             print 'RDN(0): ', $rdn -> dn, "\n";
134             print "Type count: $type_count (Expected: 3)\n";
135             print "Type [0]: $types[0] (Expected: foo)\n";
136             print "Type [1]: $types[1] (Expected: bar)\n";
137              
138             my(@values) = $rdn -> getAttributeValue('foo');
139              
140             print "Value [0]: $values[0] (Expected: FOO+bar=BAR+frob=FROB)\n";
141              
142             my($has_multi) = $dn -> hasMultivaluedRDNs;
143              
144             print "hasMulti: $has_multi (Expected: 1)\n";
145             print '-' x 50, "\n";
146             print "Part 2:\n";
147              
148             $rdn = $dn -> getRDN(1);
149              
150             @values = $rdn -> getAttributeValue('baz');
151              
152             print 'RDN(1): ', $rdn -> dn, "\n";
153             print "Value [0]: $values[0] (Expected: BAZ)\n";
154             print '-' x 50, "\n";
155              
156             Output of scripts/back.compat.pl:
157              
158             Part 1:
159             Parsing: foo=FOO + bar=BAR + frob=FROB, baz=BAZ
160             RDN count: 2 (Expected: 2)
161             DN: baz=BAZ,foo=FOO+bar=BAR+frob=FROB (Expected: baz=BAZ,foo=FOO+bar=BAR+frob=FROB)
162             X500 string: {foo=FOO+bar=BAR+frob=FROB+baz=BAZ} (Expected: {foo=FOO+bar=BAR+frob=FROB+baz=BAZ})
163             --------------------------------------------------
164             Part 2:
165             RDN(0): foo=FOO+bar=BAR+frob=FROB
166             Type count: 3 (Expected: 3)
167             Type [0]: foo (Expected: foo)
168             Type [1]: bar (Expected: bar)
169             Value [0]: FOO+bar=BAR+frob=FROB (Expected: FOO+bar=BAR+frob=FROB)
170             hasMulti: 1 (Expected: 1)
171             --------------------------------------------------
172             Part 2:
173             RDN(1): baz=BAZ
174             Value [0]: BAZ (Expected: BAZ)
175             --------------------------------------------------
176              
177             =head1 Description
178              
179             C provides a L-based parser for parsing X.500 Distinguished Names.
180              
181             This module emulates the DN parts of L.
182              
183             Notes:
184              
185             =over 4
186              
187             =item o C
188              
189             This module was based on the obsolete L:
190             Lightweight Directory Access Protocol (v3): UTF-8 String Representation of Distinguished Names.
191              
192             =item o C and C
193              
194             These modules are based on L:
195             Lightweight Directory Access Protocol (LDAP): String Representation of Distinguished Names.
196              
197             =back
198              
199             See also L and L.
200              
201             =head1 Distributions
202              
203             This module is available as a Unix-style distro (*.tgz).
204              
205             See L
206             for help on unpacking and installing distros.
207              
208             =head1 Installation
209              
210             Install C as you would any C module:
211              
212             Run:
213              
214             cpanm X500::DN::Marpa
215              
216             or run:
217              
218             sudo cpan X500::DN::Marpa
219              
220             or unpack the distro, and then either:
221              
222             perl Build.PL
223             ./Build
224             ./Build test
225             sudo ./Build install
226              
227             or:
228              
229             perl Makefile.PL
230             make (or dmake or nmake)
231             make test
232             make install
233              
234             =head1 Constructor and Initialization
235              
236             C is called as C<< my($parser) = X500::DN::Marpa::DN -> new(k1 => v1, k2 => v2, ...) >>.
237              
238             It returns a new object of type C.
239              
240             Key-value pairs accepted in the parameter list (see corresponding methods for details:
241              
242             =over 4
243              
244             =item o (None)
245              
246             =back
247              
248             =head1 Methods
249              
250             This module is a subclass of L and shares all its options to new(), and all its
251             methods. See L and L.
252              
253             Further, it has these methods:
254              
255             =head2 getRFC2253String()
256              
257             Returns the DN as a string.
258              
259             And yes, it's really based on RFC4514, as it says in the L.
260              
261             The DN is what was passed to L.
262              
263             =head2 getRDN($n)
264              
265             Returns an object of type L, containing the $n-th RDN, or returns '' if $n
266             is out of range.
267              
268             $n counts from 0.
269              
270             The returned object has already parsed the RDN, so you use that object via the methods documented in
271             L.
272              
273             If the input is 'UID=nobody@example.com,DC=example,DC=com', C returns an object which has
274             parsed 'uid=nobody@example.com'. Note the lower-case 'uid'.
275              
276             Warning: The parent class L counts RDNs from 1.
277              
278             =head2 getRDNs()
279              
280             Returns the number of RDNs in the DN parsed.
281              
282             =head2 getX500String()
283              
284             Returns what L calls an X500 version of the DN.
285              
286             =head2 hasMultivaluedRDNs()
287              
288             Returns a Boolean, 0 meaning there are no multvalued RDNs, and 1 meaning there is at least 1 such
289             RDN.
290              
291             =head2 new()
292              
293             See L for details on the parameters accepted by L.
294              
295             =head2 ParseRFC2253($dn)
296              
297             Parses $dn and returns $self (sic).
298              
299             This has to be the first method (after L of course) which you call on an object of type
300             C.
301              
302             So, you are expected to do this:
303              
304             my($parser) = X500::DN::Marpa::DN -> new;
305              
306             $parser -> ParseRFC2253($a_dn);
307              
308             And to just ignore the return value. After this, you call methods on $parser.
309              
310             If you do this:
311              
312             my($parser) = X500::DN::Marpa::DN -> new;
313             my($dn) = $parse -> ParseRFC2253($a_dn);
314              
315             It will work of course, but you have 2 copies of $parser, and you (probably) call methods on $dn.
316              
317             So, you could do this:
318              
319             my($dn) = X500::DN::Marpa::DN -> new -> ParseRFC2253($a_dn);
320              
321             And just ignore the intermediary copy, which has been discarded. After this, you call methods on
322             $dn.
323              
324             This means that to patch old code, just convert:
325              
326             my($dn) = X500::DN -> ParseRFC2253
327              
328             Into:
329              
330             my($dn) = X500::DN::Marpa::DN -> new -> ParseRFC2253
331              
332             =head1 FAQ
333              
334             See L.
335              
336             =head2 How to I transition to C before switching to C?
337              
338             See scripts/back.compat.pl.
339              
340             =head2 How do I upgrade code from C to C?
341              
342             See scripts/synopsis.pl.
343              
344             You can think of scripts/synopsis.pl as scripts/forward.compat.pl!
345              
346             =head2 How do you handle attribute values in double-quotes?
347              
348             RFC4514 does not discuss this topic.
349              
350             So, I ignore the quotes, because I assume none of your other software accepts them anyway, since
351             you're not using them any more, right?
352              
353             =head1 References
354              
355             See L.
356              
357             =head1 See Also
358              
359             L.
360              
361             =head1 Machine-Readable Change Log
362              
363             The file Changes was converted into Changelog.ini by L.
364              
365             =head1 Version Numbers
366              
367             Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
368              
369             =head1 Repository
370              
371             L
372              
373             =head1 Support
374              
375             Email the author, or log a bug on RT:
376              
377             L.
378              
379             =head1 Author
380              
381             L was written by Ron Savage Iron@savage.net.auE> in 2015.
382              
383             Marpa's homepage: L.
384              
385             My homepage: L.
386              
387             =head1 Copyright
388              
389             Australian copyright (c) 2015, Ron Savage.
390              
391             All Programs of mine are 'OSI Certified Open Source Software';
392             you can redistribute them and/or modify them under the terms of
393             The Artistic License 2.0, a copy of which is available at:
394             http://opensource.org/licenses/alphabetical.
395              
396             =cut