Passed
Pull Request — master (#45)
by X
02:29
created

provideDataForInvalidFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class xKerman_Restricted_Test_UnserializeTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    public function testNull()
6
    {
7
        $this->assertNull(xKerman_Restricted_unserialize('N;'));
8
    }
9
    public function provideDataForInvalidNull()
10
    {
11
        return array('empty string' => array('input' => ''), 'missing tag' => array('input' => ';'), 'missing semicolon' => array('input' => 'N'));
12
    }
13
    /**
14
     * @dataProvider provideDataForInvalidNull
15
     * @expectedException xKerman_Restricted_UnserializeFailedException
16
     */
17
    public function testInvalidNull($input)
18
    {
19
        xKerman_Restricted_unserialize($input);
20
    }
21
    public function provideDataForBooleanTest()
22
    {
23
        return array('true' => array('input' => 'b:1;', 'expected' => true), 'false' => array('input' => 'b:0;', 'expected' => false));
24
    }
25
    /**
26
     * @dataProvider provideDataForBooleanTest
27
     */
28
    public function testBoolean($input, $expected)
29
    {
30
        $this->assertSame($expected, xKerman_Restricted_unserialize($input));
31
    }
32
    public function provideDataForIntegerTest()
33
    {
34
        return array('positive integer w/o plus sign' => array('input' => 'i:1;', 'expected' => 1), 'positive integer w/ plus sign' => array('input' => 'i:+1;', 'expected' => 1), 'positive integer greater than 9' => array('input' => 'i:10;', 'expected' => 10), 'negative integer' => array('input' => 'i:-1;', 'expected' => -1), 'integer w/ leading zero' => array('input' => 'i:0987;', 'expected' => 987));
35
    }
36
    public function provideDataForInvalidBooleanTest()
37
    {
38
        return array('empty string' => array('input' => ''), 'missing tag' => array('input' => ':0;'), 'missing value' => array('input' => 'b:;'), 'missing semicolon' => array('input' => 'b:0'), 'value is not boolean' => array('input' => 'b:2;'));
39
    }
40
    /**
41
     * @dataProvider provideDataForInvalidBooleanTest
42
     * @expectedException xKerman_Restricted_UnserializeFailedException
43
     */
44
    public function testInvalidBoolean($input)
45
    {
46
        xKerman_Restricted_unserialize($input);
47
    }
48
    /**
49
     * @dataProvider provideDataForIntegerTest
50
     */
51
    public function testInteger($input, $expected)
52
    {
53
        $this->assertSame($expected, xKerman_Restricted_unserialize($input));
54
    }
55
    public function provideDataForInvalidInteger()
56
    {
57
        return array('empty string' => array('input' => ''), 'missing tag' => array('input' => ':0;'), 'missing value' => array('input' => 'i:;'), 'missing semicolon' => array('input' => 'i:0'), 'sign only' => array('input' => 'i:+;'), 'multiple sign' => array('input' => 'i:++6;'), 'float value' => array('input' => 'i:1.0;'), 'hex value' => array('input' => 'i:0x50;'), 'binary value' => array('input' => 'i:0b111;'));
58
    }
59
    /**
60
     * @dataProvider provideDataForInvalidInteger
61
     * @expectedException xKerman_Restricted_UnserializeFailedException
62
     */
63
    public function testInvalidInteger($input)
64
    {
65
        xKerman_Restricted_unserialize($input);
66
    }
67
    public function provideDataForFloatTest()
68
    {
69
        return array('positive double w/o plus sign and point' => array('input' => 'd:1;', 'expected' => 1.0), 'positive double w/ plus sign' => array('input' => 'd:+1.5;', 'expected' => 1.5), 'negative double' => array('input' => 'd:-1.5;', 'expected' => -1.5), 'positive double w/o dicimal part' => array('input' => 'd:2.;', 'expected' => 2.0), 'positive double w/o integer part and plus sign' => array('input' => 'd:.5;', 'expected' => 0.5), 'positive double w/o integer part w/ plus sign' => array('input' => 'd:+.5;', 'expected' => 0.5), 'negative double w/o integer part' => array('input' => 'd:-.5;', 'expected' => -0.5), 'positive double represented as exponential notion w/o plus sign' => array('input' => 'd:1.0e10;', 'expected' => 10000000000.0), 'positive double represented as exponential notion w/ plus sign' => array('input' => 'd:+1.0e10;', 'expected' => 10000000000.0), 'negative double represented as exponential notion' => array('input' => 'd:-1.0e10;', 'expected' => -10000000000.0), 'positive double represented as exponential notion w/ positive exponential part' => array('input' => 'd:25E+2;', 'expected' => 2500.0), 'positive double represented as exponential notion w/ negative exponential part' => array('input' => 'd:25E-2;', 'expeced' => 0.25), 'positive infinity' => array('input' => 'd:INF;', 'expected' => INF), 'negative infinity' => array('input' => 'd:-INF;', 'expected' => -INF));
70
    }
71
    /**
72
     * @dataProvider provideDataForFloatTest
73
     */
74
    public function testFloat($input, $expected)
75
    {
76
        $this->assertSame($expected, xKerman_Restricted_unserialize($input));
77
    }
78
    public function provideDataForInvalidFloat()
79
    {
80
        return array('empty string' => array('input' => ''), 'missing tag' => array('input' => ':0;'), 'missing value' => array('input' => 'd:;'), 'missing semicolon' => array('input' => 'd:0'), 'sign only' => array('input' => 'd:+;'), 'multiple sign' => array('input' => 'd:++6;'), 'dot only' => array('input' => 'd:.;'), 'dot and exponential' => array('input' => 'd:.E;'), 'dot and exponential part' => array('input' => 'd:.E1;'), 'infinity with plus' => array('input' => 'd:+INF;'), 'nan with plus' => array('input' => 'd:+NAN;'), 'nan with plus' => array('input' => 'd:-NAN;'), 'float in exponential part' => array('input' => 'd:1.0e1.0;'), 'only exponential part' => array('input' => 'd:e1;'));
81
    }
82
    /**
83
     * @dataProvider provideDataForInvalidFloat
84
     * @expectedException xKerman_Restricted_UnserializeFailedException
85
     */
86
    public function testInvalidFloat($input)
87
    {
88
        xKerman_Restricted_unserialize($input);
89
    }
90
    public function testNan()
91
    {
92
        $this->assertTrue(is_nan(xKerman_Restricted_unserialize('d:NAN;')));
93
    }
94
    public function provideDataForStringTest()
95
    {
96
        return array('empty string' => array('input' => 's:0:"";', 'expected' => ''), 'single character (a)' => array('input' => 's:1:"a";', 'expected' => 'a'), 'single character (double quote)' => array('input' => serialize('"'), 'expected' => '"'), 'single character (single quote)' => array('input' => serialize("'"), 'expected' => "'"), 'single character (null byte)' => array('input' => serialize("\0"), 'expected' => "\0"), 'japanese character' => array('input' => serialize('こんにちは'), 'expected' => 'こんにちは'));
97
    }
98
    /**
99
     * @dataProvider provideDataForStringTest
100
     */
101
    public function testString($input, $expected)
102
    {
103
        $this->assertSame($expected, xKerman_Restricted_unserialize($input));
104
    }
105
    public function provideDataForInvalidString()
106
    {
107
        return array('empty string' => array('input' => ''), 'length is missing' => array('input' => 's::"";'), 'length is not number' => array('input' => 's:a:"";'), 'length is not integer' => array('input' => 's:1.0:"a";'), 'length is negative' => array('input' => 's:-1:"";'), 'length contains plus sign' => array('input' => 's:+1:"a";'), 'no quote' => array('input' => 's:1:a;'), 'open quote exist but close quote not exist' => array('input' => 's:1:"a;'), 'close quote exist but open quote not exist' => array('input' => 's:1:a";'), 'enclosed by single quote' => array('input' => "s:1:'a';"));
108
    }
109
    /**
110
     * @dataProvider provideDataForInvalidString
111
     * @expectedException xKerman_Restricted_UnserializeFailedException
112
     */
113
    public function testInvalidString($input)
114
    {
115
        xKerman_Restricted_unserialize($input);
116
    }
117
    public function provideDataForEscapedStringTest()
118
    {
119
        return array('empty string' => array('input' => 'S:0:"";', 'expected' => ''), 'single character (a, not escaped)' => array('input' => 'S:1:"a";', 'expected' => 'a'), 'single character (a, escaped)' => array('input' => 'S:1:"\\61";', 'expected' => 'a'), 'single character (j, escaped, upper)' => array('input' => 'S:1:"\\6A";', 'expected' => 'j'), 'single character (j, escaped, lower)' => array('input' => 'S:1:"\\6a";', 'expected' => 'j'), 'single character (double quote)' => array('input' => 'S:1:""";', 'expected' => '"'), 'single character (null byte)' => array('input' => 'S:1:"\\00";', 'expected' => "\0"), 'single character (\\xFF)' => array('input' => 'S:1:"\\FF";', 'expected' => "�"));
120
    }
121
    /**
122
     * @dataProvider provideDataForEscapedStringTest
123
     */
124
    public function testEscapedString($input, $expected)
125
    {
126
        $this->assertSame($expected, xKerman_Restricted_unserialize($input));
127
    }
128
    public function provideDataForInvalidEscapedString()
129
    {
130
        return array('empty string' => array('input' => ''), 'length is missing' => array('input' => 'S::"";'), 'length is not number' => array('input' => 'S:a:"";'), 'length is not integer' => array('input' => 'S:1.0:"a";'), 'length is negative' => array('input' => 'S:-1:"";'), 'length contains plus sign' => array('input' => 'S:+1:"a";'), 'no quote' => array('input' => 'S:1:a;'), 'open quote exist but close quote not exist' => array('input' => 'S:1:"a;'), 'close quote exist but open quote not exist' => array('input' => 'S:1:a";'), 'enclosed by single quote' => array('input' => "S:1:'a';"), 'escape range error (first part)' => array('input' => 'S:1:"\\ag";'), 'escape range error (second part)' => array('input' => 'S:1:"\\ga";'), 'escaped string is short' => array('input' => 'S:1:"\\1";'));
131
    }
132
    /**
133
     * @dataProvider provideDataForInvalidEscapedString
134
     * @expectedException xKerman_Restricted_UnserializeFailedException
135
     */
136
    public function testInvalidEscapedString($input)
137
    {
138
        xKerman_Restricted_unserialize($input);
139
    }
140
    public function provideDataForArrayTest()
141
    {
142
        return array('empty array' => array('input' => 'a:0:{}', 'expected' => array()), 'one element array' => array('input' => 'a:1:{i:0;s:1:"a";}', 'expected' => array('a')), 'nested array' => array('input' => serialize(array(array(), array(array(1), null), "a" => array("b" => "c"))), 'expected' => array(array(), array(array(1), null), "a" => array("b" => "c"))), 'escaped key array' => array('input' => 'a:1:{S:1:"\\62";N;}', 'expected' => array('b' => null)));
143
    }
144
    /**
145
     * @dataProvider provideDataForArrayTest
146
     */
147
    public function testArray($input, $expected)
148
    {
149
        $this->assertSame($expected, xKerman_Restricted_unserialize($input));
150
    }
151
    public function provideDataForInvalidArrayTest()
152
    {
153
        return array('empty string' => array('input' => ''), 'array length is missing' => array('input' => 'a::{}'), 'array length is not number' => array('input' => 'a:s:{}'), 'array length is not integer' => array('input' => 'a:1.0:{}'), 'array length is negative' => array('input' => 'a:-1:{}'), 'length contains plus sign' => array('input' => 'a:+0:{}'), 'array length is smaller than actual items' => array('input' => 'a:0:{i:0;s:1:"a";}'), 'array length is greater than actual items' => array('input' => 'a:2:{i:0;s:1:"a";}'), 'array key is not integer nor string' => array('input' => 'a:1:{N;i:0;}'), 'open brace not exist' => array('input' => 'a:0:}'), 'close brace not exist' => array('input' => 'a:0:{'), 'braces not exist' => array('input' => 'a:0:'), 'value not exist' => array('input' => 'a:1:{i:0;}'));
154
    }
155
    /**
156
     * @dataProvider provideDataForInvalidArrayTest
157
     * @expectedException xKerman_Restricted_UnserializeFailedException
158
     */
159
    public function testInvalidArray($input)
160
    {
161
        xKerman_Restricted_unserialize($input);
162
    }
163
}