diff --git a/hackertray/firefox.py b/hackertray/firefox.py index 1f7b78c..187a29e 100644 --- a/hackertray/firefox.py +++ b/hackertray/firefox.py @@ -3,12 +3,31 @@ import sqlite3 import shutil import os import sys - +from pathlib import Path +import configparser class Firefox: HISTORY_TMP_LOCATION = '/tmp/hackertray.firefox' HISTORY_FILE_NAME = '/places.sqlite' + @staticmethod + def default_firefox_profile_path(): + profile_file_path = Path.home().joinpath(".mozilla/firefox/profiles.ini") + profile_path = None + if (os.path.exists(profile_file_path)): + parser = configparser.ConfigParser() + parser.read(profile_file_path) + for section in parser.sections(): + if parser[section]["Default"] == "1": + if parser[section]["IsRelative"] == "1": + profile_path = str(Path.home().joinpath(".mozilla/firefox/").joinpath(parser[section]["Path"])) + else: + profile_path = parser[section]["Path"] + if profile_path and Path.is_dir(Path(profile_path)): + return profile_path + else: + raise RuntimeError("Couldn't find default Firefox profile") + @staticmethod def search(urls, config_folder_path): Firefox.setup(config_folder_path) diff --git a/test/firefox_test.py b/test/firefox_test.py index 5c5d28a..faf504f 100644 --- a/test/firefox_test.py +++ b/test/firefox_test.py @@ -1,10 +1,12 @@ import unittest import os +import pathlib +from pathlib import Path from hackertray import Firefox -class ChromeTest(unittest.TestCase): - def runTest(self): +class FirefoxTest(unittest.TestCase): + def test_history(self): config_folder_path = os.getcwd()+'/test/' data = Firefox.search([ "http://www.hckrnews.com/", @@ -12,4 +14,19 @@ class ChromeTest(unittest.TestCase): "http://wiki.ubuntu.com/", "http://invalid_url/"], config_folder_path) - self.assertTrue(data == [True,True,True,False]) \ No newline at end of file + self.assertTrue(data == [True,True,True,False]) + + def test_default(self): + test_default_path = Path.home().joinpath(".mozilla/firefox/x0ran0o9.default") + if(os.environ.get('TRAVIS') == 'true'): + if not os.path.exists(test_default_path): + os.makedirs(test_default_path) + with open(str(Path.home().joinpath('.mozilla/firefox/profiles.ini')), 'w') as f: + f.write(""" +[Profile1] +Name=default +IsRelative=1 +Path=x0ran0o9.default +Default=1 + """) + self.assertTrue(Firefox.default_firefox_profile_path()==Path.home().joinpath(".mozilla/firefox/x0ran0o9.default"))