2015-08-29 01:26:48 +02:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* generates a config unit test class
|
|
|
|
*
|
|
|
|
* This generator is meant to test all possible configuration combinations
|
|
|
|
* without having to write endless amounts of code manually.
|
|
|
|
*
|
|
|
|
* DANGER: Too many options/settings and too high max iteration setting may trigger
|
|
|
|
* a fork bomb. Please save your work before executing this script.
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
include 'Bootstrap.php';
|
2015-08-29 20:29:14 +02:00
|
|
|
|
2018-07-01 16:25:50 +02:00
|
|
|
$vd = array('view', 'delete');
|
|
|
|
$vcd = array('view', 'create', 'delete');
|
2015-08-29 20:29:14 +02:00
|
|
|
|
2016-08-09 11:54:42 +02:00
|
|
|
new ConfigurationTestGenerator(array(
|
2015-08-31 00:01:35 +02:00
|
|
|
'main/discussion' => array(
|
2015-08-29 01:26:48 +02:00
|
|
|
array(
|
|
|
|
'setting' => true,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 01:26:48 +02:00
|
|
|
array(
|
2018-07-01 16:25:50 +02:00
|
|
|
'conditions' => array('steps' => $vd),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'RegExp',
|
|
|
|
'args' => array(
|
2018-07-01 16:25:50 +02:00
|
|
|
'#<div[^>]*id="opendiscussionoption"[^>]*>#',
|
2015-08-29 01:26:48 +02:00
|
|
|
'$content',
|
2016-10-29 10:24:08 +02:00
|
|
|
'outputs enabled discussion correctly',
|
2015-08-29 01:26:48 +02:00
|
|
|
),
|
2015-08-29 20:29:14 +02:00
|
|
|
), array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'traffic/limit' => 10),
|
2016-10-29 10:24:08 +02:00
|
|
|
'settings' => array('$_POST["opendiscussion"] = "neither 1 nor 0"'),
|
|
|
|
'type' => 'Equals',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
1,
|
|
|
|
'$response["status"]',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when discussions are enabled, but invalid flag posted, fail to create paste',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
), array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'traffic/limit' => 10),
|
2016-10-29 10:24:08 +02:00
|
|
|
'settings' => array('$_POST["opendiscussion"] = "neither 1 nor 0"'),
|
|
|
|
'type' => 'False',
|
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'$this->_model->exists(Helper::getPasteId())',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when discussions are enabled, but invalid flag posted, paste is not created',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
2015-08-29 01:26:48 +02:00
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vcd,
|
2015-08-29 01:26:48 +02:00
|
|
|
), array(
|
|
|
|
'setting' => false,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 01:26:48 +02:00
|
|
|
array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'NotRegExp',
|
2015-08-31 00:01:35 +02:00
|
|
|
'args' => array(
|
2018-07-01 16:25:50 +02:00
|
|
|
'#<div[^>]*id="opendiscussionoption"[^>]*>#',
|
2015-08-31 00:01:35 +02:00
|
|
|
'$content',
|
2016-10-29 10:24:08 +02:00
|
|
|
'outputs disabled discussion correctly',
|
2015-08-31 00:01:35 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vd,
|
2015-08-31 00:01:35 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'main/opendiscussion' => array(
|
|
|
|
array(
|
|
|
|
'setting' => true,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-31 00:01:35 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('main/discussion' => true),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'RegExp',
|
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
|
2015-08-29 01:26:48 +02:00
|
|
|
'$content',
|
2016-10-29 10:24:08 +02:00
|
|
|
'outputs checked discussion correctly',
|
2015-08-31 00:01:35 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vd,
|
2015-08-31 00:01:35 +02:00
|
|
|
), array(
|
|
|
|
'setting' => false,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-31 00:01:35 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('main/discussion' => true),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'NotRegExp',
|
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
|
2015-08-31 00:01:35 +02:00
|
|
|
'$content',
|
2016-10-29 10:24:08 +02:00
|
|
|
'outputs unchecked discussion correctly',
|
2015-08-29 01:26:48 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vd,
|
2015-08-29 01:26:48 +02:00
|
|
|
),
|
|
|
|
),
|
2015-08-29 20:29:14 +02:00
|
|
|
'main/burnafterreadingselected' => array(
|
|
|
|
array(
|
|
|
|
'setting' => true,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'RegExp',
|
2015-08-29 20:29:14 +02:00
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<input[^>]+id="burnafterreading"[^>]*checked="checked"[^>]*>#',
|
2015-08-29 20:29:14 +02:00
|
|
|
'$content',
|
|
|
|
'preselects burn after reading option',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'affects' => array('view'),
|
|
|
|
), array(
|
|
|
|
'setting' => false,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'NotRegExp',
|
2015-08-29 20:29:14 +02:00
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<input[^>]+id="burnafterreading"[^>]*checked="checked"[^>]*>#',
|
2015-08-29 20:29:14 +02:00
|
|
|
'$content',
|
|
|
|
'burn after reading option is unchecked',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2015-08-29 10:41:10 +02:00
|
|
|
'affects' => array('view'),
|
|
|
|
),
|
|
|
|
),
|
2015-08-31 00:01:35 +02:00
|
|
|
'main/password' => array(
|
|
|
|
array(
|
|
|
|
'setting' => true,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-31 00:01:35 +02:00
|
|
|
array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'RegExp',
|
2015-08-31 00:01:35 +02:00
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<div[^>]*id="password"[^>]*>#',
|
2015-08-31 00:01:35 +02:00
|
|
|
'$content',
|
2016-10-29 10:24:08 +02:00
|
|
|
'outputs password input correctly',
|
2015-08-31 00:01:35 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vd,
|
2015-08-31 00:01:35 +02:00
|
|
|
), array(
|
|
|
|
'setting' => false,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-31 00:01:35 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('main/discussion' => true),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'NotRegExp',
|
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<div[^>]*id="password"[^>]*>#',
|
2015-08-31 00:01:35 +02:00
|
|
|
'$content',
|
2016-10-29 10:24:08 +02:00
|
|
|
'removes password input correctly',
|
2015-08-31 00:01:35 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vd,
|
2015-08-31 00:01:35 +02:00
|
|
|
),
|
|
|
|
),
|
2015-08-29 20:29:14 +02:00
|
|
|
'main/template' => array(
|
|
|
|
array(
|
|
|
|
'setting' => 'page',
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'RegExp',
|
2015-08-29 20:29:14 +02:00
|
|
|
'args' => array(
|
2017-10-08 12:27:37 +02:00
|
|
|
'#<link[^>]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/privatebin\.css\\?\d[\d\.]+\d+"[^>]*/>#',
|
2015-08-29 20:29:14 +02:00
|
|
|
'$content',
|
|
|
|
'outputs "page" stylesheet correctly',
|
|
|
|
),
|
|
|
|
), array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'NotRegExp',
|
2015-08-29 20:29:14 +02:00
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<link[^>]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/bootstrap/bootstrap-\d[\d\.]+\d\.css"[^>]*/>#',
|
2015-08-29 20:29:14 +02:00
|
|
|
'$content',
|
|
|
|
'removes "bootstrap" stylesheet correctly',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vd,
|
2015-08-29 20:29:14 +02:00
|
|
|
), array(
|
|
|
|
'setting' => 'bootstrap',
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'NotRegExp',
|
2015-08-29 20:29:14 +02:00
|
|
|
'args' => array(
|
2017-10-08 12:27:37 +02:00
|
|
|
'#<link[^>]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/privatebin\.css\\?\d[\d\.]+\d+"[^>]*/>#',
|
2015-08-29 20:29:14 +02:00
|
|
|
'$content',
|
|
|
|
'removes "page" stylesheet correctly',
|
|
|
|
),
|
|
|
|
), array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'type' => 'RegExp',
|
2015-08-29 20:29:14 +02:00
|
|
|
'args' => array(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<link[^>]+type="text/css"[^>]+rel="stylesheet"[^>]+href="css/bootstrap/bootstrap-\d[\d\.]+\d\.css"[^>]*/>#',
|
2015-08-29 20:29:14 +02:00
|
|
|
'$content',
|
|
|
|
'outputs "bootstrap" stylesheet correctly',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2018-07-01 16:25:50 +02:00
|
|
|
'affects' => $vd,
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'main/sizelimit' => array(
|
|
|
|
array(
|
|
|
|
'setting' => 10,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'traffic/limit' => 10),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'Equals',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
1,
|
|
|
|
'$response["status"]',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when sizelimit limit exceeded, fail to create paste',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-10-29 10:24:08 +02:00
|
|
|
'affects' => array('create'),
|
2015-08-29 20:29:14 +02:00
|
|
|
), array(
|
|
|
|
'setting' => 2097152,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'traffic/limit' => 0, 'main/burnafterreadingselected' => true),
|
2016-10-29 10:24:08 +02:00
|
|
|
'settings' => array('sleep(3)'),
|
|
|
|
'type' => 'Equals',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
0,
|
|
|
|
'$response["status"]',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when sizelimit limit is not reached, successfully create paste',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
), array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'traffic/limit' => 0, 'main/burnafterreadingselected' => true),
|
2016-10-29 10:24:08 +02:00
|
|
|
'settings' => array('sleep(3)'),
|
|
|
|
'type' => 'True',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
'$this->_model->exists($response["id"])',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when sizelimit limit is not reached, paste exists after posting data',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-10-29 10:24:08 +02:00
|
|
|
'affects' => array('create'),
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'traffic/limit' => array(
|
|
|
|
array(
|
|
|
|
'setting' => 0,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'Equals',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
0,
|
|
|
|
'$response["status"]',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when traffic limit is disabled, successfully create paste',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
), array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'True',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
'$this->_model->exists($response["id"])',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when traffic limit is disabled, paste exists after posting data',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-10-29 10:24:08 +02:00
|
|
|
'affects' => array('create'),
|
2015-08-29 20:29:14 +02:00
|
|
|
), array(
|
|
|
|
'setting' => 10,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('steps' => array('create')),
|
2016-10-29 10:24:08 +02:00
|
|
|
'type' => 'Equals',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
1,
|
|
|
|
'$response["status"]',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when traffic limit is on and we do not wait, fail to create paste',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-10-29 10:24:08 +02:00
|
|
|
'affects' => array('create'),
|
2015-08-29 20:29:14 +02:00
|
|
|
), array(
|
|
|
|
'setting' => 2,
|
2016-10-29 10:24:08 +02:00
|
|
|
'tests' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
|
2016-10-29 10:24:08 +02:00
|
|
|
'settings' => array('sleep(3)'),
|
|
|
|
'type' => 'Equals',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
0,
|
|
|
|
'$response["status"]',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when traffic limit is on and we wait, successfully create paste',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
), array(
|
|
|
|
'conditions' => array('steps' => array('create'), 'main/sizelimit' => 2097152),
|
2016-10-29 10:24:08 +02:00
|
|
|
'settings' => array('sleep(3)'),
|
|
|
|
'type' => 'True',
|
|
|
|
'args' => array(
|
2015-08-29 20:29:14 +02:00
|
|
|
'$this->_model->exists($response["id"])',
|
2016-10-29 10:24:08 +02:00
|
|
|
'when traffic limit is on and we wait, paste exists after posting data',
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2016-10-29 10:24:08 +02:00
|
|
|
'affects' => array('create'),
|
2015-08-29 20:29:14 +02:00
|
|
|
),
|
|
|
|
),
|
2015-08-29 10:41:10 +02:00
|
|
|
));
|
|
|
|
|
2016-08-09 11:54:42 +02:00
|
|
|
class ConfigurationTestGenerator
|
2015-08-29 10:41:10 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* endless loop protection, since we're working with a recursive function,
|
|
|
|
* creating factorial configurations
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-08-31 00:01:35 +02:00
|
|
|
const MAX_ITERATIONS = 2000;
|
2015-08-29 10:41:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* options to test
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_options;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iteration count to guarantee timely end
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $_iterationCount = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generated configurations
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_configurations = array(
|
2016-10-29 10:24:08 +02:00
|
|
|
array('options' => array(), 'tests' => array(), 'affects' => array()),
|
2015-08-29 10:41:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constructor, generates the configuration test
|
|
|
|
* @param array $options
|
|
|
|
*/
|
2016-07-26 08:19:35 +02:00
|
|
|
public function __construct($options)
|
|
|
|
{
|
2015-08-29 10:41:10 +02:00
|
|
|
$this->_options = $options;
|
|
|
|
// generate all possible combinations of options: options^settings
|
|
|
|
$this->_generateConfigurations();
|
|
|
|
$this->_writeConfigurationTest();
|
|
|
|
}
|
2015-08-29 01:26:48 +02:00
|
|
|
|
2015-08-29 10:41:10 +02:00
|
|
|
/**
|
|
|
|
* write configuration test file based on generated configuration array
|
|
|
|
*/
|
|
|
|
private function _writeConfigurationTest()
|
|
|
|
{
|
2017-10-08 12:27:37 +02:00
|
|
|
$defaultOptions = parse_ini_file(CONF_SAMPLE, true);
|
2016-10-29 10:24:08 +02:00
|
|
|
$code = $this->_getHeader();
|
2015-08-29 10:41:10 +02:00
|
|
|
foreach ($this->_configurations as $key => $conf) {
|
|
|
|
$fullOptions = array_replace_recursive($defaultOptions, $conf['options']);
|
2016-10-29 10:24:08 +02:00
|
|
|
$options = Helper::varExportMin($fullOptions, true);
|
2015-08-29 10:41:10 +02:00
|
|
|
foreach ($conf['affects'] as $step) {
|
2015-08-29 20:29:14 +02:00
|
|
|
$testCode = $preCode = array();
|
2015-08-29 10:41:10 +02:00
|
|
|
foreach ($conf['tests'] as $tests) {
|
2015-08-29 20:29:14 +02:00
|
|
|
foreach ($tests[0] as $test) {
|
|
|
|
// skip if test does not affect this step
|
|
|
|
if (!in_array($step, $tests[1])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// skip if not all test conditions are met
|
2015-08-29 10:41:10 +02:00
|
|
|
if (array_key_exists('conditions', $test)) {
|
2015-08-29 20:29:14 +02:00
|
|
|
while (list($path, $setting) = each($test['conditions'])) {
|
|
|
|
if ($path == 'steps' && !in_array($step, $setting)) {
|
2015-08-29 10:41:10 +02:00
|
|
|
continue 2;
|
2016-07-26 08:19:35 +02:00
|
|
|
} elseif ($path != 'steps') {
|
2015-08-29 20:29:14 +02:00
|
|
|
list($section, $option) = explode('/', $path);
|
|
|
|
if ($fullOptions[$section][$option] !== $setting) {
|
|
|
|
continue 2;
|
|
|
|
}
|
2015-08-29 10:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:29:14 +02:00
|
|
|
if (array_key_exists('settings', $test)) {
|
|
|
|
foreach ($test['settings'] as $setting) {
|
|
|
|
$preCode[$setting] = $setting;
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 10:41:10 +02:00
|
|
|
$args = array();
|
|
|
|
foreach ($test['args'] as $arg) {
|
|
|
|
if (is_string($arg) && strpos($arg, '$') === 0) {
|
|
|
|
$args[] = $arg;
|
|
|
|
} else {
|
2016-08-09 11:54:42 +02:00
|
|
|
$args[] = Helper::varExportMin($arg, true);
|
2015-08-29 10:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:29:14 +02:00
|
|
|
$testCode[] = array($test['type'], implode(', ', $args));
|
2015-08-29 10:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-29 20:29:14 +02:00
|
|
|
$code .= $this->_getFunction(
|
2019-07-08 19:56:05 +02:00
|
|
|
ucfirst($step), $key, $options, $preCode, $testCode, $fullOptions['main']['discussion']
|
2015-08-29 20:29:14 +02:00
|
|
|
);
|
2015-08-29 10:41:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$code .= '}' . PHP_EOL;
|
2016-08-09 11:54:42 +02:00
|
|
|
file_put_contents('ConfigurationCombinationsTest.php', $code);
|
2015-08-29 10:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get header of configuration test file
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function _getHeader()
|
|
|
|
{
|
|
|
|
return <<<'EOT'
|
2015-08-29 01:26:48 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2015-09-22 23:21:31 +02:00
|
|
|
* DO NOT EDIT: This file is generated automatically using configGenerator.php
|
2015-08-29 01:26:48 +02:00
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
|
2019-07-08 19:56:05 +02:00
|
|
|
use PrivateBin\Controller;
|
2016-08-09 11:54:42 +02:00
|
|
|
use PrivateBin\Data\Filesystem;
|
2016-08-22 16:20:14 +02:00
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
|
|
|
use PrivateBin\Persistence\TrafficLimiter;
|
2019-07-08 19:56:05 +02:00
|
|
|
use PrivateBin\Request;
|
2016-08-09 11:54:42 +02:00
|
|
|
|
|
|
|
class ConfigurationCombinationsTest extends PHPUnit_Framework_TestCase
|
2015-08-29 01:26:48 +02:00
|
|
|
{
|
2016-08-09 11:54:42 +02:00
|
|
|
private $_conf;
|
|
|
|
|
2015-08-29 01:26:48 +02:00
|
|
|
private $_model;
|
|
|
|
|
2016-08-09 11:54:42 +02:00
|
|
|
private $_path;
|
2015-08-29 01:26:48 +02:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::confBackup();
|
2017-10-08 12:27:37 +02:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
2016-08-09 11:54:42 +02:00
|
|
|
$this->_model = Filesystem::getInstance(array('dir' => $this->_path));
|
2015-08-29 01:26:48 +02:00
|
|
|
$this->reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2017-10-08 12:27:37 +02:00
|
|
|
unlink(CONF);
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::confRestore();
|
|
|
|
Helper::rmDir($this->_path);
|
2017-10-08 12:27:37 +02:00
|
|
|
}
|
2015-08-29 01:26:48 +02:00
|
|
|
|
|
|
|
public function reset($configuration = array())
|
|
|
|
{
|
|
|
|
$_POST = array();
|
|
|
|
$_GET = array();
|
|
|
|
$_SERVER = array();
|
2016-08-22 16:20:14 +02:00
|
|
|
if ($this->_model->exists(Helper::getPasteId()))
|
|
|
|
$this->_model->delete(Helper::getPasteId());
|
|
|
|
$configuration['model_options']['dir'] = $this->_path;
|
2016-08-09 11:54:42 +02:00
|
|
|
Helper::createIniFile(CONF, $configuration);
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 20:29:14 +02:00
|
|
|
|
2015-08-29 01:26:48 +02:00
|
|
|
EOT;
|
2015-08-29 10:41:10 +02:00
|
|
|
}
|
2015-08-29 01:26:48 +02:00
|
|
|
|
2015-08-29 20:29:14 +02:00
|
|
|
/**
|
|
|
|
* get unit tests function block
|
|
|
|
*
|
|
|
|
* @param string $step
|
|
|
|
* @param int $key
|
|
|
|
* @param array $options
|
|
|
|
* @param array $preCode
|
|
|
|
* @param array $testCode
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-07-08 19:56:05 +02:00
|
|
|
private function _getFunction($step, $key, &$options, $preCode, $testCode, $discussionEnabled)
|
2015-08-29 20:29:14 +02:00
|
|
|
{
|
|
|
|
if (count($testCode) == 0) {
|
2016-10-31 11:32:12 +01:00
|
|
|
echo "skipping creation of test$step$key, no valid tests found for configuration: $options" . PHP_EOL;
|
2015-08-29 20:29:14 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$preString = $testString = '';
|
|
|
|
foreach ($preCode as $setting) {
|
|
|
|
$preString .= " $setting;" . PHP_EOL;
|
|
|
|
}
|
|
|
|
foreach ($testCode as $test) {
|
|
|
|
$type = $test[0];
|
|
|
|
$args = $test[1];
|
|
|
|
$testString .= " \$this->assert$type($args);" . PHP_EOL;
|
|
|
|
}
|
|
|
|
$code = <<<EOT
|
|
|
|
/**
|
|
|
|
* @runInSeparateProcess
|
|
|
|
*/
|
|
|
|
public function test$step$key()
|
|
|
|
{
|
|
|
|
\$this->reset($options);
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
// step specific initialization
|
|
|
|
switch ($step) {
|
|
|
|
case 'Create':
|
2019-07-08 19:56:05 +02:00
|
|
|
if ($discussionEnabled) {
|
|
|
|
$code .= PHP_EOL . <<<'EOT'
|
|
|
|
$paste = Helper::getPasteJson();
|
|
|
|
EOT;
|
|
|
|
} else {
|
|
|
|
$code .= PHP_EOL . <<<'EOT'
|
|
|
|
$paste = json_decode(Helper::getPasteJson(), true);
|
|
|
|
$paste['adata'][2] = 0;
|
|
|
|
$paste = json_encode($paste);
|
|
|
|
EOT;
|
|
|
|
}
|
2015-08-29 20:29:14 +02:00
|
|
|
$code .= PHP_EOL . <<<'EOT'
|
2019-07-08 19:56:05 +02:00
|
|
|
$file = tempnam(sys_get_temp_dir(), 'FOO');
|
|
|
|
file_put_contents($file, $paste);
|
|
|
|
Request::setInputStream($file);
|
2015-11-09 21:39:42 +01:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
2019-07-08 19:56:05 +02:00
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '::1';
|
2016-08-22 16:20:14 +02:00
|
|
|
TrafficLimiter::canPass();
|
2015-08-29 20:29:14 +02:00
|
|
|
EOT;
|
|
|
|
break;
|
|
|
|
case 'Read':
|
|
|
|
$code .= PHP_EOL . <<<'EOT'
|
2016-08-22 16:20:14 +02:00
|
|
|
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
|
2019-07-08 19:56:05 +02:00
|
|
|
$_SERVER['QUERY_STRING'] = Helper::getPasteId();
|
2019-01-21 23:49:33 +01:00
|
|
|
$_GET[Helper::getPasteId()] = '';
|
2018-07-01 16:25:50 +02:00
|
|
|
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest';
|
2015-08-29 20:29:14 +02:00
|
|
|
EOT;
|
|
|
|
break;
|
|
|
|
case 'Delete':
|
|
|
|
$code .= PHP_EOL . <<<'EOT'
|
2016-08-22 16:20:14 +02:00
|
|
|
$this->_model->create(Helper::getPasteId(), Helper::getPaste());
|
|
|
|
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists before deleting data');
|
|
|
|
$_GET['pasteid'] = Helper::getPasteId();
|
2019-05-10 07:55:39 +02:00
|
|
|
$_GET['deletetoken'] = hash_hmac('sha256', Helper::getPasteId(), $this->_model->read(Helper::getPasteId())['meta']['salt']);
|
2015-08-29 20:29:14 +02:00
|
|
|
EOT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all steps
|
|
|
|
$code .= PHP_EOL . $preString;
|
|
|
|
$code .= <<<'EOT'
|
|
|
|
ob_start();
|
2019-07-08 19:56:05 +02:00
|
|
|
new Controller;
|
2015-08-29 20:29:14 +02:00
|
|
|
$content = ob_get_contents();
|
2016-08-22 16:20:14 +02:00
|
|
|
ob_end_clean();
|
2015-08-29 20:29:14 +02:00
|
|
|
EOT;
|
|
|
|
|
|
|
|
// step specific tests
|
|
|
|
switch ($step) {
|
|
|
|
case 'Create':
|
|
|
|
$code .= <<<'EOT'
|
|
|
|
|
|
|
|
$response = json_decode($content, true);
|
|
|
|
EOT;
|
|
|
|
break;
|
|
|
|
case 'Read':
|
|
|
|
$code .= <<<'EOT'
|
|
|
|
|
2018-07-01 16:25:50 +02:00
|
|
|
$response = json_decode($content, true);
|
|
|
|
$this->assertEquals(0, $response['status'], 'outputs success status');
|
|
|
|
$this->assertEquals(Helper::getPasteId(), $response['id'], 'outputs id correctly');
|
|
|
|
$this->assertEquals(Helper::getPaste()['data'], $response['data'], 'outputs data correctly');
|
2015-08-29 20:29:14 +02:00
|
|
|
EOT;
|
|
|
|
break;
|
|
|
|
case 'Delete':
|
|
|
|
$code .= <<<'EOT'
|
|
|
|
|
2016-05-22 18:35:07 +02:00
|
|
|
$this->assertRegExp(
|
2016-08-22 16:20:14 +02:00
|
|
|
'#<div[^>]*id="status"[^>]*>.*Paste was properly deleted[^<]*</div>#s',
|
2015-08-29 20:29:14 +02:00
|
|
|
$content,
|
|
|
|
'outputs deleted status correctly'
|
|
|
|
);
|
2016-08-22 16:20:14 +02:00
|
|
|
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste successfully deleted');
|
2015-08-29 20:29:14 +02:00
|
|
|
EOT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $code . PHP_EOL . PHP_EOL . $testString . ' }' . PHP_EOL . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
2015-08-29 01:26:48 +02:00
|
|
|
/**
|
2015-08-29 10:41:10 +02:00
|
|
|
* recursive function to generate configurations based on options
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @return array
|
2015-08-29 01:26:48 +02:00
|
|
|
*/
|
2015-08-29 10:41:10 +02:00
|
|
|
private function _generateConfigurations()
|
2015-08-29 01:26:48 +02:00
|
|
|
{
|
2015-08-29 10:41:10 +02:00
|
|
|
// recursive factorial function
|
|
|
|
if (++$this->_iterationCount > self::MAX_ITERATIONS) {
|
|
|
|
echo 'max iterations reached, stopping', PHP_EOL;
|
|
|
|
return $this->_configurations;
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
2015-08-29 10:41:10 +02:00
|
|
|
echo "generateConfigurations: iteration $this->_iterationCount", PHP_EOL;
|
|
|
|
$continue = list($path, $settings) = each($this->_options);
|
|
|
|
if ($continue === false) {
|
|
|
|
return $this->_configurations;
|
|
|
|
}
|
|
|
|
list($section, $option) = explode('/', $path);
|
|
|
|
for ($c = 0, $max = count($this->_configurations); $c < $max; ++$c) {
|
|
|
|
if (!array_key_exists($section, $this->_configurations[$c]['options'])) {
|
|
|
|
$this->_configurations[$c]['options'][$section] = array();
|
|
|
|
}
|
|
|
|
if (count($settings) == 0) {
|
|
|
|
throw new Exception("Check your \$options: option $option has no settings!");
|
|
|
|
}
|
|
|
|
// set the first setting in the original configuration
|
|
|
|
$setting = current($settings);
|
|
|
|
$this->_addSetting($this->_configurations[$c], $setting, $section, $option);
|
2015-08-29 01:26:48 +02:00
|
|
|
|
2015-08-29 10:41:10 +02:00
|
|
|
// create clones for each of the other settings
|
|
|
|
while ($setting = next($settings)) {
|
2016-10-29 10:24:08 +02:00
|
|
|
$clone = $this->_configurations[$c];
|
2015-08-29 10:41:10 +02:00
|
|
|
$this->_configurations[] = $this->_addSetting($clone, $setting, $section, $option);
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
2015-08-29 10:41:10 +02:00
|
|
|
reset($settings);
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
2015-08-29 10:41:10 +02:00
|
|
|
return $this->_generateConfigurations();
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 10:41:10 +02:00
|
|
|
/**
|
|
|
|
* add a setting to the given configuration
|
|
|
|
*
|
|
|
|
* @param array $configuration
|
|
|
|
* @param array $setting
|
|
|
|
* @param string $section
|
|
|
|
* @param string $option
|
|
|
|
* @throws Exception
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-07-26 08:19:35 +02:00
|
|
|
private function _addSetting(&$configuration, &$setting, &$section, &$option)
|
|
|
|
{
|
2015-08-29 10:41:10 +02:00
|
|
|
if (++$this->_iterationCount > self::MAX_ITERATIONS) {
|
|
|
|
echo 'max iterations reached, stopping', PHP_EOL;
|
|
|
|
return $configuration;
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
2015-08-29 10:41:10 +02:00
|
|
|
echo "addSetting: iteration $this->_iterationCount", PHP_EOL;
|
|
|
|
if (
|
|
|
|
array_key_exists($option, $configuration['options'][$section]) &&
|
|
|
|
$configuration['options'][$section][$option] === $setting['setting']
|
|
|
|
) {
|
2016-08-09 11:54:42 +02:00
|
|
|
$val = Helper::varExportMin($setting['setting'], true);
|
2015-08-29 10:41:10 +02:00
|
|
|
throw new Exception("Endless loop or error in options detected: option '$option' already exists with setting '$val' in one of the configurations!");
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
2015-08-29 10:41:10 +02:00
|
|
|
$configuration['options'][$section][$option] = $setting['setting'];
|
2016-10-29 10:24:08 +02:00
|
|
|
$configuration['tests'][$option] = array($setting['tests'], $setting['affects']);
|
2015-08-29 10:41:10 +02:00
|
|
|
foreach ($setting['affects'] as $affects) {
|
|
|
|
if (!in_array($affects, $configuration['affects'])) {
|
|
|
|
$configuration['affects'][] = $affects;
|
|
|
|
}
|
2015-08-29 01:26:48 +02:00
|
|
|
}
|
|
|
|
return $configuration;
|
|
|
|
}
|
|
|
|
}
|