Skip to content

Commit

Permalink
Update and fix conditionals tests
Browse files Browse the repository at this point in the history
  • Loading branch information
denzel-farmer committed Mar 27, 2024
1 parent 927d66f commit 6d6d328
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
13 changes: 11 additions & 2 deletions pytests/php-samples/conditions_field_get.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

class SomeClass {
public $getfield;

public function foo() {
return "foo";
}
}


/* Main purpose for this test - capture the method-call condition */

$object = 'O:6:"Logger":2:{s:7:"logtype";s:9:"TEMPORARY";s:3:"log";O:8:"TempFile":1:{s:8:"filename";s:9:"FILE_PATH";}}';
Expand All @@ -8,7 +17,7 @@
//
// test field access: get
//
$readval = $data->logtypeget;
print($readval);
$readval = $data->getfield;


?>
12 changes: 10 additions & 2 deletions pytests/php-samples/conditions_field_set.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

class SomeClass {
public $setfield;

public function foo() {
return "foo";
}
}


/* Main purpose for this test - capture the method-call condition */

$object = 'O:6:"Logger":2:{s:7:"logtype";s:9:"TEMPORARY";s:3:"log";O:8:"TempFile":1:{s:8:"filename";s:9:"FILE_PATH";}}';
Expand All @@ -8,8 +17,7 @@
//
// test field access: set
//
$data->logtypeput = "NEWVAL";
print($data->logtypeput);
$data->setfield = "NEWVAL";


?>
37 changes: 29 additions & 8 deletions pytests/php-samples/conditions_two_calls_test.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
<?php

class LoneClass {
public function lone_call() {
return "foo";
}
}

class SecondClass {
public function second_call() {
return "bar";
}

}

class FirstClass {
public function first_call() {
$second_class_obj = new SecondClass();

return $second_class_obj;
}
}




/* Main purpose for this test - capture the method-call condition */

$object = 'O:6:"Logger":2:{s:7:"logtype";s:9:"TEMPORARY";s:3:"log";O:8:"TempFile":1:{s:8:"filename";s:9:"FILE_PATH";}}';
$data = unserialize($object); // POI bug
//
// this is currently ignored by our analysis
//
$data->hey();
//
// this is currently caught by our analysis
//
$data->ola()->to_you_to();
$data->lone_call();


$data2 = unserialize($object); // POI bug
$data2->first_call()->second_call();

?>
16 changes: 8 additions & 8 deletions pytests/test_conditions_fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@


@pytest.mark.datafiles(SAMPLES_DIR / CONDITIONS_FIELD_GET_NAME)
@pytest.mark.skip()
def test_conditions_field_get(datafiles, tmp_path):
fragment_path = datafiles / CONDITIONS_FIELD_GET_NAME

results = do_analysis(fragment_path, tmp_path)

expected_result = [
{'filename': 'conditions_field_get.php', 'lineNumber': 6,
'allowedTypes': [''], 'allowedClasses': []}
{'filename': 'conditions_field_get.php', 'lineNumber': 15,
'allowedTypes': ['', 'SomeClass'], 'allowedClasses': ['SomeClass']}
]

assert compare_results(expected_result, results)
Expand All @@ -26,14 +25,13 @@ def test_conditions_field_get(datafiles, tmp_path):


@pytest.mark.datafiles(SAMPLES_DIR / CONDITIONS_FIELD_SET_NAME)
@pytest.mark.skip()
def test_conditions_field_set(datafiles, tmp_path):
fragment_path = datafiles / CONDITIONS_FIELD_SET_NAME

results = do_analysis(fragment_path, tmp_path)

expected_result = [{'filename': 'conditions_field_set.php',
'lineNumber': 6, 'allowedTypes': [''], 'allowedClasses': []}]
'lineNumber': 15, 'allowedTypes': ['SomeClass'], 'allowedClasses': ['SomeClass']}]

assert compare_results(expected_result, results)

Expand All @@ -57,20 +55,22 @@ def test_conditions_test(datafiles, tmp_path):


@pytest.mark.datafiles(SAMPLES_DIR / CONDITIONS_TWO_CALLS_TEST_NAME)
@pytest.mark.skip()
def test_conditions_two_calls_test(datafiles, tmp_path):
fragment_path = datafiles / CONDITIONS_TWO_CALLS_TEST_NAME

results = do_analysis(fragment_path, tmp_path)

expected_result = [{'filename': 'conditions_two_calls_test.php',
'lineNumber': 6, 'allowedTypes': ['', ''], 'allowedClasses': []}]

'lineNumber': 30, 'allowedTypes': ['LoneClass'], 'allowedClasses': ['LoneClass']},
{'filename': 'conditions_two_calls_test.php', 'lineNumber': 34,
'allowedTypes': ['FirstCall'], 'allowedClasses': ['FirstCall']}
]
assert compare_results(expected_result, results)


CONDITIONALS_TEST_NAME = "conditionals_test.php"


@pytest.mark.datafiles(SAMPLES_DIR / CONDITIONALS_TEST_NAME)
def test_conditionals(datafiles, tmp_path):
fragment_path = datafiles / CONDITIONALS_TEST_NAME
Expand Down

0 comments on commit 6d6d328

Please sign in to comment.