| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015 |
- <?php
- class privatebinTest extends PHPUnit_Framework_TestCase
- {
- protected $_model;
- public function setUp()
- {
- /* Setup Routine */
- $this->_model = privatebin_data::getInstance(array('dir' => PATH . 'data'));
- $this->reset();
- }
- public function tearDown()
- {
- /* Tear Down Routine */
- helper::confRestore();
- }
- public function reset()
- {
- $_POST = array();
- $_GET = array();
- $_SERVER = array();
- if ($this->_model->exists(helper::getPasteId()))
- $this->_model->delete(helper::getPasteId());
- helper::confRestore();
- }
- /**
- * @runInSeparateProcess
- */
- public function testView()
- {
- $this->reset();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertContains(
- '<title>PrivateBin</title>',
- $content,
- 'outputs title correctly'
- );
- $this->assertNotContains(
- 'id="shortenbutton"',
- $content,
- 'doesn\'t output shortener button'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testViewLanguageSelection()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['main']['languageselection'] = true;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_COOKIE['lang'] = 'de';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertContains(
- '<title>PrivateBin</title>',
- $content,
- 'outputs title correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testViewForceLanguageDefault()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['main']['languageselection'] = false;
- $options['main']['languagedefault'] = 'fr';
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_COOKIE['lang'] = 'de';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertContains(
- '<title>PrivateBin</title>',
- $content,
- 'outputs title correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testViewUrlShortener()
- {
- $shortener = 'https://shortener.example.com/api?link=';
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['main']['urlshortener'] = $shortener;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_COOKIE['lang'] = 'de';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#id="shortenbutton"[^>]*data-shortener="' . preg_quote($shortener) . '"#',
- $content,
- 'outputs configured shortener URL correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testHtaccess()
- {
- $this->reset();
- $dirs = array('cfg', 'lib');
- foreach ($dirs as $dir) {
- $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
- @unlink($file);
- }
- ob_start();
- new privatebin;
- ob_end_clean();
- foreach ($dirs as $dir) {
- $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
- $this->assertFileExists(
- $file,
- "$dir htaccess recreated"
- );
- }
- }
- /**
- * @expectedException Exception
- * @expectedExceptionCode 2
- */
- public function testConf()
- {
- $this->reset();
- helper::confBackup();
- file_put_contents(CONF, '');
- ob_start();
- new privatebin;
- ob_end_clean();
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreate()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $paste = $this->_model->read($response['id']);
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $paste->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateInvalidTimelimit()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste(array('expire' => 25));
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- trafficlimiter::canPass();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $paste = $this->_model->read($response['id']);
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $paste->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateInvalidSize()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['main']['sizelimit'] = 10;
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateProxyHeader()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['header'] = 'X_FORWARDED_FOR';
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_SERVER['HTTP_X_FORWARDED_FOR'] = '::2';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $paste = $this->_model->read($response['id']);
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $paste->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateDuplicateId()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- $_POST = helper::getPaste();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateValidExpire()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_POST['expire'] = '5min';
- $_POST['formatter'] = 'foo';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- $time = time();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $paste = $this->_model->read($response['id']);
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $paste->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- $this->assertGreaterThanOrEqual($time + 300, $paste->meta->expire_date, 'time is set correctly');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateValidExpireWithDiscussion()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_POST['expire'] = '5min';
- $_POST['opendiscussion'] = '1';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- $time = time();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $paste = $this->_model->read($response['id']);
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $paste->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- $this->assertGreaterThanOrEqual($time + 300, $paste->meta->expire_date, 'time is set correctly');
- $this->assertEquals(1, $paste->meta->opendiscussion, 'discussion is enabled');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateInvalidExpire()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_POST['expire'] = 'foo';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $paste = $this->_model->read($response['id']);
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $paste->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateInvalidBurn()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_POST['burnafterreading'] = 'neither 1 nor 0';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateInvalidOpenDiscussion()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_POST['opendiscussion'] = 'neither 1 nor 0';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateAttachment()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- $options['main']['fileupload'] = true;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPasteWithAttachment();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exists before posting data');
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $original = json_decode(json_encode($_POST));
- $stored = $this->_model->read($response['id']);
- foreach (array('data', 'attachment', 'attachmentname') as $key) {
- $this->assertEquals($original->$key, $stored->$key);
- }
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $stored->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateTooSoon()
- {
- $this->reset();
- $_POST = helper::getPaste();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- ob_end_clean();
- $this->_model->delete(helper::getPasteId());
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateValidNick()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getPaste();
- $_POST['nickname'] = helper::getComment()['meta']['nickname'];
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
- $paste = $this->_model->read($response['id']);
- $this->assertEquals(
- hash_hmac('sha256', $response['id'], $paste->meta->salt),
- $response['deletetoken'],
- 'outputs valid delete token'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateInvalidNick()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getCommentPost();
- $_POST['pasteid'] = helper::getPasteId();
- $_POST['parentid'] = helper::getPasteId();
- $_POST['nickname'] = 'foo';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateComment()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getCommentPost();
- $_POST['pasteid'] = helper::getPasteId();
- $_POST['parentid'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), $response['id']), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateInvalidComment()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getCommentPost();
- $_POST['pasteid'] = helper::getPasteId();
- $_POST['parentid'] = 'foo';
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateCommentDiscussionDisabled()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getCommentPost();
- $_POST['pasteid'] = helper::getPasteId();
- $_POST['parentid'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- $paste = helper::getPaste(array('opendiscussion' => false));
- $this->_model->create(helper::getPasteId(), $paste);
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateCommentInvalidPaste()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $_POST = helper::getCommentPost();
- $_POST['pasteid'] = helper::getPasteId();
- $_POST['parentid'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testCreateDuplicateComment()
- {
- $this->reset();
- $options = parse_ini_file(CONF, true);
- $options['traffic']['limit'] = 0;
- helper::confBackup();
- helper::createIniFile(CONF, $options);
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- $this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment());
- $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data');
- $_POST = helper::getCommentPost();
- $_POST['pasteid'] = helper::getPasteId();
- $_POST['parentid'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- $_SERVER['REMOTE_ADDR'] = '::1';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testRead()
- {
- $this->reset();
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertContains(
- '<div id="cipherdata" class="hidden">' .
- htmlspecialchars(helper::getPasteAsJson(), ENT_NOQUOTES) .
- '</div>',
- $content,
- 'outputs data correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadInvalidId()
- {
- $this->reset();
- $_SERVER['QUERY_STRING'] = 'foo';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="errormessage"[^>]*>.*Invalid paste ID\.</div>#',
- $content,
- 'outputs error correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadNonexisting()
- {
- $this->reset();
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="errormessage"[^>]*>.*Paste does not exist[^<]*</div>#',
- $content,
- 'outputs error correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadExpired()
- {
- $this->reset();
- $expiredPaste = helper::getPaste(array('expire_date' => 1344803344));
- $this->_model->create(helper::getPasteId(), $expiredPaste);
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="errormessage"[^>]*>.*Paste does not exist[^<]*</div>#',
- $content,
- 'outputs error correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadBurn()
- {
- $this->reset();
- $burnPaste = helper::getPaste(array('burnafterreading' => true));
- $this->_model->create(helper::getPasteId(), $burnPaste);
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- unset($burnPaste['meta']['salt']);
- $this->assertContains(
- '<div id="cipherdata" class="hidden">' .
- htmlspecialchars(helper::getPasteAsJson($burnPaste['meta']), ENT_NOQUOTES) .
- '</div>',
- $content,
- 'outputs data correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadJson()
- {
- $this->reset();
- $paste = helper::getPaste();
- $this->_model->create(helper::getPasteId(), $paste);
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs success status');
- $this->assertEquals(helper::getPasteId(), $response['id'], 'outputs data correctly');
- $this->assertStringEndsWith('?' . $response['id'], $response['url'], 'returned URL points to new paste');
- $this->assertEquals($paste['data'], $response['data'], 'outputs data correctly');
- $this->assertEquals($paste['meta']['formatter'], $response['meta']['formatter'], 'outputs format correctly');
- $this->assertEquals($paste['meta']['postdate'], $response['meta']['postdate'], 'outputs postdate correctly');
- $this->assertEquals($paste['meta']['opendiscussion'], $response['meta']['opendiscussion'], 'outputs opendiscussion correctly');
- $this->assertEquals(0, $response['comment_count'], 'outputs comment_count correctly');
- $this->assertEquals(0, $response['comment_offset'], 'outputs comment_offset correctly');
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadInvalidJson()
- {
- $this->reset();
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs error status');
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadOldSyntax()
- {
- $this->reset();
- $oldPaste = helper::getPaste();
- $meta = array(
- 'syntaxcoloring' => true,
- 'postdate' => $oldPaste['meta']['postdate'],
- 'opendiscussion' => $oldPaste['meta']['opendiscussion'],
- );
- $oldPaste['meta'] = $meta;
- $this->_model->create(helper::getPasteId(), $oldPaste);
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $meta['formatter'] = 'syntaxhighlighting';
- $this->assertContains(
- '<div id="cipherdata" class="hidden">' .
- htmlspecialchars(helper::getPasteAsJson($meta), ENT_NOQUOTES) .
- '</div>',
- $content,
- 'outputs data correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testReadOldFormat()
- {
- $this->reset();
- $oldPaste = helper::getPaste();
- unset($oldPaste['meta']['formatter']);
- $this->_model->create(helper::getPasteId(), $oldPaste);
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $oldPaste['meta']['formatter'] = 'plaintext';
- unset($oldPaste['meta']['salt']);
- $this->assertContains(
- '<div id="cipherdata" class="hidden">' .
- htmlspecialchars(helper::getPasteAsJson($oldPaste['meta']), ENT_NOQUOTES) .
- '</div>',
- $content,
- 'outputs data correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testDelete()
- {
- $this->reset();
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
- $paste = $this->_model->read(helper::getPasteId());
- $_GET['pasteid'] = helper::getPasteId();
- $_GET['deletetoken'] = hash_hmac('sha256', helper::getPasteId(), $paste->meta->salt);
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="status"[^>]*>.*Paste was properly deleted[^<]*</div>#s',
- $content,
- 'outputs deleted status correctly'
- );
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
- }
- /**
- * @runInSeparateProcess
- */
- public function testDeleteInvalidId()
- {
- $this->reset();
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- $_GET['pasteid'] = 'foo';
- $_GET['deletetoken'] = 'bar';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="errormessage"[^>]*>.*Invalid paste ID\.</div>#',
- $content,
- 'outputs delete error correctly'
- );
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testDeleteInexistantId()
- {
- $this->reset();
- $_GET['pasteid'] = helper::getPasteId();
- $_GET['deletetoken'] = 'bar';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="errormessage"[^>]*>.*Paste does not exist[^<]*</div>#',
- $content,
- 'outputs delete error correctly'
- );
- }
- /**
- * @runInSeparateProcess
- */
- public function testDeleteInvalidToken()
- {
- $this->reset();
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- $_GET['pasteid'] = helper::getPasteId();
- $_GET['deletetoken'] = 'bar';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="errormessage"[^>]*>.*Wrong deletion token[^<]*</div>#',
- $content,
- 'outputs delete error correctly'
- );
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
- }
- /**
- * @runInSeparateProcess
- */
- public function testDeleteBurnAfterReading()
- {
- $this->reset();
- $burnPaste = helper::getPaste(array('burnafterreading' => true));
- $this->_model->create(helper::getPasteId(), $burnPaste);
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
- $_POST['deletetoken'] = 'burnafterreading';
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(0, $response['status'], 'outputs status');
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
- }
- /**
- * @runInSeparateProcess
- */
- public function testDeleteInvalidBurnAfterReading()
- {
- $this->reset();
- $this->_model->create(helper::getPasteId(), helper::getPaste());
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
- $_POST['deletetoken'] = 'burnafterreading';
- $_SERVER['QUERY_STRING'] = helper::getPasteId();
- $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
- $_SERVER['REQUEST_METHOD'] = 'POST';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $response = json_decode($content, true);
- $this->assertEquals(1, $response['status'], 'outputs status');
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
- }
- /**
- * @runInSeparateProcess
- */
- public function testDeleteExpired()
- {
- $this->reset();
- $expiredPaste = helper::getPaste(array('expire_date' => 1000));
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exist before being created');
- $this->_model->create(helper::getPasteId(), $expiredPaste);
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
- $_GET['pasteid'] = helper::getPasteId();
- $_GET['deletetoken'] = 'does not matter in this context, but has to be set';
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="errormessage"[^>]*>.*Paste does not exist[^<]*</div>#',
- $content,
- 'outputs error correctly'
- );
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
- }
- /**
- * @runInSeparateProcess
- */
- public function testDeleteMissingPerPasteSalt()
- {
- $this->reset();
- $paste = helper::getPaste();
- unset($paste['meta']['salt']);
- $this->_model->create(helper::getPasteId(), $paste);
- $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
- $_GET['pasteid'] = helper::getPasteId();
- $_GET['deletetoken'] = hash_hmac('sha256', helper::getPasteId(), serversalt::get());
- ob_start();
- new privatebin;
- $content = ob_get_contents();
- $this->assertRegExp(
- '#<div[^>]*id="status"[^>]*>.*Paste was properly deleted[^<]*</div>#s',
- $content,
- 'outputs deleted status correctly'
- );
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
- }
- }
|