ruby-mtbl 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ruby-mtbl.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'ruby-mtbl'
5
+ s.version = '1.0.1'
6
+ s.author = 'HD Moore'
7
+ s.email = 'x@hdm.io'
8
+ s.license = 'Apache-2.0'
9
+ s.homepage = "https://www.github.com/hdm/ruby-mtbl"
10
+ s.summary = %q{ruby-mtbl: Ruby interface to the MTBL SortedString library}
11
+ s.description = %q{
12
+ The ruby-mtbl gem provides a Ruby interface to Farsight Security's MTBL SortedString library.
13
+ }.gsub(/\s+/, ' ').strip
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.extensions = [ 'ext/mtbl/extconf.rb' ]
18
+ s.require_paths = ['lib']
19
+ end
@@ -0,0 +1,112 @@
1
+ require 'mtbl'
2
+
3
+ RSpec.describe MTBL::Reader, "#new" do
4
+
5
+ context "missing file" do
6
+ it "throws an exception" do
7
+ expect { MTBL::Reader.new("missing.mtbl") }.to raise_error(::RuntimeError)
8
+ end
9
+ end
10
+
11
+ context "valid file" do
12
+ test_mtbl1 = File.expand_path(File.join(File.dirname(__FILE__), "..", "examples", "test1.mtbl"))
13
+ it "opens successfully" do
14
+ expect { reader = MTBL::Reader.new(test_mtbl1) }.to_not raise_error
15
+ end
16
+ end
17
+ end
18
+
19
+ RSpec.describe MTBL::Reader, "#iterator" do
20
+ test_mtbl1 = File.expand_path(File.join(File.dirname(__FILE__), "..", "examples", "test1.mtbl"))
21
+
22
+ context "finds all records" do
23
+ it "has 676 entries" do
24
+ iterator = MTBL::Reader.new(test_mtbl1).iterator
25
+ count = 0
26
+ while (r = iterator.next)
27
+ count += 1
28
+ end
29
+ expect(count).to be(676)
30
+ end
31
+
32
+ it "returns the first entry" do
33
+ iterator = MTBL::Reader.new(test_mtbl1).iterator
34
+ expect(iterator.next).to eq(["aa","1"])
35
+ end
36
+
37
+ it "returns the last entry" do
38
+ iterator = MTBL::Reader.new(test_mtbl1).iterator
39
+ last_entry = nil
40
+ while(r = iterator.next)
41
+ last_entry = r
42
+ end
43
+ expect(last_entry).to eq(["zz","676"])
44
+ end
45
+ end
46
+ end
47
+
48
+ RSpec.describe MTBL::Reader, "#get" do
49
+ test_mtbl1 = File.expand_path(File.join(File.dirname(__FILE__), "..", "examples", "test1.mtbl"))
50
+
51
+ context "finds an exact key" do
52
+ it "finds aj" do
53
+ expect(MTBL::Reader.new(test_mtbl1).get("aj").next).to eq(["aj", "10"])
54
+ end
55
+ it "finds zx" do
56
+ expect(MTBL::Reader.new(test_mtbl1).get("zx").next).to eq(["zx", "674"])
57
+ end
58
+ end
59
+ end
60
+
61
+ RSpec.describe MTBL::Reader, "#get_prefix" do
62
+ test_mtbl1 = File.expand_path(File.join(File.dirname(__FILE__), "..", "examples", "test1.mtbl"))
63
+
64
+ context "finds prefixes" do
65
+ it "finds a*" do
66
+ count = 0
67
+ iterator = MTBL::Reader.new(test_mtbl1).get_prefix("a")
68
+ while(r = iterator.next)
69
+ count += 1
70
+ end
71
+ expect(count).to be(26)
72
+ end
73
+
74
+ it "finds z*" do
75
+ count = 0
76
+ iterator = MTBL::Reader.new(test_mtbl1).get_prefix("z")
77
+ while(r = iterator.next)
78
+ count += 1
79
+ end
80
+ expect(count).to be(26)
81
+ end
82
+
83
+ it "finds jj" do
84
+ expect(MTBL::Reader.new(test_mtbl1).get_prefix("jj").next).to eq(["jj", "244"])
85
+ end
86
+ end
87
+ end
88
+
89
+ RSpec.describe MTBL::Reader, "#get_range" do
90
+ test_mtbl1 = File.expand_path(File.join(File.dirname(__FILE__), "..", "examples", "test1.mtbl"))
91
+
92
+ context "finds ranges" do
93
+
94
+ it "finds ab..az" do
95
+ count = 0
96
+ iterator = MTBL::Reader.new(test_mtbl1).get_range("ab", "az")
97
+ while(r = iterator.next)
98
+ count += 1
99
+ end
100
+ expect(count).to eq(25)
101
+ end
102
+
103
+ it "finds ca..jp" do
104
+ count = 0
105
+ iterator = MTBL::Reader.new(test_mtbl1).get_range("ca", "jp")
106
+ while(r = iterator.next)
107
+ count += 1
108
+ end
109
+ expect(count).to eq(198)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,29 @@
1
+ require 'mtbl'
2
+ require 'tempfile'
3
+
4
+ RSpec.describe MTBL::Sorter, "#new" do
5
+
6
+ context "records" do
7
+ sorter = MTBL::Sorter.new
8
+
9
+ it "handles invalid adds" do
10
+ expect { sorter.add(:invalid, "test") }.to raise_error(ArgumentError)
11
+ expect { sorter.add("test", :invalid) }.to raise_error(ArgumentError)
12
+ expect { sorter.add("test", 9000) }.to raise_error(ArgumentError)
13
+ expect { sorter.add(400, "blah") }.to raise_error(ArgumentError)
14
+ end
15
+
16
+ it "accepts sequential keys" do
17
+ expect { sorter.add("1000", "test1") }.to_not raise_error
18
+ expect { sorter.add("1005", "test2") }.to_not raise_error
19
+ expect { sorter.add("1010", "test3") }.to_not raise_error
20
+ end
21
+
22
+ it "accepts non-sequential keys" do
23
+ expect { sorter.add("1001", "test1") }.to_not raise_error
24
+ expect { sorter.add("1004", "test1") }.to_not raise_error
25
+ expect { sorter.add("1009", "test1") }.to_not raise_error
26
+ end
27
+
28
+ end
29
+ end
data/spec/mtbl_spec.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'mtbl'
2
+
3
+ RSpec.describe MTBL, "#constants" do
4
+ context "class" do
5
+ it "has classes" do
6
+ expect(MTBL.constants.include?(:Reader)).to be(true)
7
+ expect(MTBL.constants.include?(:Writer)).to be(true)
8
+ expect(MTBL.constants.include?(:Utils)).to be(true)
9
+ expect(MTBL.constants.include?(:Sorter)).to be(true)
10
+ expect(MTBL.constants.include?(:COMPRESSION_NONE)).to be(true)
11
+ expect(MTBL.constants.include?(:COMPRESSION_ZLIB)).to be(true)
12
+ expect(MTBL.constants.include?(:COMPRESSION_SNAPPY)).to be(true)
13
+ expect(MTBL.constants.include?(:COMPRESSION_LZ4)).to be(true)
14
+ expect(MTBL.constants.include?(:COMPRESSION_LZ4HC)).to be(true)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,40 @@
1
+ require 'mtbl'
2
+ require 'tempfile'
3
+
4
+ RSpec.describe MTBL::Utils, "#metadata" do
5
+ test_mtbl1 = File.expand_path(File.join(File.dirname(__FILE__), "..", "examples", "test1.mtbl"))
6
+
7
+ context "invalid file" do
8
+
9
+ it "throws an exception with an invalid path" do
10
+ expect{ MTBL::Utils.metadata("missing.mtbl") }.to raise_error(RuntimeError)
11
+ end
12
+
13
+ it "throws an exception with an invalid file" do
14
+ tfile = Tempfile.new("mtbl")
15
+ expect{ MTBL::Utils.metadata(tfile.path) }.to raise_error(RuntimeError)
16
+ end
17
+
18
+ it "throws an exception with a non-string argument" do
19
+ expect{ MTBL::Utils.metadata(3000) }.to raise_error(ArgumentError)
20
+ end
21
+
22
+ end
23
+
24
+ context "valid file" do
25
+ info = MTBL::Utils.metadata(test_mtbl1)
26
+ it "has correct metadata" do
27
+ expect(info[:filesize]).to eq(2866)
28
+ expect(info[:index_block_offset]).to eq(2332)
29
+ expect(info[:index_bytes]).to eq(22)
30
+ expect(info[:data_block_bytes]).to eq(2332)
31
+ expect(info[:data_block_size]).to eq(8192)
32
+ expect(info[:data_block_count]).to eq(1)
33
+ expect(info[:entry_count]).to eq(676)
34
+ expect(info[:key_bytes]).to eq(1352)
35
+ expect(info[:value_bytes]).to eq(1920)
36
+ expect(info[:compression]).to eq("zlib")
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,101 @@
1
+ require 'mtbl'
2
+ require 'tempfile'
3
+
4
+ RSpec.describe MTBL::Writer, "#new" do
5
+
6
+ context "arguments" do
7
+ it "throws an exception on missing filename" do
8
+ expect { MTBL::Writer.new() }.to raise_error(ArgumentError)
9
+ end
10
+
11
+ it "does not throw an exception with a valid filename" do
12
+ tfile = Tempfile.new("mtbl-writer")
13
+ tpath = tfile.path
14
+ tfile.unlink
15
+ expect { MTBL::Writer.new(tpath) }.to_not raise_error
16
+ File.unlink(tpath)
17
+ end
18
+
19
+ it "throws an exception with existing file" do
20
+ tfile = Tempfile.new("mtbl-writer")
21
+ expect { MTBL::Writer.new(tfile.path) }.to raise_error(ArgumentError)
22
+ end
23
+
24
+ it "throws an exception with non-string arguments" do
25
+ expect { MTBL::Writer.new(9000) }.to raise_error(ArgumentError)
26
+ end
27
+
28
+ it "throws an exception with non-integer compression type" do
29
+ tfile = Tempfile.new("mtbl-writer")
30
+ tpath = tfile.path
31
+ tfile.unlink
32
+ expect { MTBL::Writer.new(tpath, "blagh") }.to raise_error(ArgumentError)
33
+ end
34
+
35
+ it "throws an exception with an invalid compression type" do
36
+ tfile = Tempfile.new("mtbl-writer")
37
+ tpath = tfile.path
38
+ tfile.unlink
39
+ expect { MTBL::Writer.new(tpath, 999) }.to raise_error(ArgumentError)
40
+ end
41
+
42
+ it "does not throw an exception with a valid compression type" do
43
+ tfile = Tempfile.new("mtbl-writer")
44
+ tpath = tfile.path
45
+ tfile.unlink
46
+ expect { MTBL::Writer.new(tpath, MTBL::COMPRESSION_ZLIB) }.to_not raise_error
47
+ File.unlink(tpath)
48
+ end
49
+
50
+ it "throws an exception with an non-integer block size" do
51
+ tfile = Tempfile.new("mtbl-writer")
52
+ tpath = tfile.path
53
+ tfile.unlink
54
+ expect { MTBL::Writer.new(tpath, MTBL::COMPRESSION_ZLIB, "blargh") }.to raise_error(ArgumentError)
55
+ end
56
+
57
+ it "does not throw an exception with an integer block size" do
58
+ tfile = Tempfile.new("mtbl-writer")
59
+ tpath = tfile.path
60
+ tfile.unlink
61
+ expect { MTBL::Writer.new(tpath, MTBL::COMPRESSION_ZLIB, 2048) }.to_not raise_error
62
+ File.unlink(tpath)
63
+ end
64
+
65
+ it "throws an exception with an non-integer restart interval" do
66
+ tfile = Tempfile.new("mtbl-writer")
67
+ tpath = tfile.path
68
+ tfile.unlink
69
+ expect { MTBL::Writer.new(tpath, MTBL::COMPRESSION_ZLIB, 4096, "blargh") }.to raise_error(ArgumentError)
70
+ end
71
+
72
+ end
73
+
74
+ context "records" do
75
+ tfile = Tempfile.new("mtbl-writer")
76
+ tpath = tfile.path
77
+ tfile.unlink
78
+ writer = MTBL::Writer.new(tpath)
79
+
80
+ it "handles invalid adds" do
81
+ expect { writer.add(:invalid, "test") }.to raise_error(ArgumentError)
82
+ expect { writer.add("test", :invalid) }.to raise_error(ArgumentError)
83
+ expect { writer.add("test", 9000) }.to raise_error(ArgumentError)
84
+ expect { writer.add(400, "blah") }.to raise_error(ArgumentError)
85
+ end
86
+
87
+ it "accepts sequential keys" do
88
+ expect { writer.add("1000", "test1") }.to_not raise_error
89
+ expect { writer.add("1005", "test2") }.to_not raise_error
90
+ expect { writer.add("1010", "test3") }.to_not raise_error
91
+ end
92
+
93
+ it "does not accept non-sequential keys" do
94
+ expect { writer.add("1001", "test1") }.to raise_error(RuntimeError)
95
+ expect { writer.add("1004", "test1") }.to raise_error(RuntimeError)
96
+ expect { writer.add("1009", "test1") }.to raise_error(RuntimeError)
97
+ end
98
+
99
+ File.unlink(tpath)
100
+ end
101
+ end
@@ -0,0 +1,96 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Allows RSpec to persist some state between runs in order to support
54
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
55
+ # you configure your source control system to ignore this file.
56
+ config.example_status_persistence_file_path = "spec/examples.txt"
57
+
58
+ # Limits the available syntax to the non-monkey patched syntax that is
59
+ # recommended. For more details, see:
60
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
61
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
63
+ config.disable_monkey_patching!
64
+
65
+ # This setting enables warnings. It's recommended, but in some cases may
66
+ # be too noisy due to issues in dependencies.
67
+ config.warnings = true
68
+
69
+ # Many RSpec users commonly either run the entire suite or an individual
70
+ # file, and it's useful to allow more verbose output when running an
71
+ # individual spec file.
72
+ if config.files_to_run.one?
73
+ # Use the documentation formatter for detailed output,
74
+ # unless a formatter has already been configured
75
+ # (e.g. via a command-line flag).
76
+ config.default_formatter = 'doc'
77
+ end
78
+
79
+ # Print the 10 slowest examples and example groups at the
80
+ # end of the spec run, to help surface which specs are running
81
+ # particularly slow.
82
+ config.profile_examples = 10
83
+
84
+ # Run specs in random order to surface order dependencies. If you find an
85
+ # order dependency and want to debug it, you can fix the order by providing
86
+ # the seed, which is printed after each run.
87
+ # --seed 1234
88
+ config.order = :random
89
+
90
+ # Seed global randomization in this process using the `--seed` CLI option.
91
+ # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # test failures related to randomization by passing the same `--seed` value
93
+ # as the one that triggered the failure.
94
+ Kernel.srand config.seed
95
+ =end
96
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-mtbl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - HD Moore
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The ruby-mtbl gem provides a Ruby interface to Farsight Security's MTBL
14
+ SortedString library.
15
+ email: x@hdm.io
16
+ executables:
17
+ - rmtbl_create
18
+ - rmtbl_dump
19
+ - rmtbl_info
20
+ extensions:
21
+ - ext/mtbl/extconf.rb
22
+ extra_rdoc_files: []
23
+ files:
24
+ - ".rspec"
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - bin/rmtbl_create
31
+ - bin/rmtbl_dump
32
+ - bin/rmtbl_info
33
+ - examples/test.rb
34
+ - examples/test1.mtbl
35
+ - ext/mtbl/extconf.rb
36
+ - ext/mtbl/ruby-mtbl.c
37
+ - ruby-mtbl.gemspec
38
+ - spec/mtbl_reader_spec.rb
39
+ - spec/mtbl_sorter_spec.rb
40
+ - spec/mtbl_spec.rb
41
+ - spec/mtbl_utils_spec.rb
42
+ - spec/mtbl_writer_spec.rb
43
+ - spec/spec_helper.rb
44
+ homepage: https://www.github.com/hdm/ruby-mtbl
45
+ licenses:
46
+ - Apache-2.0
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.5.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: 'ruby-mtbl: Ruby interface to the MTBL SortedString library'
68
+ test_files: []