Passed
Push — master ( edb799...f409da )
by Petr
07:56
created

ProtocolTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 7
1
<?php
2
3
namespace ProtocolsTests\Fsp;
4
5
6
use CommonTestClass;
7
use kalanis\RemoteRequest\Connection;
8
use kalanis\RemoteRequest\Protocols\Fsp;
9
use kalanis\RemoteRequest\RequestException;
10
use kalanis\RemoteRequest\Translations;
11
12
13
class ProtocolQueryMock
14
{
15
    /**
16
     * What we send into server
17
     * @return resource|null
18
     */
19
    public function getRequestSimple()
20
    {
21
        return Common::makeDummyQuery([
22
            0x41, # CC_GET_DIR
23
            0x7f, # checksum
24
            0x01, 0x02, # key
25
            0x03, 0x04, # sequence
26
            0x00, 0x05, # data_length
27
            0x00, 0x00, 0x00, 0x00, # position
28
            'DATA', 0x00, # content "DATA"
29
            0x01, 0x00, # xtra
30
        ]);
31
    }
32
    /**
33
     * What we send into server
34
     * @return resource|null
35
     */
36
    public function getRequestFailedChk()
37
    {
38
        return Common::makeDummyQuery([
39
            0x41, # CC_GET_DIR
40
            0xAC, # checksum
41
            0x01, 0x02, # key
42
            0x03, 0x04, # sequence
43
            0x00, 0x05, # data_length
44
            0x00, 0x00, 0x00, 0x00, # position
45
            'DATA', 0x00, # content "DATA"
46
            0x01, 0x00, # xtra
47
        ]);
48
    }
49
}
50
51
class ProcessorMock extends Connection\Processor
52
{
53
    /**
54
     * What server responds
55
     * @return resource|null
56
     */
57
    public function getResponseSimple()
58
    {
59
        return Common::makeDummyQuery([
60
            0x42, # CC_GET_FILE
61
            0x8b, # checksum - 139
62
            0x01, 0x02, # key
63
            0x03, 0x04, # sequence
64
            0x00, 0x04, # data_length
65
            0x00, 0x00, 0x00, 0x20, # position
66
            'DATA', # content "DATA"
67
            # no xtra
68
        ]);
69
    }
70
71
    /**
72
     * What server responds
73
     * @return resource|null
74
     */
75
    public function getResponseReal()
76
    {
77
        return Common::makeDummyQuery([
78
            0x10, # CC_VERSION
79
            0x1B, # checksum 27
80
            0xF5, 0x8C, # key
81
            0x00, 0x10, # sequence
82
            0x00, 0x0E, # data_length
83
            0x00, 0x00, 0x00, 0x03, # position
84
            0x66, 0x73, 0x70, 0x64, 0x20, 0x32, 0x2E, 0x38, 0x2E, 0x31, 0x62, 0x32, 0x39, 0x00, # content "fspd 2.8.1b29"
85
            0x21, 0x05, 0xAC, # xtra
86
        ]);
87
    }
88
89
    /**
90
     * What server should not respond
91
     * @return resource|null
92
     */
93
    public function getResponseFailedChk()
94
    {
95
        return Common::makeDummyQuery([
96
            0x42, # CC_GET_FILE
97
            0x9A, # checksum - fail!
98
            0x01, 0x02, # key
99
            0x03, 0x04, # sequence
100
            0x00, 0x04, # data_length
101
            0x00, 0x00, 0x00, 0x20, # position
102
            'DATA', # content "DATA"
103
        ]);
104
    }
105
106
    public function getResponseShort()
107
    {
108
        return Common::makeDummyQuery([
109
            0x81, # CC_TEST
110
            0x31, # checksum
111
            0x01, 0x02, # key
112
        ]);
113
    }
114
115
    public function getResponseLong()
116
    {
117
        return Common::makeDummyQuery([
118
            0x81, # CC_TEST
119
            0x0B, # checksum
120
            0x01, 0x02, # key
121
            0x03, 0x04, # sequence
122
            0xff, 0xff, # data_length
123
            0x00, 0x00, 0x00, 0x00, # position
124
            str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 29), # shitty content
125
            # no extra data
126
        ]);
127
    }
128
}
129
130
131
class ProtocolTest extends CommonTestClass
132
{
133
    public function testQuerySimple(): void
134
    {
135
        $mock = new ProtocolQueryMock();
136
        $lib = new Fsp\Query();
137
        $lib->setCommand(Fsp::CC_GET_DIR)
138
            ->setKey(258)
139
            ->setSequence(772)
140
            ->setFilePosition(0)
141
            ->setContent('DATA' . chr(0))
142
            ->setExtraData(chr(01) . chr(0));
143
144
        $this->assertEquals(stream_get_contents($mock->getRequestSimple()), $lib->getPacket());
145
    }
146
147
    public function testQueryFailChecksum(): void
148
    {
149
        $mock = new ProtocolQueryMock();
150
        $lib = new Fsp\Query();
151
        $lib->setCommand(Fsp::CC_GET_DIR)
152
            ->setKey(258)
153
            ->setSequence(772)
154
            ->setFilePosition(0)
155
            ->setContent('DATA' . chr(0))
156
            ->setExtraData(chr(01) . chr(0));
157
158
        $this->assertNotEquals(stream_get_contents($mock->getRequestFailedChk()), $lib->getPacket());
159
    }
160
161
    /**
162
     * @throws RequestException
163
     */
164
    public function testAnswerSimple(): void
165
    {
166
        $lang = new Translations();
167
        $lib = new ProcessorMock($lang);
168
        $read = new Fsp\Answer($lang);
169
//        $read->canDump = true;
170
        $read->setResponse($lib->getResponseSimple())->process();
171
        $this->assertEquals(Fsp::CC_GET_FILE, $read->getCommand());
172
        $this->assertEquals(258, $read->getKey());
173
        $this->assertEquals(772, $read->getSequence());
174
        $this->assertEquals(32, $read->getFilePosition());
175
        $this->assertEquals('DATA', $read->getContent());
176
    }
177
178
    /**
179
     * @throws RequestException
180
     */
181
    public function testAnswerFailChecksumSimple(): void
182
    {
183
        $lang = new Translations();
184
        $lib = new ProcessorMock($lang);
185
        $read = new Fsp\Answer($lang);
186
        $this->expectException(RequestException::class);
187
        $read->setResponse($lib->getResponseFailedChk())->process();
188
    }
189
190
    /**
191
     * @throws RequestException
192
     */
193
    public function testAnswerShort(): void
194
    {
195
        $lang = new Translations();
196
        $mock = new ProcessorMock($lang);
197
        $read = new Fsp\Answer($lang);
198
        $this->expectException(RequestException::class);
199
        $read->setResponse($mock->getResponseShort())->process();
200
        Fsp\Answer\AnswerFactory::getObject($read)->process();
201
        $this->expectExceptionMessageMatches('Response too short');
202
    }
203
204
    /**
205
     * @throws RequestException
206
     */
207
    public function testAnswerLong(): void
208
    {
209
        $lang = new Translations();
210
        $mock = new ProcessorMock($lang);
211
        $read = new Fsp\Answer($lang);
212
        $this->expectException(RequestException::class);
213
        $read->setResponse($mock->getResponseLong())->process();
214
        Fsp\Answer\AnswerFactory::getObject($read)->process();
215
        $this->expectExceptionMessageMatches('Response too large');
216
    }
217
218
    /**
219
     * @throws RequestException
220
     * fails on real hash - need to calculate it "by hand"!
221
     */
222
    public function testAnswerReal(): void
223
    {
224
        $lang = new Translations();
225
        $lib = new ProcessorMock($lang);
226
        $read = new Fsp\Answer($lang);
227
//        $read->canDump = true;
228
        $read->setResponse($lib->getResponseReal())->process();
229
        $this->assertEquals(Fsp::CC_VERSION, $read->getCommand());
230
        $this->assertEquals(62860, $read->getKey());
231
        $this->assertEquals(16, $read->getSequence());
232
        $this->assertEquals(3, $read->getFilePosition()); // 3 at extra
233
        $this->assertEquals('fspd 2.8.1b29' . chr(0), $read->getContent());
234
    }
235
}
236