Completed
Push — master ( 1c8af5...5745ff )
by thomas
04:13 queued 02:50
created

DecodeTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 80
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testDecodeFails() 0 5 1
A invalidChecksumProvider() 0 11 1
A testValidChecksum() 0 9 1
A failedDecodeFixtures() 0 8 1
A validChecksumProvider() 0 8 1
A testInvalidChecksum() 0 4 1
1
<?php
2
3
namespace BitWasp\Test\Unit\Bech32;
4
5
6
use BitWasp\Bech32\Exception\Bech32Exception;
7
use BitWasp\Test\Bech32\TestCase;
8
9
class DecodeTest extends TestCase
10
{
11
    public function failedDecodeFixtures()
12
    {
13
        return [
14
            [str_pad("", 91, "A"), "Bech32 string cannot exceed 90 characters in length"],
15
            ["\x10", "Out of range character in bech32 string"],
16
            ["aB", "Data contains mixture of higher/lower case characters"],
17
            ["bcbcbc1bc", "Invalid location for `1` character"],
18
            ["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5", "Invalid bech32 checksum"],
19
        ];
20
    }
21
22
    /**
23
     * @param string $bech32
24
     * @dataProvider failedDecodeFixtures
25
     */
26
    public function testDecodeFails($bech32, $exceptionMsg)
27
    {
28
        $this->expectException(Bech32Exception::class);
29
        $this->expectExceptionMessage($exceptionMsg);
30
        \BitWasp\Bech32\decode($bech32);
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function validChecksumProvider()
37
    {
38
        return [
39
            ["A12UEL5L"],
40
            ["an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs"],
41
            ["abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw"],
42
            ["11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j"],
43
            ["split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w"],
44
        ];
45
    }
46
47
    /**
48
     * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L90
49
     * @param string $hasValidChecksum
50
     * @dataProvider validChecksumProvider
51
     */
52
    public function testValidChecksum($hasValidChecksum)
53
    {
54
        \BitWasp\Bech32\decode($hasValidChecksum);
55
56
        $pos = strrpos($hasValidChecksum, "1");
57
        $invalidChecksum = substr($hasValidChecksum, 0, $pos+1) . chr(ord($hasValidChecksum[$pos+1])^1) . substr($hasValidChecksum, $pos+2);
58
59
        $this->expectException(Bech32Exception::class);
60
        \BitWasp\Bech32\decode($invalidChecksum);
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function invalidChecksumProvider()
67
    {
68
        return [
69
            [" 1nwldj5"],
70
            ["\x7f"."1axkwrx"],
71
            ["an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx"],
72
            ["pzry9x0s0muk"],
73
            ["1pzry9x0s0muk"],
74
            ["x1b4n0q5v"],
75
            ["li1dgmt3"],
76
            ["de1lg7wt"."\xff"],
77
        ];
78
    }
79
80
    /**
81
     * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L100
82
     * @param string $hasValidChecksum
83
     * @dataProvider invalidChecksumProvider
84
     */
85
    public function testInvalidChecksum($hasValidChecksum)
86
    {
87
        $this->expectException(Bech32Exception::class);
88
        \BitWasp\Bech32\decode($hasValidChecksum);
89
    }
90
}
91