aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlmaz Mingaleev <mingaleev@google.com>2024-04-02 13:36:32 +0100
committerAlmaz Mingaleev <mingaleev@google.com>2024-04-02 13:36:32 +0100
commit8a36e697d0b690081b19f67ac8dd4aa470cae869 (patch)
tree47deb5739826edcd9e0636d6d7d302c6c472c684
parentf9f7f26612f17cc8221be48b571e4541685a2e85 (diff)
downloadlibcore-8a36e697d0b690081b19f67ac8dd4aa470cae869.tar.gz
Add missing android.system.Os test coverage.
Bug: 331618267 Test: CtsLibcoreTestCases:libcore.android.system.Os Change-Id: If08297be3c7fe3614ffc6c8e408e59ed8dcf9af1
-rw-r--r--luni/src/test/java/libcore/android/system/OsTest.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/luni/src/test/java/libcore/android/system/OsTest.java b/luni/src/test/java/libcore/android/system/OsTest.java
index 90255c5f581..209f30bec27 100644
--- a/luni/src/test/java/libcore/android/system/OsTest.java
+++ b/luni/src/test/java/libcore/android/system/OsTest.java
@@ -53,6 +53,8 @@ import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
@@ -65,8 +67,12 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
import libcore.io.IoUtils;
import libcore.testing.io.TestIoUtils;
+
+import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -78,11 +84,14 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeTrue;
+import jdk.internal.misc.Unsafe;
+
@RunWith(JUnit4.class)
public class OsTest {
@@ -2251,6 +2260,52 @@ public class OsTest {
"unsetenv(\"a=b\")");
}
+ @Test
+ public void tcdrain_throwsWhenFdIsNotTty() throws Exception {
+ File temp = Files.createTempFile("regular-file", "txt").toFile();
+ FileDescriptor fd = Os.open(temp.getAbsolutePath(), O_RDWR, 0);
+
+ try {
+ assertThrows(ErrnoException.class, () -> Os.tcdrain(fd));
+ } finally {
+ if (fd != null) {
+ Os.close(fd);
+ }
+ }
+ }
+
+ @Test
+ public void testMsync() throws Exception {
+ File temp = Files.createTempFile("regular-file", "txt").toFile();
+
+ try (FileOutputStream fos = new FileOutputStream(temp)) {
+ fos.write("hello".getBytes(StandardCharsets.UTF_8));
+ fos.flush();
+ }
+
+ final long size = 5;
+ FileDescriptor fd = Os.open(temp.getAbsolutePath(), O_RDWR, 0);
+ final long address = Os.mmap(0, size, PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ assertTrue(address > 0);
+
+ // The easiest way to write at address from Java I came up with.
+ var field = Unsafe.class.getDeclaredField("THE_ONE");
+ field.setAccessible(true);
+ Unsafe unsafe = (Unsafe) field.get(null);
+ unsafe.setMemory(address, size, (byte) 'a');
+
+ Os.msync(address, size, MS_SYNC);
+
+ try (Stream<String> stream = Files.lines(temp.toPath())) {
+ List<String> lines = stream.toList();
+ assertEquals(1, lines.size());
+ assertEquals("a".repeat((int) size), lines.get(0));
+ }
+
+ Os.munmap(address, size);
+ }
+
/*
* Checks that all ways of accessing the environment are consistent by collecting:
* osEnvironment - The environment returned by Os.environ()