AccessTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Andreas Fischer <bantu@owncloud.com>
  7. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\User_LDAP\Tests;
  29. use OCA\User_LDAP\Access;
  30. use OCA\User_LDAP\Connection;
  31. use OCA\User_LDAP\Exceptions\ConstraintViolationException;
  32. use OCA\User_LDAP\FilesystemHelper;
  33. use OCA\User_LDAP\Helper;
  34. use OCA\User_LDAP\ILDAPWrapper;
  35. use OCA\User_LDAP\LDAP;
  36. use OCA\User_LDAP\LogWrapper;
  37. use OCA\User_LDAP\User\Manager;
  38. use OCP\IAvatarManager;
  39. use OCP\IConfig;
  40. use OCP\IDBConnection;
  41. use OCP\Image;
  42. use OCP\IUserManager;
  43. /**
  44. * Class AccessTest
  45. *
  46. * @group DB
  47. *
  48. * @package OCA\User_LDAP\Tests
  49. */
  50. class AccessTest extends \Test\TestCase {
  51. /** @var Connection|\PHPUnit_Framework_MockObject_MockObject */
  52. private $connection;
  53. /** @var LDAP|\PHPUnit_Framework_MockObject_MockObject */
  54. private $ldap;
  55. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  56. private $userManager;
  57. /** @var Helper|\PHPUnit_Framework_MockObject_MockObject */
  58. private $helper;
  59. /** @var Access */
  60. private $access;
  61. public function setUp() {
  62. $this->connection = $this->createMock(Connection::class);
  63. $this->ldap = $this->createMock(LDAP::class);
  64. $this->userManager = $this->createMock(Manager::class);
  65. $this->helper = $this->createMock(Helper::class);
  66. $this->access = new Access(
  67. $this->connection,
  68. $this->ldap,
  69. $this->userManager,
  70. $this->helper
  71. );
  72. }
  73. private function getConnectorAndLdapMock() {
  74. $lw = $this->createMock(ILDAPWrapper::class);
  75. $connector = $this->getMockBuilder(Connection::class)
  76. ->setConstructorArgs([$lw, null, null])
  77. ->getMock();
  78. $um = $this->getMockBuilder(Manager::class)
  79. ->setConstructorArgs([
  80. $this->createMock(IConfig::class),
  81. $this->createMock(FilesystemHelper::class),
  82. $this->createMock(LogWrapper::class),
  83. $this->createMock(IAvatarManager::class),
  84. $this->createMock(Image::class),
  85. $this->createMock(IDBConnection::class),
  86. $this->createMock(IUserManager::class)])
  87. ->getMock();
  88. $helper = new Helper(\OC::$server->getConfig());
  89. return array($lw, $connector, $um, $helper);
  90. }
  91. public function testEscapeFilterPartValidChars() {
  92. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  93. $access = new Access($con, $lw, $um, $helper);
  94. $input = 'okay';
  95. $this->assertTrue($input === $access->escapeFilterPart($input));
  96. }
  97. public function testEscapeFilterPartEscapeWildcard() {
  98. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  99. $access = new Access($con, $lw, $um, $helper);
  100. $input = '*';
  101. $expected = '\\\\*';
  102. $this->assertTrue($expected === $access->escapeFilterPart($input));
  103. }
  104. public function testEscapeFilterPartEscapeWildcard2() {
  105. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  106. $access = new Access($con, $lw, $um, $helper);
  107. $input = 'foo*bar';
  108. $expected = 'foo\\\\*bar';
  109. $this->assertTrue($expected === $access->escapeFilterPart($input));
  110. }
  111. /** @dataProvider convertSID2StrSuccessData */
  112. public function testConvertSID2StrSuccess(array $sidArray, $sidExpected) {
  113. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  114. $access = new Access($con, $lw, $um, $helper);
  115. $sidBinary = implode('', $sidArray);
  116. $this->assertSame($sidExpected, $access->convertSID2Str($sidBinary));
  117. }
  118. public function convertSID2StrSuccessData() {
  119. return array(
  120. array(
  121. array(
  122. "\x01",
  123. "\x04",
  124. "\x00\x00\x00\x00\x00\x05",
  125. "\x15\x00\x00\x00",
  126. "\xa6\x81\xe5\x0e",
  127. "\x4d\x6c\x6c\x2b",
  128. "\xca\x32\x05\x5f",
  129. ),
  130. 'S-1-5-21-249921958-728525901-1594176202',
  131. ),
  132. array(
  133. array(
  134. "\x01",
  135. "\x02",
  136. "\xFF\xFF\xFF\xFF\xFF\xFF",
  137. "\xFF\xFF\xFF\xFF",
  138. "\xFF\xFF\xFF\xFF",
  139. ),
  140. 'S-1-281474976710655-4294967295-4294967295',
  141. ),
  142. );
  143. }
  144. public function testConvertSID2StrInputError() {
  145. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  146. $access = new Access($con, $lw, $um, $helper);
  147. $sidIllegal = 'foobar';
  148. $sidExpected = '';
  149. $this->assertSame($sidExpected, $access->convertSID2Str($sidIllegal));
  150. }
  151. public function testGetDomainDNFromDNSuccess() {
  152. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  153. $access = new Access($con, $lw, $um, $helper);
  154. $inputDN = 'uid=zaphod,cn=foobar,dc=my,dc=server,dc=com';
  155. $domainDN = 'dc=my,dc=server,dc=com';
  156. $lw->expects($this->once())
  157. ->method('explodeDN')
  158. ->with($inputDN, 0)
  159. ->will($this->returnValue(explode(',', $inputDN)));
  160. $this->assertSame($domainDN, $access->getDomainDNFromDN($inputDN));
  161. }
  162. public function testGetDomainDNFromDNError() {
  163. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  164. $access = new Access($con, $lw, $um, $helper);
  165. $inputDN = 'foobar';
  166. $expected = '';
  167. $lw->expects($this->once())
  168. ->method('explodeDN')
  169. ->with($inputDN, 0)
  170. ->will($this->returnValue(false));
  171. $this->assertSame($expected, $access->getDomainDNFromDN($inputDN));
  172. }
  173. private function getResemblesDNInputData() {
  174. return $cases = array(
  175. array(
  176. 'input' => 'foo=bar,bar=foo,dc=foobar',
  177. 'interResult' => array(
  178. 'count' => 3,
  179. 0 => 'foo=bar',
  180. 1 => 'bar=foo',
  181. 2 => 'dc=foobar'
  182. ),
  183. 'expectedResult' => true
  184. ),
  185. array(
  186. 'input' => 'foobarbarfoodcfoobar',
  187. 'interResult' => false,
  188. 'expectedResult' => false
  189. )
  190. );
  191. }
  192. public function testStringResemblesDN() {
  193. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  194. $access = new Access($con, $lw, $um, $helper);
  195. $cases = $this->getResemblesDNInputData();
  196. $lw->expects($this->exactly(2))
  197. ->method('explodeDN')
  198. ->will($this->returnCallback(function ($dn) use ($cases) {
  199. foreach($cases as $case) {
  200. if($dn === $case['input']) {
  201. return $case['interResult'];
  202. }
  203. }
  204. return null;
  205. }));
  206. foreach($cases as $case) {
  207. $this->assertSame($case['expectedResult'], $access->stringResemblesDN($case['input']));
  208. }
  209. }
  210. public function testStringResemblesDNLDAPmod() {
  211. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  212. $lw = new \OCA\User_LDAP\LDAP();
  213. $access = new Access($con, $lw, $um, $helper);
  214. if(!function_exists('ldap_explode_dn')) {
  215. $this->markTestSkipped('LDAP Module not available');
  216. }
  217. $cases = $this->getResemblesDNInputData();
  218. foreach($cases as $case) {
  219. $this->assertSame($case['expectedResult'], $access->stringResemblesDN($case['input']));
  220. }
  221. }
  222. public function testCacheUserHome() {
  223. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  224. $access = new Access($con, $lw, $um, $helper);
  225. $con->expects($this->once())
  226. ->method('writeToCache');
  227. $access->cacheUserHome('foobar', '/foobars/path');
  228. }
  229. public function testBatchApplyUserAttributes() {
  230. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  231. $access = new Access($con, $lw, $um, $helper);
  232. $mapperMock = $this->getMockBuilder('\OCA\User_LDAP\Mapping\UserMapping')
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $mapperMock->expects($this->any())
  236. ->method('getNameByDN')
  237. ->will($this->returnValue('a_username'));
  238. $userMock = $this->getMockBuilder('\OCA\User_LDAP\User\User')
  239. ->disableOriginalConstructor()
  240. ->getMock();
  241. $access->connection->expects($this->any())
  242. ->method('__get')
  243. ->will($this->returnValue('displayName'));
  244. $access->setUserMapper($mapperMock);
  245. $displayNameAttribute = strtolower($access->connection->ldapUserDisplayName);
  246. $data = array(
  247. array(
  248. 'dn' => 'foobar',
  249. $displayNameAttribute => 'barfoo'
  250. ),
  251. array(
  252. 'dn' => 'foo',
  253. $displayNameAttribute => 'bar'
  254. ),
  255. array(
  256. 'dn' => 'raboof',
  257. $displayNameAttribute => 'oofrab'
  258. )
  259. );
  260. $userMock->expects($this->exactly(count($data)))
  261. ->method('processAttributes');
  262. $um->expects($this->exactly(count($data)))
  263. ->method('get')
  264. ->will($this->returnValue($userMock));
  265. $access->batchApplyUserAttributes($data);
  266. }
  267. public function dNAttributeProvider() {
  268. // corresponds to Access::resemblesDN()
  269. return array(
  270. 'dn' => array('dn'),
  271. 'uniqueMember' => array('uniquemember'),
  272. 'member' => array('member'),
  273. 'memberOf' => array('memberof')
  274. );
  275. }
  276. /**
  277. * @dataProvider dNAttributeProvider
  278. */
  279. public function testSanitizeDN($attribute) {
  280. list($lw, $con, $um, $helper) = $this->getConnectorAndLdapMock();
  281. $dnFromServer = 'cn=Mixed Cases,ou=Are Sufficient To,ou=Test,dc=example,dc=org';
  282. $lw->expects($this->any())
  283. ->method('isResource')
  284. ->will($this->returnValue(true));
  285. $lw->expects($this->any())
  286. ->method('getAttributes')
  287. ->will($this->returnValue(array(
  288. $attribute => array('count' => 1, $dnFromServer)
  289. )));
  290. $access = new Access($con, $lw, $um, $helper);
  291. $values = $access->readAttribute('uid=whoever,dc=example,dc=org', $attribute);
  292. $this->assertSame($values[0], strtolower($dnFromServer));
  293. }
  294. /**
  295. * @expectedException \Exception
  296. * @expectedExceptionMessage LDAP password changes are disabled
  297. */
  298. public function testSetPasswordWithDisabledChanges() {
  299. $this->connection
  300. ->method('__get')
  301. ->willReturn(false);
  302. $this->access->setPassword('CN=foo', 'MyPassword');
  303. }
  304. public function testSetPasswordWithLdapNotAvailable() {
  305. $this->connection
  306. ->method('__get')
  307. ->willReturn(true);
  308. $connection = $this->createMock(LDAP::class);
  309. $this->connection
  310. ->expects($this->once())
  311. ->method('getConnectionResource')
  312. ->willReturn($connection);
  313. $this->ldap
  314. ->expects($this->once())
  315. ->method('isResource')
  316. ->with($connection)
  317. ->willReturn(false);
  318. $this->assertFalse($this->access->setPassword('CN=foo', 'MyPassword'));
  319. }
  320. /**
  321. * @expectedException \OC\HintException
  322. * @expectedExceptionMessage Password change rejected.
  323. */
  324. public function testSetPasswordWithRejectedChange() {
  325. $this->connection
  326. ->method('__get')
  327. ->willReturn(true);
  328. $connection = $this->createMock(LDAP::class);
  329. $this->connection
  330. ->expects($this->once())
  331. ->method('getConnectionResource')
  332. ->willReturn($connection);
  333. $this->ldap
  334. ->expects($this->once())
  335. ->method('isResource')
  336. ->with($connection)
  337. ->willReturn(true);
  338. $this->ldap
  339. ->expects($this->once())
  340. ->method('modReplace')
  341. ->with($connection, 'CN=foo', 'MyPassword')
  342. ->willThrowException(new ConstraintViolationException());
  343. $this->access->setPassword('CN=foo', 'MyPassword');
  344. }
  345. public function testSetPassword() {
  346. $this->connection
  347. ->method('__get')
  348. ->willReturn(true);
  349. $connection = $this->createMock(LDAP::class);
  350. $this->connection
  351. ->expects($this->once())
  352. ->method('getConnectionResource')
  353. ->willReturn($connection);
  354. $this->ldap
  355. ->expects($this->once())
  356. ->method('isResource')
  357. ->with($connection)
  358. ->willReturn(true);
  359. $this->ldap
  360. ->expects($this->once())
  361. ->method('modReplace')
  362. ->with($connection, 'CN=foo', 'MyPassword')
  363. ->willReturn(true);
  364. $this->assertTrue($this->access->setPassword('CN=foo', 'MyPassword'));
  365. }
  366. }