Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.commcare.formplayer.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.commcare.cases.instance.CaseInstanceTreeElement;
import org.commcare.cases.model.Case;
import org.commcare.cases.query.QueryContext;
import org.commcare.formplayer.sandbox.SqlStorage;
import org.commcare.modern.engine.cases.RecordObjectCache;
import org.javarosa.core.model.instance.InstanceBase;
import org.javarosa.core.services.storage.IStorageUtilityIndexed;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

/**
* Reproduces USH-6370: CaseInstanceTreeElement.getStorageCacheName() returns "casedb"
* for ALL instances, causing RecordObjectCache collisions between the user's casedb
* and case search results when they share a QueryContext.
*/
public class CaseSearchCacheCollisionTest {

private static final String DB_FILE = "test_cache_collision.db";
private Connection connection;

@BeforeEach
public void setUp() throws Exception {
new File(DB_FILE).delete();
connection = DriverManager.getConnection("jdbc:sqlite:" + DB_FILE);
}

@AfterEach
public void tearDown() throws Exception {
if (connection != null && !connection.isClosed()) {
connection.close();
}
new File(DB_FILE).delete();
}

/**
* Demonstrates that two CaseInstanceTreeElement instances — one for the user's
* casedb and one for case search results — return the same getStorageCacheName().
* This shared cache key is the root cause of USH-6370.
*/
@Test
public void testStorageCacheNameCollision() {
SqlStorage<Case> casedbStorage = new SqlStorage<>(
() -> connection, Case.class, "user_casedb");
SqlStorage<Case> searchResultsStorage = new SqlStorage<>(
() -> connection, Case.class, "search_results");

CaseInstanceTreeElement casedbInstance = new CaseInstanceTreeElement(
new InstanceBase("casedb"), casedbStorage);
CaseInstanceTreeElement resultsInstance = new CaseInstanceTreeElement(
new InstanceBase("results"), searchResultsStorage);

// Both instances return "casedb" regardless of which storage they wrap
assertEquals(casedbInstance.getStorageCacheName(), resultsInstance.getStorageCacheName(),
"Both instances return the same storageCacheName, causing cache collisions");
Comment on lines +65 to +66
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The new test CaseSearchCacheCollisionTest.java asserts known incorrect behavior, verifying that two different storage instances produce the same cache name. This test will fail when the bug is fixed.
Severity: LOW

Suggested Fix

The test should assert the correct behavior, which is that different storage instances have different cache names. Use assertNotEquals instead of assertEquals to validate that casedbInstance.getStorageCacheName() and resultsInstance.getStorageCacheName() are distinct. Alternatively, if this test is intended to be temporary, add an @Disabled or @Ignore annotation and a comment explaining that it should be updated or removed when the underlying bug is fixed.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location:
src/test/java/org/commcare/formplayer/tests/CaseSearchCacheCollisionTest.java#L65-L66

Potential issue: The new test file `CaseSearchCacheCollisionTest.java` is designed to
pass when a known bug (cache key collision) is present. The test asserts that different
storage instances produce the same cache name, for example
`assertEquals(casedbInstance.getStorageCacheName(),
resultsInstance.getStorageCacheName())`. This is the opposite of the desired behavior.
While this test passes now, it will fail when the underlying bug is fixed, creating a
future maintenance burden as the test will need to be inverted or deleted. The test
lacks any `@Disabled` annotations or comments to indicate its temporary nature.

Did we get this right? 👍 / 👎 to inform future reviews.

}

/**
* Demonstrates the full collision: when casedb records are bulk-loaded into the
* shared RecordObjectCache, a subsequent lookup for a search-results record with
* the same SQLite row ID returns the casedb record instead.
*
* In production this manifests as case search results showing data (case_type,
* case_name, etc.) from the user's casedb instead of from the search results.
*/
@Test
public void testRecordObjectCacheCrossContamination() {
// Two CaseInstanceTreeElement instances backed by different storages
IStorageUtilityIndexed<Case> casedbStorage = new SqlStorage<>(
() -> connection, Case.class, "user_casedb");
IStorageUtilityIndexed<Case> searchResultsStorage = new SqlStorage<>(
() -> connection, Case.class, "search_results");

CaseInstanceTreeElement casedbInstance = new CaseInstanceTreeElement(
new InstanceBase("casedb"), casedbStorage);
CaseInstanceTreeElement resultsInstance = new CaseInstanceTreeElement(
new InstanceBase("results"), searchResultsStorage);

// Create a shared QueryContext with scope above BULK_QUERY_THRESHOLD
// (in production, both instances share the EvaluationContext's QueryContext)
QueryContext context = new QueryContext();
context = context.checkForDerivativeContextAndReturn(100);

// Simulate bulk loading from the casedb instance: a Case of type "person"
// gets loaded into the RecordObjectCache under key ("casedb", 1)
Case casedbCase = new Case("Alice", "person");
casedbCase.setCaseId("casedb-case-1");
casedbCase.setID(1);

RecordObjectCache<Case> recordCache = context.getQueryCache(RecordObjectCache.class);
String casedbCacheName = casedbInstance.getStorageCacheName();
recordCache.getLoadedCaseMap(casedbCacheName).put(1, casedbCase);

// Now the search results instance has a different Case at record ID 1,
// type "facility" — but it will never see it through the cache
String resultsCacheName = resultsInstance.getStorageCacheName();

// The cache keys are identical — this is the bug
assertEquals("casedb", casedbCacheName);
assertEquals("casedb", resultsCacheName);

// The results instance asks for record ID 1 and gets the casedb's Case
assertTrue(recordCache.isLoaded(resultsCacheName, 1),
"Cache reports record 1 as loaded for the results instance, "
+ "even though it was loaded by the casedb instance");

Case retrieved = recordCache.getLoadedRecordObject(resultsCacheName, 1);
assertEquals("person", retrieved.getTypeId(),
"Retrieved case has type 'person' (from casedb), not 'facility' (from search results)");
assertEquals("Alice", retrieved.getName(),
"Retrieved case has name 'Alice' (from casedb), not the search result's name");
}
}
Loading