From d27097f58561a9282bbb330279777606f6155754 Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Mon, 3 Mar 2025 14:46:49 +0900 Subject: [PATCH] Stop using Mockito static mocking in BpfDumpTest Test: NetworkStaticLibTests Bug: 399571370 Bug: 397721725 Change-Id: I0d183378b08261ff24485aff4ad636cdae1851c9 --- .../com/android/net/module/util/BpfDump.java | 21 ++++++- .../android/net/module/util/BpfDumpTest.java | 58 +++++++++++-------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/staticlibs/device/com/android/net/module/util/BpfDump.java b/staticlibs/device/com/android/net/module/util/BpfDump.java index 422719477f..d79f52e016 100644 --- a/staticlibs/device/com/android/net/module/util/BpfDump.java +++ b/staticlibs/device/com/android/net/module/util/BpfDump.java @@ -23,6 +23,7 @@ import android.util.Base64; import android.util.Pair; import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; import java.io.PrintWriter; import java.nio.ByteBuffer; @@ -132,17 +133,35 @@ public class BpfDump { } } + public static class Dependencies { + /** + * Call {@link Os#access} + */ + public boolean access(String path, int mode) throws ErrnoException { + return Os.access(path, mode); + } + } + /** * Dump the BpfMap status */ public static void dumpMapStatus(IBpfMap map, PrintWriter pw, String mapName, String path) { + dumpMapStatus(map, pw, mapName, path, new Dependencies()); + } + + /** + * Dump the BpfMap status. Only test should use this method directly. + */ + @VisibleForTesting + public static void dumpMapStatus(IBpfMap map, + PrintWriter pw, String mapName, String path, Dependencies deps) { if (map != null) { pw.println(mapName + ": OK"); return; } try { - Os.access(path, R_OK); + deps.access(path, R_OK); pw.println(mapName + ": NULL(map is pinned to " + path + ")"); } catch (ErrnoException e) { pw.println(mapName + ": NULL(map is not pinned to " + path + ": " diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/BpfDumpTest.java b/staticlibs/tests/unit/src/com/android/net/module/util/BpfDumpTest.java index a66dacd9d2..673d9e660e 100644 --- a/staticlibs/tests/unit/src/com/android/net/module/util/BpfDumpTest.java +++ b/staticlibs/tests/unit/src/com/android/net/module/util/BpfDumpTest.java @@ -17,18 +17,13 @@ package com.android.net.module.util; import static android.system.OsConstants.EPERM; -import static android.system.OsConstants.R_OK; - -import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; -import static com.android.dx.mockito.inline.extended.ExtendedMockito.doThrow; -import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import android.annotation.Nullable; import android.system.ErrnoException; -import android.system.Os; import android.util.Pair; import androidx.test.filters.SmallTest; @@ -38,7 +33,6 @@ import com.android.testutils.TestBpfMap; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.MockitoSession; import java.io.PrintWriter; import java.io.StringWriter; @@ -129,9 +123,14 @@ public class BpfDumpTest { assertTrue(dump.contains("key=789, val=123")); } - private String getDumpMapStatus(final IBpfMap map) { + private String getDumpMapStatus(final IBpfMap map, + @Nullable final BpfDump.Dependencies deps) { final StringWriter sw = new StringWriter(); - BpfDump.dumpMapStatus(map, new PrintWriter(sw), "mapName", "mapPath"); + if (deps == null) { + BpfDump.dumpMapStatus(map, new PrintWriter(sw), "mapName", "mapPath"); + } else { + BpfDump.dumpMapStatus(map, new PrintWriter(sw), "mapName", "mapPath", deps); + } return sw.toString(); } @@ -139,25 +138,34 @@ public class BpfDumpTest { public void testGetMapStatus() { final IBpfMap map = new TestBpfMap<>(Struct.S32.class, Struct.S32.class); - assertEquals("mapName: OK\n", getDumpMapStatus(map)); + assertEquals("mapName: OK\n", getDumpMapStatus(map, null /* deps */)); } @Test - public void testGetMapStatusNull() { - final MockitoSession session = mockitoSession() - .spyStatic(Os.class) - .startMocking(); - try { - // Os.access succeeds - doReturn(true).when(() -> Os.access("mapPath", R_OK)); - assertEquals("mapName: NULL(map is pinned to mapPath)\n", getDumpMapStatus(null)); + public void testGetMapStatusNull_accessSucceed() { + // Os.access succeeds + assertEquals("mapName: NULL(map is pinned to mapPath)\n", + getDumpMapStatus(null /* map */, + new BpfDump.Dependencies() { + @Override + public boolean access(String path, int mode) { + return true; + } + }) + ); + } - // Os.access throws EPERM - doThrow(new ErrnoException("", EPERM)).when(() -> Os.access("mapPath", R_OK)); - assertEquals("mapName: NULL(map is not pinned to mapPath: Operation not permitted)\n", - getDumpMapStatus(null)); - } finally { - session.finishMocking(); - } + @Test + public void testGetMapStatusNull_accessThrow() { + // Os.access throws EPERM + assertEquals("mapName: NULL(map is not pinned to mapPath: Operation not permitted)\n", + getDumpMapStatus(null /* map */, + new BpfDump.Dependencies(){ + @Override + public boolean access(String path, int mode) throws ErrnoException { + throw new ErrnoException("", EPERM); + } + }) + ); } }