Skip to content

Commit 5a73606

Browse files
committed
enhance tests with mocked composer's runtime api
Test detection of packages when Composer is used but package metadata is bogus: - package_name is null, package_version is valid - package_name is valid, package_version is null - package_name is null, package_version is null.
1 parent f6b438a commit 5a73606

File tree

7 files changed

+194
-2
lines changed

7 files changed

+194
-2
lines changed

agent/lib_composer.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,18 @@ static void nr_execute_handle_autoload_composer_get_packages_information(
9696
" $packages = array();"
9797
" foreach (\\Composer\\InstalledVersions::getAllRawData() as $installed) { "
9898
" foreach ($installed['versions'] as $packageName => $packageData) {"
99+
" if (!is_string($packageName)) {"
100+
" continue;"
101+
" }"
99102
" if (is_array($root_package) && array_key_exists('name', $root_package) && $packageName == $root_package['name']) {"
100103
" continue;"
101104
" }"
102-
" if (isset($packageData['pretty_version'])) {"
103-
" $packages[$packageName] = ltrim($packageData['pretty_version'], 'v');"
105+
" if (!array_key_exists('pretty_version', $packageData)) {"
106+
" continue;"
107+
" }"
108+
" $pretty_version = $packageData['pretty_version'];"
109+
" if (is_string($pretty_version)) {"
110+
" $packages[$packageName] = ltrim($pretty_version, 'v');"
104111
" }"
105112
" }"
106113
" }"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is the usual entry point for installing and using PSR-4 Autoloader.
9+
The agent verifies its presence to determine if PSR-4 Autoloader is used.
10+
*/
11+
12+
require_once __DIR__ . '/composer/autoload_real.php';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is the core of real Composer's runtime API, and agent verifies its presence to determine if Composer is used.
9+
It contains \Composer\InstalledVersions class with methods used by the agent to get the installed packages and their versions.
10+
*/
11+
12+
namespace Composer;
13+
class InstalledVersions
14+
{
15+
// This Composer's runtime API method is used by the agent to get the list of installed packages:
16+
public static function getAllRawData()
17+
{
18+
$installed = require __DIR__ . '/installed.php';
19+
// This mock only returns a single dataset; in real life, there could be more
20+
return array($installed);
21+
}
22+
23+
// This Composer's runtime API method is used by the agent to get the root package:
24+
public static function getRootPackage()
25+
{
26+
$installed = self::getAllRawData();
27+
// This mock only returns a single dataset; in real life, there could be more
28+
return $installed[0]['root'];
29+
}
30+
31+
// Mock of 'composer show' used by integration tests to generate list of packages:
32+
public static function show() {
33+
$installed = self::getAllRawData();
34+
foreach ($installed[0]['versions'] as $package => $info) {
35+
if (!is_string($package)) {
36+
continue;
37+
}
38+
if (!is_array($info)) {
39+
continue;
40+
}
41+
if (!array_key_exists('pretty_version', $info)) {
42+
continue;
43+
}
44+
if (!is_string($info['pretty_version'])) {
45+
continue;
46+
}
47+
$version = ltrim($info['pretty_version'], 'v');
48+
echo "$package => $version\n";
49+
}
50+
}
51+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is needed by real Composer's Autoload functionality, and agent uses its presence to determine if Composer is used.
9+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
This file is needed by real Composer's runtime API, and agent verifies its presence to determine if Composer is used.
9+
This file contains phpized version of composer.json for the project and its dependencies.
10+
*/
11+
return array(
12+
// Mocked data: root package
13+
'root' => array(
14+
'pretty_version' => 'v1.0.0',
15+
'version' => '1.0.0.0',
16+
'type' => 'project'
17+
),
18+
// Mocked invalid package data:
19+
// - package without name and version
20+
// - package without name but with version
21+
// - package with name but without version
22+
// Mocked valid package data:
23+
// - package with name and version
24+
'versions' => array(
25+
array(
26+
'version' => '1.1.3.0',
27+
'type' => 'library'
28+
),
29+
array(
30+
'pretty_version' => 'v2.1.3',
31+
'version' => '2.1.3.0',
32+
'type' => 'library'
33+
),
34+
'vendor2/package2' => array(
35+
'version' => '3.1.5.0',
36+
'type' => 'library'
37+
),
38+
'laravel/framework' => array(
39+
'pretty_version' => '11.4.5',
40+
'version' => '11.4.5',
41+
'type' => 'library'
42+
),
43+
)
44+
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
A very simple mock of laravel/framework package used to similate detection of Laravel framework
9+
which results in php package harvest.
10+
*/
11+
12+
namespace Illuminate\Foundation;
13+
14+
class Application
15+
{
16+
const VERSION = '11.4.5';
17+
18+
public function __construct()
19+
{
20+
echo "";
21+
}
22+
}
23+
24+
// force detection on PHP 8.2+
25+
echo "";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
Test detection of packages when Composer is used but package metadata is bogus:
9+
- package_name is null, package_version is valid
10+
- package_name is valid, package_version is null
11+
- package_name is null, package_version is null.
12+
Supportability metric for Autoloader and Composer libraries should be present because
13+
composer install is not broken. However the agent should create package harvest only
14+
for packages with valid (non null and of type string) package name and version.
15+
No errors should be generated.
16+
*/
17+
18+
/*INI
19+
newrelic.loglevel=verbosedebug
20+
newrelic.vulnerability_management.composer_api.enabled=true
21+
*/
22+
23+
/*EXPECT_PHP_PACKAGES
24+
command=php composer-show.php packages-with-broken-composer-02/vendor/composer/InstalledVersions.php
25+
expected_packages=laravel/framework
26+
*/
27+
28+
/*EXPECT_METRICS_EXIST
29+
Supportability/library/Autoloader/detected, 1
30+
Supportability/library/Composer/detected, 1
31+
Supportability/PHP/package/laravel/framework/11/detected
32+
*/
33+
34+
/*EXPECT_METRICS_DONT_EXIST
35+
*/
36+
37+
/*EXPECT_TRACED_ERRORS null*/
38+
39+
// Simulate autoloader usage:
40+
require 'packages-with-broken-composer-02/vendor/autoload.php';
41+
// Simulate package usage (normally this would be done by the autoloader):
42+
include 'packages-with-broken-composer-02/vendor/laravel/framework/src/Illuminate/Foundation/Application.php';
43+
// Trigger instrumentation that generates packages and package-specific metrics:
44+
$app = new Illuminate\Foundation\Application();

0 commit comments

Comments
 (0)