summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2012-12-24 02:02:24 -0800
committerelijah <elijah@riseup.net>2012-12-24 02:02:24 -0800
commit457c2bf135be68b3c49ff20d3f23a6f8507aeda5 (patch)
tree73afc8568c6a83706111b19d35ba04d0cd94b2e0 /test
initial commit
Diffstat (limited to 'test')
-rw-r--r--test/files/hkp_vindex_result21
-rw-r--r--test/files/leap_public_key46
-rw-r--r--test/files/leap_vindex_result4
-rw-r--r--test/test_helper.rb75
-rw-r--r--test/unit/hkp_test.rb120
-rw-r--r--test/unit/server_test.rb34
-rw-r--r--test/unit/test_helper.rb1
7 files changed, 301 insertions, 0 deletions
diff --git a/test/files/hkp_vindex_result b/test/files/hkp_vindex_result
new file mode 100644
index 0000000..677c242
--- /dev/null
+++ b/test/files/hkp_vindex_result
@@ -0,0 +1,21 @@
+info:1:7
+pub:0EE5BE979282D80B9F7540F1CCD2ED94D21739E9:1:4096:1180812858:1424970931:
+uid:Lemur <lemur@leap.se>:1330362947::
+uid:Lemur <lemur@example.org>:1330362946::
+uat::::
+pub:B59941F741EAEAF8726F45D88974E514A54B6365:1:2048:944761959:1180812858:
+uid:Lemur <lemur@leap.se>:1032066007::
+uid:Lemur <lemur@example.rog>:1165682982::
+uat::::
+pub:3790027A:1:4096:1354554342::
+uid:Gazelle <gazelle@leap.se>:1354554342::
+pub:997FA77F:1:4096:1243472517::
+uid:Gazelle <gazelle@leap.se>:1304892367::
+uid:Gazelle <gazelle@example.org>:1354352902::
+pub:3A893F69:1:4096:1304037659::
+uid:Frog <frog@example.org>:1304037659::
+pub:00440025:1:4096:1243467172::
+uid:Frog <frog@leap.se>:1243467172::
+pub:2518C3B5:17:1024:1089226421::
+uid:Frog <frog@leap.se>:1089226421::
+
diff --git a/test/files/leap_public_key b/test/files/leap_public_key
new file mode 100644
index 0000000..d58631c
--- /dev/null
+++ b/test/files/leap_public_key
@@ -0,0 +1,46 @@
+-----BEGIN PGP PUBL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VK8z9E
+GI5K8JDyhA29/4baCpPEgWgnGTwBOYTk3g6QsX2eZqq+SodYNMARORmw3B+vOFH2assFmbXh
+qVoEtIHLKZ/S8lPe2Tlg1WxNUPpft2KbsElpiW8S9mhQQpY9o0keLLwV7hoyU8CdPXk9jdNL
+32oWEnnrR7pmG+Tw6VzbPjlRxHLa2AppI4YTdvpqdrI8mqKnXb6rsogWoR8AEQEAAYkCJQQY
+AQgADwUCUIJaqQIbDAUJAeEzgAAKCRDQQesRsWR0kEJPD/9g8Qp5Rz0c16PxER7kNsK+Ej6u
+2AXo+TOtc3caZ3nbGWUfm0o3OrhAQIRvoFHyAQjcaXnRh/v2GRbZ1L3ZVIwjeQXN/iGSve87
+h/Im2UHZIXj92N2xU0zIPNqPrVUeMJ63ubOnUL+4syzxkqJFKUcNIUFwAY4caaT04AYw6dRW
+Fc5ns7CGwqxUoxNUIIwUsHw0ntzukxsABOMqchinDWWliduokDZUY209VYu1XCnYY7tLFmCu
+K2pdyo5ax7E9DoaAxbfLTLDYrLwlli8G0pI0H1sOUkeqeWKQcezGow7KEjKdSVXsUcNDqZLB
+Ap1fS0tMLJokZOebL+yDTSsQfS6+MGMo9wv/QYQ/D/dV6vjZxR8T6g5ZkU5JD6EA3wep2HbB
+h6G9GdVNE0K/3gBm9XeJ4blwWbx6n9TaevixV0WBe2iCxDFgVPiqG0Sc+f1+3ejcaRDcgDjI
+c38O1z9Z0WxjSTh1Qben+D5zWHZvw/jRIokBltVS2yQqweZJffb4JCI2zuOpRo73YqRlKc45
+Kg3tIzmnlSwJLs5XjRuXhG+SZ/qydwWQ8KBL1FPUIb6oKDfkChga9WIrawVa2SNU6IngSBw2
+m+Jfd21IVScX2mzLzATe1GMDs9RY7GoxD0cznbv2HXzFvdOSI8ukWatlw3S4AICetp44dEJi
+eFpdYglTIA==
+=wcWV
+-----END PGP PUBLIC KEY BLOCK----- \ No newline at end of file
diff --git a/test/files/leap_vindex_result b/test/files/leap_vindex_result
new file mode 100644
index 0000000..cdb4e46
--- /dev/null
+++ b/test/files/leap_vindex_result
@@ -0,0 +1,4 @@
+info:1:1
+pub:E818C478D3141282F7590D29D041EB11B1647490:1:4096:1350720169:1382256169:
+uid:Cloudadmin at LEAP.se (Collective key for infrastructure at LEAP.se) <cloudadmin@leap.se>:1350720169::
+
diff --git a/test/test_helper.rb b/test/test_helper.rb
new file mode 100644
index 0000000..031a90f
--- /dev/null
+++ b/test/test_helper.rb
@@ -0,0 +1,75 @@
+$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
+
+require 'rubygems'
+require 'minitest/autorun'
+require 'webmock/minitest'
+require 'nickserver'
+
+class MiniTest::Unit::TestCase
+ # Add global extensions to the test case class here
+
+ def setup
+ # by default, mock all non-localhost network connections
+ WebMock.disable_net_connect!(:allow_localhost => true)
+ end
+
+ def file_content(filename)
+ (@file_contents ||= {})[filename] ||= File.read("%s/files/%s" % [File.dirname(__FILE__), filename])
+ end
+
+ def real_network
+ if ENV['REAL_NET'] == 'true'
+ WebMock.allow_net_connect!
+ yield
+ WebMock.disable_net_connect!
+ end
+ end
+
+ def stub_vindex_response(uid, opts = {})
+ options = {:status => 200, :body => ""}.merge(opts)
+ stub_http_request(:get, Nickserver::Config.sks_url).with(
+ :query => {:op => 'vindex', :search => uid, :exact => 'on', :options => 'mr', :fingerprint => 'on'}
+ ).to_return(options)
+ end
+
+ def stub_get_response(key_id, opts = {})
+ options = {:status => 200, :body => ""}.merge(opts)
+ stub_http_request(:get, Nickserver::Config.sks_url).with(
+ :query => {:op => 'get', :search => "0x"+key_id, :exact => 'on', :options => 'mr'}
+ ).to_return(options)
+ end
+
+ #def without_webmock
+ # WebMock.disable_net_connect!(:allow_localhost => true)
+ # yield
+ # WebMock.disable_net_connect!
+ #end
+end
+
+#
+# a simple EM connection with callbacks for various lifecycle stages,
+# useful for testing
+#
+class TestSocketClient < EventMachine::Connection
+ attr_writer :onopen, :onclose, :onmessage
+ attr_reader :data
+
+ def initialize
+ @state = :new
+ @data = []
+ end
+
+ def receive_data(data)
+ @data << data
+ if @state == :new
+ @onopen.call if @onopen
+ @state = :open
+ else
+ @onmessage.call(data) if @onmessage
+ end
+ end
+
+ def unbind
+ @onclose.call if @onclose
+ end
+end
diff --git a/test/unit/hkp_test.rb b/test/unit/hkp_test.rb
new file mode 100644
index 0000000..c9c28cf
--- /dev/null
+++ b/test/unit/hkp_test.rb
@@ -0,0 +1,120 @@
+require File.expand_path('test_helper', File.dirname(__FILE__))
+
+class HkpTest < MiniTest::Unit::TestCase
+
+ def test_key_info_expired
+ fetch_key_info(:hkp_vindex_result, 'lemur@leap.se') do |keys|
+ assert_equal 1, keys.length, 'should find a single key'
+ assert_equal ['lemur@example.org', 'lemur@leap.se'].sort, keys.first.uids.sort, 'should find both uids'
+ assert_equal '0EE5BE979282D80B9F7540F1CCD2ED94D21739E9', keys.first.keyid
+ end
+ end
+
+ def test_key_info_multiple_valid_results
+ fetch_key_info :hkp_vindex_result, 'gazelle@leap.se' do |keys|
+ assert_equal 2, keys.length, 'should find two keys'
+ assert_equal ['gazelle@leap.se'], keys.first.uids
+ assert_equal '3790027A', keys.first.keyid
+ assert keys.last.uids.include? 'gazelle@leap.se'
+ end
+ end
+
+ def test_key_info_reject_keysize
+ fetch_key_info :hkp_vindex_result, 'frog@leap.se' do |keys|
+ assert_equal 1, keys.length, 'should find one key'
+ assert_equal '00440025', keys.first.keyid
+ end
+ end
+
+ def test_key_info_not_found
+ uid = 'leaping_lemur@leap.se'
+ stub_vindex_response(uid, :status => 404)
+ test_em_errback "Nickserver::HKP::FetchKeyInfo.new.search '#{uid}'" do |error|
+ assert_equal 404, error
+ end
+ end
+
+ def test_key_info_real_network
+ real_network do
+ uid = 'elijah@riseup.net'
+ test_em_callback "Nickserver::HKP::FetchKeyInfo.new.search '#{uid}'" do |keys|
+ assert_equal 1, keys.size
+ assert keys.first.keyid =~ /00440025$/
+ end
+ end
+ end
+
+ def test_fetch_key
+ uid = 'cloudadmin@leap.se'
+ key_id = 'E818C478D3141282F7590D29D041EB11B1647490'
+ stub_vindex_response(uid, :body => file_content(:leap_vindex_result))
+ stub_get_response(key_id, :body => file_content(:leap_public_key))
+
+ test_em_callback "Nickserver::HKP::FetchKey.new.get '#{uid}'" do |key_text|
+ assert_equal file_content(:leap_public_key), key_text
+ end
+ end
+
+ def test_fetch_key_not_found
+ uid = 'cloudadmin@leap.se'
+ key_id = 'E818C478D3141282F7590D29D041EB11B1647490'
+
+ stub_vindex_response(uid, :body => file_content(:leap_vindex_result))
+ stub_get_response(key_id, :status => 404)
+
+ test_em_errback "Nickserver::HKP::FetchKey.new.get '#{uid}'" do |error|
+ assert_equal 404, error
+ end
+ end
+
+ protected
+
+ #
+ # Takes a code snippet that returns a Deferrable, and yields the callback result.
+ # Assertion fails if errback is called instead of callback.
+ #
+ # This method takes care of the calls to EM.run and EM.stop. It works kind of like EM.run_block,
+ # except I couldn't get run_block to work with multiple nested HTTP requests.
+ #
+ def test_em_callback(code, &block)
+ EM.run do
+ deferrable = instance_eval(code)
+ deferrable.callback {|response|
+ EM.stop
+ yield response
+ return
+ }
+ deferrable.errback {|response|
+ EM.stop
+ flunk "Expecting callback, but errback invoked with response: #{response}"
+ }
+ end
+ assert false, 'should not get here'
+ end
+
+ #
+ # like test_em_callback, except value yielded is the result of errback, and
+ # we raise an exception if errback was not called.
+ #
+ def test_em_errback(code, &block)
+ EM.run do
+ deferrable = instance_eval(code)
+ deferrable.callback {|response|
+ EM.stop
+ flunk "Expecting errback, but callback invoked with response: #{response}"
+ }
+ deferrable.errback {|response|
+ EM.stop
+ yield response
+ return
+ }
+ end
+ assert false, 'should not get here'
+ end
+
+ def fetch_key_info(body_source, uid, &block)
+ stub_vindex_response(uid, :body => file_content(body_source))
+ test_em_callback "Nickserver::HKP::FetchKeyInfo.new.search '#{uid}'", &block
+ end
+
+end
diff --git a/test/unit/server_test.rb b/test/unit/server_test.rb
new file mode 100644
index 0000000..ba462d0
--- /dev/null
+++ b/test/unit/server_test.rb
@@ -0,0 +1,34 @@
+require File.expand_path('test_helper', File.dirname(__FILE__))
+
+class ServerTest < MiniTest::Unit::TestCase
+
+ def test_server
+ uid = 'cloudadmin@leap.se'
+ key_id = 'E818C478D3141282F7590D29D041EB11B1647490'
+ stub_vindex_response(uid, :body => file_content(:leap_vindex_result))
+ stub_get_response(key_id, :body => file_content(:leap_public_key))
+
+ EM.run do
+ EM.start_server '0.0.0.0', Nickserver::Config.port, Nickserver::Server
+
+ params = {:query => {}, :path => "key/#{CGI.escape(uid)}"}
+ http = EventMachine::HttpRequest.new("http://localhost:#{Nickserver::Config.port}").get(params)
+ http.callback {
+ assert_equal file_content(:leap_public_key), http.response
+ EM.stop
+ }.errback {
+ puts http.error
+ EM.stop
+ }
+
+ #socket = EM.connect('0.0.0.0', Nickserver::Config.port, TestSocketClient)
+ #socket.onopen = lambda {
+ # server.players.size.should == 1
+ # socket.data.last.chomp.should == "READY"
+ # EM.stop
+ #}
+ end
+ end
+
+end
+
diff --git a/test/unit/test_helper.rb b/test/unit/test_helper.rb
new file mode 100644
index 0000000..ade21a0
--- /dev/null
+++ b/test/unit/test_helper.rb
@@ -0,0 +1 @@
+require File.dirname(File.dirname(__FILE__)) + '/test_helper' \ No newline at end of file