def test_chrome_two_proflies_one_invalid(tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') browser_name = 'chrome' table_name = 'urls' profile_names = ['Profile 1 JUNK', 'Profile 2'] file_name = 'History' expected_browser_paths = {'Profile 2': Path(profile_rootpath, 'Profile 2')} actual_browser_paths = make_browser_paths( browser=browser_name, profile_root=profile_rootpath, profiles=profile_names, ) urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=profile_names, ) actual_records = list(urls_records_yielder) assert expected_browser_paths == actual_browser_paths assert len(actual_records) == 9 assert [record['id'] for record in actual_records] == list(range(1, 10)) assert actual_records[5]['id'] == 6 assert actual_records[5]['url'] == 'https://about.gitlab.com/' assert actual_records[-1]['id'] == 9 assert actual_records[-1][ 'url'] == 'https://www.howtogeek.com/255653/how-to-find-your-chrome-profile-folder-on-windows-mac-and-linux/'
def test_browser_chrome_access_filtered_fields(tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') browser_name = 'chrome' table_name = 'urls' profile_name = 'Profile 1' file_name = 'History' fields = [ 'id', 'url', 'title', 'last_visit_time', 'last_visit_readable', ] urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], fieldnames=fields, ) records = [record for record in urls_records_yielder] records_fieldnames = set(tuple(record_.keys()) for record_ in records) records_fieldnames = records_fieldnames.pop() additional_fields = ('browser', 'profile', 'file', 'table') expected_fieldnames = [*fields, *additional_fields] assert not set(records_fieldnames).difference(expected_fieldnames)
def test_browser_access_filtered_fields(tests_root): profile_rootpath = Path(tests_root, 'firefox_databases') browser_name = 'firefox' table_name = 'moz_places' profile_name = 'test_profile1' file_name = 'places.sqlite' fields = [ 'id', 'url', 'title', 'last_visit_date', 'last_visit_readable', ] moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], fieldnames=fields, ) records = [record for record in moz_places_records_yielder] records_fieldnames = set(tuple(record_.keys()) for record_ in records) records_fieldnames = records_fieldnames.pop() additional_fields = ('browser', 'profile', 'file', 'table') expected_fieldnames = [*fields, *additional_fields] assert not set(records_fieldnames).difference(expected_fieldnames)
def test_firefox_two_proflies_one_invalid(tests_root): profile_rootpath = Path(tests_root, 'firefox_databases') browser_name = 'firefox' table_name = 'moz_places' profile_names = ['test_profile1_JUNK', 'test_profile2'] file_name = 'places.sqlite' expected_browser_paths = { 'test_profile2': Path(profile_rootpath, 'z786c76dv78.test_profile2') } actual_browser_paths = make_browser_paths( browser=browser_name, profile_root=profile_rootpath, profiles=profile_names, ) moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=profile_names, ) actual_records = list(moz_places_records_yielder) assert expected_browser_paths == actual_browser_paths assert len(actual_records) == 20 assert [record['id'] for record in actual_records] == list(range(1, 21)) assert actual_records[0]['url'] == 'http://www.linuxmint.com/start/tessa' assert actual_records[-1]['id'] == 20 assert actual_records[-1]['url'] == 'https://www.youtube.com/'
def make_records_yielders( browsers_data: BrowserData, app_path: PathInfo, ) -> List[Generator]: """ Creates a list of generators, each yielding the records for each discovered browser. :param browsers_data: NamedTuple of discovered browsers, from find_installed_browsers(). :param app_path: Path to dir where all app info is stored :return: List of generators yielding records for each browser. """ browser_yielder = [] for browser_datum in browsers_data: file, _ = tuple(browser_datum.file_tables.items())[0] table_name, fields_list = tuple(browser_datum.table_fields.items())[0] each_browser_records_yielder = make_browser_records_yielder( browser=browser_datum.browser, profile_root=browser_datum.path, filename=file, tablename=table_name, profiles=browser_datum.profiles, fieldnames=fields_list, copies_subpath=app_path, ) browser_yielder.append(each_browser_records_yielder) return browser_yielder
def test_browser_access_two_profiles_same_file_table(tests_root): profile_rootpath = Path(tests_root, 'firefox_databases') browser_name = 'firefox' profiles = ['test_profile1', 'test_profile2'] file_name = 'places.sqlite' table_name = 'moz_places' moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=profiles, ) profile_1_2_records_using_browser = [ record for record in moz_places_records_yielder ] sort_by_url = lambda item: item['url'] profile_1_2_records_using_browser.sort(key=sort_by_url) profile1_table = Table( table='moz_places', path=Path( profile_rootpath, 't87e6f86.test_profile1', 'places.sqlite', ), browser='mozilla', filename='places.sqlite', profile='test_profile1', ) profile1_table.make_records_yielder() profile1_records = list(profile1_table.records_yielder) profile2_table = Table( table='moz_places', path=Path( profile_rootpath, 'z786c76dv78.test_profile2', 'places.sqlite', ), browser='mozilla', filename='places.sqlite', profile='test_profile2', ) profile2_table.make_records_yielder() profile2_records = list(profile2_table.records_yielder) profile_1_2_records_using_tables = [*profile1_records, *profile2_records] profile_1_2_records_using_tables.sort(key=sort_by_url) table_records_urls = [ record['url'] for record in profile_1_2_records_using_tables ] browser_records_urls = [ record['url'] for record in profile_1_2_records_using_browser ] assert table_records_urls == browser_records_urls
def test_browser_access_two_profiles_same_file_table(tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') browser_name = 'chrome' table_name = 'urls' file_name = 'History' urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=['Profile 1', 'Profile 2'], ) profile_1_2_records_using_browser = [ record for record in urls_records_yielder ] sort_by_url = lambda item: item['url'] profile_1_2_records_using_browser.sort(key=sort_by_url) profile1_table = Table( table=table_name, path=Path( profile_rootpath, 'Profile 1', file_name, ), browser=browser_name, filename=file_name, profile='Profile 1', ) profile1_table.make_records_yielder() profile1_records = list(profile1_table.records_yielder) profile2_table = Table( table=table_name, path=Path( profile_rootpath, 'Profile 2', 'History', ), browser=browser_name, filename=file_name, profile='Profile 2', ) profile2_table.make_records_yielder() profile2_records = list(profile2_table.records_yielder) profile_1_2_records_using_tables = [*profile1_records, *profile2_records] profile_1_2_records_using_tables.sort(key=sort_by_url) table_records_urls = [ record['url'] for record in profile_1_2_records_using_tables ] browser_records_urls = [ record['url'] for record in profile_1_2_records_using_browser ] assert table_records_urls == browser_records_urls
def test_browser_access_single_profile_file_table_with_timestamp(tests_root): profile_rootpath = Path(tests_root, 'firefox_databases') profile_path = Path( profile_rootpath, 't87e6f86.test_profile1', 'places.sqlite', ) browser_name = 'firefox' table_name = 'moz_places' profile_name = 'test_profile1' file_name = 'places.sqlite' moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], ) browser_profile1_records = [ record for record in moz_places_records_yielder ] sort_by_id = lambda item: item['url'] browser_profile1_records.sort(key=sort_by_id) profile1_table = Table( table=table_name, path=profile_path, browser=browser_name, filename=file_name, profile=profile_name, ) profile1_table.make_records_yielder() table_profile1_records = list(profile1_table.records_yielder) table_profile1_records.sort(key=sort_by_id) table_profile1_records_urls = [ record['url'] for record in table_profile1_records ] browser_profile1_records_urls = [ record['url'] for record in browser_profile1_records ] assert table_profile1_records_urls == browser_profile1_records_urls assert len(table_profile1_records) == len(browser_profile1_records) assert check_records_unique_with_field(records=table_profile1_records, field='id') assert check_records_unique_with_field(records=browser_profile1_records, field='id') assert sorted(table_profile1_records_urls) == sorted( browser_profile1_records_urls)
def test_firefox_invalid_file(tests_root): profile_rootpath = Path(tests_root, 'firefox_databases') browser_name = 'firefox' table_name = 'moz_places' profile_name = ['test_profile1'] file_name = 'places.sqlite_JUNK' moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=profile_name, ) with pytest.raises(sqlite3.OperationalError): list(moz_places_records_yielder)
def test_firefox_invalid_profileroot(tests_root): profile_rootpath = Path(tests_root, 'firefox_databases_JUNK') browser_name = 'firefox' table_name = 'moz_places' profile_name = 'test_profile1' file_name = 'places.sqlite_JUNK' moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], ) with pytest.raises(FileNotFoundError): list(moz_places_records_yielder)
def test_chrome_invalid_file(tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') browser_name = 'chrome' table_name = 'urls' profile_name = 'Profile 1' file_name = 'History JUNK' urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], ) with pytest.raises(sqlite3.OperationalError): list(urls_records_yielder)
def test_chrome_invalid_table(tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') browser_name = 'chrome' table_name = 'urls_JUNK' profile_name = 'Profile 1' file_name = 'History' urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], ) with pytest.raises(TableAccessError): print(list(urls_records_yielder))
def test_chrome_invalid_profileroot(tests_root): profile_rootpath = Path(tests_root, 'chrome_databases_JUNK') browser_name = 'chrome' table_name = 'urls' profile_name = 'Profile 1' file_name = 'History' moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], ) with pytest.raises(FileNotFoundError): list(moz_places_records_yielder)
def test_browser_access_single_profile_file_table_without_timestamp( tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') profile_path = Path( profile_rootpath, 'Profile 1', 'History', ) browser_name = 'chrome' table_name = 'keyword_search_terms' profile_name = 'Profile 1' file_name = 'History' urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], ) browser_profile1_records = [record for record in urls_records_yielder] sort_by_id = lambda item: item['url_id'] browser_profile1_records.sort(key=sort_by_id) profile1_table = Table( table=table_name, path=profile_path, browser=browser_name, filename=file_name, profile=profile_name, ) profile1_table.make_records_yielder() table_profile1_records = list(profile1_table.records_yielder) table_profile1_records.sort(key=sort_by_id) table_profile1_records_ids = [ record['url_id'] for record in table_profile1_records ] browser_profile1_records_ids = [ record['url_id'] for record in browser_profile1_records ] assert table_profile1_records_ids == browser_profile1_records_ids
def test_chrome_invalid_proflie(tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') browser_name = 'chrome' table_name = 'urls' profile_name = ['Profile 1 JUNK'] file_name = 'History' actual_browser_paths = make_browser_paths( browser=browser_name, profile_root=profile_rootpath, profiles=profile_name, ) urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=profile_name, ) yielded_records = list(urls_records_yielder) assert actual_browser_paths == {} assert yielded_records == []
def test_firefox_invalid_proflie(tests_root): profile_rootpath = Path(tests_root, 'firefox_databases') browser_name = 'firefox' table_name = 'moz_places' profile_name = ['test_profile1_JUNK'] file_name = 'places.sqlite' actual_browser_paths = make_browser_paths( browser=browser_name, profile_root=profile_rootpath, profiles=profile_name, ) moz_places_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=profile_name, ) yielded_records = list(moz_places_records_yielder) assert actual_browser_paths == {} assert yielded_records == []
def test_browser_chrome_access_single_profile_file_table_with_timestamp( tests_root): profile_rootpath = Path(tests_root, 'chrome_databases') browser_name = 'chrome' table_name = 'urls' profile_name = 'Profile 1' file_name = 'History' profile_path = Path( profile_rootpath, 'Profile 1', 'History', ) urls_records_yielder = make_browser_records_yielder( browser=browser_name, profile_root=profile_rootpath, filename=file_name, tablename=table_name, profiles=[profile_name], ) browser_profile1_records = [record for record in urls_records_yielder] sort_by_id = lambda item: item['id'] browser_profile1_records.sort(key=sort_by_id) with sqlite3.connect(str(profile_path)) as conn: conn.row_factory = sqlite3.Row cur = conn.cursor() cur.execute('''SELECT * FROM urls''') records = [dict(record) for record in cur] browser_profile1_records_ids = [ record['id'] for record in browser_profile1_records ] profile1_records_ids = [record['id'] for record in records] assert len(records) == len(browser_profile1_records) assert check_records_unique_with_field(records=records, field='id') assert check_records_unique_with_field(records=browser_profile1_records, field='id') assert sorted(profile1_records_ids) == sorted(browser_profile1_records_ids)
records = [record for record in moz_places_records_yielder] records_fieldnames = set(tuple(record_.keys()) for record_ in records) records_fieldnames = records_fieldnames.pop() additional_fields = ('browser', 'profile', 'file', 'table') expected_fieldnames = [*fields, *additional_fields] assert not set(records_fieldnames).difference(expected_fieldnames) if __name__ == '__main__': # pragma: no cover tests_root = '/home/kshitij/workspace/united-states-of-browsers/tests' profile_rootpath = Path(tests_root, 'firefox_databases') fields = [ 'id', 'url', 'title', 'last_visit_date', 'last_visit_readable', ] moz_places_records_yielder = make_browser_records_yielder( browser='firefox', profile_root=profile_rootpath, filename='places.sqlite', tablename='moz_places', profiles=None, fieldnames=fields, ) for record in moz_places_records_yielder: print(record)