Add Chrome/Chromium cookie extractor. Same as the Firefox one, but with different table and field names. Will refactor them both to remove duplication.

master
Jeff Dallien 2012-02-21 19:22:56 -05:00
parent 628780e0db
commit 64d118cc85
2 changed files with 149 additions and 0 deletions

View File

@ -0,0 +1,43 @@
require 'sqlite3'
module CookieExtractor
class ChromeCookieExtractor
def initialize(cookie_file)
@cookie_file = cookie_file
end
def extract
db = SQLite3::Database.new @cookie_file
db.results_as_hash = true
@result = []
db.execute("SELECT * FROM cookies") do |row|
@result << [ row['host_key'],
true_false_word(is_domain_wide(row['host_key'])),
row['path'],
true_false_word(row['secure']),
row['expires_utc'],
row['name'],
row['value']
].join("\t")
end
@result
end
private
def is_domain_wide(hostname)
hostname[0..0] == "."
end
def true_false_word(value)
if value == "1" || value == 1 || value == true
"TRUE"
elsif value == "0" || value == 0 || value == false
"FALSE"
else
raise "Invalid value passed to true_false_word: #{value.inspect}"
end
end
end
end

View File

@ -0,0 +1,106 @@
require File.join(File.dirname(__FILE__), "spec_helper")
describe CookieExtractor::ChromeCookieExtractor do
before :each do
@fake_cookie_db = double("cookie database", :results_as_hash= => true)
SQLite3::Database.should_receive(:new).
with('filename').
and_return(@fake_cookie_db)
end
describe "with a cookie that has a host starting with a dot" do
before :each do
@fake_cookie_db.should_receive(:execute).and_yield(
{ 'host_key' => '.dallien.net',
'path' => '/',
'secure' => '0',
'expires_utc' => '1234567890',
'name' => 'NAME',
'value' => 'VALUE'})
@extractor = CookieExtractor::ChromeCookieExtractor.new('filename')
@result = @extractor.extract
end
it "should return one cookie string" do
@result.size.should == 1
end
it "should put TRUE in the domain wide field" do
cookie_string = @result.first
cookie_string.split("\t")[1].should == "TRUE"
end
it "should build the correct cookie string" do
cookie_string = @result.first
cookie_string.should ==
".dallien.net\tTRUE\t/\tFALSE\t1234567890\tNAME\tVALUE"
end
end
describe "with a cookie that has a host not starting with a dot" do
before :each do
@fake_cookie_db.should_receive(:execute).and_yield(
{ 'host_key' => 'jeff.dallien.net',
'path' => '/path',
'secure' => '1',
'expires_utc' => '1234567890',
'name' => 'NAME',
'value' => 'VALUE'})
@extractor = CookieExtractor::ChromeCookieExtractor.new('filename')
@result = @extractor.extract
end
it "should return one cookie string" do
@result.size.should == 1
end
it "should put FALSE in the domain wide field" do
cookie_string = @result.first
cookie_string.split("\t")[1].should == "FALSE"
end
it "should build the correct cookie string" do
cookie_string = @result.first
cookie_string.should ==
"jeff.dallien.net\tFALSE\t/path\tTRUE\t1234567890\tNAME\tVALUE"
end
end
describe "with a cookie that is not marked as secure" do
before :each do
@fake_cookie_db.should_receive(:execute).and_yield(
{ 'host_key' => '.dallien.net',
'path' => '/',
'secure' => '0',
'expires_utc' => '1234567890',
'name' => 'NAME',
'value' => 'VALUE'})
@extractor = CookieExtractor::ChromeCookieExtractor.new('filename')
@result = @extractor.extract
end
it "should put FALSE in the secure field" do
cookie_string = @result.first
cookie_string.split("\t")[3].should == "FALSE"
end
end
describe "with a cookie that is marked as secure" do
before :each do
@fake_cookie_db.should_receive(:execute).and_yield(
{ 'host_key' => '.dallien.net',
'path' => '/',
'secure' => '1',
'expires_utc' => '1234567890',
'name' => 'NAME',
'value' => 'VALUE'})
@extractor = CookieExtractor::ChromeCookieExtractor.new('filename')
@result = @extractor.extract
end
it "should put TRUE in the secure field" do
cookie_string = @result.first
cookie_string.split("\t")[3].should == "TRUE"
end
end
end