File tree Expand file tree Collapse file tree 3 files changed +39
-4
lines changed Expand file tree Collapse file tree 3 files changed +39
-4
lines changed Original file line number Diff line number Diff line change 7272
7373desc "(Provisional) Run tests for SNMP"
7474task :test_snmp do |t |
75- run_test_set t , [ 'tests/testsnmp.rb' ]
75+ run_test_set t , [ 'tests/testsnmp.rb' , 'tests/testber.rb' ]
7676end
7777
7878spec = eval ( File . read ( "net-ldap.gemspec" ) )
Original file line number Diff line number Diff line change @@ -409,9 +409,22 @@ def to_ber_length_encoding
409409class Bignum
410410
411411 def to_ber
412- i = [ self ] . pack ( 'w' )
413- i . length > 126 and raise Net ::BER ::BerError . new ( "range error in bignum" )
414- [ 2 , i . length ] . pack ( "CC" ) + i
412+ #i = [self].pack('w')
413+ #i.length > 126 and raise Net::BER::BerError.new( "range error in bignum" )
414+ #[2, i.length].pack("CC") + i
415+
416+ # Ruby represents Bignums as two's-complement numbers so we may actually be
417+ # good as far as representing negatives goes.
418+ # I'm sure this implementation can be improved performance-wise if necessary.
419+ sz = self . size
420+ out = "\000 " * sz
421+ ( sz *8 ) . times { |bit |
422+ if self [ bit ] == 1
423+ out [ bit /8 ] += ( 1 << ( bit % 8 ) )
424+ end
425+ }
426+
427+ [ 2 , sz ] . pack ( "CC" ) + out . reverse
415428 end
416429
417430end
Original file line number Diff line number Diff line change @@ -23,6 +23,28 @@ def test_ber_integers
2323 assert_equal ( "\002 \005 \222 \320 \227 \344 \000 " , 5000000000 . to_ber )
2424 end
2525
26+ def test_ber_bignums
27+ # Some of these values are Fixnums and some are Bignums. Different BER code.
28+ [
29+ 5 ,
30+ 50 ,
31+ 500 ,
32+ 5000 ,
33+ 50000 ,
34+ 500000 ,
35+ 5000000 ,
36+ 50000000 ,
37+ 500000000 ,
38+ 1000000000 ,
39+ 2000000000 ,
40+ 3000000000 ,
41+ 4000000000 ,
42+ 5000000000
43+ ] . each { |val |
44+ assert_equal ( val , val . to_ber . read_ber )
45+ }
46+ end
47+
2648 def test_ber_parsing
2749 assert_equal ( 6 , "\002 \001 \006 " . read_ber ( Net ::LDAP ::AsnSyntax ) )
2850 assert_equal ( "testing" , "\004 \007 testing" . read_ber ( Net ::LDAP ::AsnSyntax ) )
You can’t perform that action at this time.
0 commit comments