2727use OC \IntegrityCheck \Helpers \FileAccessHelper ;
2828use OC \IntegrityCheck \Verifier \Verifier ;
2929use OC \IntegrityCheck \Verifier \VerificationResult ;
30+ use OC \Memcache \ArrayCache ;
3031use OC \Memcache \NullCache ;
3132use OC \Memcache \Redis ;
3233use OCP \App \IAppManager ;
33- use OCP \ICacheFactory ;
3434use OCP \IConfig ;
35+ use Test \Memcache \FixedCacheFactory ;
3536use Test \TestCase ;
3637
3738/**
@@ -48,7 +49,7 @@ class CheckerTest extends TestCase {
4849 private $ fileAccessHelper ;
4950 /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
5051 private $ config ;
51- /** @var ICacheFactory | \PHPUnit\Framework\MockObject\MockObject */
52+ /** @var FixedCacheFactory */
5253 private $ cacheFactory ;
5354 /** @var IAppManager | \PHPUnit\Framework\MockObject\MockObject */
5455 private $ appManager ;
@@ -61,7 +62,7 @@ public function setUp(): void {
6162 $ this ->fileAccessHelper = $ this ->createMock (FileAccessHelper::class);
6263 $ this ->appLocator = $ this ->createMock (AppLocator::class);
6364 $ this ->config = $ this ->createMock (IConfig::class);
64- $ this ->cacheFactory = $ this -> createMock (ICacheFactory::class );
65+ $ this ->cacheFactory = new FixedCacheFactory ( new NullCache () );
6566 $ this ->appManager = $ this ->createMock (IAppManager::class);
6667 $ this ->verifier = $ this ->createMock (Verifier::class);
6768
@@ -87,12 +88,6 @@ public function setUp(): void {
8788 ->method ('getAllApps ' )
8889 ->willReturn ([]);
8990
90- $ this ->cacheFactory
91- ->expects ($ this ->any ())
92- ->method ('create ' )
93- ->with ('oc.integritycheck.checker ' )
94- ->willReturn (new NullCache ());
95-
9691 $ this ->checker = new Checker (
9792 $ this ->environmentHelper ,
9893 $ this ->fileAccessHelper ,
@@ -103,6 +98,69 @@ public function setUp(): void {
10398 \OC ::$ server ->getTempManager (),
10499 $ this ->verifier
105100 );
101+
102+ $ this ->assertSame ([Checker::CACHE_KEY ], $ this ->cacheFactory ->getRequestedPrefixes ());
103+ }
104+
105+ /**
106+ * The results describe the files on disk of this host, so they belong in the
107+ * host local cache tier - not in a distributed one where another host could
108+ * hand back a verdict about an installation it cannot see.
109+ */
110+ public function testUsesTheLocalCacheTier () {
111+ $ cacheFactory = $ this ->createMock (FixedCacheFactory::class);
112+ $ cacheFactory ->expects ($ this ->once ())
113+ ->method ('createLocal ' )
114+ ->with (Checker::CACHE_KEY )
115+ ->willReturn (new NullCache ());
116+ $ cacheFactory ->expects ($ this ->never ())->method ('createDistributed ' );
117+ $ cacheFactory ->expects ($ this ->never ())->method ('create ' );
118+
119+ new Checker (
120+ $ this ->environmentHelper ,
121+ $ this ->fileAccessHelper ,
122+ $ this ->appLocator ,
123+ $ this ->config ,
124+ $ cacheFactory ,
125+ $ this ->appManager ,
126+ \OC ::$ server ->getTempManager (),
127+ $ this ->verifier
128+ );
129+ }
130+
131+ /**
132+ * storeResults() writes one entry per scope next to CACHE_KEY, and a rescan
133+ * has to invalidate all of them - removing CACHE_KEY alone left every per app
134+ * verdict cached forever.
135+ */
136+ public function testRescanningDropsThePerAppResults () {
137+ $ cache = new ArrayCache ();
138+ $ checker = new Checker (
139+ $ this ->environmentHelper ,
140+ $ this ->fileAccessHelper ,
141+ $ this ->appLocator ,
142+ $ this ->config ,
143+ new FixedCacheFactory ($ cache ),
144+ $ this ->appManager ,
145+ \OC ::$ server ->getTempManager (),
146+ $ this ->verifier
147+ );
148+
149+ $ this ->environmentHelper ->method ('getChannel ' )->willReturn ('stable ' );
150+ $ this ->environmentHelper ->method ('getServerRoot ' )->willReturn (\OC ::$ SERVERROOT );
151+ $ this ->verifier ->method ('verify ' )->willReturn (VerificationResult::passed ());
152+
153+ $ cache ->set ('SomeApp ' , '{"SomeApp":[]} ' );
154+ $ cache ->set ('SomeOtherApp ' , '{"SomeOtherApp":[]} ' );
155+ $ cache ->set (Checker::CACHE_KEY , '{"SomeApp":[]} ' );
156+
157+ $ checker ->runInstanceVerification ();
158+
159+ // only the results of this run are left - the verification passed, so
160+ // there is nothing to report
161+ $ this ->assertNull ($ cache ->get ('SomeApp ' ));
162+ $ this ->assertNull ($ cache ->get ('SomeOtherApp ' ));
163+ $ this ->assertSame ('[] ' , $ cache ->get (Checker::CACHE_KEY ));
106164 }
107165
108166 public function testIgnoredAppSignatureWithoutSignatureData () {
@@ -630,12 +688,7 @@ public function testVerifyCachedAppSignatureCheck() {
630688 $ redisObj ->method ('get ' )
631689 ->with ('SomeApp ' )
632690 ->willReturn ('[] ' );
633- $ cacheFactory = $ this ->createMock (ICacheFactory::class);
634- $ cacheFactory
635- ->expects ($ this ->any ())
636- ->method ('create ' )
637- ->with ('oc.integritycheck.checker ' )
638- ->will ($ this ->returnValue ($ redisObj ));
691+ $ cacheFactory = new FixedCacheFactory ($ redisObj );
639692 $ checker = new Checker (
640693 $ this ->environmentHelper ,
641694 $ this ->fileAccessHelper ,
@@ -654,12 +707,7 @@ public function testAppNotCachedSignatureCheck() {
654707 $ redisObj ->method ('get ' )
655708 ->with ('SomeApp ' )
656709 ->willReturn (null );
657- $ cacheFactory = $ this ->createMock (ICacheFactory::class);
658- $ cacheFactory
659- ->expects ($ this ->any ())
660- ->method ('create ' )
661- ->with ('oc.integritycheck.checker ' )
662- ->will ($ this ->returnValue ($ redisObj ));
710+ $ cacheFactory = new FixedCacheFactory ($ redisObj );
663711 $ checker = new Checker (
664712 $ this ->environmentHelper ,
665713 $ this ->fileAccessHelper ,
@@ -702,10 +750,7 @@ public function testHasPassedCheckWithExceptionResult() {
702750 ]
703751 ]));
704752
705- $ cacheFactory = $ this ->createMock (ICacheFactory::class);
706- $ cacheFactory ->expects ($ this ->any ())
707- ->method ('create ' )
708- ->willReturn (new NullCache ());
753+ $ cacheFactory = new FixedCacheFactory (new NullCache ());
709754
710755 $ checker = new Checker (
711756 $ this ->environmentHelper ,
@@ -732,10 +777,7 @@ public function testHasPassedCheckWithEmptyResults() {
732777 ->with ('core ' , 'oc.integritycheck.checker ' , '{} ' )
733778 ->willReturn ('{} ' );
734779
735- $ cacheFactory = $ this ->createMock (ICacheFactory::class);
736- $ cacheFactory ->expects ($ this ->any ())
737- ->method ('create ' )
738- ->willReturn (new NullCache ());
780+ $ cacheFactory = new FixedCacheFactory (new NullCache ());
739781
740782 $ checker = new Checker (
741783 $ this ->environmentHelper ,
@@ -766,10 +808,7 @@ public function testHasPassedCheckWithFileMissing() {
766808 ]
767809 ]));
768810
769- $ cacheFactory = $ this ->createMock (ICacheFactory::class);
770- $ cacheFactory ->expects ($ this ->any ())
771- ->method ('create ' )
772- ->willReturn (new NullCache ());
811+ $ cacheFactory = new FixedCacheFactory (new NullCache ());
773812
774813 $ checker = new Checker (
775814 $ this ->environmentHelper ,
0 commit comments