Skip to content

Commit

Permalink
adjust tests and fix file cookie handling
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed Dec 6, 2023
1 parent b15da4f commit 5697ecf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions FileCookie.php
Expand Up @@ -26,8 +26,8 @@ class FileCookie
public function __construct($ident, $rand)
{
global $conf;
$path = $conf['tmpdir'] . '/captcha/' . date('Y-m-d') . '/' . md5($ident . $rand) . '.cookie';
io_makeFileDir($path);
$this->path = $conf['tmpdir'] . '/captcha/' . date('Y-m-d') . '/' . md5($ident . $rand) . '.cookie';
io_makeFileDir($this->path);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions _test/HelperTest.php
Expand Up @@ -2,6 +2,7 @@

namespace dokuwiki\plugin\captcha\test;

use dokuwiki\plugin\captcha\FileCookie;
use DokuWikiTest;

/**
Expand Down Expand Up @@ -57,7 +58,8 @@ public function testCheck()
$rand = 0;
$code = $helper->generateCaptchaCode($helper->fixedIdent(), $rand);

$this->callInaccessibleMethod($helper, 'storeCaptchaCookie', [$helper->fixedIdent(), $rand]);
$cookie = new FileCookie($helper->fixedIdent(), $rand);
$cookie->set();

// check with missing secrect -> fail
$INPUT->set($this->getInaccessibleProperty($helper, 'field_in'), $code);
Expand All @@ -71,7 +73,7 @@ public function testCheck()
$this->assertFalse($helper->check(true));

// set the cookie but change the ID -> fail
$this->callInaccessibleMethod($helper, 'storeCaptchaCookie', [$helper->fixedIdent(), $rand]);
$cookie->set();
$ID = 'test:fail';
$this->assertFalse($helper->check(false));
}
Expand Down Expand Up @@ -104,7 +106,8 @@ public function testCleanup()
$this->assertEquals(array(), $dirs);

// store a cookie
$this->callInaccessibleMethod($helper, 'storeCaptchaCookie', ['test', 0]);
$cookie = new FileCookie('test', 0);
$cookie->set();

// nothing but today's data
$dirs = glob("$path/*");
Expand All @@ -130,7 +133,7 @@ public function testCleanup()
);

// clean up
$helper->cleanCaptchaCookies();
FileCookie::clean();

// nothing but today's data
$dirs = glob("$path/*");
Expand Down
3 changes: 2 additions & 1 deletion helper.php
Expand Up @@ -194,7 +194,8 @@ protected function generateMagicCode($ident, $rand)
{
$ident = hexdec(substr(md5($ident), 5, 5)); // use part of the md5 to generate an int
$rand *= 0xFFFFF; // bitmask from the random number
return md5($rand ^ $ident); // combine both values
$comb = (int) $rand ^ $ident; // combine both values
return md5($comb);
}

/**
Expand Down

0 comments on commit 5697ecf

Please sign in to comment.