Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 9f49de2

Browse files
committed
added tests and test action
1 parent 698b102 commit 9f49de2

3 files changed

Lines changed: 74 additions & 53 deletions

File tree

tests/test-js.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>CryptoJS Aes PHP Tests</title>
5+
<title>CryptoJS Aes JS Tests</title>
66
<script src="../dist/cryptojs-aes.min.js"></script>
77
<script src="../dist/cryptojs-aes-format.js"></script>
88
</head>
@@ -22,26 +22,31 @@
2222
} else {
2323
results.innerText = 'FAIL'
2424
}
25+
results.setAttribute('data-result', '1')
2526
}
2627
break
2728
case 'encode': {
2829
const encrypted = CryptoJSAesJson.encrypt(originalValue, password)
30+
console.log(encrypted)
2931
if (encrypted.includes('"ct"') && encrypted.includes('"iv"') && encrypted.includes('"s"')) {
3032
results.innerText = 'OK'
3133
}
34+
results.setAttribute('data-result', '1')
3235
}
3336
break
3437
case 'decode': {
35-
const encrypted = '{"ct":"Ub4hX3ftm2xF1uU8A2gRCNlG2CxZpja5xjcXNP/CqwXLr7p5dQ+DXO2bZtTqCQlL","iv":"88c2a4ae3cbb5a3e68d367a795465103","s":"af114239cbf37e89"}'
38+
const encrypted = '{"ct":"tEwvZSmr9wWagPz9y/Wug3YbPBjzrAlZ7vIR25bh1eGxqXxATuMJYG8O1fQWNRSj","iv":"b04916e6d6bfc4a567004adee9763f70","s":"41c3279e949d2f7d"}'
3639
const decrypted = CryptoJSAesJson.decrypt(encrypted, password)
3740
if (JSON.stringify(decrypted) === JSON.stringify(originalValue)) {
3841
results.innerText = 'OK'
3942
}
43+
results.setAttribute('data-result', '1')
4044
}
4145
break
4246
case 'cross': {
4347
const encrypted = CryptoJSAesJson.encrypt(originalValue, password)
44-
results.innerText = await (await fetch('test-php.php?action=js&encoded=' + btoa(encrypted) + '&password=' + btoa(password))).text()
48+
results.innerText = (await (await fetch('test-php.php?action=js&encoded=' + btoa(encrypted) + '&password=' + btoa(password))).text()).match(/<body>(.*?)<\/body>/s)[1].trim()
49+
results.setAttribute('data-result', '1')
4550
}
4651
break
4752
}

tests/test-php.php

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,69 @@
77
$originalValue = ['foo' => 'bar', 'test' => '123öäüß'];
88
$password = "fancypensy_0192661";
99

10-
if (isset($_GET['action'])) {
11-
switch ($_GET['action']) {
12-
case 'js':
13-
$decrypted = CryptoJsAes::decrypt(base64_decode($_GET['encoded']), base64_decode($_GET['password']));
14-
if ($decrypted === $originalValue) {
15-
echo "OK";
16-
} else {
17-
echo "FAIL";
18-
}
19-
break;
20-
case 'encode':
21-
$encrypted = CryptoJsAes::encrypt($originalValue, $password);
22-
if (strpos($encrypted, '"ct"') !== false && strpos($encrypted, '"ev"') !== false && strpos($encrypted,
23-
'"s"') !== false) {
24-
echo '<div id="results">OK</div>';
25-
}
26-
break;
27-
case 'decode':
28-
$encrypted = '{"ct":"Ub4hX3ftm2xF1uU8A2gRCNlG2CxZpja5xjcXNP/CqwXLr7p5dQ+DXO2bZtTqCQlL","iv":"88c2a4ae3cbb5a3e68d367a795465103","s":"af114239cbf37e89"}';
29-
$decrypted = CryptoJsAes::decrypt($encrypted, $password);
30-
if ($decrypted === $originalValue) {
31-
echo '<div id="results">OK</div>';
32-
}
33-
break;
34-
case 'cross':
35-
?>
36-
<div id="results"></div>
37-
<iframe id="myframe"
38-
src="test-js.html?action=php&encoded=<?= base64_encode(CryptoJsAes::encrypt($originalValue,
39-
$password)) ?>&password=<?= base64_encode($password) ?>" style="visibility: hidden"></iframe>
40-
<script>
41-
(async function () {
42-
const frame = document.getElementById('myframe').contentWindow
43-
44-
function check () {
45-
let innerResults = frame.document.getElementById('results')
46-
if (!innerResults || !innerResults.innerText) {
47-
setTimeout(check, 200)
48-
return
49-
}
50-
document.getElementById('results').innerText = innerResults.innerText
10+
?>
11+
<!DOCTYPE html>
12+
<html lang="en">
13+
<head>
14+
<meta charset="UTF-8">
15+
<title>CryptoJS Aes PHP Tests</title>
16+
</head>
17+
<body>
18+
<?php
19+
if (isset($_GET['action'])) {
20+
switch ($_GET['action']) {
21+
case 'js':
22+
$decrypted = CryptoJsAes::decrypt(base64_decode($_GET['encoded']), base64_decode($_GET['password']));
23+
if ($decrypted === $originalValue) {
24+
echo "OK";
25+
} else {
26+
echo "FAIL";
27+
}
28+
break;
29+
case 'encode':
30+
$encrypted = CryptoJsAes::encrypt($originalValue, $password);
31+
if (strpos($encrypted, '"ct"') !== false && strpos($encrypted, '"iv"') !== false && strpos($encrypted,
32+
'"s"') !== false) {
33+
echo '<div id="results" data-result>OK</div>';
34+
}
35+
break;
36+
case 'decode':
37+
$encrypted = '{"ct":"tEwvZSmr9wWagPz9y/Wug3YbPBjzrAlZ7vIR25bh1eGxqXxATuMJYG8O1fQWNRSj","iv":"b04916e6d6bfc4a567004adee9763f70","s":"41c3279e949d2f7d"}';
38+
$decrypted = CryptoJsAes::decrypt($encrypted, $password);
39+
if ($decrypted === $originalValue) {
40+
echo '<div id="results" data-result>OK</div>';
5141
}
42+
break;
43+
case 'cross':
44+
?>
45+
<div id="results"></div>
46+
<iframe id="myframe"
47+
src="test-js.html?action=php&encoded=<?= base64_encode(CryptoJsAes::encrypt($originalValue,
48+
$password)) ?>&password=<?= base64_encode($password) ?>"
49+
style="visibility: hidden"></iframe>
50+
<script>
51+
(async function () {
52+
const frame = document.getElementById('myframe').contentWindow
5253

53-
check()
54-
})()
55-
</script>
56-
<?php
57-
break;
54+
function check () {
55+
let innerResults = frame.document.getElementById('results')
56+
if (!innerResults || !innerResults.innerText) {
57+
setTimeout(check, 200)
58+
return
59+
}
60+
document.getElementById('results').innerText = innerResults.innerText
61+
document.getElementById('results').setAttribute('data-result', '1')
62+
}
63+
64+
check()
65+
})()
66+
</script>
67+
<?php
68+
break;
69+
}
5870
}
59-
}
71+
72+
?>
73+
</body>
74+
</html>
75+
<?php

tests/test.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ test('Run all tests', async ({ page }) => {
77
})
88
const phpPortMapping = [
99
9970, // 7.0
10-
9982, // 8.2
10+
//9982, // 8.2
1111
]
1212
const actionsMap = ['encode', 'decode', 'cross']
1313
for (let i = 0; i < phpPortMapping.length; i++) {
1414
const port = phpPortMapping[i]
1515
for (let j = 0; j < actionsMap.length; j++) {
1616
const action = actionsMap[j]
1717
await page.goto('http://127.0.0.1:' + port + '/tests/test-js.html?action=' + action)
18-
await expect(page.locator('#results')).toHaveText('OK')
18+
await expect(page.locator('[data-result]')).toHaveText('OK', { timeout: 20000 })
1919
await page.goto('http://127.0.0.1:' + port + '/tests/test-php.php?action=' + action)
20-
await expect(page.locator('#results')).toHaveText('OK')
20+
await expect(page.locator('[data-result]')).toHaveText('OK', { timeout: 20000 })
2121
}
2222
}
2323
})

0 commit comments

Comments
 (0)