From cbea303c306b4e2b2bdb867f5f33c6fb03fefb22 Mon Sep 17 00:00:00 2001 From: polcak Date: Mar 30 2021 13:37:19 +0000 Subject: Merge pull request #98 from martinbednar/integrationTests4 Integration tests: Add toString() tests. --- diff --git a/tests/integration_tests/testing/tests_definition/test_toString.py b/tests/integration_tests/testing/tests_definition/test_toString.py new file mode 100644 index 0000000..d8f2100 --- /dev/null +++ b/tests/integration_tests/testing/tests_definition/test_toString.py @@ -0,0 +1,39 @@ +# +# JavaScript Restrictor is a browser extension which increases level +# of security, anonymity and privacy of the user while browsing the +# internet. +# +# Copyright (C) 2021 Martin Bednar +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import pytest + +from values_getters import get_methods_toString + + +## Setup method - it is run before toString tests execution starts. +# +# This setup method initialize variable methods_toString that contains methods.toString() and +# this variable is provided to methods_toString test and the methods.toString() in the variable are compared with real values. +@pytest.fixture(scope='module', autouse=True) +def methods_toString(browser): + return get_methods_toString(browser.driver) + + +## Test methods.toString(). They should be always unchanged by JSR. +def test_methods_toString(browser, methods_toString): + for method in methods_toString: + assert methods_toString[method] == browser.real.methods_toString[method] diff --git a/tests/integration_tests/testing/values_expected.py b/tests/integration_tests/testing/values_expected.py index b7cde16..7b6f09a 100644 --- a/tests/integration_tests/testing/values_expected.py +++ b/tests/integration_tests/testing/values_expected.py @@ -69,7 +69,8 @@ level0 = TestedValues( 'accuracy': 'EXACTLY'}, performance={'value': 'REAL VALUE', 'accuracy': 'EXACTLY'}, - protect_canvas=False + protect_canvas=False, + methods_toString='REAL VALUE' ) ## Expected values for default level 1 of JSR. @@ -111,7 +112,8 @@ level1 = TestedValues( 'accuracy': 0.01}, performance={'value': 'REAL VALUE', 'accuracy': 10}, - protect_canvas=False + protect_canvas=False, + methods_toString='REAL VALUE' ) ## Expected values for default level 2 of JSR. @@ -153,7 +155,8 @@ level2 = TestedValues( 'accuracy': 0.1}, performance={'value': 'REAL VALUE', 'accuracy': 100}, - protect_canvas=True + protect_canvas=True, + methods_toString='REAL VALUE' ) ## Expected values for default level 3 of JSR. @@ -185,5 +188,6 @@ level3 = TestedValues( 'accuracy': 1.0}, performance={'value': 'REAL VALUE', 'accuracy': 1}, - protect_canvas=True + protect_canvas=True, + methods_toString='REAL VALUE' ) diff --git a/tests/integration_tests/testing/values_getters.py b/tests/integration_tests/testing/values_getters.py index 9e3d72e..24148a1 100644 --- a/tests/integration_tests/testing/values_getters.py +++ b/tests/integration_tests/testing/values_getters.py @@ -116,3 +116,144 @@ def is_canvas_spoofed(driver): return "ERROR" else: return is_spoofed + + +## Get methods.toString(). +# +# Only executing javascript and geting returned values. Testing page is needed. +def get_methods_toString(driver): + APIs = { + "Date": { + "static_methods": [ + "now", + "parse", + "UTC" + ], + "instance_methods": { + "getDate", + "getDay", + "getFullYear", + "getHours", + "getMilliseconds", + "getMinutes", + "getMonth", + "getSeconds", + "getTime", + "getTimezoneOffset", + "getUTCDate", + "getUTCDay", + "getUTCFullYear", + "getUTCHours", + "getUTCMilliseconds", + "getUTCMinutes", + "getUTCMonth", + "getUTCSeconds", + "getYear", + "setDate", + "setFullYear", + "setHours", + "setMilliseconds", + "setMinutes", + "setMonth", + "setSeconds", + "setTime", + "setUTCDate", + "setUTCFullYear", + "setUTCHours", + "setUTCMilliseconds", + "setUTCMinutes", + "setUTCMonth", + "setUTCSeconds", + "setYear", + "toDateString", + "toISOString", + "toJSON", + "toGMTString", + "toLocaleDateString", + "toLocaleString", + "toString", + "toTimeString", + "toUTCString", + "valueOf" + } + }, + "performance": { + "static_methods": [ + "now", + "clearMarks", + "clearMeasures", + "clearResourceTimings", + "getEntries", + "getEntriesByName", + "getEntriesByType", + "mark", + "measure", + "setResourceTimingBufferSize", + "toJSON" + ] + }, + "navigator": { + "static_methods": [ + "getCurrentPosition", + "watchPosition", + "clearWatch" + ] + }, + "canvas": { + "inject_code": "var canvas = document.getElementById('canvas1');", + "static_methods": [ + "getContext" + ] + } + } + + driver.get(get_config("testing_page")) + + output = {} + + + for API in APIs: + if 'static_methods' in APIs[API]: + inject_code = "" + if 'inject_code' in APIs[API]: + inject_code = APIs[API]['inject_code'] + + for method in APIs[API]['static_methods']: + method_toString = "" + try: + method_toString = driver.execute_script(inject_code + "return " + API + "." + method + ".toString()") + except: + output[API + '.' + method] = None + else: + output[API + '.' + method] = method_toString + + + if 'instance_methods' in APIs[API]: + constructor_toString = "" + try: + constructor_toString = driver.execute_script("return " + API + ".toString()") + except: + output[API] = None + else: + output[API] = constructor_toString + + + for method in APIs[API]['instance_methods']: + method_toString = "" + try: + method_toString = driver.execute_script("return " + API + "." + method + ".toString()") + except: + output[API + '.prototype.' + method] = None + else: + output[API + '.prototype.' + method] = method_toString + + + method_toString = "" + try: + method_toString = driver.execute_script("return new " + API + "()." + method + ".toString()") + except: + output[API + '.' + method] = None + else: + output[API + '.' + method] = method_toString + + return output diff --git a/tests/integration_tests/testing/values_real.py b/tests/integration_tests/testing/values_real.py index 34d94fc..60c2274 100644 --- a/tests/integration_tests/testing/values_real.py +++ b/tests/integration_tests/testing/values_real.py @@ -55,5 +55,6 @@ def init(driver): referrer=values_getters.get_referrer(driver), time=None, performance=None, - protect_canvas=None + protect_canvas=None, + methods_toString=values_getters.get_methods_toString(driver) ) diff --git a/tests/integration_tests/testing/values_tested.py b/tests/integration_tests/testing/values_tested.py index 4c324fd..1ee07c9 100644 --- a/tests/integration_tests/testing/values_tested.py +++ b/tests/integration_tests/testing/values_tested.py @@ -103,7 +103,8 @@ class TestedValues: referrer, time, performance, - protect_canvas + protect_canvas, + methods_toString ): self.navigator = Navigator( user_agent, @@ -134,3 +135,4 @@ class TestedValues: self.time = time self.performance = performance self.protect_canvas = protect_canvas + self.methods_toString = methods_toString