<?php
class Test_Of_Orga_Server_Cert extends WebTestCase {

	function Test_Of_Login() {
		$this->WebTestCase('Login Tests');
	}

	function testOrgServerCert() {

		//echo "tLu 01";	  
		$this->get('http://'.BASE_URL);
		// echo "tLu 011";
		$this->assertWantedText('Login');
		// echo "tLu 012";
		$this->clickLink('Normal Login'); // Password Login
		//echo "tLu 013";
		$this->assertWantedText('Pass Phrase:');

		//echo "tLu 02\n";

		$this->setField('email', NORMAL_ORG_USER1);
		$this->setField('pword', NORMAL_ORG_USER1_PW);
		$this->clickSubmit('Login');

		//echo "tLu 021\n";
		$this->assertWantedText('My Details');
		$this->assertWantedText('Logout');

		//echo "tLu 02";

		$this->clickLink('+ Server Certificates');
		$this->clickLink('New', 3); // New Org Server Cert

		$this->assertWantedText('Acceptable Use');

		//echo "tLu 03";

		$this->setField('rootcert', '1'); // Class 1
		$this->setField('oldid', '20');

		$this->setField('CSR', "-----BEGIN CERTIFICATE REQUEST-----\nMIIBoTCCAQoCAQAwYTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUx\nITAfBgNVBAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEaMBgGA1UEAxMRd3d3\nLmZ1dHVyZXdhcmUuYXQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJ37N4o2\n9W9HFDiu3WA+Nl3X0xLKgMvrRK2eogxlXDSZJs1DOrvdaoLo4jHGwygTlkRj3LrD\n3i+m1SSYIrnUyGetyAjBkJK42dXzHX22/klJSBzTF+ozcoIjvT+hL7wHDDt8IvEj\nyRN2ZnYvPmjLCynF6EKlRrKxlNA9wt1OQt5LAgMBAAGgADANBgkqhkiG9w0BAQUF\nAAOBgQCQhAU2Wpappz8dW07OCIp7WoSvy4H1Jss5wybC5Sd4Xeug1gi1+yMw3AAe\nFngact1rkXQW91+zurdrLKBXpuMUp9m9NgYjMPHYoIs+B7zSihOC5yVsC9JLqYc9\n++TPj13hh61nNX/7DWptyTD+1yUER3eaNFXmGCjyVqBf4Vtmmg==\n-----END CERTIFICATE REQUEST-----\n");

		//echo "Jetzt wirds spannend ...\n";
		$this->clickSubmit('Submit');

		$this->assertNoUnwantedText('hit the back button and try again');
		$this->assertNoUnwantedText('Acceptable Use');
		$this->assertNoUnwantedText('CommonName field was blank', 'CSR is not correct !!!');
		$this->assertWantedText('Please make sure the following');

		$this->assertWantedText(ORG_1_CN);
		$this->assertWantedText(ORG_1_C);
		$this->assertWantedText(ORG_1_L);
		$this->assertWantedText(ORG_1_O);

		$this->setField('oldid', '21');

		$stringpage = $this->clickSubmit('Submit');

		$this->assertWantedText('BEGIN CERTIFICATE');

		$p1 = strpos($stringpage, '-----BEGIN CERTIFICATE-----');
		$p2 = strpos($stringpage, '-----END CERTIFICATE-----') + 25;

		// grab the certificate
		$cert = substr($stringpage, $p1, $p2 - $p1);

		// get the DN from the certificate using openssl

		$handle1 = fopen("cert1.crt", "w");
		fwrite($handle1, $cert);
		fclose($handle1);

		// fetch the DN of the cert
		$cmdline = "openssl x509 -in cert1.crt -noout -subject >cert1.txt";

		exec($cmdline);

		unlink("cert1.crt");

		$handle2 = fopen("cert1.txt", 'r');

		$resultstring = fread($handle2, 512);

		fclose($handle2); // this removes the file
		unlink("cert1.txt");

		echo $resultstring;

		$value = strpos($resultstring, ORG_1_CN);
		
		$value1 = strpos($resultstring, ORG_1_C);
		$value2 = strpos($resultstring, ORG_1_L);
		$value3 = strpos($resultstring, ORG_1_O);
		
		//    echo "Value: $value\n";

		$this->assertTrue($value, 'Common Name is not matching !');
		$this->assertTrue($value1, 'Country name is not matching !');
		$this->assertTrue($value2, 'Localisation name is not matching !');
		$this->assertTrue($value3, 'Organisation name is not matching !');

	}
}
?>