2022-07-01 09:36:52 +04:00
|
|
|
import os
|
2021-11-01 18:04:19 +04:00
|
|
|
import re
|
2022-03-25 16:45:38 +04:00
|
|
|
import unittest
|
|
|
|
|
2022-03-17 10:24:58 +04:00
|
|
|
from python_aternos import atjsparse
|
2021-11-01 18:04:19 +04:00
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
CONV_TOKEN_ARROW = '''(() => {window["AJAX_TOKEN"]=("2r" + "KO" + "A1" + "IFdBcHhEM" + "61" + "6cb");})();'''
|
|
|
|
CONV_TOKEN_FUNC = '(function(){window["AJAX_TOKEN"]=("2r" + "KO" + "A1" + "IFdBcHhEM" + "61" + "6cb");})()'
|
|
|
|
|
|
|
|
|
2022-03-25 16:45:38 +04:00
|
|
|
class TestJs2Py(unittest.TestCase):
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
def setUp(self) -> None:
|
|
|
|
|
2022-07-01 09:36:52 +04:00
|
|
|
self.path = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
self.samples = os.path.join(self.path, 'samples')
|
|
|
|
self.input = os.path.join(self.samples, 'token_input.txt')
|
|
|
|
self.output = os.path.join(self.samples, 'token_output.txt')
|
|
|
|
|
2022-06-23 15:13:56 +04:00
|
|
|
self.tests = []
|
2022-07-01 09:36:52 +04:00
|
|
|
with open(self.input, 'rt') as f:
|
2022-06-23 15:13:56 +04:00
|
|
|
lines = re.split(r'[\r\n]', f.read())
|
2022-07-01 09:36:52 +04:00
|
|
|
del lines[-1] # remove empty line at the end
|
2022-06-23 15:13:56 +04:00
|
|
|
self.tests = lines
|
|
|
|
|
2022-07-01 09:36:52 +04:00
|
|
|
self.results = []
|
|
|
|
with open(self.output, 'rt') as f:
|
|
|
|
lines = re.split(r'[\r\n]', f.read())
|
|
|
|
del lines[-1] # remove empty line at the end
|
|
|
|
self.results = lines
|
2022-06-23 15:13:56 +04:00
|
|
|
|
|
|
|
def test_base64(self) -> None:
|
|
|
|
|
|
|
|
encoded = 'QEhlbGxvIFdvcmxkIQ=='
|
|
|
|
decoded = atjsparse.atob(encoded)
|
|
|
|
self.assertEqual(decoded, '@Hello World!')
|
|
|
|
|
|
|
|
def test_conv(self) -> None:
|
|
|
|
|
|
|
|
token = CONV_TOKEN_ARROW
|
|
|
|
f = atjsparse.to_ecma5_function(token)
|
|
|
|
self.assertEqual(f, CONV_TOKEN_FUNC)
|
|
|
|
|
|
|
|
def test_ecma6parse(self) -> None:
|
|
|
|
|
|
|
|
code = '''
|
|
|
|
window.t0 =
|
|
|
|
window['document']&&
|
|
|
|
!window[["p","Ma"].reverse().join('')]||
|
|
|
|
!window[["ut","meo","i","etT","s"].reverse().join('')];'''
|
|
|
|
|
|
|
|
part1 = '''window.t1 = Boolean(window['document']);'''
|
|
|
|
part2 = '''window.t2 = Boolean(!window[["p","Ma"].reverse().join('')]);'''
|
|
|
|
part3 = '''window.t3 = Boolean(!window[["ut","meo","i","etT","s"].reverse().join('')]);'''
|
|
|
|
|
|
|
|
ctx0 = atjsparse.exec(code)
|
|
|
|
ctx1 = atjsparse.exec(part1)
|
|
|
|
ctx2 = atjsparse.exec(part2)
|
|
|
|
ctx3 = atjsparse.exec(part3)
|
|
|
|
|
|
|
|
self.assertEqual(ctx0.window['t0'], False)
|
|
|
|
self.assertEqual(ctx1.window['t1'], True)
|
|
|
|
self.assertEqual(ctx2.window['t2'], False)
|
|
|
|
self.assertEqual(ctx3.window['t3'], False)
|
|
|
|
|
|
|
|
def test_exec(self) -> None:
|
|
|
|
|
|
|
|
for i, f in enumerate(self.tests):
|
|
|
|
ctx = atjsparse.exec(f)
|
|
|
|
res = ctx.window['AJAX_TOKEN']
|
|
|
|
self.assertEqual(res, self.results[i])
|
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
del self.tests
|
|
|
|
del self.results
|