From 36b06f46df6bdece418195ca805334f532b4784e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Tobias=20Skjong-B=C3=B8rsting?= Date: Fri, 28 Jan 2011 14:50:54 +0100 Subject: [PATCH] Fix undefined method 'to_ber' for nil:NilClass. Convert unless nil. Made modify_ops a method to allow for unit testing of it. Added unit tests for add, delete and replace. --- lib/net/ldap.rb | 23 +++++++++++++++-------- test/test_ldap_connection.rb | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 test/test_ldap_connection.rb diff --git a/lib/net/ldap.rb b/lib/net/ldap.rb index b18762d..758fee5 100644 --- a/lib/net/ldap.rb +++ b/lib/net/ldap.rb @@ -1456,6 +1456,20 @@ class Net::LDAP::Connection #:nodoc: result_code end + def self.modify_ops args + modify_ops = [] + a = args[:operations] and a.each {|op, attr, values| + # TODO, fix the following line, which gives a bogus error + # if the opcode is invalid. + op_1 = {:add => 0, :delete => 1, :replace => 2} [op.to_sym].to_ber_enumerated + values = [values].flatten.map { |v| + v.to_ber unless v.nil? + }.to_ber_set + modify_ops << [op_1,[attr.to_s.to_ber,values].to_ber_sequence].to_ber + } + modify_ops + end + #-- # TODO: need to support a time limit, in case the server fails to respond. # TODO: We're throwing an exception here on empty DN. Should return a @@ -1465,14 +1479,7 @@ class Net::LDAP::Connection #:nodoc: #++ def modify(args) modify_dn = args[:dn] or raise "Unable to modify empty DN" - modify_ops = [] - a = args[:operations] and a.each { |op, attr, values| - # TODO, fix the following line, which gives a bogus error if the - # opcode is invalid. - op_1 = { :add => 0, :delete => 1, :replace => 2 }[op.to_sym].to_ber_enumerated - modify_ops << [op_1, [attr.to_s.to_ber, Array(values).map { |v| v.to_ber unless v.nil? }.to_ber_set].to_ber_sequence].to_ber_sequence - } - + modify_ops = modify_ops args[:operations] request = [modify_dn.to_ber, modify_ops.to_ber_sequence].to_ber_appsequence(6) pkt = [next_msgid.to_ber, request].to_ber_sequence diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb new file mode 100644 index 0000000..52c09ab --- /dev/null +++ b/test/test_ldap_connection.rb @@ -0,0 +1,24 @@ +require 'common' + +class TestLDAP < Test::Unit::TestCase + def test_modify_ops_delete + args = {:operations=>[[:delete, "mail"]]} + result = Net::LDAP::Connection.modify_ops(args) + expected = ["0\r\n\x01\x010\b\x04\x04mail1\x00"] + assert_equal(expected, result) + end + + def test_modify_ops_add + args = {:operations=>[[:add, "mail", "testuser@example.com"]]} + result = Net::LDAP::Connection.modify_ops(args) + expected = ["0#\n\x01\x000\x1E\x04\x04mail1\x16\x04\x14testuser@example.com"] + assert_equal(expected, result) + end + + def test_modify_ops_replace + args = {:operations=>[[:replace, "mail", "testuser@example.com"]]} + result = Net::LDAP::Connection.modify_ops(args) + expected = ["0#\n\x01\x020\x1E\x04\x04mail1\x16\x04\x14testuser@example.com"] + assert_equal(expected, result) + end +end