Skip to content

Commit 0d9206d

Browse files
author
blackhedd
committed
added Net::BER::BerIdentifiedOid type and related tests
1 parent 2ef2176 commit 0d9206d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/net/ber.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ def to_ber
5757
end
5858
end
5959

60+
class BerIdentifiedOid
61+
attr_accessor :ber_identifier
62+
def initialize oid
63+
if oid.is_a?(String)
64+
oid = oid.split(/\./).map {|s| s.to_i }
65+
end
66+
@value = oid
67+
end
68+
def to_ber
69+
# Provisional implementation.
70+
# We ASSUME that our incoming value is an array, and we
71+
# use the Array#to_ber_oid method defined below.
72+
# We probably should obsolete that method, actually, in
73+
# and move the code here.
74+
# WE ARE NOT CURRENTLY ENCODING THE BER-IDENTIFIER.
75+
# This implementation currently hardcodes 6, the universal OID tag.
76+
ary = @value.dup
77+
first = ary.shift
78+
raise Net::BER::BerError.new(" invalid OID" ) unless [0,1,2].include?(first)
79+
first = first * 40 + ary.shift
80+
ary.unshift first
81+
oid = ary.pack("w*")
82+
[6, oid.length].pack("CC") + oid
83+
end
84+
end
85+
6086
#--
6187
# This condenses our nicely self-documenting ASN hashes down
6288
# to an array for fast lookups.

tests/testber.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def test_ber_parser_on_ldap_bind_request
5757
end
5858

5959

60+
def test_oid
61+
oid = Net::BER::BerIdentifiedOid.new( [1,3,6,1,2,1,1,1,0] )
62+
assert_equal( "\006\b+\006\001\002\001\001\001\000", oid.to_ber )
63+
oid = Net::BER::BerIdentifiedOid.new( "1.3.6.1.2.1.1.1.0" )
64+
assert_equal( "\006\b+\006\001\002\001\001\001\000", oid.to_ber )
65+
end
6066

6167

6268
end

0 commit comments

Comments
 (0)