Skip to content

Commit

Permalink
Update tests for Code Obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Dec 29, 2023
1 parent b7d3247 commit 7373989
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Plugin Name: Test Plugin Code Obfuscation ionCube Multiple Errors
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Some plugin description.
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-code-obfuscation-ioncube-multiple-errors
*
* @package test-plugin-code-obfuscation-ioncube-multiple-errors
*/

/**
* Just the word "ionCube" in this file will trigger the detection.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php // Just the word "ionCube" in this file will trigger the detection.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Plugin Name: Test Plugin Code Obfuscation Source Guardian Multiple Errors
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Some plugin description.
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-code-obfuscation-sourceguardian-multiple-errors
*
* @package test-plugin-code-obfuscation-sourceguardian-multiple-errors
*/

/**
* Files contain code which is used to detect Source Guardian obfuscated files.
*/
require_once __DIR__ . '/obfuscated.php';
require_once __DIR__ . '/obfuscated2.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
if(function_exists('sg_load')) {
$random_global = 'random_string';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$domain = 'sourceguardian.com';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Plugin Name: Test Plugin Code Obfuscation Zend Guard Multiple Errors
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Some plugin description.
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/performance/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-code-obfuscation-zendguard-multiple-errors
*
* @package test-plugin-code-obfuscation-zendguard-multiple-errors
*/

// This constant is defined here to prevent fatal errors from the file below.
if ( ! defined( 'Zend' ) ) {
define( 'Zend', true );
}

/**
* Files contain code which is used to detect Zend Guard obfuscated files.
*/
require_once __DIR__ . '/obfuscated.php';
require_once __DIR__ . '/obfuscated2.php';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php @Zend;

$random_global = 'random_string';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$message = 'This file was encoded by Zend';
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ public function test_run_with_obfuscation_errors( $type_flag, $plugin_basename,
$this->assertSame( 'obfuscated_code_detected', $errors[ $expected_file ][0][0][0]['code'] );
}

/**
* @dataProvider data_obfuscation_services_multiple_files
*/
public function test_run_with_obfuscation_multiple_errors( $type_flag, $plugin_basename, $expected_files ) {
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . $plugin_basename );
$check_result = new Check_Result( $check_context );

$check = new Code_Obfuscation_Check( $type_flag );
$check->run( $check_result );

$errors = $check_result->get_errors();

$this->assertNotEmpty( $errors );
$this->assertSame( sort( $expected_files ), sort( array_keys( $errors ) ) );
$this->assertSame( 2, $check_result->get_error_count() );

$this->assertTrue( isset( $errors[ $expected_files[0] ][0][0][0] ) );
$this->assertSame( 'obfuscated_code_detected', $errors[ $expected_files[0] ][0][0][0]['code'] );
$this->assertTrue( isset( $errors[ $expected_files[1] ][0][0][0] ) );
$this->assertSame( 'obfuscated_code_detected', $errors[ $expected_files[1] ][0][0][0]['code'] );
}

public function data_obfuscation_services() {
return array(
'Zend Guard' => array(
Expand All @@ -52,6 +74,26 @@ public function data_obfuscation_services() {
);
}

public function data_obfuscation_services_multiple_files() {
return array(
'Zend Guard Multiple' => array(
Code_Obfuscation_Check::TYPE_ZEND,
'test-plugin-code-obfuscation-zendguard-multiple-errors/load.php',
array( 'obfuscated.php', 'obfuscated2.php' ),
),
'Source Guardian Multiple' => array(
Code_Obfuscation_Check::TYPE_SOURCEGUARDIAN,
'test-plugin-code-obfuscation-sourceguardian-multiple-errors/load.php',
array( 'obfuscated.php', 'obfuscated2.php' ),
),
'ionCube Multiple' => array(
Code_Obfuscation_Check::TYPE_IONCUBE,
'test-plugin-code-obfuscation-ioncube-multiple-errors/load.php',
array( 'load.php', 'obfuscated.php' ),
),
);
}

public function test_run_without_any_obfuscation_errors() {
// Test plugin without any obfuscation.
$check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-i18n-usage-without-errors/load.php' );
Expand Down

0 comments on commit 7373989

Please sign in to comment.