📄 security.test.php
字号:
$this->Controller->Security->requireDelete('deleted'); $this->Controller->Security->startup($this->Controller); $this->assertFalse($this->Controller->failed); }/** * testRequireDeleteSucceedWrongMethod method * * @access public * @return void */ function testRequireDeleteSucceedWrongMethod() { $_SERVER['REQUEST_METHOD'] = 'POST'; $this->Controller->action = 'posted'; $this->Controller->Security->requireDelete('deleted'); $this->Controller->Security->startup($this->Controller); $this->assertFalse($this->Controller->failed); }/** * testRequireLoginSettings method * * @access public * @return void */ function testRequireLoginSettings() { $this->Controller->Security->requireLogin( 'add', 'edit', array('type' => 'basic', 'users' => array('admin' => 'password')) ); $this->assertEqual($this->Controller->Security->requireLogin, array('add', 'edit')); $this->assertEqual($this->Controller->Security->loginUsers, array('admin' => 'password')); }/** * testRequireLoginAllActions method * * @access public * @return void */ function testRequireLoginAllActions() { $this->Controller->Security->requireLogin( array('type' => 'basic', 'users' => array('admin' => 'password')) ); $this->assertEqual($this->Controller->Security->requireLogin, array('*')); $this->assertEqual($this->Controller->Security->loginUsers, array('admin' => 'password')); }/** * testValidatePostNoModel method * * @access public * @return void */ function testValidatePostNoModel() { $this->Controller->Security->startup($this->Controller); $key = $this->Controller->params['_Token']['key']; $data['anything'] = 'some_data'; $data['__Token']['key'] = $key; $fields = $this->__sortFields(array('anything', '__Token' => array('key' => $key))); $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $data['__Token']['fields'] = $fields; $this->Controller->data = $data; $result = $this->Controller->Security->__validatePost($this->Controller); $this->assertTrue($result); $this->assertEqual($this->Controller->data, $data); }/** * testValidatePostSimple method * * @access public * @return void */ function testValidatePostSimple() { $this->Controller->Security->startup($this->Controller); $key = $this->Controller->params['_Token']['key']; $data['Model']['username'] = ''; $data['Model']['password'] = ''; $data['__Token']['key'] = $key; $fields = array('Model' => array('username','password'), '__Token' => array('key' => $key)); $fields = $this->__sortFields($fields); $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $data['__Token']['fields'] = $fields; $this->Controller->data = $data; $result = $this->Controller->Security->__validatePost($this->Controller); $this->assertTrue($result); $this->assertEqual($this->Controller->data, $data); }/** * testValidatePostCheckbox method * * @access public * @return void */ function testValidatePostCheckbox() { $this->Controller->Security->startup($this->Controller); $key = $this->Controller->params['_Token']['key']; $data['Model']['username'] = ''; $data['Model']['password'] = ''; $data['_Model']['valid'] = '0'; $data['__Token']['key'] = $key; $fields = array( 'Model' => array('username', 'password', 'valid'), '_Model' => array('valid' => '0'), '__Token' => array('key' => $key) ); $fields = $this->__sortFields($fields); $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $data['__Token']['fields'] = $fields; $this->Controller->data = $data; $result = $this->Controller->Security->__validatePost($this->Controller); $this->assertTrue($result); unset($data['_Model']); $data['Model']['valid'] = '0'; $this->assertEqual($this->Controller->data, $data); }/** * testValidatePostHidden method * * @access public * @return void */ function testValidatePostHidden() { $this->Controller->Security->startup($this->Controller); $key = $this->Controller->params['_Token']['key']; $data['Model']['username'] = ''; $data['Model']['password'] = ''; $data['_Model']['hidden'] = '0'; $data['__Token']['key'] = $key; $fields = array( 'Model' => array('username', 'password', 'hidden'), '_Model' => array('hidden' => '0'), '__Token' => array('key' => $key) ); $fields = $this->__sortFields($fields); $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $data['__Token']['fields'] = $fields; $this->Controller->data = $data; $result = $this->Controller->Security->__validatePost($this->Controller); $this->assertTrue($result); unset($data['_Model']); $data['Model']['hidden'] = '0'; $this->assertTrue($this->Controller->data == $data); }/** * testValidatePostWithDisabledFields method * * @access public * @return void */ function testValidatePostWithDisabledFields() { $this->Controller->Security->startup($this->Controller); $this->Controller->Security->disabledFields = array('Model.username', 'Model.password'); $key = $this->Controller->params['_Token']['key']; $data['Model']['username'] = ''; $data['Model']['password'] = ''; $data['_Model']['hidden'] = '0'; $data['__Token']['key'] = $key; $fields = array( 'Model' => array('hidden'), '_Model' => array('hidden' => '0'), '__Token' => array('key' => $key) ); $fields = $this->__sortFields($fields); $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $data['__Token']['fields'] = $fields; $this->Controller->data = $data; $result = $this->Controller->Security->__validatePost($this->Controller); $this->assertTrue($result); unset($data['_Model']); $data['Model']['hidden'] = '0'; $this->assertTrue($this->Controller->data == $data); }/** * testValidateHiddenMultipleModel method * * @access public * @return void */ function testValidateHiddenMultipleModel() { $this->Controller->Security->startup($this->Controller); $key = $this->Controller->params['_Token']['key']; $data['Model']['username'] = ''; $data['Model']['password'] = ''; $data['_Model']['valid'] = '0'; $data['_Model2']['valid'] = '0'; $data['_Model3']['valid'] = '0'; $data['__Token']['key'] = $key; $fields = array( 'Model' => array('username', 'password', 'valid'), 'Model2'=> array('valid'), 'Model3'=> array('valid'), '_Model2'=> array('valid' => '0'), '_Model3'=> array('valid' => '0'), '_Model' => array('valid' => '0'), '__Token' => array('key' => $key) ); $fields = urlencode(Security::hash(serialize($this->__sortFields($fields)) . Configure::read('Security.salt'))); $data['__Token']['fields'] = $fields; $this->Controller->data = $data; $result = $this->Controller->Security->__validatePost($this->Controller); $this->assertTrue($result); unset($data['_Model'], $data['_Model2'], $data['_Model3']); $data['Model']['valid'] = '0'; $data['Model2']['valid'] = '0'; $data['Model3']['valid'] = '0'; $this->assertTrue($this->Controller->data == $data); } function testLoginValidation() { } function testValidateHasManyModel() { $this->Controller->Security->startup($this->Controller); $key = $this->Controller->params['_Token']['key']; $data['Model'][0]['username'] = 'username'; $data['Model'][0]['password'] = 'password'; $data['Model'][1]['username'] = 'username'; $data['Model'][1]['password'] = 'password'; $data['_Model'][0]['hidden'] = 'value'; $data['_Model'][1]['hidden'] = 'value'; $data['_Model'][0]['valid'] = '0'; $data['_Model'][1]['valid'] = '0'; $data['__Token']['key'] = $key; $fields = array( 'Model' => array( 0 => array('username', 'password', 'valid'), 1 => array('username', 'password', 'valid')), '_Model' => array( 0 => array('hidden' => 'value', 'valid' => '0'), 1 => array('hidden' => 'value', 'valid' => '0')), '__Token' => array('key' => $key)); $fields = $this->__sortFields($fields); $fields = urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))); $data['__Token']['fields'] = $fields; $this->Controller->data = $data; $result = $this->Controller->Security->__validatePost($this->Controller); $this->assertTrue($result); unset($data['_Model']); $data['Model'][0]['hidden'] = 'value'; $data['Model'][1]['hidden'] = 'value'; $data['Model'][0]['valid'] = '0'; $data['Model'][1]['valid'] = '0'; $this->assertTrue($this->Controller->data == $data); } function testLoginRequest() { $this->Controller->Security->startup($this->Controller); $realm = 'cakephp.org'; $options = array('realm' => $realm, 'type' => 'basic'); $result = $this->Controller->Security->loginRequest($options); $expected = 'WWW-Authenticate: Basic realm="'.$realm.'"'; $this->assertEqual($result, $expected); $this->Controller->Security->startup($this->Controller); $options = array('realm' => $realm, 'type' => 'digest'); $result = $this->Controller->Security->loginRequest($options); $this->assertPattern('/realm="'.$realm.'"/', $result); $this->assertPattern('/qop="auth"/', $result); } function testGenerateDigestResponseHash() { $this->Controller->Security->startup($this->Controller); $realm = 'cakephp.org'; $loginData = array('realm' => $realm, 'users' => array('Willy Smith' => 'password')); $this->Controller->Security->requireLogin($loginData); $data = array( 'username' => 'Willy Smith', 'password' => 'password', 'nonce' => String::uuid(), 'nc' => 1, 'cnonce' => 1, 'realm' => $realm, 'uri' => 'path_to_identifier', 'qop' => 'testme' ); $_SERVER['REQUEST_METHOD'] = 'POST'; $result = $this->Controller->Security->generateDigestResponseHash($data); $expected = md5( md5($data['username'] . ':' . $loginData['realm'].':'.$data['password']) . ':' . $data['nonce'] . ':' . $data['nc'] . ':' . $data['cnonce'] . ':' . $data['qop'] . ':' . md5(env('REQUEST_METHOD') . ':' . $data['uri'])); $this->assertIdentical($result, $expected); } function testLoginCredentials() { $this->Controller->Security->startup($this->Controller); $_SERVER['PHP_AUTH_USER'] = $user = 'Willy Test'; $_SERVER['PHP_AUTH_PW'] = $pw = 'some password for the nice test'; $result = $this->Controller->Security->loginCredentials('basic'); $expected = array('username' => $user, 'password' => $pw); $this->assertIdentical($result, $expected); if (version_compare(phpversion(), '5.1') != -1) { $_SERVER['PHP_AUTH_DIGEST'] = $digest = <<<DIGEST Digest username="Mufasa", realm="testrealm@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", uri="/dir/index.html", qop=auth, nc=00000001, cnonce="0a4f113b", response="6629fae49393a05397450978507c4ef1", opaque="5ccc069c403ebaf9f0171e9517f40e41"DIGEST; $expected = array( 'username' => 'Mufasa', 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093', 'uri' => '/dir/index.html', 'qop' => 'auth', 'nc' => '00000001', 'cnonce' => '0a4f113b', 'response' => '6629fae49393a05397450978507c4ef1', 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41' ); $result = $this->Controller->Security->loginCredentials('digest'); $this->assertIdentical($result, $expected); } } function testParseDigestAuthData() { $this->Controller->Security->startup($this->Controller); $digest = <<<DIGEST Digest username="Mufasa", realm="testrealm@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", uri="/dir/index.html", qop=auth, nc=00000001, cnonce="0a4f113b", response="6629fae49393a05397450978507c4ef1", opaque="5ccc069c403ebaf9f0171e9517f40e41"DIGEST; $expected = array( 'username' => 'Mufasa', 'nonce' => 'dcd98b7102dd2f0e8b11d0f600bfb0c093', 'uri' => '/dir/index.html', 'qop' => 'auth', 'nc' => '00000001', 'cnonce' => '0a4f113b', 'response' => '6629fae49393a05397450978507c4ef1', 'opaque' => '5ccc069c403ebaf9f0171e9517f40e41' ); $result = $this->Controller->Security->parseDigestAuthData($digest); $this->assertIdentical($result, $expected); $result = $this->Controller->Security->parseDigestAuthData(''); $this->assertNull($result); } function __sortFields($fields) { foreach ($fields as $key => $value) { if ($key[0] != '_' && is_array($fields[$key])) { sort($fields[$key]); } } ksort($fields, SORT_STRING); return $fields; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -