From e9230a00c1627d84b79b52972c94efe137188a6f Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Tue, 16 Apr 2013 00:03:03 +0900 Subject: [PATCH] Add tests for MozillaStore#upgrade_database and fix its bugs. --- lib/http/cookie_jar/mozilla_store.rb | 16 +++++----- test/test_http_cookie_jar.rb | 46 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/lib/http/cookie_jar/mozilla_store.rb b/lib/http/cookie_jar/mozilla_store.rb index 7b6e325..3b577ef 100644 --- a/lib/http/cookie_jar/mozilla_store.rb +++ b/lib/http/cookie_jar/mozilla_store.rb @@ -141,9 +141,9 @@ class HTTP::CookieJar st_update = @db.prepare("UPDATE moz_cookies SET baseDomain = :baseDomain WHERE id = :id") @db.execute("SELECT id, host FROM moz_cookies") { |row| - domain_name = DomainName.new(row[:host]) + domain_name = DomainName.new(row['host'][/\A\.?(.*)/, 1]) domain = domain_name.domain || domain_name.hostname - st_update.execute(:baseDomain => domain, :id => row[:id]) + st_update.execute(:baseDomain => domain, :id => row['id']) } @db.execute("CREATE INDEX moz_basedomain ON moz_cookies (baseDomain)") @@ -151,12 +151,12 @@ class HTTP::CookieJar when 3 st_delete = @db.prepare("DELETE FROM moz_cookies WHERE id = :id") - prev_row = nil + prev_row = nil @db.execute(<<-'SQL') { |row| SELECT id, name, host, path FROM moz_cookies ORDER BY name ASC, host ASC, path ASC, expiry ASC SQL - if %w[name host path].all? { |col| row[col] == prev_row[col] } + if %w[name host path].all? { |col| prev_row and row[col] == prev_row[col] } st_delete.execute(prev_row['id']) end prev_row = row @@ -289,8 +289,8 @@ class HTTP::CookieJar attrs[:domain] = row['host'] attrs[:path] = row['path'] attrs[:expires_at] = Time.at(row['expiry']) - attrs[:accessed_at] = Time.at(row['lastAccessed']) - attrs[:created_at] = Time.at(row['creationTime']) + attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0) + attrs[:created_at] = Time.at(row['creationTime'] || 0) attrs[:secure] = secure attrs[:httponly] = row['isHttpOnly'] != 0 }) @@ -325,8 +325,8 @@ class HTTP::CookieJar attrs[:domain] = row['host'] attrs[:path] = row['path'] attrs[:expires_at] = Time.at(row['expiry']) - attrs[:accessed_at] = Time.at(row['lastAccessed']) - attrs[:created_at] = Time.at(row['creationTime']) + attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0) + attrs[:created_at] = Time.at(row['creationTime'] || 0) attrs[:secure] = row['isSecure'] != 0 attrs[:httponly] = row['isHttpOnly'] != 0 }) diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index 34ab832..4846929 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -781,6 +781,52 @@ module TestHTTPCookieJar { :store => :mozilla, :filename => ":memory:" }, { :store => :mozilla, :filename => ":memory:" }) end + + def test_upgrade_mozillastore + Dir.mktmpdir { |dir| + filename = File.join(dir, 'cookies.sqlite') + + sqlite = SQLite3::Database.new(filename) + sqlite.execute(<<-'SQL') + CREATE TABLE moz_cookies ( + id INTEGER PRIMARY KEY, + name TEXT, + value TEXT, + host TEXT, + path TEXT, + expiry INTEGER, + isSecure INTEGER, + isHttpOnly INTEGER) + SQL + sqlite.execute(<<-'SQL') + PRAGMA user_version = 1 + SQL + + begin + st_insert = sqlite.prepare(<<-'SQL') + INSERT INTO moz_cookies ( + id, name, value, host, path, expiry, isSecure, isHttpOnly + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) + SQL + + st_insert.execute(1, 'name1', 'value1', '.example.co.jp', '/', 2312085765, 0, 0) + st_insert.execute(2, 'name1', 'value2', '.example.co.jp', '/', 2312085765, 0, 0) + st_insert.execute(3, 'name1', 'value3', 'www.example.co.jp', '/', 2312085765, 0, 0) + ensure + st_insert.close if st_insert + end + + sqlite.close + jar = HTTP::CookieJar.new(:store => :mozilla, :filename => filename) + + assert_equal 2, jar.to_a.size + assert_equal 2, jar.cookies('http://www.example.co.jp/').size + + cookie, *rest = jar.cookies('http://host.example.co.jp/') + assert_send [rest, :empty?] + assert_equal 'value2', cookie.value + } + end end if begin require 'sqlite3' true