Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(IntegrityCheck): Ensure the check is run if no results are available #46174

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/settings/lib/Controller/CheckSetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function getFailedIntegrityCheckFiles(): DataDisplayResponse {

$completeResults = $this->checker->getResults();

if ($completeResults === null) {
return new DataDisplayResponse('Integrity checker has not been run. Integrity information not available.');
}

if (!empty($completeResults)) {
$formattedTextResponse = 'Technical information
=====================
Expand Down
9 changes: 8 additions & 1 deletion apps/settings/lib/SetupChecks/CodeIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ public function getCategory(): string {
public function run(): SetupResult {
if (!$this->checker->isCodeCheckEnforced()) {
return SetupResult::info($this->l10n->t('Integrity checker has been disabled. Integrity cannot be verified.'));
} elseif ($this->checker->hasPassedCheck()) {
}

// If there are no results we need to run the verification
if ($this->checker->getResults() === null) {
$this->checker->runInstanceVerification();
}

if ($this->checker->hasPassedCheck()) {
return SetupResult::success($this->l10n->t('No altered files'));
} else {
return SetupResult::error(
Expand Down
135 changes: 135 additions & 0 deletions apps/settings/tests/SetupChecks/CodeIntegrityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Settings\Tests;

use OC\IntegrityCheck\Checker;
use OCA\Settings\SetupChecks\CodeIntegrity;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\SetupCheck\SetupResult;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class CodeIntegrityTest extends TestCase {

private IL10N&MockObject $l10n;
private IURLGenerator&MockObject $urlGenerator;
private Checker&MockObject $checker;

protected function setUp(): void {
parent::setUp();

$this->l10n = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()->getMock();
$this->l10n->expects($this->any())
->method('t')
->willReturnCallback(function ($message, array $replace) {
return vsprintf($message, $replace);
});
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->checker = $this->createMock(Checker::class);
}

public function testSkipOnDisabled(): void {
$this->checker->expects($this->atLeastOnce())
->method('isCodeCheckEnforced')
->willReturn(false);

$check = new CodeIntegrity(
$this->l10n,
$this->urlGenerator,
$this->checker,
);
$this->assertEquals(SetupResult::INFO, $check->run()->getSeverity());
}

public function testSuccessOnEmptyResults(): void {
$this->checker->expects($this->atLeastOnce())
->method('isCodeCheckEnforced')
->willReturn(true);
$this->checker->expects($this->atLeastOnce())
->method('getResults')
->willReturn([]);
$this->checker->expects(($this->atLeastOnce()))
->method('hasPassedCheck')
->willReturn(true);

$check = new CodeIntegrity(
$this->l10n,
$this->urlGenerator,
$this->checker,
);
$this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
}

public function testCheckerIsReRunWithoutResults(): void {
$this->checker->expects($this->atLeastOnce())
->method('isCodeCheckEnforced')
->willReturn(true);
$this->checker->expects($this->atLeastOnce())
->method('getResults')
->willReturn(null);
$this->checker->expects(($this->atLeastOnce()))
->method('hasPassedCheck')
->willReturn(true);

// This is important and must be called
$this->checker->expects($this->once())
->method('runInstanceVerification');

$check = new CodeIntegrity(
$this->l10n,
$this->urlGenerator,
$this->checker,
);
$this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
}

public function testCheckerIsNotReReInAdvance(): void {
$this->checker->expects($this->atLeastOnce())
->method('isCodeCheckEnforced')
->willReturn(true);
$this->checker->expects($this->atLeastOnce())
->method('getResults')
->willReturn(['mocked']);
$this->checker->expects(($this->atLeastOnce()))
->method('hasPassedCheck')
->willReturn(true);

// There are results thus this must never be called
$this->checker->expects($this->never())
->method('runInstanceVerification');

$check = new CodeIntegrity(
$this->l10n,
$this->urlGenerator,
$this->checker,
);
$this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity());
}

public function testErrorOnMissingIntegrity(): void {
$this->checker->expects($this->atLeastOnce())
->method('isCodeCheckEnforced')
->willReturn(true);
$this->checker->expects($this->atLeastOnce())
->method('getResults')
->willReturn(['mocked']);
$this->checker->expects(($this->atLeastOnce()))
->method('hasPassedCheck')
->willReturn(false);

$check = new CodeIntegrity(
$this->l10n,
$this->urlGenerator,
$this->checker,
);
$this->assertEquals(SetupResult::ERROR, $check->run()->getSeverity());
}
}
15 changes: 10 additions & 5 deletions lib/private/IntegrityCheck/Checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,23 +373,28 @@ private function verify(string $signaturePath, string $basePath, string $certifi
*/
public function hasPassedCheck(): bool {
$results = $this->getResults();
if (empty($results)) {
if ($results !== null && empty($results)) {
return true;
}

return false;
}

/**
* @return array
* @return array|null Either the results or null if no results available
*/
public function getResults(): array {
public function getResults(): array|null {
$cachedResults = $this->cache->get(self::CACHE_KEY);
if (!\is_null($cachedResults) and $cachedResults !== false) {
return json_decode($cachedResults, true);
}

return $this->appConfig?->getValueArray('core', self::CACHE_KEY, lazy: true) ?? [];
if ($this->appConfig?->hasKey('core', self::CACHE_KEY, lazy: true)) {
return $this->appConfig->getValueArray('core', self::CACHE_KEY, lazy: true);
}

// No results available
return null;
}

/**
Expand All @@ -399,7 +404,7 @@ public function getResults(): array {
* @param array $result
*/
private function storeResults(string $scope, array $result) {
$resultArray = $this->getResults();
$resultArray = $this->getResults() ?? [];
unset($resultArray[$scope]);
if (!empty($result)) {
$resultArray[$scope] = $result;
Expand Down
Loading