Actualización

This commit is contained in:
Xes
2025-04-10 12:36:07 +02:00
parent 1da7c3f3b9
commit 4aff98e77b
3147 changed files with 320647 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
<?php
/**
* A very simple class that you can pass a base-string, and then have it returned again.
* Used for testing the signature-methods
*/
class Mock_OAuthBaseStringRequest {
private $provided_base_string;
public $base_string; // legacy
public function __construct($bs) { $this->provided_base_string = $bs; }
public function get_signature_base_string() { return $this->provided_base_string; }
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* A mock store for testing
*/
class Mock_OAuthDataStore extends OAuthDataStore {
private $consumer;
private $request_token;
private $access_token;
private $nonce;
function __construct() {
$this->consumer = new OAuthConsumer("key", "secret", NULL);
$this->request_token = new OAuthToken("requestkey", "requestsecret", 1);
$this->access_token = new OAuthToken("accesskey", "accesssecret", 1);
$this->nonce = "nonce";
}
function lookup_consumer($consumer_key) {
if ($consumer_key == $this->consumer->key) return $this->consumer;
return NULL;
}
function lookup_token($consumer, $token_type, $token) {
$token_attrib = $token_type . "_token";
if ($consumer->key == $this->consumer->key
&& $token == $this->$token_attrib->key) {
return $this->$token_attrib;
}
return NULL;
}
function lookup_nonce($consumer, $token, $nonce, $timestamp) {
if ($consumer->key == $this->consumer->key
&& (($token && $token->key == $this->request_token->key)
|| ($token && $token->key == $this->access_token->key))
&& $nonce == $this->nonce) {
return $this->nonce;
}
return NULL;
}
function new_request_token($consumer, $callback = null) {
if ($consumer->key == $this->consumer->key) {
return $this->request_token;
}
return NULL;
}
function new_access_token($token, $consumer, $verifier = null) {
if ($consumer->key == $this->consumer->key
&& $token->key == $this->request_token->key) {
return $this->access_token;
}
return NULL;
}
}

View File

@@ -0,0 +1,47 @@
<?php
/**
* A mock implementation of OAuthSignatureMethod_RSA_SHA1
* Always returns the signatures described in
* http://wiki.oauth.net/TestCases section 9.3 ("RSA-SHA1")
*/
class Mock_OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod_RSA_SHA1 {
public function fetch_private_cert(&$request) {
$cert = <<<EOD
-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALRiMLAh9iimur8V
A7qVvdqxevEuUkW4K+2KdMXmnQbG9Aa7k7eBjK1S+0LYmVjPKlJGNXHDGuy5Fw/d
7rjVJ0BLB+ubPK8iA/Tw3hLQgXMRRGRXXCn8ikfuQfjUS1uZSatdLB81mydBETlJ
hI6GH4twrbDJCR2Bwy/XWXgqgGRzAgMBAAECgYBYWVtleUzavkbrPjy0T5FMou8H
X9u2AC2ry8vD/l7cqedtwMPp9k7TubgNFo+NGvKsl2ynyprOZR1xjQ7WgrgVB+mm
uScOM/5HVceFuGRDhYTCObE+y1kxRloNYXnx3ei1zbeYLPCHdhxRYW7T0qcynNmw
rn05/KO2RLjgQNalsQJBANeA3Q4Nugqy4QBUCEC09SqylT2K9FrrItqL2QKc9v0Z
zO2uwllCbg0dwpVuYPYXYvikNHHg+aCWF+VXsb9rpPsCQQDWR9TT4ORdzoj+Nccn
qkMsDmzt0EfNaAOwHOmVJ2RVBspPcxt5iN4HI7HNeG6U5YsFBb+/GZbgfBT3kpNG
WPTpAkBI+gFhjfJvRw38n3g/+UeAkwMI2TJQS4n8+hid0uus3/zOjDySH3XHCUno
cn1xOJAyZODBo47E+67R4jV1/gzbAkEAklJaspRPXP877NssM5nAZMU0/O/NGCZ+
3jPgDUno6WbJn5cqm8MqWhW1xGkImgRk+fkDBquiq4gPiT898jusgQJAd5Zrr6Q8
AO/0isr/3aa6O6NLQxISLKcPDk2NOccAfS/xOtfOz4sJYM3+Bs4Io9+dZGSDCA54
Lw03eHTNQghS0A==
-----END PRIVATE KEY-----
EOD;
return $cert;
}
public function fetch_public_cert(&$request) {
$cert = <<<EOD
-----BEGIN CERTIFICATE-----
MIIBpjCCAQ+gAwIBAgIBATANBgkqhkiG9w0BAQUFADAZMRcwFQYDVQQDDA5UZXN0
IFByaW5jaXBhbDAeFw03MDAxMDEwODAwMDBaFw0zODEyMzEwODAwMDBaMBkxFzAV
BgNVBAMMDlRlc3QgUHJpbmNpcGFsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
gQC0YjCwIfYoprq/FQO6lb3asXrxLlJFuCvtinTF5p0GxvQGu5O3gYytUvtC2JlY
zypSRjVxwxrsuRcP3e641SdASwfrmzyvIgP08N4S0IFzEURkV1wp/IpH7kH41Etb
mUmrXSwfNZsnQRE5SYSOhh+LcK2wyQkdgcMv11l4KoBkcwIDAQABMA0GCSqGSIb3
DQEBBQUAA4GBAGZLPEuJ5SiJ2ryq+CmEGOXfvlTtEL2nuGtr9PewxkgnOjZpUy+d
4TvuXJbNQc8f4AMWL/tO9w0Fk80rWKp9ea8/df4qMq5qlFWlx6yOLQxumNOmECKb
WpkUQDIDJEoFUzKMVuJf4KO/FJ345+BNLGgbJ6WujreoM1X/gYfdnJ/J
-----END CERTIFICATE-----
EOD;
return $cert;
}
}

View File

@@ -0,0 +1,10 @@
<?php
require 'common.php';
class OAuthConsumerTest extends PHPUnit_Framework_TestCase {
public function testConvertToString() {
$consumer = new OAuthConsumer('key', 'secret');
$this->assertEquals('OAuthConsumer[key=key,secret=secret]', (string) $consumer);
}
}

View File

@@ -0,0 +1,329 @@
<?php
/*
* Tests of OAuthRequest
*
* The tests works by using OAuthTestUtils::build_request
* to populare $_SERVER, $_GET & $_POST.
*
* Most of the base string and signature tests
* are either very simple or based upon
* http://wiki.oauth.net/TestCases
*/
require_once dirname(__FILE__) . '/common.php';
class OAuthRequestTest extends PHPUnit_Framework_TestCase {
public function testCanGetSingleParameter() {
// Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
$request = new OAuthRequest('', '', array('test'=>'foo'));
$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');
$request = new OAuthRequest('', '', array('test'=>array('foo', 'bar')));
$this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to read back parameter');
$request = new OAuthRequest('', '', array('test'=>'foo', 'bar'=>'baz'));
$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');
$this->assertEquals( 'baz', $request->get_parameter('bar'), 'Failed to read back parameter');
}
public function testGetAllParameters() {
// Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
$request = new OAuthRequest('', '', array('test'=>'foo'));
$this->assertEquals( array('test'=>'foo'), $request->get_parameters(), 'Failed to read back parameters');
$request = new OAuthRequest('', '', array('test'=>'foo', 'bar'=>'baz'));
$this->assertEquals( array('test'=>'foo', 'bar'=>'baz'), $request->get_parameters(), 'Failed to read back parameters');
$request = new OAuthRequest('', '', array('test'=>array('foo', 'bar')));
$this->assertEquals( array('test'=>array('foo', 'bar')), $request->get_parameters(), 'Failed to read back parameters');
}
public function testSetParameters() {
$request = new OAuthRequest('', '');
$this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to assert that non-existing parameter is NULL');
$request->set_parameter('test', 'foo');
$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to set single-entry parameter');
$request->set_parameter('test', 'bar');
$this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to set single-entry parameter');
$request->set_parameter('test', 'bar', false);
$this->assertEquals( 'bar', $request->get_parameter('test'), 'Failed to set single-entry parameter');
}
public function testUnsetParameter() {
$request = new OAuthRequest('', '');
$this->assertEquals( NULL, $request->get_parameter('test'));
$request->set_parameter('test', 'foo');
$this->assertEquals( 'foo', $request->get_parameter('test'));
$request->unset_parameter('test');
$this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to unset parameter');
}
public function testCreateRequestFromConsumerAndToken() {
$cons = new OAuthConsumer('key', 'kd94hf93k423kf44');
$token = new OAuthToken('token', 'pfkkdhi9sl3r4s00');
$request = OAuthRequest::from_consumer_and_token($cons, $token, 'POST', 'http://example.com');
$this->assertEquals('POST', $request->get_normalized_http_method());
$this->assertEquals('http://example.com', $request->get_normalized_http_url());
$this->assertEquals('1.0', $request->get_parameter('oauth_version'));
$this->assertEquals($cons->key, $request->get_parameter('oauth_consumer_key'));
$this->assertEquals($token->key, $request->get_parameter('oauth_token'));
$this->assertEquals(time(), $request->get_parameter('oauth_timestamp'));
$this->assertRegExp('/[0-9a-f]{32}/', $request->get_parameter('oauth_nonce'));
// We don't know what the nonce will be, except it'll be md5 and hence 32 hexa digits
$request = OAuthRequest::from_consumer_and_token($cons, $token, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
$this->assertEquals('foo', $request->get_parameter('oauth_nonce'));
$request = OAuthRequest::from_consumer_and_token($cons, NULL, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
$this->assertNull($request->get_parameter('oauth_token'));
// Test that parameters given in the $http_url instead of in the $parameters-parameter
// will still be picked up
$request = OAuthRequest::from_consumer_and_token($cons, $token, 'POST', 'http://example.com/?foo=bar');
$this->assertEquals('http://example.com/', $request->get_normalized_http_url());
$this->assertEquals('bar', $request->get_parameter('foo'));
}
public function testBuildRequestFromPost() {
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'foo=bar&baz=blargh');
$this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuthRequest::from_request()->get_parameters(), 'Failed to parse POST parameters');
}
public function testBuildRequestFromGet() {
OAuthTestUtils::build_request('GET', 'http://testbed/test?foo=bar&baz=blargh');
$this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuthRequest::from_request()->get_parameters(), 'Failed to parse GET parameters');
}
public function testBuildRequestFromHeader() {
$test_header = 'OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"';
OAuthTestUtils::build_request('POST', 'http://testbed/test', '', $test_header);
$this->assertEquals(array('oauth_foo'=>'bar','oauth_baz'=>'bla,rgh'), OAuthRequest::from_request()->get_parameters(), 'Failed to split auth-header correctly');
}
public function testHasProperParameterPriority() {
$test_header = 'OAuth realm="",oauth_foo=header';
OAuthTestUtils::build_request('POST', 'http://testbed/test?oauth_foo=get', 'oauth_foo=post', $test_header);
$this->assertEquals('header', OAuthRequest::from_request()->get_parameter('oauth_foo'), 'Loaded parameters in with the wrong priorities');
OAuthTestUtils::build_request('POST', 'http://testbed/test?oauth_foo=get', 'oauth_foo=post');
$this->assertEquals('post', OAuthRequest::from_request()->get_parameter('oauth_foo'), 'Loaded parameters in with the wrong priorities');
OAuthTestUtils::build_request('POST', 'http://testbed/test?oauth_foo=get');
$this->assertEquals('get', OAuthRequest::from_request()->get_parameter('oauth_foo'), 'Loaded parameters in with the wrong priorities');
}
public function testNormalizeHttpMethod() {
OAuthTestUtils::build_request('POST', 'http://testbed/test');
$this->assertEquals('POST', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: POST');
OAuthTestUtils::build_request('post', 'http://testbed/test');
$this->assertEquals('POST', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: post');
OAuthTestUtils::build_request('GET', 'http://testbed/test');
$this->assertEquals('GET', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: GET');
OAuthTestUtils::build_request('PUT', 'http://testbed/test');
$this->assertEquals('PUT', OAuthRequest::from_request()->get_normalized_http_method(), 'Failed to normalize HTTP method: PUT');
}
public function testNormalizeParameters() {
// This is mostly repeats of OAuthUtilTest::testParseParameters & OAuthUtilTest::TestBuildHttpQuery
// Tests taken from
// http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'name');
$this->assertEquals( 'name=', OAuthRequest::from_request()->get_signable_parameters());
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=b');
$this->assertEquals( 'a=b', OAuthRequest::from_request()->get_signable_parameters());
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=b&c=d');
$this->assertEquals( 'a=b&c=d', OAuthRequest::from_request()->get_signable_parameters());
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=x%21y&a=x+y');
$this->assertEquals( 'a=x%20y&a=x%21y', OAuthRequest::from_request()->get_signable_parameters());
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'x%21y=a&x=a');
$this->assertEquals( 'x=a&x%21y=a', OAuthRequest::from_request()->get_signable_parameters());
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'a=1&c=hi there&f=25&f=50&f=a&z=p&z=t');
$this->assertEquals( 'a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t', OAuthRequest::from_request()->get_signable_parameters());
}
public function testNormalizeHttpUrl() {
OAuthTestUtils::build_request('POST', 'http://example.com');
$this->assertEquals('http://example.com', OAuthRequest::from_request()->get_normalized_http_url());
OAuthTestUtils::build_request('POST', 'https://example.com');
$this->assertEquals('https://example.com', OAuthRequest::from_request()->get_normalized_http_url());
// Tests that http on !80 and https on !443 keeps the port
OAuthTestUtils::build_request('POST', 'http://example.com:8080');
$this->assertEquals('http://example.com:8080', OAuthRequest::from_request()->get_normalized_http_url());
OAuthTestUtils::build_request('POST', 'https://example.com:80');
$this->assertEquals('https://example.com:80', OAuthRequest::from_request()->get_normalized_http_url());
OAuthTestUtils::build_request('POST', 'http://example.com:443');
$this->assertEquals('http://example.com:443', OAuthRequest::from_request()->get_normalized_http_url());
OAuthTestUtils::build_request('POST', 'http://Example.COM');
$this->assertEquals('http://example.com', OAuthRequest::from_request()->get_normalized_http_url());
// Emulate silly behavior by some clients, where there Host header includes the port
OAuthTestUtils::build_request('POST', 'http://example.com');
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'];
$this->assertEquals('http://example.com', OAuthRequest::from_request()->get_normalized_http_url());
}
public function testBuildPostData() {
OAuthTestUtils::build_request('POST', 'http://example.com');
$this->assertEquals('', OAuthRequest::from_request()->to_postdata());
OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
$this->assertEquals('foo=bar', OAuthRequest::from_request()->to_postdata());
OAuthTestUtils::build_request('GET', 'http://example.com?foo=bar');
$this->assertEquals('foo=bar', OAuthRequest::from_request()->to_postdata());
}
public function testBuildUrl() {
OAuthTestUtils::build_request('POST', 'http://example.com');
$this->assertEquals('http://example.com', OAuthRequest::from_request()->to_url());
OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
$this->assertEquals('http://example.com?foo=bar', OAuthRequest::from_request()->to_url());
OAuthTestUtils::build_request('GET', 'http://example.com?foo=bar');
$this->assertEquals('http://example.com?foo=bar', OAuthRequest::from_request()->to_url());
}
public function testConvertToString() {
OAuthTestUtils::build_request('POST', 'http://example.com');
$this->assertEquals('http://example.com', (string) OAuthRequest::from_request());
OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
$this->assertEquals('http://example.com?foo=bar', (string) OAuthRequest::from_request());
OAuthTestUtils::build_request('GET', 'http://example.com?foo=bar');
$this->assertEquals('http://example.com?foo=bar', (string) OAuthRequest::from_request());
}
public function testBuildHeader() {
OAuthTestUtils::build_request('POST', 'http://example.com');
$this->assertEquals('Authorization: OAuth', OAuthRequest::from_request()->to_header());
$this->assertEquals('Authorization: OAuth realm="test"', OAuthRequest::from_request()->to_header('test'));
OAuthTestUtils::build_request('POST', 'http://example.com', 'foo=bar');
$this->assertEquals('Authorization: OAuth', OAuthRequest::from_request()->to_header());
$this->assertEquals('Authorization: OAuth realm="test"', OAuthRequest::from_request()->to_header('test'));
OAuthTestUtils::build_request('POST', 'http://example.com', 'oauth_test=foo');
$this->assertEquals('Authorization: OAuth oauth_test="foo"', OAuthRequest::from_request()->to_header());
$this->assertEquals('Authorization: OAuth realm="test",oauth_test="foo"', OAuthRequest::from_request()->to_header('test'));
// Is headers supposted to be Urlencoded. More to the point:
// Should it be baz = bla,rgh or baz = bla%2Crgh ??
// - morten.fangel
OAuthTestUtils::build_request('POST', 'http://example.com', '', 'OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"');
$this->assertEquals('Authorization: OAuth oauth_foo="bar",oauth_baz="bla%2Crgh"', OAuthRequest::from_request()->to_header());
$this->assertEquals('Authorization: OAuth realm="test",oauth_foo="bar",oauth_baz="bla%2Crgh"', OAuthRequest::from_request()->to_header('test'));
}
public function testWontBuildHeaderWithArrayInput() {
$this->setExpectedException('OAuthException');
OAuthTestUtils::build_request('POST', 'http://example.com', 'oauth_foo=bar&oauth_foo=baz');
OAuthRequest::from_request()->to_header();
}
public function testBuildBaseString() {
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'n=v');
$this->assertEquals('POST&http%3A%2F%2Ftestbed%2Ftest&n%3Dv', OAuthRequest::from_request()->get_signature_base_string());
OAuthTestUtils::build_request('POST', 'http://testbed/test', 'n=v&n=v2');
$this->assertEquals('POST&http%3A%2F%2Ftestbed%2Ftest&n%3Dv%26n%3Dv2', OAuthRequest::from_request()->get_signature_base_string());
OAuthTestUtils::build_request('GET', 'http://example.com?n=v');
$this->assertEquals('GET&http%3A%2F%2Fexample.com&n%3Dv', OAuthRequest::from_request()->get_signature_base_string());
$params = 'oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_timestamp=1191242090';
$params .= '&oauth_nonce=hsu94j3884jdopsl&oauth_signature_method=PLAINTEXT&oauth_signature=ignored';
OAuthTestUtils::build_request('POST', 'https://photos.example.net/request_token', $params);
$this->assertEquals('POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&oauth_'
.'consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884j'
.'dopsl%26oauth_signature_method%3DPLAINTEXT%26oauth_timestam'
.'p%3D1191242090%26oauth_version%3D1.0',
OAuthRequest::from_request()->get_signature_base_string());
$params = 'file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03';
$params .= '&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh';
$params .= '&oauth_signature=ignored&oauth_signature_method=HMAC-SHA1';
OAuthTestUtils::build_request('GET', 'http://photos.example.net/photos?'.$params);
$this->assertEquals('GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation'
.'.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%'
.'3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26o'
.'auth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jd'
.'k%26oauth_version%3D1.0%26size%3Doriginal',
OAuthRequest::from_request()->get_signature_base_string());
}
public function testBuildSignature() {
$params = 'file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03';
$params .= '&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh';
$params .= '&oauth_signature=ignored&oauth_signature_method=HMAC-SHA1';
OAuthTestUtils::build_request('GET', 'http://photos.example.net/photos?'.$params);
$r = OAuthRequest::from_request();
$cons = new OAuthConsumer('key', 'kd94hf93k423kf44');
$token = new OAuthToken('token', 'pfkkdhi9sl3r4s00');
$hmac = new OAuthSignatureMethod_HMAC_SHA1();
$plaintext = new OAuthSignatureMethod_PLAINTEXT();
$this->assertEquals('tR3+Ty81lMeYAr/Fid0kMTYa/WM=', $r->build_signature($hmac, $cons, $token));
$this->assertEquals('kd94hf93k423kf44&pfkkdhi9sl3r4s00', $r->build_signature($plaintext, $cons, $token));
}
public function testSign() {
$params = 'file=vacation.jpg&size=original&oauth_version=1.0&oauth_consumer_key=dpf43f3p2l4k3l03';
$params .= '&oauth_token=nnch734d00sl2jdk&oauth_timestamp=1191242096&oauth_nonce=kllo9940pd9333jh';
$params .= '&oauth_signature=__ignored__&oauth_signature_method=HMAC-SHA1';
OAuthTestUtils::build_request('GET', 'http://photos.example.net/photos?'.$params);
$r = OAuthRequest::from_request();
$cons = new OAuthConsumer('key', 'kd94hf93k423kf44');
$token = new OAuthToken('token', 'pfkkdhi9sl3r4s00');
$hmac = new OAuthSignatureMethod_HMAC_SHA1();
$plaintext = new OAuthSignatureMethod_PLAINTEXT();
// We need to test both what the parameter is, and how the serialized request is..
$r->sign_request($hmac, $cons, $token);
$this->assertEquals('HMAC-SHA1', $r->get_parameter('oauth_signature_method'));
$this->assertEquals('tR3+Ty81lMeYAr/Fid0kMTYa/WM=', $r->get_parameter('oauth_signature'));
$expectedPostdata = 'file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&'
. 'oauth_signature=tR3%2BTy81lMeYAr%2FFid0kMTYa%2FWM%3D&oauth_signature_method=HMAC-SHA1&'
. 'oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original';
$this->assertEquals( $expectedPostdata, $r->to_postdata());
$r->sign_request($plaintext, $cons, $token);
$this->assertEquals('PLAINTEXT', $r->get_parameter('oauth_signature_method'));
$this->assertEquals('kd94hf93k423kf44&pfkkdhi9sl3r4s00', $r->get_parameter('oauth_signature'));
$expectedPostdata = 'file=vacation.jpg&oauth_consumer_key=dpf43f3p2l4k3l03&oauth_nonce=kllo9940pd9333jh&'
. 'oauth_signature=kd94hf93k423kf44%26pfkkdhi9sl3r4s00&oauth_signature_method=PLAINTEXT&'
. 'oauth_timestamp=1191242096&oauth_token=nnch734d00sl2jdk&oauth_version=1.0&size=original';
$this->assertEquals( $expectedPostdata, $r->to_postdata());
}
}
?>

View File

@@ -0,0 +1,225 @@
<?php
require_once dirname(__FILE__) . '/common.php';
require_once dirname(__FILE__) . '/Mock_OAuthDataStore.php';
/**
* Tests of OAuthUtil
*/
class OAuthServerTest extends PHPUnit_Framework_TestCase {
private $consumer;
private $request_token;
private $access_token;
private $hmac_sha1;
private $plaintext;
private $server;
public function setUp() {
$this->consumer = new OAuthConsumer('key', 'secret');
$this->request_token = new OAuthToken('requestkey', 'requestsecret');
$this->access_token = new OAuthToken('accesskey', 'accesssecret');
$this->hmac_sha1 = new OAuthSignatureMethod_HMAC_SHA1();
$this->plaintext = new OAuthSignatureMethod_PLAINTEXT();
$this->server = new OAuthServer( new Mock_OAuthDataStore() );
$this->server->add_signature_method( $this->hmac_sha1 );
$this->server->add_signature_method( $this->plaintext );
}
public function testAcceptValidRequest() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
list($consumer, $token) = $this->server->verify_request( $request );
$this->assertEquals( $this->consumer, $consumer );
$this->assertEquals( $this->access_token, $token );
$request->sign_request( $this->hmac_sha1, $this->consumer, $this->access_token );
list($consumer, $token) = $this->server->verify_request( $request );
$this->assertEquals( $this->consumer, $consumer );
$this->assertEquals( $this->access_token, $token );
}
public function testAcceptRequestWithoutVersion() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->unset_parameter('oauth_version');
$request->sign_request( $this->hmac_sha1, $this->consumer, $this->access_token );
$this->server->verify_request( $request );
}
public function testRejectRequestSignedWithRequestToken() {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->request_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->request_token );
$this->setExpectedException('OAuthException');
$this->server->verify_request( $request );
}
public function testRejectRequestWithMissingParameters() {
// The list of required parameters is taken from
// Chapter 7 ("Accessing Protected Resources")
$required_parameters = array(
'oauth_consumer_key',
'oauth_token',
'oauth_signature_method',
'oauth_signature',
'oauth_timestamp',
'oauth_nonce'
);
foreach( $required_parameters AS $required ) {
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
try {
$request->unset_parameter( $required );
$this->server->verify_request($request);
$this->fail('Allowed a request without `' . $required . '`');
} catch( OAuthException $e ) { /* expected */ }
}
}
public function testRejectPastTimestamp() {
// We change the timestamp to be 10 hours ago, it should throw an exception
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->set_parameter( 'oauth_timestamp', $request->get_parameter('oauth_timestamp') - 10*60*60, false);
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$this->setExpectedException('OAuthException');
$this->server->verify_request($request);
}
public function testRejectFutureTimestamp() {
// We change the timestamp to be 10 hours in the future, it should throw an exception
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->set_parameter( 'oauth_timestamp', $request->get_parameter('oauth_timestamp') + 10*60*60, false);
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$this->setExpectedException('OAuthException');
$this->server->verify_request($request);
}
public function testRejectUsedNonce() {
// We give a known nonce and should see an exception
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
// The Mock datastore is set to say that the `nonce` nonce is known
$request->set_parameter( 'oauth_nonce', 'nonce', false);
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$this->setExpectedException('OAuthException');
$this->server->verify_request($request);
}
public function testRejectInvalidSignature() {
// We change the signature post-signing to be something invalid
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$request->set_parameter( 'oauth_signature', '__whatever__', false);
$this->setExpectedException('OAuthException');
$this->server->verify_request($request);
}
public function testRejectInvalidConsumer() {
// We use the consumer-key "unknown", which isn't known by the datastore.
$unknown_consumer = new OAuthConsumer('unknown', '__unused__');
$request = OAuthRequest::from_consumer_and_token( $unknown_consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $unknown_consumer, $this->access_token );
$this->setExpectedException('OAuthException');
$this->server->verify_request( $request );
}
public function testRejectInvalidToken() {
// We use the access-token "unknown" which isn't known by the datastore
$unknown_token = new OAuthToken('unknown', '__unused__');
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $unknown_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $unknown_token );
$this->setExpectedException('OAuthException');
$this->server->verify_request( $request );
}
public function testRejectUnknownSignatureMethod() {
// We use a server that only supports HMAC-SHA1, but requests with PLAINTEXT signature
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$server = new OAuthServer( new Mock_OAuthDataStore() );
$server->add_signature_method( $this->hmac_sha1 );
$this->setExpectedException('OAuthException');
$server->verify_request( $request );
}
public function testRejectUnknownVersion() {
// We use the version "1.0a" which isn't "1.0", so reject the request
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$request->set_parameter('oauth_version', '1.0a', false);
$this->setExpectedException('OAuthException');
$this->server->verify_request( $request );
}
public function testCreateRequestToken() {
// We request a new Request Token
$request = OAuthRequest::from_consumer_and_token( $this->consumer, NULL, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, NULL );
$token = $this->server->fetch_request_token($request);
$this->assertEquals($this->request_token, $token);
}
public function testRejectSignedRequestTokenRequest() {
// We request a new Request Token, but the request is signed with a token which should fail
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->request_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->request_token );
$this->setExpectedException('OAuthException');
$token = $this->server->fetch_request_token($request);
}
public function testCreateAccessToken() {
// We request a new Access Token
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->request_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->request_token );
$token = $this->server->fetch_access_token($request);
$this->assertEquals($this->access_token, $token);
}
public function testRejectUnsignedAccessTokenRequest() {
// We request a new Access Token, but we didn't sign the request with a Access Token
$request = OAuthRequest::from_consumer_and_token( $this->consumer, NULL, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, NULL );
$this->setExpectedException('OAuthException');
$token = $this->server->fetch_access_token($request);
}
public function testRejectAccessTokenSignedAccessTokenRequest() {
// We request a new Access Token, but the request is signed with an access token, so fail!
$request = OAuthRequest::from_consumer_and_token( $this->consumer, $this->access_token, 'POST', 'http://example.com');
$request->sign_request( $this->plaintext, $this->consumer, $this->access_token );
$this->setExpectedException('OAuthException');
$token = $this->server->fetch_access_token($request);
}
}

View File

@@ -0,0 +1,60 @@
<?php
require_once 'common.php';
require_once 'Mock_OAuthBaseStringRequest.php';
class OAuthSignatureMethodHmacSha1Test extends PHPUnit_Framework_TestCase {
private $method;
public function setUp() {
$this->method = new OAuthSignatureMethod_HMAC_SHA1();
}
public function testIdentifyAsHmacSha1() {
$this->assertEquals('HMAC-SHA1', $this->method->get_name());
}
public function testBuildSignature() {
// Tests taken from http://wiki.oauth.net/TestCases section 9.2 ("HMAC-SHA1")
$request = new Mock_OAuthBaseStringRequest('bs');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = NULL;
$this->assertEquals('egQqG5AJep5sJ7anhXju1unge2I=', $this->method->build_signature( $request, $consumer, $token) );
$request = new Mock_OAuthBaseStringRequest('bs');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = new OAuthToken('__unused__', 'ts');
$this->assertEquals('VZVjXceV7JgPq/dOTnNmEfO0Fv8=', $this->method->build_signature( $request, $consumer, $token) );
$request = new Mock_OAuthBaseStringRequest('GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26'
. 'oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26'
. 'oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal');
$consumer = new OAuthConsumer('__unused__', 'kd94hf93k423kf44');
$token = new OAuthToken('__unused__', 'pfkkdhi9sl3r4s00');
$this->assertEquals('tR3+Ty81lMeYAr/Fid0kMTYa/WM=', $this->method->build_signature( $request, $consumer, $token) );
}
public function testVerifySignature() {
// Tests taken from http://wiki.oauth.net/TestCases section 9.2 ("HMAC-SHA1")
$request = new Mock_OAuthBaseStringRequest('bs');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = NULL;
$signature = 'egQqG5AJep5sJ7anhXju1unge2I=';
$this->assertTrue( $this->method->check_signature( $request, $consumer, $token, $signature) );
$request = new Mock_OAuthBaseStringRequest('bs');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = new OAuthToken('__unused__', 'ts');
$signature = 'VZVjXceV7JgPq/dOTnNmEfO0Fv8=';
$this->assertTrue($this->method->check_signature( $request, $consumer, $token, $signature) );
$request = new Mock_OAuthBaseStringRequest('GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26'
. 'oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26'
. 'oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal');
$consumer = new OAuthConsumer('__unused__', 'kd94hf93k423kf44');
$token = new OAuthToken('__unused__', 'pfkkdhi9sl3r4s00');
$signature = 'tR3+Ty81lMeYAr/Fid0kMTYa/WM=';
$this->assertTrue($this->method->check_signature( $request, $consumer, $token, $signature) );
}
}

View File

@@ -0,0 +1,79 @@
<?php
require_once 'common.php';
require_once 'Mock_OAuthBaseStringRequest.php';
class OAuthSignatureMethodPlaintextTest extends PHPUnit_Framework_TestCase {
private $method;
public function setUp() {
$this->method = new OAuthSignatureMethod_PLAINTEXT();
}
public function testIdentifyAsPlaintext() {
$this->assertEquals('PLAINTEXT', $this->method->get_name());
}
public function testBuildSignature() {
// Tests based on from http://wiki.oauth.net/TestCases section 9.2 ("HMAC-SHA1")
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = NULL;
$this->assertEquals('cs&', $this->method->build_signature( $request, $consumer, $token) );
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = new OAuthToken('__unused__', 'ts');
$this->assertEquals('cs&ts', $this->method->build_signature( $request, $consumer, $token) );
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'kd94hf93k423kf44');
$token = new OAuthToken('__unused__', 'pfkkdhi9sl3r4s00');
$this->assertEquals('kd94hf93k423kf44&pfkkdhi9sl3r4s00', $this->method->build_signature( $request, $consumer, $token) );
// Tests taken from Chapter 9.4.1 ("Generating Signature") from the spec
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'djr9rjt0jd78jf88');
$token = new OAuthToken('__unused__', 'jjd999tj88uiths3');
$this->assertEquals('djr9rjt0jd78jf88&jjd999tj88uiths3', $this->method->build_signature( $request, $consumer, $token) );
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'djr9rjt0jd78jf88');
$token = new OAuthToken('__unused__', 'jjd99$tj88uiths3');
$this->assertEquals('djr9rjt0jd78jf88&jjd99%24tj88uiths3', $this->method->build_signature( $request, $consumer, $token) );
}
public function testVerifySignature() {
// Tests based on from http://wiki.oauth.net/TestCases section 9.2 ("HMAC-SHA1")
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = NULL;
$signature = 'cs&';
$this->assertTrue( $this->method->check_signature( $request, $consumer, $token, $signature) );
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'cs');
$token = new OAuthToken('__unused__', 'ts');
$signature = 'cs&ts';
$this->assertTrue($this->method->check_signature( $request, $consumer, $token, $signature) );
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'kd94hf93k423kf44');
$token = new OAuthToken('__unused__', 'pfkkdhi9sl3r4s00');
$signature = 'kd94hf93k423kf44&pfkkdhi9sl3r4s00';
$this->assertTrue($this->method->check_signature( $request, $consumer, $token, $signature) );
// Tests taken from Chapter 9.4.1 ("Generating Signature") from the spec
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'djr9rjt0jd78jf88');
$token = new OAuthToken('__unused__', 'jjd999tj88uiths3');
$signature = 'djr9rjt0jd78jf88&jjd999tj88uiths3';
$this->assertTrue($this->method->check_signature( $request, $consumer, $token, $signature) );
$request = new Mock_OAuthBaseStringRequest('__unused__');
$consumer = new OAuthConsumer('__unused__', 'djr9rjt0jd78jf88');
$token = new OAuthToken('__unused__', 'jjd99$tj88uiths3');
$signature = 'djr9rjt0jd78jf88&jjd99%24tj88uiths3';
$this->assertTrue($this->method->check_signature( $request, $consumer, $token, $signature) );
}
}

View File

@@ -0,0 +1,43 @@
<?php
require_once 'common.php';
require_once 'Mock_OAuthBaseStringRequest.php';
require_once 'Mock_OAuthSignatureMethod_RSA_SHA1.php';
class OAuthSignatureMethodRsaSha1Test extends PHPUnit_Framework_TestCase {
private $method;
public function setUp() {
$this->method = new Mock_OAuthSignatureMethod_RSA_SHA1();
}
public function testIdentifyAsRsaSha1() {
$this->assertEquals('RSA-SHA1', $this->method->get_name());
}
public function testBuildSignature() {
if( ! function_exists('openssl_get_privatekey') ) {
$this->markTestSkipped('OpenSSL not available, can\'t test RSA-SHA1 functionality');
}
// Tests taken from http://wiki.oauth.net/TestCases section 9.3 ("RSA-SHA1")
$request = new Mock_OAuthBaseStringRequest('GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacaction.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3D13917289812797014437%26oauth_signature_method%3DRSA-SHA1%26oauth_timestamp%3D1196666512%26oauth_version%3D1.0%26size%3Doriginal');
$consumer = new OAuthConsumer('dpf43f3p2l4k3l03', '__unused__');
$token = NULL;
$signature = 'jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=';
$this->assertEquals($signature, $this->method->build_signature( $request, $consumer, $token) );
}
public function testVerifySignature() {
if( ! function_exists('openssl_get_privatekey') ) {
$this->markTestSkipped('OpenSSL not available, can\'t test RSA-SHA1 functionality');
}
// Tests taken from http://wiki.oauth.net/TestCases section 9.3 ("RSA-SHA1")
$request = new Mock_OAuthBaseStringRequest('GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacaction.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3D13917289812797014437%26oauth_signature_method%3DRSA-SHA1%26oauth_timestamp%3D1196666512%26oauth_version%3D1.0%26size%3Doriginal');
$consumer = new OAuthConsumer('dpf43f3p2l4k3l03', '__unused__');
$token = NULL;
$signature = 'jvTp/wX1TYtByB1m+Pbyo0lnCOLIsyGCH7wke8AUs3BpnwZJtAuEJkvQL2/9n4s5wUmUl4aCI4BwpraNx4RtEXMe5qg5T1LVTGliMRpKasKsW//e+RinhejgCuzoH26dyF8iY2ZZ/5D1ilgeijhV/vBka5twt399mXwaYdCwFYE=';
$this->assertTrue($this->method->check_signature( $request, $consumer, $token, $signature) );
}
}

View File

@@ -0,0 +1,20 @@
<?php
require_once 'common.php';
class OAuthTokenTest extends PHPUnit_Framework_TestCase {
public function testSerialize() {
$token = new OAuthToken('token', 'secret');
$this->assertEquals('oauth_token=token&oauth_token_secret=secret', $token->to_string());
$token = new OAuthToken('token&', 'secret%');
$this->assertEquals('oauth_token=token%26&oauth_token_secret=secret%25', $token->to_string());
}
public function testConvertToString() {
$token = new OAuthToken('token', 'secret');
$this->assertEquals('oauth_token=token&oauth_token_secret=secret', (string) $token);
$token = new OAuthToken('token&', 'secret%');
$this->assertEquals('oauth_token=token%26&oauth_token_secret=secret%25', (string) $token);
}
}

View File

@@ -0,0 +1,151 @@
<?php
require_once dirname(__FILE__) . '/common.php';
/**
* Tests of OAuthUtil
*/
class OAuthUtilTest extends PHPUnit_Framework_TestCase {
public function testUrlencode() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Parameter Encoding")
$this->assertEquals('abcABC123', OAuthUtil::urlencode_rfc3986('abcABC123'));
$this->assertEquals('-._~', OAuthUtil::urlencode_rfc3986('-._~'));
$this->assertEquals('%25', OAuthUtil::urlencode_rfc3986('%'));
$this->assertEquals('%2B', OAuthUtil::urlencode_rfc3986('+'));
$this->assertEquals('%0A', OAuthUtil::urlencode_rfc3986("\n"));
$this->assertEquals('%20', OAuthUtil::urlencode_rfc3986(' '));
$this->assertEquals('%7F', OAuthUtil::urlencode_rfc3986("\x7F"));
//$this->assertEquals('%C2%80', OAuthUtil::urlencode_rfc3986("\x00\x80"));
//$this->assertEquals('%E3%80%81', OAuthUtil::urlencode_rfc3986("\x30\x01"));
// Last two checks disabled because of lack of UTF-8 support, or lack
// of knowledge from me (morten.fangel) on how to use it properly..
// A few tests to ensure code-coverage
$this->assertEquals( '', OAuthUtil::urlencode_rfc3986(NULL));
$this->assertEquals( '', OAuthUtil::urlencode_rfc3986(new stdClass()));
}
public function testUrldecode() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Parameter Encoding")
$this->assertEquals('abcABC123', OAuthUtil::urldecode_rfc3986('abcABC123'));
$this->assertEquals('-._~', OAuthUtil::urldecode_rfc3986('-._~'));
$this->assertEquals('%', OAuthUtil::urldecode_rfc3986('%25'));
$this->assertEquals('+', OAuthUtil::urldecode_rfc3986('%2B'));
$this->assertEquals("\n", OAuthUtil::urldecode_rfc3986('%0A'));
$this->assertEquals(' ', OAuthUtil::urldecode_rfc3986('%20'));
$this->assertEquals("\x7F", OAuthUtil::urldecode_rfc3986('%7F'));
//$this->assertEquals("\x00\x80", OAuthUtil::urldecode_rfc3986('%C2%80'));
//$this->assertEquals("\x30\x01", OAuthUtil::urldecode_rfc3986('%E3%80%81'));
// Last two checks disabled because of lack of UTF-8 support, or lack
// of knowledge from me (morten.fangel) on how to use it properly..
}
public function testParseParameter() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
$this->assertEquals(
array('name'=>''),
OAuthUtil::parse_parameters('name')
);
$this->assertEquals(
array('a'=>'b'),
OAuthUtil::parse_parameters('a=b')
);
$this->assertEquals(
array('a'=>'b','c'=>'d'),
OAuthUtil::parse_parameters('a=b&c=d')
);
$this->assertEquals(
array('a'=>array('x!y','x y')),
OAuthUtil::parse_parameters('a=x!y&a=x+y')
);
$this->assertEquals(
array('x!y'=>'a', 'x' =>'a'),
OAuthUtil::parse_parameters('x!y=a&x=a')
);
}
public function testBuildHttpQuery() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
$this->assertEquals(
'name=',
OAuthUtil::build_http_query(array('name'=>''))
);
$this->assertEquals(
'a=b',
OAuthUtil::build_http_query(array('a'=>'b'))
);
$this->assertEquals(
'a=b&c=d',
OAuthUtil::build_http_query(array('a'=>'b','c'=>'d'))
);
$this->assertEquals(
'a=x%20y&a=x%21y',
OAuthUtil::build_http_query(array('a'=>array('x!y','x y')))
);
$this->assertEquals(
'x=a&x%21y=a',
OAuthUtil::build_http_query(array('x!y'=>'a', 'x' =>'a'))
);
// Test taken from the Spec 9.1.1
$this->assertEquals(
'a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t',
OAuthUtil::build_http_query(array('a'=>'1', 'c' =>'hi there', 'f'=>array(25, 50, 'a'), 'z'=>array('p','t')))
);
// From issue 164, by hidetaka
// Based on discussion at
// http://groups.google.com/group/oauth/browse_thread/thread/7c698004be0d536/dced7b6c82b917b2?lnk=gst&q=sort#
$this->assertEquals(
'x=200&x=25&y=B&y=a',
OAuthUtil::build_http_query(array('x'=>array(25, 200), 'y'=>array('a', 'B')))
);
}
public function testSplitHeader() {
$this->assertEquals(
array('oauth_foo'=>'bar','oauth_baz'=>'bla,rgh'),
OAuthUtil::split_header('OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"')
);
$this->assertEquals(
array(),
OAuthUtil::split_header('OAuth realm="",foo=bar,baz="bla,rgh"')
);
$this->assertEquals(
array('foo'=>'bar', 'baz'=>'bla,rgh'),
OAuthUtil::split_header('OAuth realm="",foo=bar,baz="bla,rgh"', false)
);
$this->assertEquals(
array('oauth_foo' => 'hi there'),
OAuthUtil::split_header('OAuth realm="",oauth_foo=hi+there,foo=bar,baz="bla,rgh"')
);
}
public function testGetHeaders() {
if (function_exists('apache_request_headers')) {
$this->markTestSkipped('We assume the apache module is well tested. Since this module is present, no need testing our suplement');
}
$_SERVER['HTTP_HOST'] = 'foo';
$_SERVER['HTTP_X_WHATEVER'] = 'bar';
$this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar'), OAuthUtil::get_headers() );
// Test picking up the Content-Type of POST requests running as an Apache module but not having the ARH method
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar', 'Content-Type'=>'application/x-www-form-urlencoded'), OAuthUtil::get_headers() );
// Test picking up the Content-Type of POST requests when using CGI
unset($_SERVER['CONTENT_TYPE']);
$this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar'), OAuthUtil::get_headers() );
$_ENV['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar', 'Content-Type'=>'application/x-www-form-urlencoded'), OAuthUtil::get_headers() );
}
}

View File

@@ -0,0 +1,60 @@
<?php
require dirname(__FILE__).'/../init.php';
/**
* A simple utils class for methods needed
* during some of the tests
*/
class OAuthTestUtils {
private static function reset_request_vars() {
$_SERVER = array();
$_POST = array();
$_GET = array();
}
/**
* Populates $_{SERVER,GET,POST} and whatever environment-variables needed to test everything..
*
* @param string $method GET or POST
* @param string $uri What URI is the request to (eg http://example.com/foo?bar=baz)
* @param string $post_data What should the post-data be
* @param string $auth_header What to set the Authorization header to
*/
public static function build_request( $method, $uri, $post_data = '', $auth_header = '' ) {
self::reset_request_vars();
$method = strtoupper($method);
$parts = parse_url($uri);
$scheme = $parts['scheme'];
$port = isset( $parts['port'] ) && $parts['port'] ? $parts['port'] : ( $scheme === 'https' ? '443' : '80' );
$host = $parts['host'];
$path = isset( $parts['path'] ) ? $parts['path'] : NULL;
$query = isset( $parts['query'] ) ? $parts['query'] : NULL;
if( $scheme == 'https') {
$_SERVER['HTTPS'] = 'on';
}
$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['HTTP_HOST'] = $host;
$_SERVER['SERVER_NAME'] = $host;
$_SERVER['SERVER_PORT'] = $port;
$_SERVER['SCRIPT_NAME'] = $path;
$_SERVER['REQUEST_URI'] = $path . '?' . $query;
$_SERVER['QUERY_STRING'] = $query.'';
parse_str($query, $_GET);
if( $method == 'POST' ) {
$_SERVER['HTTP_CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$_POST = parse_str($post_data);
OAuthRequest::$POST_INPUT = 'data:application/x-www-form-urlencoded,'.$post_data;
}
if( $auth_header != '' ) {
$_SERVER['HTTP_AUTHORIZATION'] = $auth_header;
}
}
}