xref: /dokuwiki/lib/plugins/authplain/_test/escaping.test.php (revision b857f6cb524f22ed48a5a08553b2a370969dda38)
1<?php
2
3/**
4 * These tests are designed to test the capacity of pluginauth to handle
5 * correct escaping of colon field delimiters and backslashes in user content.
6 *
7 * (Note that these tests set some Real Names, etc. that are may not be
8 * valid in the broader dokuwiki context, but the tests ensure that
9 * authplain won't get unexpectedly surprised.)
10 *
11 * @group plugin_authplain
12 * @group auth_plugins
13 * @group plugins
14 * @group bundled_plugins
15 */
16class helper_plugin_authplain_escaping_test extends DokuWikiTest {
17
18    protected $pluginsEnabled = array('authplainharness');
19    /** @var  auth_plugin_authplain|auth_plugin_authplainharness */
20    protected $auth;
21
22    protected function reloadUsers() {
23        /* auth caches data loaded from file, but recreated object forces reload */
24        $this->auth = new auth_plugin_authplainharness();
25    }
26
27    function setUp() {
28        global $config_cascade;
29        parent::setUp();
30        $name = $config_cascade['plainauth.users']['default'];
31        copy($name, $name.".orig");
32        $this->reloadUsers();
33    }
34
35    function tearDown() {
36        global $config_cascade;
37        parent::tearDown();
38        $name = $config_cascade['plainauth.users']['default'];
39        copy($name.".orig", $name);
40    }
41
42    public function testMediawikiPasswordHash() {
43        global $conf;
44        $conf['passcrypt'] = 'mediawiki';
45        $this->auth->createUser("mwuser", "12345", "Mediawiki User", "me@example.com");
46        $this->reloadUsers();
47        $this->assertTrue($this->auth->checkPass("mwuser", "12345"));
48        $mwuser = $this->auth->getUserData("mwuser");
49        $this->assertStringStartsWith(":B:",$mwuser['pass']);
50        $this->assertEquals("Mediawiki User",$mwuser['name']);
51    }
52
53    public function testNameWithColons() {
54        $name = ":Colon: User:";
55        $this->auth->createUser("colonuser", "password", $name, "me@example.com");
56        $this->reloadUsers();
57        $user = $this->auth->getUserData("colonuser");
58        $this->assertEquals($name,$user['name']);
59    }
60
61    public function testNameWithBackslashes() {
62        $name = "\\Slash\\ User\\";
63        $this->auth->createUser("slashuser", "password", $name, "me@example.com");
64        $this->reloadUsers();
65        $user = $this->auth->getUserData("slashuser");
66        $this->assertEquals($name,$user['name']);
67    }
68
69    public function testModifyUser() {
70        global $conf;
71        $conf['passcrypt'] = 'mediawiki';
72        $user = $this->auth->getUserData("testuser");
73        $user['name'] = "\\New:Crazy:Name\\";
74        $user['pass'] = "awesome new password";
75        $this->auth->modifyUser("testuser", $user);
76        $this->reloadUsers();
77
78        $saved = $this->auth->getUserData("testuser");
79        $this->assertEquals($saved['name'], $user['name']);
80        $this->assertTrue($this->auth->checkPass("testuser", $user['pass']));
81    }
82
83    // really only required for developers to ensure this plugin will
84    // work with systems running on PCRE 6.6 and lower.
85    public function testLineSplit(){
86        $this->auth->setPregsplit_safe(false);
87
88        $names = array(
89          'plain',
90          'ut-fठ8',
91          'colon:',
92          'backslash\\',
93          'alltogether\\ठ:'
94        );
95        $userpass = 'user:password_hash:';
96        $other_user_data = ':email@address:group1,group2';
97
98        foreach ($names as $testname) {
99            $escaped = str_replace(array('\\',':'),array('\\\\','\\:'),$testname);   // escape : & \
100            $test_line = $userpass.$escaped.$other_user_data;
101            $result = $this->auth->splitUserData($test_line);
102
103            $this->assertEquals($escaped, $result[2]);
104        }
105    }
106
107}
108
109class auth_plugin_authplainharness extends auth_plugin_authplain {
110
111    /**
112     * @param boolean $bool
113     */
114    public function setPregsplit_safe($bool) {
115        $this->_pregsplit_safe = $bool;
116    }
117
118    public function getPregsplit_safe(){
119        return $this->_pregsplit_safe;
120    }
121
122    /**
123     * @param string $line
124     */
125    public function splitUserData($line){
126        return $this->_splitUserData($line);
127    }
128}
129