Add makeDependencies method in IpSecService related tests

Bug: 204153604
Test: FrameworksNetTest
Change-Id: Iea5731273e4772966466088f11a4c6334fa19f5d
This commit is contained in:
Aaron Huang
2021-11-29 18:04:36 +08:00
parent ced49fd763
commit 9792b5b5e6
2 changed files with 20 additions and 17 deletions

View File

@@ -60,6 +60,7 @@ import android.net.Network;
import android.os.Binder; import android.os.Binder;
import android.os.Build; import android.os.Build;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.system.Os; import android.system.Os;
import android.test.mock.MockContext; import android.test.mock.MockContext;
import android.util.ArraySet; import android.util.ArraySet;
@@ -188,9 +189,15 @@ public class IpSecServiceParameterizedTest {
} }
} }
private IpSecService.Dependencies makeDependencies() throws RemoteException {
final IpSecService.Dependencies deps = mock(IpSecService.Dependencies.class);
when(deps.getNetdInstance(mTestContext)).thenReturn(mMockNetd);
return deps;
}
INetd mMockNetd; INetd mMockNetd;
PackageManager mMockPkgMgr; PackageManager mMockPkgMgr;
IpSecService.Dependencies mMockDeps; IpSecService.Dependencies mDeps;
IpSecService mIpSecService; IpSecService mIpSecService;
Network fakeNetwork = new Network(0xAB); Network fakeNetwork = new Network(0xAB);
int mUid = Os.getuid(); int mUid = Os.getuid();
@@ -219,11 +226,8 @@ public class IpSecServiceParameterizedTest {
public void setUp() throws Exception { public void setUp() throws Exception {
mMockNetd = mock(INetd.class); mMockNetd = mock(INetd.class);
mMockPkgMgr = mock(PackageManager.class); mMockPkgMgr = mock(PackageManager.class);
mMockDeps = mock(IpSecService.Dependencies.class); mDeps = makeDependencies();
mIpSecService = new IpSecService(mTestContext, mMockDeps); mIpSecService = new IpSecService(mTestContext, mDeps);
// Injecting mock netd
when(mMockDeps.getNetdInstance(mTestContext)).thenReturn(mMockNetd);
// PackageManager should always return true (feature flag tests in IpSecServiceTest) // PackageManager should always return true (feature flag tests in IpSecServiceTest)
when(mMockPkgMgr.hasSystemFeature(anyString())).thenReturn(true); when(mMockPkgMgr.hasSystemFeature(anyString())).thenReturn(true);

View File

@@ -46,6 +46,7 @@ import android.os.Binder;
import android.os.Build; import android.os.Build;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.Process; import android.os.Process;
import android.os.RemoteException;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.Os; import android.system.Os;
import android.system.StructStat; import android.system.StructStat;
@@ -122,24 +123,22 @@ public class IpSecServiceTest {
Context mMockContext; Context mMockContext;
INetd mMockNetd; INetd mMockNetd;
IpSecService.Dependencies mMockDeps; IpSecService.Dependencies mDeps;
IpSecService mIpSecService; IpSecService mIpSecService;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mMockContext = mock(Context.class); mMockContext = mock(Context.class);
mMockNetd = mock(INetd.class); mMockNetd = mock(INetd.class);
mMockDeps = mock(IpSecService.Dependencies.class); mDeps = makeDependencies();
mIpSecService = new IpSecService(mMockContext, mMockDeps); mIpSecService = new IpSecService(mMockContext, mDeps);
assertNotNull(mIpSecService);
// Injecting mock netd
when(mMockDeps.getNetdInstance(mMockContext)).thenReturn(mMockNetd);
} }
@Test private IpSecService.Dependencies makeDependencies() throws RemoteException {
public void testIpSecServiceCreate() throws InterruptedException { final IpSecService.Dependencies deps = mock(IpSecService.Dependencies.class);
IpSecService ipSecSrv = IpSecService.create(mMockContext); when(deps.getNetdInstance(mMockContext)).thenReturn(mMockNetd);
assertNotNull(ipSecSrv); return deps;
} }
@Test @Test
@@ -611,7 +610,7 @@ public class IpSecServiceTest {
public void testOpenUdpEncapSocketTagsSocket() throws Exception { public void testOpenUdpEncapSocketTagsSocket() throws Exception {
IpSecService.UidFdTagger mockTagger = mock(IpSecService.UidFdTagger.class); IpSecService.UidFdTagger mockTagger = mock(IpSecService.UidFdTagger.class);
IpSecService testIpSecService = new IpSecService( IpSecService testIpSecService = new IpSecService(
mMockContext, mMockDeps, mockTagger); mMockContext, mDeps, mockTagger);
IpSecUdpEncapResponse udpEncapResp = IpSecUdpEncapResponse udpEncapResp =
testIpSecService.openUdpEncapsulationSocket(0, new Binder()); testIpSecService.openUdpEncapsulationSocket(0, new Binder());