1<?php
2
3namespace OAuthTest\Unit\OAuth1\Signature;
4
5use OAuth\OAuth1\Signature\Signature;
6
7class SignatureTest extends \PHPUnit_Framework_TestCase
8{
9    /**
10     * @covers OAuth\OAuth1\Signature\Signature::__construct
11     */
12    public function testConstructCorrectInterface()
13    {
14        $signature = new Signature($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'));
15
16        $this->assertInstanceOf('\\OAuth\\OAuth1\\Signature\\SignatureInterface', $signature);
17    }
18
19    /**
20     * @covers OAuth\OAuth1\Signature\Signature::__construct
21     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
22     */
23    public function testSetHashingAlgorithm()
24    {
25        $signature = new Signature($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'));
26
27        $this->assertNull($signature->setHashingAlgorithm('foo'));
28    }
29
30    /**
31     * @covers OAuth\OAuth1\Signature\Signature::__construct
32     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
33     */
34    public function testSetTokenSecret()
35    {
36        $signature = new Signature($this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'));
37
38        $this->assertNull($signature->setTokenSecret('foo'));
39    }
40
41    /**
42     * @covers OAuth\OAuth1\Signature\Signature::__construct
43     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
44     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
45     * @covers OAuth\OAuth1\Signature\Signature::getSignature
46     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
47     * @covers OAuth\OAuth1\Signature\Signature::hash
48     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
49     */
50    public function testGetSignatureBareUri()
51    {
52        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
53        $credentials->expects($this->any())
54            ->method('getConsumerSecret')
55            ->will($this->returnValue('foo'));
56
57
58        $signature = new Signature($credentials);
59
60        $signature->setHashingAlgorithm('HMAC-SHA1');
61        $signature->setTokenSecret('foo');
62
63        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
64        $uri->expects($this->any())
65            ->method('getQuery')
66            ->will($this->returnValue(''));
67        $uri->expects($this->any())
68            ->method('getScheme')
69            ->will($this->returnValue('http'));
70        $uri->expects($this->any())
71            ->method('getRawAuthority')
72            ->will($this->returnValue(''));
73        $uri->expects($this->any())
74            ->method('getPath')
75            ->will($this->returnValue('/foo'));
76
77        $this->assertSame('uoCpiII/Lg/cPiF0XrU2pj4eGFQ=', $signature->getSignature($uri, array('pee' => 'haa')));
78    }
79
80    /**
81     * @covers OAuth\OAuth1\Signature\Signature::__construct
82     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
83     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
84     * @covers OAuth\OAuth1\Signature\Signature::getSignature
85     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
86     * @covers OAuth\OAuth1\Signature\Signature::hash
87     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
88     */
89    public function testGetSignatureWithQueryString()
90    {
91        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
92        $credentials->expects($this->any())
93            ->method('getConsumerSecret')
94            ->will($this->returnValue('foo'));
95
96
97        $signature = new Signature($credentials);
98
99        $signature->setHashingAlgorithm('HMAC-SHA1');
100        $signature->setTokenSecret('foo');
101
102        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
103        $uri->expects($this->any())
104            ->method('getQuery')
105            ->will($this->returnValue('param1=value1'));
106        $uri->expects($this->any())
107            ->method('getScheme')
108            ->will($this->returnValue('http'));
109        $uri->expects($this->any())
110            ->method('getRawAuthority')
111            ->will($this->returnValue(''));
112        $uri->expects($this->any())
113            ->method('getPath')
114            ->will($this->returnValue('/foo'));
115
116        $this->assertSame('LxtD+WjJBRppIUvEI79iQ7I0hSo=', $signature->getSignature($uri, array('pee' => 'haa')));
117    }
118
119    /**
120     * @covers OAuth\OAuth1\Signature\Signature::__construct
121     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
122     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
123     * @covers OAuth\OAuth1\Signature\Signature::getSignature
124     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
125     * @covers OAuth\OAuth1\Signature\Signature::hash
126     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
127     */
128    public function testGetSignatureWithAuthority()
129    {
130        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
131        $credentials->expects($this->any())
132            ->method('getConsumerSecret')
133            ->will($this->returnValue('foo'));
134
135
136        $signature = new Signature($credentials);
137
138        $signature->setHashingAlgorithm('HMAC-SHA1');
139        $signature->setTokenSecret('foo');
140
141        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
142        $uri->expects($this->any())
143            ->method('getQuery')
144            ->will($this->returnValue('param1=value1'));
145        $uri->expects($this->any())
146            ->method('getScheme')
147            ->will($this->returnValue('http'));
148        $uri->expects($this->any())
149            ->method('getRawAuthority')
150            ->will($this->returnValue('peehaa:pass'));
151        $uri->expects($this->any())
152            ->method('getPath')
153            ->will($this->returnValue('/foo'));
154
155        $this->assertSame('MHvkRndIntLrxiPkjkiCNsMEqv4=', $signature->getSignature($uri, array('pee' => 'haa')));
156    }
157
158    /**
159     * @covers OAuth\OAuth1\Signature\Signature::__construct
160     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
161     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
162     * @covers OAuth\OAuth1\Signature\Signature::getSignature
163     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
164     * @covers OAuth\OAuth1\Signature\Signature::hash
165     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
166     */
167    public function testGetSignatureWithBarePathNonExplicitTrailingHostSlash()
168    {
169        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
170        $credentials->expects($this->any())
171            ->method('getConsumerSecret')
172            ->will($this->returnValue('foo'));
173
174
175        $signature = new Signature($credentials);
176
177        $signature->setHashingAlgorithm('HMAC-SHA1');
178        $signature->setTokenSecret('foo');
179
180        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
181        $uri->expects($this->any())
182            ->method('getQuery')
183            ->will($this->returnValue('param1=value1'));
184        $uri->expects($this->any())
185            ->method('getScheme')
186            ->will($this->returnValue('http'));
187        $uri->expects($this->any())
188            ->method('getRawAuthority')
189            ->will($this->returnValue('peehaa:pass'));
190        $uri->expects($this->any())
191            ->method('getPath')
192            ->will($this->returnValue('/'));
193        $uri->expects($this->any())
194            ->method('hasExplicitTrailingHostSlash')
195            ->will($this->returnValue(false));
196
197        $this->assertSame('iFELDoiI5Oj9ixB3kHzoPvBpq0w=', $signature->getSignature($uri, array('pee' => 'haa')));
198    }
199
200    /**
201     * @covers OAuth\OAuth1\Signature\Signature::__construct
202     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
203     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
204     * @covers OAuth\OAuth1\Signature\Signature::getSignature
205     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
206     * @covers OAuth\OAuth1\Signature\Signature::hash
207     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
208     */
209    public function testGetSignatureWithBarePathWithExplicitTrailingHostSlash()
210    {
211        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
212        $credentials->expects($this->any())
213            ->method('getConsumerSecret')
214            ->will($this->returnValue('foo'));
215
216
217        $signature = new Signature($credentials);
218
219        $signature->setHashingAlgorithm('HMAC-SHA1');
220        $signature->setTokenSecret('foo');
221
222        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
223        $uri->expects($this->any())
224            ->method('getQuery')
225            ->will($this->returnValue('param1=value1'));
226        $uri->expects($this->any())
227            ->method('getScheme')
228            ->will($this->returnValue('http'));
229        $uri->expects($this->any())
230            ->method('getRawAuthority')
231            ->will($this->returnValue('peehaa:pass'));
232        $uri->expects($this->any())
233            ->method('getPath')
234            ->will($this->returnValue('/'));
235        $uri->expects($this->any())
236            ->method('hasExplicitTrailingHostSlash')
237            ->will($this->returnValue(true));
238
239        $this->assertSame('IEhUsArSTLvbQ3QYr0zzn+Rxpjg=', $signature->getSignature($uri, array('pee' => 'haa')));
240    }
241
242    /**
243     * @covers OAuth\OAuth1\Signature\Signature::__construct
244     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
245     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
246     * @covers OAuth\OAuth1\Signature\Signature::getSignature
247     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
248     * @covers OAuth\OAuth1\Signature\Signature::hash
249     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
250     */
251    public function testGetSignatureNoTokenSecretSet()
252    {
253        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
254        $credentials->expects($this->any())
255            ->method('getConsumerSecret')
256            ->will($this->returnValue('foo'));
257
258
259        $signature = new Signature($credentials);
260
261        $signature->setHashingAlgorithm('HMAC-SHA1');
262
263        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
264        $uri->expects($this->any())
265            ->method('getQuery')
266            ->will($this->returnValue('param1=value1'));
267        $uri->expects($this->any())
268            ->method('getScheme')
269            ->will($this->returnValue('http'));
270        $uri->expects($this->any())
271            ->method('getRawAuthority')
272            ->will($this->returnValue('peehaa:pass'));
273        $uri->expects($this->any())
274            ->method('getPath')
275            ->will($this->returnValue('/'));
276        $uri->expects($this->any())
277            ->method('hasExplicitTrailingHostSlash')
278            ->will($this->returnValue(true));
279
280        $this->assertSame('YMHF7FYmLq7wzGnnHWYtd1VoBBE=', $signature->getSignature($uri, array('pee' => 'haa')));
281    }
282
283    /**
284     * @covers OAuth\OAuth1\Signature\Signature::__construct
285     * @covers OAuth\OAuth1\Signature\Signature::setHashingAlgorithm
286     * @covers OAuth\OAuth1\Signature\Signature::setTokenSecret
287     * @covers OAuth\OAuth1\Signature\Signature::getSignature
288     * @covers OAuth\OAuth1\Signature\Signature::buildSignatureDataString
289     * @covers OAuth\OAuth1\Signature\Signature::hash
290     * @covers OAuth\OAuth1\Signature\Signature::getSigningKey
291     */
292    public function testGetSignatureThrowsExceptionOnUnsupportedAlgo()
293    {
294        $this->setExpectedException('\\OAuth\\OAuth1\\Signature\\Exception\\UnsupportedHashAlgorithmException');
295
296        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
297        $credentials->expects($this->any())
298            ->method('getConsumerSecret')
299            ->will($this->returnValue('foo'));
300
301
302        $signature = new Signature($credentials);
303
304        $signature->setHashingAlgorithm('UnsupportedAlgo');
305
306        $uri = $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface');
307        $uri->expects($this->any())
308            ->method('getQuery')
309            ->will($this->returnValue('param1=value1'));
310        $uri->expects($this->any())
311            ->method('getScheme')
312            ->will($this->returnValue('http'));
313        $uri->expects($this->any())
314            ->method('getRawAuthority')
315            ->will($this->returnValue('peehaa:pass'));
316        $uri->expects($this->any())
317            ->method('getPath')
318            ->will($this->returnValue('/'));
319        $uri->expects($this->any())
320            ->method('hasExplicitTrailingHostSlash')
321            ->will($this->returnValue(true));
322
323        $signature->getSignature($uri, array('pee' => 'haa'));
324    }
325}
326