Changeset 1751

Show
Ignore:
Timestamp:
08/20/08 11:40:10 (3 months ago)
Author:
dbryson
Message:

Added testing of bee controller with custom signed_post

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hive/trunk/data_webapp/test/fixtures/history_urls.yml

    r1297 r1751  
    11# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 
    2 first: 
     2 
     3# date in the future 
     4one: 
    35  id: 2 
    4   time_at: 1234.00 
    5   url: http://www.google.com 
     6  time_at: <%= string_to_microseconds(Time.now.tomorrow.to_s) %> 
     7  url: "http://www.future.com" 
    68  client_id: 2 
    7   status: ok 
     9  status: "visited" 
     10 
     11 
     12two: 
     13  id: 3 
     14  time_at: 12191111.01  
     15  url: "http://www.older.com" 
     16  client_id: 2 
     17  status: "visited" 
  • hive/trunk/data_webapp/test/functional/bee_controller_test.rb

    r1710 r1751  
    66 
    77class BeeControllerTest < Test::Unit::TestCase 
    8   fixtures :users, :heartbeat
     8  fixtures :history_url
    99   
    1010  def setup 
     
    1212    @request    = ActionController::TestRequest.new 
    1313    @response   = ActionController::TestResponse.new 
    14      
    15     # Raw data as if pulled from the BeeJob and cleaned 
    16     #@client_data = {"status"=>"running", "os"=>{"name"=>"Default Windows XP SP2", "shortname"=>"Microsoft Windows", "version"=>"XP Professional", "os_applications"=>[{"shortname"=>"Internet Explorer", "manufacturer"=>"Microsoft", "version"=>6}]}, "start"=>"2008-03-25T11:25:00", "host"=>{"org"=>"MITRE", "ip"=>"172.16.164.108", "hostname"=>"honeyclient8.mitre.org"}, "cid"=>"2df4455a3b0aa690ef4f7da84b"} 
    17     #@finger_data = {"os_processes"=>[{"name"=>"C:\\WINDOWS\\system32\\wbem\\wmiprvse.exe", "pid"=>1256, "process_files"=>[{"name"=>"C:\\WINDOWS\\PCHEALTH\\HELPCTR\\DataColl", "event"=>"Write", "file_content"=>{"size"=>-1, "mime_type"=>"UNKNOWN", "sha1"=>"C:\\WINDOWS\\PCHEALTH\\HELPCTR\\DataColl2008-03-25 11:58:05.13", "md5"=>"C:\\WINDOWS\\PCHEALTH\\HELPCTR\\DataColl2008-03-25 11:58:05.13"}, "time_at"=>"2008-03-25 11:58:05.13"}, {"name"=>"C:\\Documents and Settings", "event"=>"Write", "file_content"=>{"size"=>-1, "mime_type"=>"UNKNOWN", "sha1"=>"C:\\Documents and Settings2008-03-25 11:58:05.13", "md5"=>"C:\\Documents and Settings2008-03-25 11:58:05.13"}, "time_at"=>"2008-03-25 11:58:05.13"}, {"name"=>"C:\\Documents and Settings\\Default User", "event"=>"Write", "file_content"=>{"size"=>-1, "mime_type"=>"UNKNOWN", "sha1"=>"C:\\Documents and Settings\\Default User2008-03-25 11:58:05.13", "md5"=>"C:\\Documents and Settings\\Default User2008-03-25 11:58:05.13"}, "time_at"=>"2008-03-25 11:58:05.13"}, {"name"=>"C:\\Documents and Settings\\ALL USERS", "event"=>"Write", "file_content"=>{"size"=>-1, "mime_type"=>"UNKNOWN", "sha1"=>"C:\\Documents and Settings\\ALL USERS2008-03-25 11:58:05.13", "md5"=>"C:\\Documents and Settings\\ALL USERS2008-03-25 11:58:05.13"}, "time_at"=>"2008-03-25 11:58:05.13"}, {"name"=>"C:\\Documents and Settings\\Default User\\Start Menu\\Programs", "event"=>"Write", "file_content"=>{"size"=>-1, "mime_type"=>"UNKNOWN", "sha1"=>"C:\\Documents and Settings\\Default User\\Start Menu\\Programs2008-03-25 11:58:05.13", "md5"=>"C:\\Documents and Settings\\Default User\\Start Menu\\Programs2008-03-25 11:58:05.13"}, "time_at"=>"2008-03-25 11:58:05.13"}, {"name"=>"C:\\WINDOWS\\system32\\config\\systemprofile", "event"=>"Write", "file_content"=>{"size"=>-1, "mime_type"=>"UNKNOWN", "sha1"=>"C:\\WINDOWS\\system32\\config\\systemprofile2008-03-25 11:58:05.13", "md5"=>"C:\\WINDOWS\\system32\\config\\systemprofile2008-03-25 11:58:05.13"}, "time_at"=>"2008-03-25 11:58:05.13"}], "regkeys"=>[]}], "cid"=>"2df4455a3b0aa690ef4f7da84b"} 
    18     #@history_data = {"urls"=>[{"status"=>"visited", "url"=>"http://www.softcashier.com/members/link.php?wmid=1020&l=9&it=2&s=2", "time_at"=>1206468143.41987}], "cid"=>"2df4455a3b0aa690ef4f7da84b"} 
    1914  end 
    2015 
    2116  def test_jobs_from_bee 
    22     job_hash = { :source => { :feed_type => 'test',:name => 'drone_interface' }, 
    23                  :notify_source => 0, 
    24                  :queue_urls => { 'http://mitre.org/1' => 1, 'http://mitre.org/2' => 2, 'http://mitre.org/3' => 3 } 
    25     } 
     17    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1", :access_key => "1"} 
    2618     
    27     json = ActiveSupport::JSON.encode(job_hash) 
    28     post :add_drone_job, :data => json 
     19    data = ["http://one.com","http://two.com","http://three.com"] 
     20    json = ActiveSupport::JSON.encode(data) 
     21    signed_post(:work,json,"1","1") 
    2922    assert_response :success 
    3023 
     
    3427  end 
    3528   
     29  def test_latest_no_param 
     30    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1", :access_key => "1"} 
     31    signed_get(:latest,"1","1") 
     32    assert_response :success 
     33    
     34    decoded = ActiveSupport::JSON.decode(@response.body) 
     35    assert_equal 1, decoded.size 
     36  end 
     37   
     38  def test_latest_with_param 
     39    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1", :access_key => "1"} 
     40    signed_get(:latest,"1","1",{"id" => "12191111.01"}) 
     41    assert_response :success 
     42     
     43    decoded = ActiveSupport::JSON.decode(@response.body) 
     44    puts "DECODED: #{decoded.inspect}" 
     45    assert_equal 1, decoded.size 
     46  end 
     47   
     48  def test_no_latest_urls 
     49    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1", :access_key => "1"} 
     50    future = string_to_microseconds(Time.now.advance(:days => 3).to_s) 
     51    signed_get(:latest,"1","1",{"id" => future.to_s}) 
     52    assert_response :success 
     53     
     54    decoded = ActiveSupport::JSON.decode(@response.body) 
     55    assert_equal 0, decoded.size 
     56  end 
     57   
     58  def test_not_activated 
     59    WorkerBeeAuthorization.configuration = {:activate => false, :secret_key => "1", :access_key => "1"} 
     60    signed_post(:work,"hello","1","1",parameters=nil) 
     61    assert_response(401) 
     62  end 
     63   
     64  def test_authorized_post 
     65    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1", :access_key => "1"} 
     66    signed_post(:work,"hello","1","1",parameters=nil) 
     67    assert_response :success 
     68  end 
     69   
     70  def test_not_authorized_post 
     71    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1000", :access_key => "1"} 
     72    signed_post(:work,"hello","1","1",parameters=nil) 
     73    assert_response(401) 
     74  end 
     75   
     76  def test_authorized_get 
     77    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1", :access_key => "1"} 
     78    signed_get(:latest,"1","1") 
     79    assert_response :success 
     80  end 
     81 
     82  def test_not_authorized_get 
     83    WorkerBeeAuthorization.configuration = {:activate => true, :secret_key => "1000", :access_key => "1"} 
     84    signed_get(:latest,"1","1") 
     85    assert_response(401) 
     86  end 
     87  
    3688end 
  • hive/trunk/data_webapp/test/test_helper.rb

    r1210 r1751  
    2828  # Add more helper methods to be used by all tests here... 
    2929end 
     30 
     31require 'openssl' 
     32require 'base64' 
     33 
     34 
     35# Helper to simulate signed JSON requests 
     36ActionController::TestProcess.class_eval do 
     37   
     38  def sign_request(sk,method,path,date,content_type) 
     39    data = [method,path,date,content_type].join("\n") 
     40    #puts "Client signing: #{data}" 
     41    digest   = OpenSSL::Digest::Digest.new('sha1') 
     42    Base64.encode64(OpenSSL::HMAC.digest(digest, sk, data.to_s)) 
     43  end 
     44   
     45  def signed_post(action,body,ak,sk,parameters=nil) 
     46    date = Time.now.to_s 
     47    sign =  sign_request(sk,"POST","/bee/#{action.to_s}",date,"application/json") 
     48    @request.env['AUTHORIZATION'] = "HIVE:#{ak}:#{sign}" 
     49    @request.env['DATE'] = date 
     50    @request.env['CONTENT_TYPE'] = "application/json" 
     51    @request.env['ACCEPT'] = 'application/json' 
     52    @request.env['RAW_POST_DATA'] = body 
     53    returning send!(:post, action, parameters,nil,nil) do 
     54    end 
     55  end 
     56   
     57  def signed_get(action,ak,sk,parameters=nil) 
     58    date = Time.now.to_s 
     59    path = "/bee/#{action.to_s}" 
     60    path << "/#{parameters["id"]}" if parameters 
     61    sign =  sign_request(sk,"GET",path,date,"application/json") 
     62    @request.env['AUTHORIZATION'] = "HIVE:#{ak}:#{sign}" 
     63    @request.env['DATE'] = date 
     64    @request.env['CONTENT_TYPE'] = "application/json" 
     65    @request.env['ACCEPT'] = 'application/json' 
     66    returning send!(:get,action,parameters,nil,nil) do 
     67    end 
     68  end 
     69   
     70end