Detector browser that created the supplied cookie db by table names present.
This commit is contained in:
parent
2856bd60fe
commit
f764e35095
4 changed files with 76 additions and 2 deletions
|
@ -2,10 +2,11 @@
|
|||
|
||||
require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "cookie_extractor"))
|
||||
|
||||
# TODO: detect firefox or chrome input file and/or locate it automatically
|
||||
# TODO: Locate cookie dbs automatically
|
||||
filename = ARGV.first
|
||||
if filename
|
||||
puts CookieExtractor::FirefoxCookieExtractor.new(filename).extract.join("\n")
|
||||
extractor = CookieExtractor::BrowserDetector.new_extractor(filename)
|
||||
puts extractor.extract.join("\n")
|
||||
else
|
||||
puts "Usage: cookie_extractor /path/to/cookies.sqlite"
|
||||
end
|
||||
|
|
|
@ -2,6 +2,7 @@ require "cookie_extractor/version"
|
|||
require "cookie_extractor/common"
|
||||
require "cookie_extractor/firefox_cookie_extractor"
|
||||
require "cookie_extractor/chrome_cookie_extractor"
|
||||
require "cookie_extractor/browser_detector"
|
||||
|
||||
module CookieExtractor
|
||||
end
|
||||
|
|
29
lib/cookie_extractor/browser_detector.rb
Normal file
29
lib/cookie_extractor/browser_detector.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
module CookieExtractor
|
||||
class BrowserDetector
|
||||
|
||||
def self.new_extractor(db_filename)
|
||||
browser = detect_browser(db_filename)
|
||||
if browser
|
||||
CookieExtractor.const_get("#{browser}CookieExtractor").new(db_filename)
|
||||
else
|
||||
raise "Could not detect browser type."
|
||||
end
|
||||
end
|
||||
|
||||
def self.detect_browser(db_filename)
|
||||
db = SQLite3::Database.new(db_filename)
|
||||
browser =
|
||||
if has_table?(db, 'moz_cookies')
|
||||
'Firefox'
|
||||
elsif has_table?(db, 'cookies')
|
||||
'Chrome'
|
||||
end
|
||||
db.close
|
||||
browser
|
||||
end
|
||||
|
||||
def self.has_table?(db, table_name)
|
||||
db.table_info(table_name).size > 0
|
||||
end
|
||||
end
|
||||
end
|
43
spec/browser_detector_spec.rb
Normal file
43
spec/browser_detector_spec.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
require File.join(File.dirname(__FILE__), "spec_helper")
|
||||
|
||||
describe CookieExtractor::BrowserDetector, "determining the correct extractor to use" do
|
||||
before :each do
|
||||
@fake_cookie_db = double("cookie database", :close => true)
|
||||
SQLite3::Database.should_receive(:new).
|
||||
with('filename').
|
||||
and_return(@fake_cookie_db)
|
||||
end
|
||||
|
||||
describe "given a sqlite database with a 'moz_cookies' table" do
|
||||
before :each do
|
||||
@fake_cookie_db.should_receive(:table_info).
|
||||
with("moz_cookies").
|
||||
and_return(
|
||||
{ 'name' => 'some_field',
|
||||
'type' => "some_type" })
|
||||
end
|
||||
|
||||
it "should return a firefox extractor instance" do
|
||||
extractor = CookieExtractor::BrowserDetector.new_extractor('filename')
|
||||
extractor.instance_of?(CookieExtractor::FirefoxCookieExtractor).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "given a sqlite database with a 'cookies' table" do
|
||||
before :each do
|
||||
@fake_cookie_db.should_receive(:table_info).
|
||||
with("moz_cookies").
|
||||
and_return([])
|
||||
@fake_cookie_db.should_receive(:table_info).
|
||||
with("cookies").
|
||||
and_return(
|
||||
[{ 'name' => 'some_field',
|
||||
'type' => "some_type" }])
|
||||
end
|
||||
|
||||
it "should return a chrome extractor instance" do
|
||||
extractor = CookieExtractor::BrowserDetector.new_extractor('filename')
|
||||
extractor.instance_of?(CookieExtractor::ChromeCookieExtractor).should be_true
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue