| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043 |
- <?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'
- );
- }
- /**
- * In some webserver setups (found with Suhosin) overly long POST params are
- * silently removed, check that this case is handled
- *
- * @runInSeparateProcess
- */
- public function testCreateBrokenAttachmentUpload()
- {
- $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();
- unset($_POST['attachment']);
- $_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(1, $response['status'], 'outputs error status');
- $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
- }
- /**
- * @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');
- }
- }
|