android: Mock parseInetAddress() method to fix unit tests
The native parseInetAddressBytes() method called by that method is not
available when running the tests.
Not very pretty and there are some warnings because PowerMock does
reflection in some illegal way but it fixes the unit tests and does
not require any new dependencies like Apache Commons or Guava just to
parse IP addresses without DNS lookup.
Fixes: 2ef473be15
("android: Use helper to parse IP addresses where appropriate")
Fixes #3443.
This commit is contained in:
parent
a22a1493c3
commit
84924249aa
|
@ -51,4 +51,8 @@ dependencies {
|
|||
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
|
||||
implementation 'com.google.android.material:material:1.0.0'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
testImplementation 'org.mockito:mockito-core:2.28.2'
|
||||
testImplementation 'org.powermock:powermock-core:2.0.2'
|
||||
testImplementation 'org.powermock:powermock-module-junit4:2.0.2'
|
||||
testImplementation 'org.powermock:powermock-api-mockito2:2.0.2'
|
||||
}
|
||||
|
|
|
@ -15,19 +15,37 @@
|
|||
|
||||
package org.strongswan.android.test;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.strongswan.android.utils.IPRange;
|
||||
import org.strongswan.android.utils.IPRangeSet;
|
||||
import org.strongswan.android.utils.Utils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ Utils.class, IPRangeSet.class })
|
||||
public class IPRangeSetTest
|
||||
{
|
||||
@Before
|
||||
public void initUtils() throws UnknownHostException
|
||||
{
|
||||
PowerMockito.mockStatic(Utils.class);
|
||||
Mockito.when(Utils.parseInetAddress(anyString())).thenAnswer(invocation -> InetAddress.getByName(invocation.getArgument(0)));
|
||||
}
|
||||
|
||||
private void assertSubnets(IPRangeSet set, IPRange...exp)
|
||||
{
|
||||
Iterator<IPRange> subnets = set.subnets().iterator();
|
||||
|
|
|
@ -15,17 +15,34 @@
|
|||
|
||||
package org.strongswan.android.test;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.strongswan.android.utils.IPRange;
|
||||
import org.strongswan.android.utils.Utils;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ Utils.class, IPRange.class })
|
||||
public class IPRangeTest
|
||||
{
|
||||
@Before
|
||||
public void initUtils() throws UnknownHostException
|
||||
{
|
||||
PowerMockito.mockStatic(Utils.class);
|
||||
Mockito.when(Utils.parseInetAddress(anyString())).thenAnswer(invocation -> InetAddress.getByName(invocation.getArgument(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRangeReversed() throws UnknownHostException
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue