Permalink
Browse files

updated unit tests

  • Loading branch information...
emonti committed Mar 3, 2014
1 parent 550f9b5 commit f016d330d8c02fba85de2f3dc36f78e5d84bd8ce
View
@@ -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
View
@@ -1,4 +1,5 @@
require 'rbkb'
+require 'rbkb/extends'
require 'optparse'
# Copyright 2009 emonti at matasano.com
@@ -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
@@ -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
View
@@ -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
View
@@ -1,4 +1,5 @@
require 'rbkb/cli'
+require 'rbkb/extends'
# Copyright 2009 emonti at matasano.com
# See README.rdoc for license information
View
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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.
View
@@ -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
View
@@ -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
@@ -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
View
@@ -1,19 +0,0 @@
-require File.dirname(__FILE__) + '/test_helper.rb'
-
-class TestRbkb < Test::Unit::TestCase
-
- def setup
- end
-
- def test_truth
- assert true
- end
-
- # Must... have... green...
- def test_bones_stuff
- assert_equal Rbkb::VERSION, Rbkb.version
- assert_equal File.join(Rbkb::LIBPATH, "blah"), Rbkb.libpath("blah")
- assert_equal File.join(Rbkb::PATH, "blah"), Rbkb.path("blah")
- end
-
-end

0 comments on commit f016d33

Please sign in to comment.