From 0d9206d602a0e6b8eb1fc0900acd10e4d84322f9 Mon Sep 17 00:00:00 2001 From: blackhedd Date: Tue, 19 Dec 2006 18:27:57 +0000 Subject: [PATCH] added Net::BER::BerIdentifiedOid type and related tests --- lib/net/ber.rb | 26 ++++++++++++++++++++++++++ tests/testber.rb | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/lib/net/ber.rb b/lib/net/ber.rb index 43e87e4..6a15224 100644 --- a/lib/net/ber.rb +++ b/lib/net/ber.rb @@ -57,6 +57,32 @@ module Net end end + class BerIdentifiedOid + attr_accessor :ber_identifier + def initialize oid + if oid.is_a?(String) + oid = oid.split(/\./).map {|s| s.to_i } + end + @value = oid + end + def to_ber + # Provisional implementation. + # We ASSUME that our incoming value is an array, and we + # use the Array#to_ber_oid method defined below. + # We probably should obsolete that method, actually, in + # and move the code here. + # WE ARE NOT CURRENTLY ENCODING THE BER-IDENTIFIER. + # This implementation currently hardcodes 6, the universal OID tag. + ary = @value.dup + first = ary.shift + raise Net::BER::BerError.new(" invalid OID" ) unless [0,1,2].include?(first) + first = first * 40 + ary.shift + ary.unshift first + oid = ary.pack("w*") + [6, oid.length].pack("CC") + oid + end + end + #-- # This condenses our nicely self-documenting ASN hashes down # to an array for fast lookups. diff --git a/tests/testber.rb b/tests/testber.rb index fac4a10..970f71b 100644 --- a/tests/testber.rb +++ b/tests/testber.rb @@ -57,6 +57,12 @@ class TestBer < Test::Unit::TestCase end + def test_oid + oid = Net::BER::BerIdentifiedOid.new( [1,3,6,1,2,1,1,1,0] ) + assert_equal( "\006\b+\006\001\002\001\001\001\000", oid.to_ber ) + oid = Net::BER::BerIdentifiedOid.new( "1.3.6.1.2.1.1.1.0" ) + assert_equal( "\006\b+\006\001\002\001\001\001\000", oid.to_ber ) + end end