Skip to content

Commit

Permalink
updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emonti committed Mar 3, 2014
1 parent 550f9b5 commit f016d33
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 32 deletions.
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ require "bundler/gem_tasks"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :default => :spec

require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/test*.rb']
t.verbose = true
end
5 changes: 3 additions & 2 deletions lib/rbkb/cli.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rbkb'
require 'rbkb/extends'
require 'optparse'

# Copyright 2009 emonti at matasano.com
Expand Down Expand Up @@ -151,7 +152,7 @@ def add_range_opts(fkey, lkey)
raise "-x and -r are mutually exclusive" if @parser_got_range
@parser_got_range=true

unless m=/^(-?[0-9]+)(?::(-?[0-9]+))?$/.match(r)
unless /^(-?[0-9]+)(?::(-?[0-9]+))?$/.match(r)
raise "invalid range #{r.inspect}"
end

Expand All @@ -165,7 +166,7 @@ def add_range_opts(fkey, lkey)
raise "-x and -r are mutually exclusive" if @parser_got_range
@parser_got_range=true

unless m=/^(-?[0-9a-f]+)(?::(-?[0-9a-f]+))?$/i.match(r)
unless /^(-?[0-9a-f]+)(?::(-?[0-9a-f]+))?$/i.match(r)
raise "invalid range #{r.inspect}"
end

Expand Down
5 changes: 4 additions & 1 deletion lib/rbkb/cli/crc32.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ def parse(*args)
def go(*args)
super(*args)
@opts[:indat] ||= @stdin.read()
@stdout.puts( "%0.8x" % @opts[:indat][ @opts[:first] .. @opts[:last] ].crc32 )
dat = opts[:indat].force_to_binary
dat = dat[ @opts[:first] .. @opts[:last] ]
dat ||= ""
@stdout.puts( "%0.8x" % dat.force_to_binary.crc32 )
self.exit(0)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rbkb/cli/dedump.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rbkb/cli'
require 'rbkb/extends'

# Copyright 2009 emonti at matasano.com
# See README.rdoc for license information
Expand Down
11 changes: 5 additions & 6 deletions lib/rbkb/extends/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def dehexdump(opt={})
# iterate each line of hexdump
s.split(/\r?\n/).each do |hl|
# match and check offset
if m = dumprx.match(hl) and $1.hex == off
if dumprx.match(hl) and $1.hex == off
i+=1
# take the data chunk and unhexify it
raw = $2.unhexify
Expand Down Expand Up @@ -491,8 +491,8 @@ def bgrep(find, align=nil)

dat=self
if find.kind_of? Regexp
search = lambda do |m, buf|
if m = m.match(buf)
search = lambda do |mf, buf|
if m = mf.match(buf)
mtch = m[0]
off,endoff = m.offset(0)
return off, endoff, mtch
Expand All @@ -508,7 +508,7 @@ def bgrep(find, align=nil)

ret=[]
pos = 0
while (res = search.call(find, dat[pos..-1]))
while (res = search.call(find, dat[pos..-1].force_to_binary))
off, endoff, match = res
if align and ( pad = (pos+off).pad(align) ) != 0
pos += pad
Expand Down Expand Up @@ -571,7 +571,7 @@ def strings(opts={})
acc = /[\s[:print:]]/
ucc = /(?:#{acc}\x00)/

arx = /(#{acc}{#{min}}#{acc}*\x00?)/
arx = /(#{acc}{#{min}}#{acc}*\x00?)/
urx = /(#{ucc}{#{min}}#{ucc}*(?:\x00\x00)?)/

rx = case (opts[:encoding] || :both).to_sym
Expand All @@ -589,7 +589,6 @@ def strings(opts={})
raise "Encoding must be :unicode, :ascii, or :both"
end

off=0
ret = []

# wow ruby 1.9 string encoding is a total cluster
Expand Down
21 changes: 21 additions & 0 deletions spec/string_extends_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,25 @@
end
end

context 'strings' do
before :all do
@test_dat = RbkbString("a\000bc\001def\002gehi\003jklmn\004string 1\005string 2\020\370\f string 3\314string4\221string 5\n\000string 6\r\n\000\000\000\000string 7\000\000w\000i\000d\000e\000s\000t\000r\000i\000n\000g\000\000\000last string\000")

@expect_strings =[
[20, 28, :ascii, "string 1"],
[29, 37, :ascii, "string 2"],
[39, 49, :ascii, "\f string 3"],
[50, 57, :ascii, "string4"],
[58, 68, :ascii, "string 5\n\x00"],
[68, 79, :ascii, "string 6\r\n\x00"],
[82, 91, :ascii, "string 7\x00"],
[92, 114, :unicode, "w\x00i\x00d\x00e\x00s\x00t\x00r\x00i\x00n\x00g\x00\x00\x00"],
[114, 126, :ascii, "last string\x00"],
]
end

it "should find strings in a binary blob" do
@test_dat.strings.should == @expect_strings
end
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions test/test_cli_chars.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.join(File.dirname(__FILE__), "test_cli_helper.rb")

require 'rbkb/cli/chars'

class TestCliChars < Test::Unit::TestCase
include CliTest

Expand Down
4 changes: 0 additions & 4 deletions test/test_cli_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require File.join(File.dirname(__FILE__), 'test_helper.rb')
require 'rbkb/cli.rb'



Rbkb.require_all_libs_relative_to(File.dirname(__FILE__) + "/../lib/rbkb/cli.rb")

Rbkb::Cli::TESTING = true unless defined? Rbkb::Cli::TESTING

module CliTest
Expand Down
2 changes: 2 additions & 0 deletions test/test_cli_rstrings.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require File.join(File.dirname(__FILE__), "test_cli_helper.rb")

require 'rbkb/cli/rstrings'

# FIXME Finish test cases for rstrings cli

class TestCliRstrings < Test::Unit::TestCase
Expand Down
19 changes: 0 additions & 19 deletions test/test_rbkb.rb

This file was deleted.

0 comments on commit f016d33

Please sign in to comment.