Skip to content

Commit 8fe3ab4

Browse files
Add tests and LogoutResponse
1 parent 9272165 commit 8fe3ab4

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

lib/Saml2/LogoutRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public static function getID($request)
160160
} else {
161161
$dom = new DOMDocument();
162162
$dom = OneLogin_Saml2_Utils::loadXML($dom, $request);
163-
163+
164164
if (false === $dom) {
165165
throw new OneLogin_Saml2_Error(
166166
"XML is invalid",

lib/Saml2/LogoutResponse.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class OneLogin_Saml2_LogoutResponse
4242
*
4343
* @param OneLogin_Saml2_Settings $settings Settings.
4444
* @param string|null $response An UUEncoded SAML Logout response from the IdP.
45+
*
46+
* @throws OneLogin_Saml2_Error
4547
*/
4648
public function __construct(OneLogin_Saml2_Settings $settings, $response = null)
4749
{
@@ -63,6 +65,13 @@ public function __construct(OneLogin_Saml2_Settings $settings, $response = null)
6365
$this->document = new DOMDocument();
6466
$this->document = OneLogin_Saml2_Utils::loadXML($this->document, $this->_logoutResponse);
6567

68+
if (false === $this->document) {
69+
throw new OneLogin_Saml2_Error(
70+
"XML is invalid",
71+
OneLogin_Saml2_Error::SAML_LOGOUTRESPONSE_INVALID
72+
);
73+
}
74+
6675
if ($this->document->documentElement->hasAttribute('ID')) {
6776
$this->id = $this->document->documentElement->getAttribute('ID');
6877
}

lib/Saml2/Utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ public static function parseDuration($duration, $timestamp = null)
727727

728728
/* Parse the duration. We use a very strict pattern. */
729729
$durationRegEx = '#^(-?)P(?:(?:(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)S)?)?)|(?:(\\d+)W))$#D';
730-
$matches = array();
730+
$matches = array();
731731
if (!preg_match($durationRegEx, $duration, $matches)) {
732732
throw new Exception('Invalid ISO 8601 duration: ' . $duration);
733733
}

tests/src/OneLogin/Saml2/LogoutRequestTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function testCreateDeflatedSAMLLogoutRequestURLParameter()
265265
/**
266266
* Tests the OneLogin_Saml2_LogoutRequest Constructor.
267267
* Case: Able to generate encryptedID with MultiCert
268-
*
268+
*
269269
* @covers OneLogin_Saml2_LogoutRequest
270270
*/
271271
public function testConstructorEncryptIdUsingX509certMulti()
@@ -828,7 +828,7 @@ public function testGetXML()
828828
$logoutRequest = new OneLogin_Saml2_LogoutRequest($settings);
829829
$xml = $logoutRequest->getXML();
830830
$this->assertRegExp('#^<samlp:LogoutRequest#', $xml);
831-
831+
832832
$logoutRequestProcessed = new OneLogin_Saml2_LogoutRequest($settings, base64_encode($xml));
833833
$xml2 = $logoutRequestProcessed->getXML();
834834
$this->assertRegExp('#^<samlp:LogoutRequest#', $xml2);
@@ -849,9 +849,28 @@ public function testGetID()
849849
$xml = $logoutRequest->getXML();
850850
$id1 = OneLogin_Saml2_LogoutRequest::getID($xml);
851851
$this->assertNotNull($id1);
852-
852+
853853
$logoutRequestProcessed = new OneLogin_Saml2_LogoutRequest($settings, base64_encode($xml));
854854
$id2 = $logoutRequestProcessed->id;
855855
$this->assertEquals($id1, $id2);
856856
}
857+
858+
/**
859+
* Tests that the LogoutRequest throws an exception
860+
*
861+
* @covers OneLogin_Saml2_LogoutRequest::getID()
862+
*
863+
* @expectedException OneLogin_Saml2_Error
864+
* @expectedExceptionMessage XML is invalid
865+
*/
866+
public function testGetIDException()
867+
{
868+
$settingsDir = TEST_ROOT .'/settings/';
869+
include $settingsDir.'settings1.php';
870+
871+
$settings = new OneLogin_Saml2_Settings($settingsInfo);
872+
$logoutRequest = new OneLogin_Saml2_LogoutRequest($settings);
873+
$xml = $logoutRequest->getXML();
874+
$id1 = OneLogin_Saml2_LogoutRequest::getID($xml.'<garbage>');
875+
}
857876
}

tests/src/OneLogin/Saml2/LogoutResponseTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public function testWeCanChooseToDeflateAResponseBody()
437437
//Test that we can choose not to compress the request payload.
438438
$settingsDir = TEST_ROOT .'/settings/';
439439
include $settingsDir.'settings1.php';
440-
440+
441441
//Compression is currently turned on in settings.
442442
$settings = new OneLogin_Saml2_Settings($settingsInfo);
443443
$logoutResponse = new OneLogin_Saml2_LogoutResponse($settings, $message);
@@ -448,7 +448,7 @@ public function testWeCanChooseToDeflateAResponseBody()
448448
//Test that we can choose not to compress the request payload.
449449
$settingsDir = TEST_ROOT .'/settings/';
450450
include $settingsDir.'settings2.php';
451-
451+
452452
//Compression is currently turned on in settings.
453453
$settings = new OneLogin_Saml2_Settings($settingsInfo);
454454
$logoutResponse = new OneLogin_Saml2_LogoutResponse($settings, $message);
@@ -497,9 +497,26 @@ public function testGetID()
497497
$xml = $logoutResponse->getXML();
498498
$id1 = $logoutResponse->getID();
499499
$this->assertNotNull($id1);
500-
500+
501501
$processedLogoutResponse = new OneLogin_Saml2_LogoutResponse($settings, base64_encode($xml));
502502
$id2 = $processedLogoutResponse->getID();
503503
$this->assertEquals($id1, $id2);
504504
}
505+
506+
/**
507+
* Tests that the LogoutRequest throws an exception
508+
*
509+
* @covers OneLogin_Saml2_LogoutRequest::getID()
510+
*
511+
* @expectedException OneLogin_Saml2_Error
512+
* @expectedExceptionMessage XML is invalid
513+
*/
514+
public function testGetIDException()
515+
{
516+
$settingsDir = TEST_ROOT .'/settings/';
517+
include $settingsDir.'settings1.php';
518+
519+
$settings = new OneLogin_Saml2_Settings($settingsInfo);
520+
$logoutResponse = new OneLogin_Saml2_LogoutResponse($settings, '<garbage>');
521+
}
505522
}

0 commit comments

Comments
 (0)