Skip to content

Enhance rubocop configuration and test syntax #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
460 changes: 223 additions & 237 deletions .rubocop_todo.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/net/ldap/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def match(entry)
##
# Converts escaped characters (e.g., "\\28") to unescaped characters
# @note slawson20170317: Don't attempt to unescape 16 byte binary data which we assume are objectGUIDs
# The binary form of 5936AE79-664F-44EA-BCCB-5C39399514C6 triggers a BINARY -> UTF-8 conversion error
# The binary form of 5936AE79-664F-44EA-BCCB-5C39399514C6 triggers a BINARY -> UTF-8 conversion error
def unescape(right)
right = right.to_s
if right.length == 16 && right.encoding == Encoding::BINARY
Expand Down
2 changes: 1 addition & 1 deletion test/ber/test_ber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_utf8_encodable_strings
def test_encode_binary_data
# This is used for searching for GUIDs in Active Directory
assert_equal "\x04\x10" + "j1\xB4\xA1*\xA2zA\xAC\xA9`?'\xDDQ\x16".b,
["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").to_ber_bin
["6a31b4a12aa27a41aca9603f27dd5116"].pack("H*").to_ber_bin
end

def test_non_utf8_encodable_strings
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_ber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def test_true_ber_encoding
end

assert_includes Net::LDAP::ResultCodesSearchSuccess,
@ldap.get_operation_result.code, "should be a successful search operation"
@ldap.get_operation_result.code, "should be a successful search operation"
end
end
3 changes: 1 addition & 2 deletions test/integration/test_bind.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require_relative '../test_helper'

class TestBindIntegration < LDAPIntegrationTestCase

INTEGRATION_HOSTNAME = 'ldap.example.org'.freeze

def test_bind_success
Expand All @@ -28,7 +27,7 @@ def test_bind_anonymous_fail
assert_equal Net::LDAP::ResultCodeUnwillingToPerform, result.code
assert_equal Net::LDAP::ResultStrings[Net::LDAP::ResultCodeUnwillingToPerform], result.message
assert_equal "unauthenticated bind (DN with no password) disallowed",
result.error_message
result.error_message
assert_equal "", result.matched_dn
end

Expand Down
20 changes: 10 additions & 10 deletions test/integration/test_password_modify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class TestPasswordModifyIntegration < LDAPIntegrationTestCase
def setup
super
@admin_account = {dn: 'cn=admin,dc=example,dc=org', password: 'admin', method: :simple}
@admin_account = { dn: 'cn=admin,dc=example,dc=org', password: 'admin', method: :simple }
@ldap.authenticate @admin_account[:dn], @admin_account[:password]

@dn = 'uid=modify-password-user1,ou=People,dc=example,dc=org'
Expand Down Expand Up @@ -35,13 +35,13 @@ def test_password_modify
new_password: 'passworD2')

assert @ldap.get_operation_result.extended_response.nil?,
'Should not have generated a new password'
'Should not have generated a new password'

refute @ldap.bind(username: @dn, password: 'admin', method: :simple),
'Old password should no longer be valid'
'Old password should no longer be valid'

assert @ldap.bind(username: @dn, password: 'passworD2', method: :simple),
'New password should be valid'
'New password should be valid'
end

def test_password_modify_generate
Expand All @@ -54,10 +54,10 @@ def test_password_modify_generate
assert generated_password, 'Should have generated a password'

refute @ldap.bind(username: @dn, password: 'admin', method: :simple),
'Old password should no longer be valid'
'Old password should no longer be valid'

assert @ldap.bind(username: @dn, password: generated_password, method: :simple),
'New password should be valid'
'New password should be valid'
end

def test_password_modify_generate_no_old_password
Expand All @@ -69,10 +69,10 @@ def test_password_modify_generate_no_old_password
assert generated_password, 'Should have generated a password'

refute @ldap.bind(username: @dn, password: 'admin', method: :simple),
'Old password should no longer be valid'
'Old password should no longer be valid'

assert @ldap.bind(username: @dn, password: generated_password, method: :simple),
'New password should be valid'
'New password should be valid'
end

def test_password_modify_overwrite_old_password
Expand All @@ -81,10 +81,10 @@ def test_password_modify_overwrite_old_password
new_password: 'passworD3')

refute @ldap.bind(username: @dn, password: 'admin', method: :simple),
'Old password should no longer be valid'
'Old password should no longer be valid'

assert @ldap.bind(username: @dn, password: 'passworD3', method: :simple),
'New password should be valid'
'New password should be valid'
end

def teardown
Expand Down
5 changes: 3 additions & 2 deletions test/test_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def setup
%Q{dn: something
foo: foo
barAttribute: bar
})
},
)
end

def test_attribute
Expand All @@ -59,7 +60,7 @@ def test_modify_attribute
@entry.foo = 'bar'
assert_equal ['bar'], @entry.foo

@entry.fOo= 'baz'
@entry.fOo = 'baz'
assert_equal ['baz'], @entry.foo
end
end
1 change: 1 addition & 0 deletions test/test_filter_parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8

require_relative 'test_helper'

class TestFilterParser < Test::Unit::TestCase
Expand Down
8 changes: 4 additions & 4 deletions test/test_ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_encryption

def test_normalize_encryption_symbol
enc = @subject.send(:normalize_encryption, :start_tls)
assert_equal enc, {:method => :start_tls, :tls_options => {}}
assert_equal enc, :method => :start_tls, :tls_options => {}
end

def test_normalize_encryption_nil
Expand All @@ -104,11 +104,11 @@ def test_normalize_encryption_nil

def test_normalize_encryption_string
enc = @subject.send(:normalize_encryption, 'start_tls')
assert_equal enc, {:method => :start_tls, :tls_options => {}}
assert_equal enc, :method => :start_tls, :tls_options => {}
end

def test_normalize_encryption_hash
enc = @subject.send(:normalize_encryption, {:method => :start_tls, :tls_options => {:foo => :bar}})
assert_equal enc, {:method => :start_tls, :tls_options => {:foo => :bar}}
enc = @subject.send(:normalize_encryption, :method => :start_tls, :tls_options => { :foo => :bar })
assert_equal enc, :method => :start_tls, :tls_options => { :foo => :bar }
end
end
64 changes: 33 additions & 31 deletions test/test_ldap_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_modify_ops_add
end

def test_modify_ops_replace
args = { :operations =>[[:replace, "mail", "[email protected]"]] }
args = { :operations => [[:replace, "mail", "[email protected]"]] }
result = Net::LDAP::Connection.modify_ops(args[:operations])
expected = ["0#\n\x01\x020\x1E\x04\x04mail1\x16\x04\[email protected]"]
assert_equal(expected, result)
Expand Down Expand Up @@ -191,9 +191,9 @@ def test_queued_read_reads_until_message_id_match
result2 = make_message(2)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
conn = Net::LDAP::Connection.new(:socket => mock)

assert result = conn.queued_read(2)
Expand All @@ -206,9 +206,9 @@ def test_queued_read_modify
result2 = make_message(2, app_tag: Net::LDAP::PDU::ModifyResponse)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)

Expand All @@ -227,9 +227,9 @@ def test_queued_read_add
result2 = make_message(2, app_tag: Net::LDAP::PDU::AddResponse)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)

Expand All @@ -245,9 +245,9 @@ def test_queued_read_rename
result2 = make_message(2, app_tag: Net::LDAP::PDU::ModifyRDNResponse)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)

Expand All @@ -266,9 +266,9 @@ def test_queued_read_delete
result2 = make_message(2, app_tag: Net::LDAP::PDU::DeleteResponse)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)

Expand All @@ -284,13 +284,13 @@ def test_queued_read_setup_encryption_with_start_tls
result2 = make_message(2, app_tag: Net::LDAP::PDU::ExtendedResponse)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}, nil).
and_return(mock)
flexmock(Net::LDAP::Connection).should_receive(:wrap_with_ssl).with(mock, {}, nil)
.and_return(mock)

conn.next_msgid # simulates ongoing query

Expand All @@ -303,9 +303,9 @@ def test_queued_read_bind_simple
result2 = make_message(2, app_tag: Net::LDAP::PDU::BindResult)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)

Expand All @@ -314,7 +314,8 @@ def test_queued_read_bind_simple
assert result = conn.bind(
method: :simple,
username: "uid=user1,ou=People,dc=rubyldap,dc=com",
password: "passworD1")
password: "passworD1",
)
assert result.success?
assert_equal 2, result.message_id
end
Expand All @@ -324,9 +325,9 @@ def test_queued_read_bind_sasl
result2 = make_message(2, app_tag: Net::LDAP::PDU::BindResult)

mock = flexmock("socket")
mock.should_receive(:read_ber).
and_return(result1).
and_return(result2)
mock.should_receive(:read_ber)
.and_return(result1)
.and_return(result2)
mock.should_receive(:write)
conn = Net::LDAP::Connection.new(:socket => mock)

Expand All @@ -336,7 +337,8 @@ def test_queued_read_bind_sasl
method: :sasl,
mechanism: "fake",
initial_credential: "passworD1",
challenge_response: flexmock("challenge proc"))
challenge_response: flexmock("challenge proc"),
)
assert result.success?
assert_equal 2, result.message_id
end
Expand Down Expand Up @@ -469,8 +471,8 @@ def test_search_net_ldap_connection_event
search_result_ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""])
search_result_ber.ber_identifier = Net::LDAP::PDU::SearchResult
search_result = [1, search_result_ber]
@tcp_socket.should_receive(:read_ber).and_return(search_data).
and_return(search_result)
@tcp_socket.should_receive(:read_ber).and_return(search_data)
.and_return(search_result)

events = @service.subscribe "search.net_ldap_connection"
unread = @service.subscribe "search_messages_unread.net_ldap_connection"
Expand Down
24 changes: 12 additions & 12 deletions test/test_ldif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,54 @@ def test_ldif_with_version
def test_ldif_with_comments
str = ["# Hello from LDIF-land", "# This is an unterminated comment"]
io = StringIO.new(str[0] + "\r\n" + str[1])
ds = Net::LDAP::Dataset::read_ldif(io)
ds = Net::LDAP::Dataset.read_ldif(io)
assert_equal(str, ds.comments)
end

def test_ldif_with_password
psw = "goldbricks"
hashed_psw = "{SHA}" + Base64::encode64(Digest::SHA1.digest(psw)).chomp
hashed_psw = "{SHA}" + Base64.encode64(Digest::SHA1.digest(psw)).chomp

ldif_encoded = Base64::encode64(hashed_psw).chomp
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: Goldbrick\r\nuserPassword:: #{ldif_encoded}\r\n\r\n"))
ldif_encoded = Base64.encode64(hashed_psw).chomp
ds = Net::LDAP::Dataset.read_ldif(StringIO.new("dn: Goldbrick\r\nuserPassword:: #{ldif_encoded}\r\n\r\n"))
recovered_psw = ds["Goldbrick"][:userpassword].shift
assert_equal(hashed_psw, recovered_psw)
end

def test_ldif_with_continuation_lines
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: abcdefg\r\n hijklmn\r\n\r\n"))
ds = Net::LDAP::Dataset.read_ldif(StringIO.new("dn: abcdefg\r\n hijklmn\r\n\r\n"))
assert_equal(true, ds.key?("abcdefghijklmn"))
end

def test_ldif_with_continuation_lines_and_extra_whitespace
ds1 = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: abcdefg\r\n hijklmn\r\n\r\n"))
ds1 = Net::LDAP::Dataset.read_ldif(StringIO.new("dn: abcdefg\r\n hijklmn\r\n\r\n"))
assert_equal(true, ds1.key?("abcdefg hijklmn"))
ds2 = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: abcdefg\r\n hij klmn\r\n\r\n"))
ds2 = Net::LDAP::Dataset.read_ldif(StringIO.new("dn: abcdefg\r\n hij klmn\r\n\r\n"))
assert_equal(true, ds2.key?("abcdefghij klmn"))
end

def test_ldif_tab_is_not_continuation
ds = Net::LDAP::Dataset::read_ldif(StringIO.new("dn: key\r\n\tnotcontinued\r\n\r\n"))
ds = Net::LDAP::Dataset.read_ldif(StringIO.new("dn: key\r\n\tnotcontinued\r\n\r\n"))
assert_equal(true, ds.key?("key"))
end

def test_ldif_with_base64_dn
str = "dn:: Q049QmFzZTY0IGRuIHRlc3QsT1U9VGVzdCxPVT1Vbml0cyxEQz1leGFtcGxlLERDPWNvbQ==\r\n\r\n"
ds = Net::LDAP::Dataset::read_ldif(StringIO.new(str))
ds = Net::LDAP::Dataset.read_ldif(StringIO.new(str))
assert_equal(true, ds.key?("CN=Base64 dn test,OU=Test,OU=Units,DC=example,DC=com"))
end

def test_ldif_with_base64_dn_and_continuation_lines
str = "dn:: Q049QmFzZTY0IGRuIHRlc3Qgd2l0aCBjb250aW51YXRpb24gbGluZSxPVT1UZXN0LE9VPVVua\r\n XRzLERDPWV4YW1wbGUsREM9Y29t\r\n\r\n"
ds = Net::LDAP::Dataset::read_ldif(StringIO.new(str))
ds = Net::LDAP::Dataset.read_ldif(StringIO.new(str))
assert_equal(true, ds.key?("CN=Base64 dn test with continuation line,OU=Test,OU=Units,DC=example,DC=com"))
end

# TODO, INADEQUATE. We need some more tests
# to verify the content.
def test_ldif
File.open(TestLdifFilename, "r") do |f|
ds = Net::LDAP::Dataset::read_ldif(f)
ds = Net::LDAP::Dataset.read_ldif(f)
assert_equal(13, ds.length)
end
end
Expand All @@ -84,7 +84,7 @@ def test_to_ldif
entries = data.lines.grep(/^dn:\s*/) { $'.chomp }
dn_entries = entries.dup

ds = Net::LDAP::Dataset::read_ldif(io) do |type, value|
ds = Net::LDAP::Dataset.read_ldif(io) do |type, value|
case type
when :dn
assert_equal(dn_entries.first, value)
Expand Down
4 changes: 2 additions & 2 deletions test/test_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TestPassword < Test::Unit::TestCase
def test_psw
assert_equal("{MD5}xq8jwrcfibi0sZdZYNkSng==", Net::LDAP::Password.generate( :md5, "cashflow" ))
assert_equal("{SHA}YE4eGkN4BvwNN1f5R7CZz0kFn14=", Net::LDAP::Password.generate( :sha, "cashflow" ))
assert_equal("{MD5}xq8jwrcfibi0sZdZYNkSng==", Net::LDAP::Password.generate(:md5, "cashflow"))
assert_equal("{SHA}YE4eGkN4BvwNN1f5R7CZz0kFn14=", Net::LDAP::Password.generate(:sha, "cashflow"))
end
end
Loading