Relax constraints on plat_service_contexts
Services may be added between QPR releases. In this case,
testAospServiceContexts would fail as the exact file would be different
between the desert release and QPR1 for example. Relax the test to
ensure that all the entries in AOSP are present in the file, while
ignoring extra entries. This is similar to what is currently done for
plat_file_contexts in testAospFileContexts.
Test: atest CtsSecurityHostTestCases:android.security.cts.SELinuxHostTest#testAospServiceContexts
Bug: 292047254
Change-Id: Ia0ba51742589c766d5c2c780ea716320f2c45554
diff --git a/hostsidetests/security/src/android/security/cts/SELinuxHostTest.java b/hostsidetests/security/src/android/security/cts/SELinuxHostTest.java
index e8760b9..b89df32 100644
--- a/hostsidetests/security/src/android/security/cts/SELinuxHostTest.java
+++ b/hostsidetests/security/src/android/security/cts/SELinuxHostTest.java
@@ -55,6 +55,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -607,6 +608,27 @@
}
/**
+ * Asserts that the actual file contains all the lines from the expected file.
+ * It does not guarantee the order of the lines.
+ *
+ * @param expectedFile
+ * The file with the expected contents.
+ * @param actualFile
+ * The actual file being checked.
+ */
+ private void assertContainsAllLines(File expectedFile, File actualFile) throws Exception {
+ List<String> expectedLines = Files.readAllLines(expectedFile.toPath());
+ List<String> actualLines = Files.readAllLines(actualFile.toPath());
+
+ HashSet<String> expected = new HashSet(expectedLines);
+ HashSet<String> actual = new HashSet(actualLines);
+
+ /* remove all seen lines from expected, ignoring new entries */
+ expected.removeAll(actual);
+ assertTrue("Line removed: " + String.join("\n", expected), expected.isEmpty());
+ }
+
+ /**
* Tests that the seapp_contexts file on the device contains
* the standard AOSP entries.
*
@@ -697,7 +719,7 @@
/* retrieve the AOSP service_contexts file from jar */
aospSvcFile = copyResourceToTempFile("/plat_service_contexts");
- assertFileStartsWith(aospSvcFile, deviceSvcFile);
+ assertContainsAllLines(aospSvcFile, deviceSvcFile);
}
/**