Skip to content

Commit 75f37c5

Browse files
committed
! Fixes tests for everything BUT the number conversion
This commit removes read_ber! since no one seems to be using it. YAGNI.
1 parent 4c24cf2 commit 75f37c5

File tree

10 files changed

+185
-269
lines changed

10 files changed

+185
-269
lines changed

Manifest.txt

Whitespace-only changes.

Rakefile

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,69 @@
1-
# -*- ruby -*-
1+
require "rubygems"
2+
require "rake/gempackagetask"
3+
require "rake/rdoctask"
24

3-
require 'rubygems'
4-
require 'hoe'
5+
require "rake/testtask"
6+
Rake::TestTask.new do |t|
7+
t.libs << "test"
8+
t.test_files = FileList["test/test_*.rb"]
9+
t.verbose = true
10+
end
11+
12+
require 'spec/rake/spectask'
13+
Spec::Rake::SpecTask.new
14+
15+
task :default => ["test", 'spec']
16+
17+
# This builds the actual gem. For details of what all these options
18+
# mean, and other ones you can add, check the documentation here:
19+
#
20+
# http://rubygems.org/read/chapter/20
21+
#
22+
spec = Gem::Specification.new do |s|
523

6-
# Add 'lib' to load path.
7-
$LOAD_PATH.unshift( "#{File.dirname(__FILE__)}/lib" )
24+
# Change these as appropriate
25+
s.name = "net-ldap"
26+
s.version = "0.1.0"
27+
s.summary = "What this thing does"
28+
s.author = "Francis Cianfrocca", 'garbagecat10@gmail.com'
29+
s.author = 'Emiel van de Laar', 'gemiel@gmail.com'
30+
s.author = "Rory O'Connell", 'rory.ocon@gmail.com'
31+
s.author = "Kaspar Schiess", 'kaspar.schiess@absurd.li'
32+
33+
s.description = "Pure Ruby LDAP library"
34+
35+
# s.has_rdoc = true
36+
# s.extra_rdoc_files = %w(README.txt)
37+
# s.rdoc_options = %w(--main README.txt)
838

9-
# Pull in local 'net/ldap' as opposed to an installed version.
10-
require 'net'
39+
# Add any extra files to include in the gem
40+
s.files = %w(COPYING History.txt LICENSE Manifest.txt pre-setup.rb Rakefile README.txt Release-Announcement setup.rb) + Dir.glob("{test,lib/**/*}")
41+
s.require_paths = ["lib"]
42+
end
43+
44+
# This task actually builds the gem. We also regenerate a static
45+
# .gemspec file, which is useful if something (i.e. GitHub) will
46+
# be automatically building a gem for this project. If you're not
47+
# using GitHub, edit as appropriate.
48+
#
49+
# To publish your gem online, install the 'gemcutter' gem; Read more
50+
# about that here: http://gemcutter.org/pages/gem_docs
51+
Rake::GemPackageTask.new(spec) do |pkg|
52+
pkg.gem_spec = spec
1153

12-
Hoe.spec "net-ldap" do
13-
developer 'Francis Cianfrocca', 'garbagecat10@gmail.com'
14-
developer 'Emiel van de Laar', 'gemiel@gmail.com'
15-
developer "Rory O'Connell", 'rory.ocon@gmail.com'
54+
# Generate the gemspec file for github.
55+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
56+
File.open(file, "w") {|f| f << spec.to_ruby }
1657
end
1758

18-
# Hoe.new('net-ldap', Net::LDAP::VERSION) do |p|
19-
# p.rubyforge_name = 'net-ldap'
20-
# p.developer('Francis Cianfrocca', 'garbagecat10@gmail.com')
21-
# p.developer('Emiel van de Laar', 'gemiel@gmail.com')
22-
# end
59+
# Generate documentation
60+
Rake::RDocTask.new do |rd|
61+
rd.main = "README.txt"
62+
rd.rdoc_files.include("README.txt", "lib/**/*.rb")
63+
rd.rdoc_dir = "rdoc"
64+
end
2365

24-
# vim: syntax=Ruby
66+
desc 'Clear out RDoc and generated packages'
67+
task :clean => [:clobber_rdoc, :clobber_package] do
68+
rm "#{spec.name}.gemspec"
69+
end

lib/net/ber.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# $Id$
2-
#
31
# NET::BER
42
# Mixes ASN.1/BER convenience methods into several standard classes.
53
# Also provides BER parsing functionality.
@@ -25,8 +23,6 @@
2523
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2624
#
2725
#---------------------------------------------------------------------------
28-
#
29-
#
3026

3127
module Net
3228
module BER
@@ -105,3 +101,4 @@ def initialize oid
105101
end
106102
end
107103

104+
require 'net/ber/ber_parser'

lib/net/ber/ber_parser.rb

Lines changed: 18 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,35 @@ module BERParser
2828
}
2929
})
3030

31-
#
32-
# read_ber
33-
# TODO: clean this up so it works properly with partial
34-
# packets coming from streams that don't block when
35-
# we ask for more data (like StringIOs). At it is,
36-
# this can throw TypeErrors and other nasties.
37-
#--
38-
# BEWARE, this violates DRY and is largely equal in functionality to
39-
# read_ber_from_string. Eventually that method may subsume the functionality
40-
# of this one.
41-
#
4231
def read_ber syntax=nil
43-
# don't bother with this line, since IO#getc by definition returns nil on eof.
44-
#return nil if eof?
45-
46-
# here we'll create two different procs, one for 1.8 and one for 1.9
47-
# the reason being getc doesn't return a byte value in 1.9, so we need to
48-
# get the byte code out of the 1.9 encoded string
49-
50-
if RUBY_VERSION =~ /^1\.9/
51-
fetch_byte = Proc.new { getc.bytes.first }
52-
elsif RUBY_VERSION =~ /^1\.8/
53-
fetch_byte = Proc.new { getc }
32+
# TODO: clean this up so it works properly with partial
33+
# packets coming from streams that don't block when
34+
# we ask for more data (like StringIOs). At it is,
35+
# this can throw TypeErrors and other nasties.
36+
37+
# We might have been included in two kinds of things: IO-like
38+
# (answering to getbyte) and String-like (answering to to_s). Have
39+
# stream be a stream that is IO-like in both cases.
40+
if respond_to? :getbyte
41+
stream = self
42+
else
43+
stream = StringIO.new(self.to_s)
5444
end
45+
46+
id = stream.getbyte or return nil # don't trash this value, we'll use it later
5547

56-
id = fetch_byte.call or return nil # don't trash this value, we'll use it later
57-
#tag = id & 31
58-
#tag < 31 or raise BerError.new( "unsupported tag encoding: #{id}" )
59-
#tagclass = TagClasses[ id >> 6 ]
60-
#encoding = (id & 0x20 != 0) ? :constructed : :primitive
61-
62-
n = fetch_byte.call
48+
n = stream.getbyte
6349
lengthlength,contentlength = if n <= 127
6450
[1,n]
6551
else
6652
# Replaced the inject because it profiles hot.
67-
#j = (0...(n & 127)).inject(0) {|mem,x| mem = (mem << 8) + getc}
53+
# j = (0...(n & 127)).inject(0) {|mem,x| mem = (mem << 8) + getc}
6854
j = 0
6955
read( n & 127 ).each_byte {|n1| j = (j << 8) + n1}
7056
[1 + (n & 127), j]
7157
end
7258

73-
newobj = read contentlength
59+
newobj = stream.read contentlength
7460

7561
# This exceptionally clever and clear bit of code is verrrry slow.
7662
objtype = (syntax && syntax[id]) || BuiltinSyntax[id]
@@ -121,102 +107,11 @@ def read_ber syntax=nil
121107
n.ber_identifier = id
122108
n
123109
else
124-
#raise BerError.new( "unsupported object type: class=#{tagclass}, encoding=#{encoding}, tag=#{tag}" )
125110
raise BerError.new( "unsupported object type: id=#{id}" )
126111
end
127-
# Add the identifier bits into the object if it's a String or an Array.
128-
# We can't add extra stuff to Fixnums and booleans, not that it makes much sense anyway.
129-
# Replaced this mechanism with subclasses because the instance_eval profiled too hot.
130-
#obj and ([String,Array].include? obj.class) and obj.instance_eval "def ber_identifier; #{id}; end"
131-
#obj.ber_identifier = id if obj.respond_to?(:ber_identifier)
112+
132113
obj
133114
end
134-
135-
#--
136-
# Violates DRY! This replicates the functionality of #read_ber.
137-
# Eventually this method may replace that one.
138-
# This version of #read_ber behaves properly in the face of incomplete
139-
# data packets. If a full BER object is detected, we return an array containing
140-
# the detected object and the number of bytes consumed from the string.
141-
# If we don't detect a complete packet, return nil.
142-
#
143-
# Observe that weirdly we recursively call the original #read_ber in here.
144-
# That needs to be fixed if we ever obsolete the original method in favor of this one.
145-
def read_ber_from_string str, syntax=nil
146-
id = str[0].ord or return nil
147-
n = str[1].ord or return nil
148-
n_consumed = 2
149-
lengthlength,contentlength = if n <= 127
150-
[1,n]
151-
else
152-
n1 = n & 127
153-
return nil unless str.length >= (n_consumed + n1)
154-
j = 0
155-
n1.times {
156-
j = (j << 8) + str[n_consumed]
157-
n_consumed += 1
158-
}
159-
[1 + (n1), j]
160-
end
161-
162-
return nil unless str.length >= (n_consumed + contentlength)
163-
newobj = str[n_consumed...(n_consumed + contentlength)]
164-
n_consumed += contentlength
165-
166-
objtype = (syntax && syntax[id]) || BuiltinSyntax[id]
167-
168-
# == is expensive so sort this if/else so the common cases are at the top.
169-
obj = if objtype == :array
170-
seq = BerIdentifiedArray.new
171-
seq.ber_identifier = id
172-
sio = StringIO.new( newobj || "" )
173-
# Interpret the subobject, but note how the loop
174-
# is built: nil ends the loop, but false (a valid
175-
# BER value) does not!
176-
# Also, we can use the standard read_ber method because
177-
# we know for sure we have enough data. (Although this
178-
# might be faster than the standard method.)
179-
while (e = sio.read_ber(syntax)) != nil
180-
seq << e
181-
end
182-
seq
183-
elsif objtype == :string
184-
s = BerIdentifiedString.new( newobj || "" )
185-
s.ber_identifier = id
186-
s
187-
elsif objtype == :integer
188-
j = 0
189-
newobj.each_byte {|b| j = (j << 8) + b}
190-
j
191-
elsif objtype == :oid
192-
# cf X.690 pgh 8.19 for an explanation of this algorithm.
193-
# Potentially not good enough. We may need a BerIdentifiedOid
194-
# as a subclass of BerIdentifiedArray, to get the ber identifier
195-
# and also a to_s method that produces the familiar dotted notation.
196-
oid = newobj.unpack("w*")
197-
f = oid.shift
198-
g = if f < 40
199-
[0,f]
200-
elsif f < 80
201-
[1, f-40]
202-
else
203-
[2, f-80] # f-80 can easily be > 80. What a weird optimization.
204-
end
205-
oid.unshift g.last
206-
oid.unshift g.first
207-
oid
208-
elsif objtype == :boolean
209-
newobj != "\000"
210-
elsif objtype == :null
211-
n = BerIdentifiedNull.new
212-
n.ber_identifier = id
213-
n
214-
else
215-
raise BerError.new( "unsupported object type: id=#{id}" )
216-
end
217-
218-
[obj, n_consumed]
219-
end
220115
end
221116
end
222117
end

lib/net/ldap.rb

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
1-
# $Id$
2-
#
3-
# Net::LDAP for Ruby
4-
#
5-
#
6-
# Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7-
#
8-
# Written and maintained by Francis Cianfrocca, gmail: garbagecat10.
9-
#
10-
# This program is free software.
11-
# You may re-distribute and/or modify this program under the same terms
12-
# as Ruby itself: Ruby Distribution License or GNU General Public License.x
13-
#
14-
#
15-
# See Net::LDAP for documentation and usage samples.
16-
#
1+
require 'openssl'
172

3+
require 'net/ber'
184
require 'net/ldap/pdu'
195
require 'net/ldap/filter'
206
require 'net/ldap/dataset'
217
require 'net/ldap/psw'
228
require 'net/ldap/entry'
9+
require 'net/ldap/core_ext/all'
2310

2411
module Net
2512

@@ -258,7 +245,7 @@ class LdapError < StandardError; end
258245
SearchScope_WholeSubtree = 2
259246
SearchScopes = [SearchScope_BaseObject, SearchScope_SingleLevel, SearchScope_WholeSubtree]
260247

261-
AsnSyntax = BER.compile_syntax({
248+
AsnSyntax = Net::BER.compile_syntax({
262249
:application => {
263250
:primitive => {
264251
2 => :null # UnbindRequest body

lib/net/ldap/core_ext/string.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ def read_ber syntax=nil
3232
StringIO.new(self).read_ber(syntax)
3333
end
3434

35-
def read_ber! syntax=nil
36-
obj,n_consumed = read_ber_from_string(self, syntax)
37-
if n_consumed
38-
self.slice!(0...n_consumed)
39-
obj
40-
else
41-
nil
42-
end
43-
end
44-
35+
# def read_ber! syntax=nil
36+
# obj,n_consumed = read_ber_from_string(self, syntax)
37+
# if n_consumed
38+
# self.slice!(0...n_consumed)
39+
# obj
40+
# else
41+
# nil
42+
# end
43+
# end
4544
end
4645
end
4746
end

lib/net/ldap/dataset.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,26 @@ module Net
3232
class LDAP
3333
class Dataset < Hash
3434
attr_reader :comments
35+
3536
class IOFilter
3637
def initialize(io)
3738
@io = io
3839
end
39-
4040
def gets
4141
s = @io.gets
4242
s.chomp if s
4343
end
4444
end
45-
45+
4646
def self.read_ldif io
4747
ds = Dataset.new
48+
io = IOFilter.new(io)
4849

4950
line = io.gets
5051
dn = nil
5152

5253
while line
53-
io.gets and chomp
54+
new_line = io.gets
5455
if new_line =~ /^[\s]+/
5556
line << " " << $'
5657
else
@@ -75,7 +76,7 @@ def self.read_ldif io
7576
line = nextline
7677
end
7778
end
78-
79+
7980
ds
8081
end
8182

0 commit comments

Comments
 (0)