|
6 | 6 | import pytest |
7 | 7 | import random |
8 | 8 | import warnings |
| 9 | +from js2pysecrets.wrapper import wrapper, chain |
9 | 10 |
|
10 | 11 |
|
11 | 12 | settings = Settings() |
@@ -535,3 +536,99 @@ def test_py_js_random(input_range): |
535 | 536 | py_string = secrets.random(bits) |
536 | 537 | js_string = node.random(bits, test=True) |
537 | 538 | assert py_string == js_string |
| 539 | + |
| 540 | + |
| 541 | +@pytest.mark.parametrize("input_range", range(300)) |
| 542 | +def test_py_js_hex2bin(input_range): |
| 543 | + # bits = random_range(input_range) |
| 544 | + bits = int(input_range * 188) + 2 |
| 545 | + secrets.init() |
| 546 | + secrets.setRNG() |
| 547 | + hex_string = secrets.random(bits) |
| 548 | + py_string = secrets.hex2bin(hex_string) |
| 549 | + js_string = node._hex2bin(hex_string) |
| 550 | + assert py_string == js_string |
| 551 | + |
| 552 | + |
| 553 | +def test_hex2bin_Fail(): |
| 554 | + match = "Invalid hex character:" |
| 555 | + with pytest.raises(ValueError, match=match): |
| 556 | + secrets.hex2bin("0123efgh") |
| 557 | + # Check if any warnings were raised |
| 558 | + assert len(caught_warnings) == 1 |
| 559 | + assert issubclass(caught_warnings[0].category, Warning) |
| 560 | + assert match in str(caught_warnings[0].message) |
| 561 | + |
| 562 | + |
| 563 | +@pytest.fixture(params=[None, 128, 256, 384, 512, 640, 768, 896, 1024]) |
| 564 | +def multiple_of_bits(request): |
| 565 | + return request.param |
| 566 | + |
| 567 | + |
| 568 | +# Define your test function |
| 569 | +def test_array_split(random_string, multiple_of_bits): |
| 570 | + hex_string = secrets.str2hex(random_string) |
| 571 | + bin_string = "1" + secrets.hex2bin(hex_string) |
| 572 | + |
| 573 | + py_array = secrets.splitNumStringToIntArray(bin_string, multiple_of_bits) |
| 574 | + js_array = node._splitNumStringToIntArray(bin_string, multiple_of_bits) |
| 575 | + |
| 576 | + # Iterate over the elements of one array |
| 577 | + for i in range(len(py_array)): |
| 578 | + assert py_array[i] == js_array[i] |
| 579 | + |
| 580 | + |
| 581 | +@pytest.fixture( |
| 582 | + params=[None, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
| 583 | +) |
| 584 | +def set_init_bits(request): |
| 585 | + return request.param |
| 586 | + |
| 587 | + |
| 588 | +# Define test cases |
| 589 | +@pytest.mark.parametrize( |
| 590 | + "x, coeffs", |
| 591 | + [ |
| 592 | + (1, [21, 47, 205]), |
| 593 | + (2, [21, 47, 205]), |
| 594 | + (3, [21, 47, 205]), |
| 595 | + (4, [21, 47, 205]), |
| 596 | + (5, [21, 47, 205]), |
| 597 | + (6, [21, 47, 205]), |
| 598 | + (1, [0, 215, 162, 173, 194, 32, 53]), |
| 599 | + (2, [0, 215, 162, 173, 194, 32, 53]), |
| 600 | + (3, [0, 215, 162, 173, 194, 32, 53]), |
| 601 | + (4, [0, 215, 162, 173, 194, 32, 53]), |
| 602 | + (5, [0, 215, 162, 173, 194, 32, 53]), |
| 603 | + (6, [0, 215, 162, 173, 194, 32, 53]), |
| 604 | + (7, [0, 215, 162, 173, 194, 32, 53]), |
| 605 | + (8, [0, 215, 162, 173, 194, 32, 53]), |
| 606 | + (9, [0, 215, 162, 173, 194, 32, 53]), |
| 607 | + (10, [0, 215, 162, 173, 194, 32, 53]), |
| 608 | + (11, [0, 215, 162, 173, 194, 32, 53]), |
| 609 | + (12, [0, 215, 162, 173, 194, 32, 53]), |
| 610 | + (13, [0, 215, 162, 173, 194, 32, 53]), |
| 611 | + (14, [0, 215, 162, 173, 194, 32, 53]), |
| 612 | + (15, [0, 215, 162, 173, 194, 32, 53]), |
| 613 | + (16, [0, 215, 162, 173, 194, 32, 53]), |
| 614 | + (17, [0, 215, 162, 173, 194, 32, 53]), |
| 615 | + (18, [0, 215, 162, 173, 194, 32, 53]), |
| 616 | + (19, [0, 215, 162, 173, 194, 32, 53]), |
| 617 | + (20, [0, 215, 162, 173, 194, 32, 53]), |
| 618 | + (21, [0, 215, 162, 173, 194, 32, 53]), |
| 619 | + # Add more test cases as needed |
| 620 | + ], |
| 621 | +) |
| 622 | + |
| 623 | + |
| 624 | +# Define the test function |
| 625 | +def test_horner(x, coeffs, set_init_bits): |
| 626 | + secrets.init(set_init_bits) |
| 627 | + # assert secrets.horner(x, coeffs) == node._horner(x, coeffs) |
| 628 | + |
| 629 | + node_data = [] |
| 630 | + node_data.append(node.init(set_init_bits, list=True)) |
| 631 | + node_data.append(node._horner(x, coeffs, list=True)) |
| 632 | + js_results = chain(node_data) |
| 633 | + |
| 634 | + assert secrets.horner(x, coeffs) == js_results[-1] |
0 commit comments