Initial Release of the calendar plugin

This commit is contained in:
Tobias Hößl 2012-06-03 18:19:28 +00:00
parent 45cc9885fc
commit 7115197a33
561 changed files with 189494 additions and 0 deletions

View file

@ -0,0 +1,240 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_HTTP_AWSAuthTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_HTTP_ResponseMock
*/
private $response;
/**
* @var Sabre_HTTP_AWSAuth
*/
private $auth;
const REALM = 'SabreDAV unittest';
public function setUp() {
$this->response = new Sabre_HTTP_ResponseMock();
$this->auth = new Sabre_HTTP_AWSAuth();
$this->auth->setRealm(self::REALM);
$this->auth->setHTTPResponse($this->response);
}
public function testNoHeader() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
));
$this->auth->setHTTPRequest($request);
$result = $this->auth->init();
$this->assertFalse($result,'No AWS Authorization header was supplied, so we should have gotten false');
$this->assertEquals(Sabre_HTTP_AWSAuth::ERR_NOAWSHEADER,$this->auth->errorCode);
}
public function testIncorrectContentMD5() {
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
'HTTP_CONTENT_MD5' => 'garbage',
'REQUEST_URI' => '/',
));
$this->auth->setHTTPRequest($request);
$this->auth->init();
$result = $this->auth->validate($secretKey);
$this->assertFalse($result);
$this->assertEquals(Sabre_HTTP_AWSAuth::ERR_MD5CHECKSUMWRONG,$this->auth->errorCode);
}
public function testNoDate() {
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content,true));
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'POST',
'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
'HTTP_CONTENT_MD5' => $contentMD5,
));
$request->setBody($content);
$this->auth->setHTTPRequest($request);
$this->auth->init();
$result = $this->auth->validate($secretKey);
$this->assertFalse($result);
$this->assertEquals(Sabre_HTTP_AWSAuth::ERR_INVALIDDATEFORMAT,$this->auth->errorCode);
}
public function testFutureDate() {
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content,true));
$date = new DateTime('@' . (time() + (60*20)));
$date->setTimeZone(new DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'POST',
'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
'HTTP_CONTENT_MD5' => $contentMD5,
'HTTP_DATE' => $date,
));
$request->setBody($content);
$this->auth->setHTTPRequest($request);
$this->auth->init();
$result = $this->auth->validate($secretKey);
$this->assertFalse($result);
$this->assertEquals(Sabre_HTTP_AWSAuth::ERR_REQUESTTIMESKEWED,$this->auth->errorCode);
}
public function testPastDate() {
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content,true));
$date = new DateTime('@' . (time() - (60*20)));
$date->setTimeZone(new DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'POST',
'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
'HTTP_CONTENT_MD5' => $contentMD5,
'HTTP_X_AMZ_DATE' => $date,
));
$request->setBody($content);
$this->auth->setHTTPRequest($request);
$this->auth->init();
$result = $this->auth->validate($secretKey);
$this->assertFalse($result);
$this->assertEquals(Sabre_HTTP_AWSAuth::ERR_REQUESTTIMESKEWED,$this->auth->errorCode);
}
public function testIncorrectSignature() {
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content,true));
$date = new DateTime('now');
$date->setTimeZone(new DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'POST',
'HTTP_AUTHORIZATION' => "AWS $accessKey:sig",
'HTTP_CONTENT_MD5' => $contentMD5,
'HTTP_X_AMZ_DATE' => $date,
'REQUEST_URI' => '/',
));
$request->setBody($content);
$this->auth->setHTTPRequest($request);
$this->auth->init();
$result = $this->auth->validate($secretKey);
$this->assertFalse($result);
$this->assertEquals(Sabre_HTTP_AWSAuth::ERR_INVALIDSIGNATURE,$this->auth->errorCode);
}
public function testValidRequest() {
$accessKey = 'accessKey';
$secretKey = 'secretKey';
$content = 'thisisthebody';
$contentMD5 = base64_encode(md5($content,true));
$date = new DateTime('now');
$date->setTimeZone(new DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');
$sig = base64_encode($this->hmacsha1($secretKey,
"POST\n$contentMD5\n\n$date\nx-amz-date:$date\n/evert"
));
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'POST',
'HTTP_AUTHORIZATION' => "AWS $accessKey:$sig",
'HTTP_CONTENT_MD5' => $contentMD5,
'HTTP_X_AMZ_DATE' => $date,
'REQUEST_URI' => '/evert',
));
$request->setBody($content);
$this->auth->setHTTPRequest($request);
$this->auth->init();
$result = $this->auth->validate($secretKey);
$this->assertTrue($result,'Signature did not validate, got errorcode ' . $this->auth->errorCode);
$this->assertEquals($accessKey,$this->auth->getAccessKey());
}
public function test401() {
$this->auth->requireLogin();
$test = preg_match('/^AWS$/',$this->response->headers['WWW-Authenticate'],$matches);
$this->assertTrue($test==true,'The WWW-Authenticate response didn\'t match our pattern');
}
/**
* Generates an HMAC-SHA1 signature
*
* @param string $key
* @param string $message
* @return string
*/
private function hmacsha1($key, $message) {
$blocksize=64;
if (strlen($key)>$blocksize)
$key=pack('H*', sha1($key));
$key=str_pad($key,$blocksize,chr(0x00));
$ipad=str_repeat(chr(0x36),$blocksize);
$opad=str_repeat(chr(0x5c),$blocksize);
$hmac = pack('H*',sha1(($key^$opad).pack('H*',sha1(($key^$ipad).$message))));
return $hmac;
}
}

View file

@ -0,0 +1,111 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_HTTP_BasicAuthTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_HTTP_ResponseMock
*/
private $response;
/**
* @var Sabre_HTTP_BasicAuth
*/
private $basicAuth;
function setUp() {
$this->response = new Sabre_HTTP_ResponseMock();
$this->basicAuth = new Sabre_HTTP_BasicAuth();
$this->basicAuth->setHTTPResponse($this->response);
}
function testGetUserPassApache() {
$server = array(
'PHP_AUTH_USER' => 'admin',
'PHP_AUTH_PW' => '1234',
);
$request = new Sabre_HTTP_Request($server);
$this->basicAuth->setHTTPRequest($request);
$userPass = $this->basicAuth->getUserPass();
$this->assertEquals(
array('admin','1234'),
$userPass,
'We did not get the username and password we expected'
);
}
function testGetUserPassIIS() {
$server = array(
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('admin:1234'),
);
$request = new Sabre_HTTP_Request($server);
$this->basicAuth->setHTTPRequest($request);
$userPass = $this->basicAuth->getUserPass();
$this->assertEquals(
array('admin','1234'),
$userPass,
'We did not get the username and password we expected'
);
}
function testGetUserPassApacheEdgeCase() {
$server = array(
'REDIRECT_HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('admin:1234'),
);
$request = new Sabre_HTTP_Request($server);
$this->basicAuth->setHTTPRequest($request);
$userPass = $this->basicAuth->getUserPass();
$this->assertEquals(
array('admin','1234'),
$userPass,
'We did not get the username and password we expected'
);
}
function testGetUserPassNothing() {
$this->assertEquals(
false,
$this->basicAuth->getUserPass()
);
}
function testRequireLogin() {
$this->basicAuth->requireLogin();
$this->assertEquals('SabreDAV',$this->basicAuth->getRealm());
$this->assertEquals(
'HTTP/1.1 401 Unauthorized',
$this->response->status,
'We expected a 401 status to be set'
);
$this->assertEquals(
'Basic realm="SabreDAV"',
$this->response->headers['WWW-Authenticate'],
'The WWW-Autenticate header was not set!'
);
}
}

View file

@ -0,0 +1,226 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_HTTP_DigestAuthTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_HTTP_ResponseMock
*/
private $response;
/**
* @var Sabre_HTTP_DigestAuth
*/
private $auth;
const REALM = 'SabreDAV unittest';
public function setUp() {
$this->response = new Sabre_HTTP_ResponseMock();
$this->auth = new Sabre_HTTP_DigestAuth();
$this->auth->setRealm(self::REALM);
$this->auth->setHTTPResponse($this->response);
}
public function testDigest() {
list($nonce,$opaque) = $this->getServerTokens();
$username = 'admin';
$password = 12345;
$nc = '00002';
$cnonce = uniqid();
$digestHash = md5(
md5($username . ':' . self::REALM . ':' . $password) . ':' .
$nonce . ':' .
$nc . ':' .
$cnonce . ':' .
'auth:' .
md5('GET' . ':' . '/')
);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
'PHP_AUTH_DIGEST' => 'username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
));
$this->auth->setHTTPRequest($request);
$this->auth->init();
$this->assertEquals($username,$this->auth->getUserName());
$this->assertEquals(self::REALM,$this->auth->getRealm());
$this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
$this->assertTrue($this->auth->validatePassword($password),'Authentication is deemed invalid through validatePassword');
}
public function testDigestCGIFormat() {
list($nonce,$opaque) = $this->getServerTokens();
$username = 'admin';
$password = 12345;
$nc = '00002';
$cnonce = uniqid();
$digestHash = md5(
md5($username . ':' . self::REALM . ':' . $password) . ':' .
$nonce . ':' .
$nc . ':' .
$cnonce . ':' .
'auth:' .
md5('GET' . ':' . '/')
);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
'HTTP_AUTHORIZATION' => 'Digest username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
));
$this->auth->setHTTPRequest($request);
$this->auth->init();
$this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
$this->assertTrue($this->auth->validatePassword($password),'Authentication is deemed invalid through validatePassword');
}
public function testDigestApacheEdgeCase() {
list($nonce,$opaque) = $this->getServerTokens();
$username = 'admin';
$password = 12345;
$nc = '00002';
$cnonce = uniqid();
$digestHash = md5(
md5($username . ':' . self::REALM . ':' . $password) . ':' .
$nonce . ':' .
$nc . ':' .
$cnonce . ':' .
'auth:' .
md5('GET' . ':' . '/')
);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
'REDIRECT_HTTP_AUTHORIZATION' => 'Digest username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
));
$this->auth->setHTTPRequest($request);
$this->auth->init();
$this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
$this->assertTrue($this->auth->validatePassword($password),'Authentication is deemed invalid through validatePassword');
}
public function testInvalidDigest() {
list($nonce,$opaque) = $this->getServerTokens();
$username = 'admin';
$password = 12345;
$nc = '00002';
$cnonce = uniqid();
$digestHash = md5(
md5($username . ':' . self::REALM . ':' . $password) . ':' .
$nonce . ':' .
$nc . ':' .
$cnonce . ':' .
'auth:' .
md5('GET' . ':' . '/')
);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
'PHP_AUTH_DIGEST' => 'username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth,nc='.$nc.',cnonce="' . $cnonce . '"',
));
$this->auth->setHTTPRequest($request);
$this->auth->init();
$this->assertFalse($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . ($password . 'randomness'))),'Authentication is deemed invalid through validateA1');
}
public function testInvalidDigest2() {
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'GET',
'HTTP_AUTHORIZATION' => 'basic blablabla',
));
$this->auth->setHTTPRequest($request);
$this->auth->init();
$this->assertFalse($this->auth->validateA1(md5('user:realm:password')));
}
public function testDigestAuthInt() {
$this->auth->setQOP(Sabre_HTTP_DigestAuth::QOP_AUTHINT | Sabre_HTTP_DigestAuth::QOP_AUTH);
list($nonce,$opaque) = $this->getServerTokens(Sabre_HTTP_DigestAuth::QOP_AUTHINT| Sabre_HTTP_DigestAuth::QOP_AUTH);
$username = 'admin';
$password = 12345;
$nc = '00003';
$cnonce = uniqid();
$digestHash = md5(
md5($username . ':' . self::REALM . ':' . $password) . ':' .
$nonce . ':' .
$nc . ':' .
$cnonce . ':' .
'auth-int:' .
md5('POST' . ':' . '/' . ':' . md5('body'))
);
$request = new Sabre_HTTP_Request(array(
'REQUEST_METHOD' => 'POST',
'PHP_AUTH_DIGEST' => 'username="'.$username.'", realm="' . self::REALM . '", nonce="' . $nonce . '", uri="/", response="' . $digestHash . '", opaque="' . $opaque . '", qop=auth-int,nc='.$nc.',cnonce="' . $cnonce . '"',
));
$request->setBody('body');
$this->auth->setHTTPRequest($request);
$this->auth->init();
$this->assertTrue($this->auth->validateA1(md5($username . ':' . self::REALM . ':' . $password)),'Authentication is deemed invalid through validateA1');
}
private function getServerTokens($qop = Sabre_HTTP_DigestAuth::QOP_AUTH) {
$this->auth->requireLogin();
switch($qop) {
case Sabre_HTTP_DigestAuth::QOP_AUTH : $qopstr='auth'; break;
case Sabre_HTTP_DigestAuth::QOP_AUTHINT : $qopstr='auth-int'; break;
default : $qopstr='auth,auth-int'; break;
}
$test = preg_match('/Digest realm="'.self::REALM.'",qop="'.$qopstr.'",nonce="([0-9a-f]*)",opaque="([0-9a-f]*)"/',
$this->response->headers['WWW-Authenticate'],$matches);
$this->assertTrue($test==true,'The WWW-Authenticate response didn\'t match our pattern. We received: ' . $this->response->headers['WWW-Authenticate']);
$nonce = $matches[1];
$opaque = $matches[2];
// Reset our environment
$this->setUp();
$this->auth->setQOP($qop);
return array($nonce,$opaque);
}
}

View file

@ -0,0 +1,148 @@
<?php
/**
* @covers Sabre_HTTP_Request
*/
class Sabre_HTTP_RequestTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_HTTP_Request
*/
private $request;
function setUp() {
$server = array(
'HTTP_HOST' => 'www.example.org',
'REQUEST_METHOD' => 'PUT',
'REQUEST_URI' => '/testuri/',
'CONTENT_TYPE' => 'text/xml',
);
$this->request = new Sabre_HTTP_Request($server);
}
function testGetHeader() {
$this->assertEquals('www.example.org', $this->request->getHeader('Host'));
$this->assertEquals('text/xml', $this->request->getHeader('Content-Type'));
}
function testGetNonExistantHeader() {
$this->assertNull($this->request->getHeader('doesntexist'));
$this->assertNull($this->request->getHeader('Content-Length'));
}
function testGetHeaders() {
$expected = array(
'host' => 'www.example.org',
'content-type' => 'text/xml',
);
$this->assertEquals($expected, $this->request->getHeaders());
}
function testGetMethod() {
$this->assertEquals('PUT', $this->request->getMethod(), 'It seems as if we didn\'t get a valid HTTP Request method back');
}
function testGetUri() {
$this->assertEquals('/testuri/', $this->request->getUri(), 'We got an invalid uri back');
}
function testSetGetBody() {
$h = fopen('php://memory','r+');
fwrite($h,'testing');
rewind($h);
$this->request->setBody($h);
$this->assertEquals('testing',$this->request->getBody(true),'We didn\'t get our testbody back');
}
function testSetGetBodyStream() {
$h = fopen('php://memory','r+');
fwrite($h,'testing');
rewind($h);
$this->request->setBody($h);
$this->assertEquals('testing',stream_get_contents($this->request->getBody()),'We didn\'t get our testbody back');
}
function testDefaultInputStream() {
$h = fopen('php://memory','r+');
fwrite($h,'testing');
rewind($h);
$previousValue = Sabre_HTTP_Request::$defaultInputStream;
Sabre_HTTP_Request::$defaultInputStream = $h;
$this->assertEquals('testing',$this->request->getBody(true),'We didn\'t get our testbody back');
Sabre_HTTP_Request::$defaultInputStream = $previousValue;
}
function testGetAbsoluteUri() {
$s = array(
'HTTP_HOST' => 'sabredav.org',
'REQUEST_URI' => '/foo'
);
$r = new Sabre_HTTP_Request($s);
$this->assertEquals('http://sabredav.org/foo', $r->getAbsoluteUri());
$s = array(
'HTTP_HOST' => 'sabredav.org',
'REQUEST_URI' => '/foo',
'HTTPS' => 'on',
);
$r = new Sabre_HTTP_Request($s);
$this->assertEquals('https://sabredav.org/foo', $r->getAbsoluteUri());
}
function testGetQueryString() {
$s = array(
'QUERY_STRING' => 'bla',
);
$r = new Sabre_HTTP_Request($s);
$this->assertEquals('bla', $r->getQueryString());
$s = array();
$r = new Sabre_HTTP_Request($s);
$this->assertEquals('', $r->getQueryString());
}
function testGetPostVars() {
$post = array(
'bla' => 'foo',
);
$r = new Sabre_HTTP_Request(array(),$post);
$this->assertEquals($post, $r->getPostVars('bla'));
}
}

View file

@ -0,0 +1,27 @@
<?php
class Sabre_HTTP_ResponseMock extends Sabre_HTTP_Response {
public $headers = array();
public $status = '';
public $body = '';
function setHeader($name,$value,$overwrite = true) {
$this->headers[$name] = $value;
}
function sendStatus($code) {
$this->status = $this->getStatusMessage($code);
}
function sendBody($body) {
$this->body = $body;
}
}

View file

@ -0,0 +1,61 @@
<?php
require_once 'Sabre/HTTP/ResponseMock.php';
class Sabre_HTTP_ResponseTest extends PHPUnit_Framework_TestCase {
/**
* @var Sabre_HTTP_ResponseMock
*/
private $response;
function setUp() {
$this->response = new Sabre_HTTP_ResponseMock();
}
function testGetStatusMessage() {
$msg = $this->response->getStatusMessage(200);
$this->assertEquals('HTTP/1.1 200 OK',$msg);
}
function testSetHeader() {
$this->response->setHeader('Content-Type','text/html');
$this->assertEquals('text/html', $this->response->headers['Content-Type']);
}
function testSendStatus() {
$this->response->sendStatus(404);
$this->assertEquals('HTTP/1.1 404 Not Found', $this->response->status);
}
function testSendBody() {
ob_start();
$response = new Sabre_HTTP_Response();
$response->sendBody('hello');
$this->assertEquals('hello',ob_get_clean());
}
function testSendBodyStream() {
ob_start();
$stream = fopen('php://memory','r+');
fwrite($stream,'hello');
rewind($stream);
$response = new Sabre_HTTP_Response();
$response->sendBody($stream);
$this->assertEquals('hello',ob_get_clean());
}
}

View file

@ -0,0 +1,64 @@
<?php
class Sabre_Util_UtilTest extends PHPUnit_Framework_TestCase {
function testParseHTTPDate() {
$times = array(
'Wed, 13 Oct 2010 10:26:00 GMT',
'Wednesday, 13-Oct-10 10:26:00 GMT',
'Wed Oct 13 10:26:00 2010',
);
$expected = 1286965560;
foreach($times as $time) {
$result = Sabre_HTTP_Util::parseHTTPDate($time);
$this->assertEquals($expected, $result->format('U'));
}
$result = Sabre_HTTP_Util::parseHTTPDate('Wed Oct 6 10:26:00 2010');
$this->assertEquals(1286360760, $result->format('U'));
}
function testParseHTTPDateFail() {
$times = array(
//random string
'NOW',
// not-GMT timezone
'Wednesday, 13-Oct-10 10:26:00 UTC',
// No space before the 6
'Wed Oct 6 10:26:00 2010',
);
foreach($times as $time) {
$this->assertFalse(Sabre_HTTP_Util::parseHTTPDate($time), 'We used the string: ' . $time);
}
}
function testTimezones() {
$default = date_default_timezone_get();
date_default_timezone_set('Europe/Amsterdam');
$this->testParseHTTPDate();
date_default_timezone_set($default);
}
function testToHTTPDate() {
$dt = new DateTime('2011-12-10 12:00:00 +0200');
$this->assertEquals(
'Sat, 10 Dec 2011 10:00:00 GMT',
Sabre_HTTP_Util::toHTTPDate($dt)
);
}
}

View file

@ -0,0 +1,15 @@
<?php
class Sabre_HTTP_VersionTest extends PHPUnit_Framework_TestCase {
function testString() {
$v = Sabre_HTTP_Version::VERSION;
$this->assertEquals(-1, version_compare('1.0.0',$v));
$s = Sabre_HTTP_Version::STABILITY;
$this->assertTrue($s == 'alpha' || $s == 'beta' || $s =='stable');
}
}