DecodeTest::failedDecodeFixtures()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 16
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BitWasp\Test\Unit\Bech32;
4
5
use BitWasp\Bech32\Exception\Bech32Exception;
6
use BitWasp\Test\Bech32\TestCase;
7
8
class DecodeTest extends TestCase
9
{
10
    public function failedDecodeFixtures()
11
    {
12
        return [
13
            ["\x201nwldj5", "Out of range character in bech32 string"],
14
            ["\x7f1axkwrx", "Out of range character in bech32 string"],
15
            ["\x801eym55h", "Out of range character in bech32 string"],
16
            ["an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", "Bech32 string cannot exceed 90 characters in length"],
17
            ["10a06t8", "Bech32 string is too short"],
18
            ["1qzzfhee", "Empty HRP"],
19
            ["pzry9x0s0muk", "Missing separator character"],
20
            ["1pzry9x0s0muk", "Empty HRP"],
21
            ["x1b4n0q5v", "Invalid bech32 checksum"],
22
            ["de1lg7wt\xff", "Out of range character in bech32 string"],
23
            ["A1G7SGD8", "Invalid bech32 checksum"],
24
            ["li1dgmt3", "Too short checksum"],
25
            ["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5", "Invalid bech32 checksum"],
26
        ];
27
    }
28
29
    /**
30
     * @param string $bech32
31
     * @param string $exceptionMsg
32
     * @dataProvider failedDecodeFixtures
33
     */
34
    public function testDecodeFails($bech32, $exceptionMsg)
35
    {
36
        $this->expectException(Bech32Exception::class);
37
        $this->expectExceptionMessage($exceptionMsg);
38
        \BitWasp\Bech32\decode($bech32);
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function validChecksumProvider()
45
    {
46
        return [
47
            ["A12UEL5L"],
48
            ["a12uel5l"],
49
            ["an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs"],
50
            ["abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw"],
51
            ["11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j"],
52
            ["split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w"],
53
            ["?1ezyfcl"],
54
        ];
55
    }
56
57
    /**
58
     * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L90
59
     * @param string $hasValidChecksum
60
     * @dataProvider validChecksumProvider
61
     */
62
    public function testValidChecksum($hasValidChecksum)
63
    {
64
        \BitWasp\Bech32\decode($hasValidChecksum);
65
66
        $pos = strrpos($hasValidChecksum, "1");
67
        $invalidChecksum = substr($hasValidChecksum, 0, $pos+1) . chr(ord($hasValidChecksum[$pos+1])^1) . substr($hasValidChecksum, $pos+2);
68
69
        $this->expectException(Bech32Exception::class);
70
        \BitWasp\Bech32\decode($invalidChecksum);
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    public function invalidChecksumProvider()
77
    {
78
        return [
79
            [" 1nwldj5"],
80
            ["\x7f"."1axkwrx"],
81
            ["an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx"],
82
            ["pzry9x0s0muk"],
83
            ["1pzry9x0s0muk"],
84
            ["x1b4n0q5v"],
85
            ["li1dgmt3"],
86
            ["de1lg7wt"."\xff"],
87
        ];
88
    }
89
90
    /**
91
     * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L100
92
     * @param string $hasValidChecksum
93
     * @dataProvider invalidChecksumProvider
94
     */
95
    public function testInvalidChecksum($hasValidChecksum)
96
    {
97
        $this->expectException(Bech32Exception::class);
98
        \BitWasp\Bech32\decode($hasValidChecksum);
99
    }
100
}
101