123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- <?php
- class SessionTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Session tests',
- 'description' => 'Drupal session handling tests.',
- 'group' => 'Session'
- );
- }
- function setUp() {
- parent::setUp('session_test');
- }
-
- function testSessionSaveRegenerate() {
- $this->assertFalse(drupal_save_session(), 'drupal_save_session() correctly returns FALSE (inside of testing framework) when initially called with no arguments.', 'Session');
- $this->assertFalse(drupal_save_session(FALSE), 'drupal_save_session() correctly returns FALSE when called with FALSE.', 'Session');
- $this->assertFalse(drupal_save_session(), 'drupal_save_session() correctly returns FALSE when saving has been disabled.', 'Session');
- $this->assertTrue(drupal_save_session(TRUE), 'drupal_save_session() correctly returns TRUE when called with TRUE.', 'Session');
- $this->assertTrue(drupal_save_session(), 'drupal_save_session() correctly returns TRUE when saving has been enabled.', 'Session');
-
- $user = $this->drupalCreateUser(array('access content'));
-
- $this->sessionReset($user->uid);
-
- $this->drupalLogin($user);
- $this->assertTrue(preg_match('/HttpOnly/i', $this->drupalGetHeader('Set-Cookie', TRUE)), 'Session cookie is set as HttpOnly.');
- $this->drupalLogout();
-
-
- user_save($user, array('name' => 'session_test_user'));
- $user->name = 'session_test_user';
- $this->drupalGet('session-test/id');
- $matches = array();
- preg_match('/\s*session_id:(.*)\n/', $this->drupalGetContent(), $matches);
- $this->assertTrue(!empty($matches[1]) , 'Found session ID before logging in.');
- $original_session = $matches[1];
-
-
- $edit = array(
- 'name' => $user->name,
- 'pass' => $user->pass_raw
- );
- $this->drupalPost('user', $edit, t('Log in'));
- $this->drupalGet('user');
- $pass = $this->assertText($user->name, format_string('Found name: %name', array('%name' => $user->name)), 'User login');
- $this->_logged_in = $pass;
- $this->drupalGet('session-test/id');
- $matches = array();
- preg_match('/\s*session_id:(.*)\n/', $this->drupalGetContent(), $matches);
- $this->assertTrue(!empty($matches[1]) , 'Found session ID after logging in.');
- $this->assertTrue($matches[1] != $original_session, 'Session ID changed after login.');
- }
-
- function testDataPersistence() {
- $user = $this->drupalCreateUser(array('access content'));
-
- $this->sessionReset($user->uid);
- $this->drupalLogin($user);
- $value_1 = $this->randomName();
- $this->drupalGet('session-test/set/' . $value_1);
- $this->assertText($value_1, 'The session value was stored.', 'Session');
- $this->drupalGet('session-test/get');
- $this->assertText($value_1, 'Session correctly returned the stored data for an authenticated user.', 'Session');
-
-
- $value_2 = $this->randomName();
- $this->drupalGet('session-test/no-set/' . $value_2);
- $this->assertText($value_2, 'The session value was correctly passed to session-test/no-set.', 'Session');
- $this->drupalGet('session-test/get');
- $this->assertText($value_1, 'Session data is not saved for drupal_save_session(FALSE).', 'Session');
-
- $this->sessionReset();
- $this->sessionReset($user->uid);
- $this->assertText($value_1, 'Session data persists through browser close.', 'Session');
-
- $this->drupalLogout();
- $this->sessionReset();
- $this->drupalGet('session-test/get');
- $this->assertNoText($value_1, "After logout, previous user's session data is not available.", 'Session');
-
- $value_3 = $this->randomName();
- $this->drupalGet('session-test/set/' . $value_3);
- $this->assertText($value_3, 'Session data stored for anonymous user.', 'Session');
- $this->drupalGet('session-test/get');
- $this->assertText($value_3, 'Session correctly returned the stored data for an anonymous user.', 'Session');
-
- $value_4 = $this->randomName();
- $this->drupalGet('session-test/no-set/' . $value_4);
- $this->assertText($value_4, 'The session value was correctly passed to session-test/no-set.', 'Session');
- $this->drupalGet('session-test/get');
- $this->assertText($value_3, 'Session data is not saved for drupal_save_session(FALSE).', 'Session');
-
- $this->drupalLogin($user);
- $this->sessionReset($user->uid);
- $this->drupalGet('session-test/get');
- $this->assertNoText($value_1, 'Session has persisted for an authenticated user after logging out and then back in.', 'Session');
-
- $user2 = $this->drupalCreateUser(array('access content'));
- $this->sessionReset($user2->uid);
- $this->drupalLogin($user2);
- }
-
- function testEmptyAnonymousSession() {
-
- $this->drupalGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
-
- variable_set('cache', 1);
- $this->drupalGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
- $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
-
- $this->drupalGet('session-test/set-message');
- $this->assertSessionCookie(TRUE);
- $this->assertTrue($this->drupalGetHeader('Set-Cookie'), 'New session was started.');
-
-
- $this->drupalGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(FALSE);
- $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
- $this->assertText(t('This is a dummy message.'), 'Message was displayed.');
- $this->assertTrue(preg_match('/SESS\w+=deleted/', $this->drupalGetHeader('Set-Cookie')), 'Session cookie was deleted.');
-
- $this->drupalGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
- $this->assertNoText(t('This is a dummy message.'), 'Message was not cached.');
- $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
- $this->assertFalse($this->drupalGetHeader('Set-Cookie'), 'New session was not started.');
-
- $this->drupalGet('session-test/set-message-but-dont-save');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
-
- $this->drupalGet('');
- $this->assertSessionCookie(FALSE);
- $this->assertSessionEmpty(TRUE);
- $this->assertNoText(t('This is a dummy message.'), 'The message was not saved.');
- }
-
- function testSessionWrite() {
- $user = $this->drupalCreateUser(array('access content'));
- $this->drupalLogin($user);
- $sql = 'SELECT u.access, s.timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE u.uid = :uid';
- $times1 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
-
-
-
- sleep(1);
- $this->drupalGet('session-test/set/foo');
- $times2 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertEqual($times2->access, $times1->access, 'Users table was not updated.');
- $this->assertNotEqual($times2->timestamp, $times1->timestamp, 'Sessions table was updated.');
-
- sleep(1);
- $this->drupalGet('session-test/set/foo');
- $times3 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertEqual($times3->access, $times1->access, 'Users table was not updated.');
- $this->assertEqual($times3->timestamp, $times2->timestamp, 'Sessions table was not updated.');
-
- sleep(1);
- $this->drupalGet('');
- $times4 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertEqual($times4->access, $times3->access, 'Users table was not updated.');
- $this->assertEqual($times4->timestamp, $times3->timestamp, 'Sessions table was not updated.');
-
- variable_set('session_write_interval', 0);
- $this->drupalGet('');
- $times5 = db_query($sql, array(':uid' => $user->uid))->fetchObject();
- $this->assertNotEqual($times5->access, $times4->access, 'Users table was updated.');
- $this->assertNotEqual($times5->timestamp, $times4->timestamp, 'Sessions table was updated.');
- }
-
- function testEmptySessionID() {
- $user = $this->drupalCreateUser(array('access content'));
- $this->drupalLogin($user);
- $this->drupalGet('session-test/is-logged-in');
- $this->assertResponse(200, 'User is logged in.');
-
-
- db_query("UPDATE {sessions} SET sid = '' WHERE uid = :uid", array(':uid' => $user->uid));
-
-
-
- $this->curlClose();
- $this->additionalCurlOptions[CURLOPT_COOKIE] = rawurlencode($this->session_name) . '=;';
- $this->drupalGet('session-test/id-from-cookie');
- $this->assertRaw("session_id:\n", 'Session ID is blank as sent from cookie header.');
-
- $this->drupalGet('session-test/is-logged-in');
- $this->assertResponse(403, 'An empty session ID is not allowed.');
- }
-
- function sessionReset($uid = 0) {
-
- $this->curlClose();
- $this->loggedInUser = FALSE;
-
- $this->cookieFile = file_stream_wrapper_get_instance_by_scheme('temporary')->getDirectoryPath() . '/cookie.' . $uid . '.txt';
- $this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile;
- $this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE;
- $this->drupalGet('session-test/get');
- $this->assertResponse(200, 'Session test module is correctly enabled.', 'Session');
- }
-
- function assertSessionCookie($sent) {
- if ($sent) {
- $this->assertNotNull($this->session_id, 'Session cookie was sent.');
- }
- else {
- $this->assertNull($this->session_id, 'Session cookie was not sent.');
- }
- }
-
- function assertSessionEmpty($empty) {
- if ($empty) {
- $this->assertIdentical($this->drupalGetHeader('X-Session-Empty'), '1', 'Session was empty.');
- }
- else {
- $this->assertIdentical($this->drupalGetHeader('X-Session-Empty'), '0', 'Session was not empty.');
- }
- }
- }
- class SessionHttpsTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Session HTTPS handling',
- 'description' => 'Ensure that when running under HTTPS two session cookies are generated.',
- 'group' => 'Session'
- );
- }
- public function setUp() {
- parent::setUp('session_test');
- }
- protected function testHttpsSession() {
- global $is_https;
- if ($is_https) {
- $secure_session_name = session_name();
- $insecure_session_name = substr(session_name(), 1);
- }
- else {
- $secure_session_name = 'S' . session_name();
- $insecure_session_name = session_name();
- }
- $user = $this->drupalCreateUser(array('access administration pages'));
-
-
- $this->drupalGet('user');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user');
- $edit = array('name' => $user->name, 'pass' => $user->pass_raw);
- $this->drupalPost(NULL, $edit, t('Log in'));
-
- $this->curlClose();
- $this->drupalGet('user');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user');
- $this->drupalPost(NULL, $edit, t('Log in'));
-
- $this->assertTrue($this->cookies[$secure_session_name]['secure'], 'The secure cookie has the secure attribute');
-
- $this->assertFalse(isset($this->cookies[$insecure_session_name]));
- $ssid = $this->cookies[$secure_session_name]['value'];
- $this->assertSessionIds($ssid, $ssid, 'Session has a non-empty SID and a correct secure SID.');
- $cookie = $secure_session_name . '=' . $ssid;
-
- $this->curlClose();
- $this->drupalGet($this->httpsUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertText(t('Configuration'));
- $this->assertResponse(200);
-
- $this->curlClose();
- $this->drupalGet($this->httpUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertNoText(t('Configuration'));
- $this->assertResponse(403);
-
- $this->curlClose();
- $cookie = $insecure_session_name . '=';
- $this->drupalGet($this->httpUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertResponse(403);
-
-
-
- $this->curlClose();
- $this->drupalGet('user');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpUrl('user');
- $edit = array('name' => $user->name, 'pass' => $user->pass_raw);
- $this->drupalPost(NULL, $edit, t('Log in'));
- $this->drupalGet($this->httpUrl('admin/config'));
- $this->assertResponse(200);
- $sid = $this->cookies[$insecure_session_name]['value'];
- $this->assertSessionIds($sid, '', 'Session has the correct SID and an empty secure SID.');
-
- $this->curlClose();
- $cookie = $secure_session_name . '=';
- $this->drupalGet($this->httpsUrl('admin/config'), array(), array('Cookie: ' . $cookie));
- $this->assertResponse(403);
-
- $this->cookies = array();
- if ($is_https) {
-
- return;
- }
-
- variable_set('https', TRUE);
- $this->curlClose();
-
- $session_data = $this->randomName();
- $this->drupalGet('session-test/set/' . $session_data);
-
- $this->assertFalse(isset($this->cookies[$secure_session_name]), 'The secure cookie is not sent on insecure pages.');
-
- $this->assertFalse($this->cookies[$insecure_session_name]['secure'], 'The insecure cookie does not have the secure attribute');
-
-
- $anonymous_cookie = $insecure_session_name . '=' . $this->cookies[$insecure_session_name]['value'];
-
- $this->drupalGet('user/password');
- $form = $this->xpath('//form[@id="user-pass"]');
- $this->assertNotEqual(substr($form[0]['action'], 0, 6), 'https:', 'Password request form action is not secure');
- $form[0]['action'] = $this->httpsUrl('user');
-
- $this->drupalGet('user');
- $form = $this->xpath('//form[@id="user-login"]');
- $this->assertEqual(substr($form[0]['action'], 0, 6), 'https:', 'Login form action is secure');
- $form[0]['action'] = $this->httpsUrl('user');
- $edit = array(
- 'name' => $user->name,
- 'pass' => $user->pass_raw,
- );
- $this->drupalPost(NULL, $edit, t('Log in'));
-
- $this->assertTrue($this->cookies[$secure_session_name]['secure'], 'The secure cookie has the secure attribute');
-
- $this->assertFalse($this->cookies[$insecure_session_name]['secure'], 'The insecure cookie does not have the secure attribute');
- $sid = $this->cookies[$insecure_session_name]['value'];
- $ssid = $this->cookies[$secure_session_name]['value'];
- $this->assertSessionIds($sid, $ssid, 'Session has both secure and insecure SIDs');
- $cookies = array(
- $insecure_session_name . '=' . $sid,
- $secure_session_name . '=' . $ssid,
- );
-
-
- $this->drupalGet('session-test/get');
- $this->assertText($session_data, 'Session correctly returned the stored data set by the anonymous session.');
- foreach ($cookies as $cookie_key => $cookie) {
- foreach (array('admin/config', $this->httpsUrl('admin/config')) as $url_key => $url) {
- $this->curlClose();
- $this->drupalGet($url, array(), array('Cookie: ' . $cookie));
- if ($cookie_key == $url_key) {
- $this->assertText(t('Configuration'));
- $this->assertResponse(200);
- }
- else {
- $this->assertNoText(t('Configuration'));
- $this->assertResponse(403);
- }
- }
- }
-
-
- $this->cookies = array();
- $this->drupalGet('session-test/get', array('Cookie: ' . $anonymous_cookie));
- $this->assertNoText($session_data, 'Initial anonymous session is inactive after login.');
-
- $this->cookies = array();
-
- $this->drupalGet($this->httpsUrl('session-test/set/1'));
-
- $this->drupalGet('user');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user');
- $this->drupalPost(NULL, $edit, t('Log in'));
-
- $this->drupalGet("user/{$user->uid}/edit");
- $this->assertResponse(200);
- }
-
- public function testEmptySessionId() {
- global $is_https;
- if ($is_https) {
- $secure_session_name = session_name();
- }
- else {
- $secure_session_name = 'S' . session_name();
- }
-
- variable_set('https', TRUE);
- $admin_user = $this->drupalCreateUser(array('access administration pages'));
- $standard_user = $this->drupalCreateUser(array('access content'));
-
-
-
- $edit = array(
- 'name' => $admin_user->name,
- 'pass' => $admin_user->pass_raw
- );
- $this->drupalGet('user');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpUrl('user');
- $this->drupalPost(NULL, $edit, t('Log in'));
- $this->curlClose();
-
- $edit = array(
- 'name' => $standard_user->name,
- 'pass' => $standard_user->pass_raw
- );
- $this->drupalGet('user');
- $form = $this->xpath('//form[@id="user-login"]');
- $form[0]['action'] = $this->httpsUrl('user');
- $this->drupalPost(NULL, $edit, t('Log in'));
-
- curl_setopt($this->curlHandle, CURLOPT_COOKIE, "$secure_session_name=");
- $this->drupalGet($this->httpsUrl('user'));
- $this->assertNoText($admin_user->name, 'User is not logged in as admin');
- $this->assertNoText($standard_user->name, "The user's own name is not displayed because the invalid session cookie has logged them out.");
- }
-
- protected function assertSessionIds($sid, $ssid, $assertion_text) {
- $args = array(
- ':sid' => $sid,
- ':ssid' => $ssid,
- );
- return $this->assertTrue(db_query('SELECT timestamp FROM {sessions} WHERE sid = :sid AND ssid = :ssid', $args)->fetchField(), $assertion_text);
- }
-
- protected function httpsUrl($url) {
- global $base_url;
- return $base_url . '/modules/simpletest/tests/https.php?q=' . $url;
- }
-
- protected function httpUrl($url) {
- global $base_url;
- return $base_url . '/modules/simpletest/tests/http.php?q=' . $url;
- }
- }
|