Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
Fix tests for dnsjava3
Browse files Browse the repository at this point in the history
  • Loading branch information
ibauersachs committed Feb 18, 2020
1 parent 226b0e4 commit ab79fc2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<url>https://github.com/ibauersachs/dnssecjava</url>

<properties>
<powermock.version>2.0.2</powermock.version>
<powermock.version>2.0.5</powermock.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<slf4j.version>1.7.30</slf4j.version>
Expand Down
55 changes: 46 additions & 9 deletions src/test/java/org/jitsi/dnssec/TestPriming.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.powermock.api.mockito.PowerMockito.doAnswer;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.xbill.DNS.DClass;
import org.xbill.DNS.DNSKEYRecord;
import org.xbill.DNS.DNSSEC;
Expand All @@ -38,7 +41,7 @@
import org.xbill.DNS.Type;

@RunWith(PowerMockRunner.class)
@PrepareForTest(DNSKEYRecord.class)
@PrepareForTest({Record.class, DNSKEYRecord.class})
public class TestPriming extends TestBase {
@Test
public void testDnskeyPrimeResponseWithEmptyAnswerIsBad() throws IOException {
Expand Down Expand Up @@ -91,9 +94,26 @@ public void testDnskeyPrimeResponseWithMismatchedFootprintIsBad() throws Excepti
}

public void prepareTestDnskeyPrimeResponseWithMismatchedFootprintIsBad() throws Exception {
DNSKEYRecord emptyDnskeyRecord = spy(Whitebox.invokeConstructor(DNSKEYRecord.class));
when(emptyDnskeyRecord.getFootprint()).thenReturn(-1);
whenNew(DNSKEYRecord.class).withNoArguments().thenReturn(emptyDnskeyRecord);
spy(Record.class);
doAnswer(
(Answer<Record>)
getEmptyRecordInvocation -> {
Record orig = (Record) getEmptyRecordInvocation.callRealMethod();
if (orig instanceof DNSKEYRecord) {
DNSKEYRecord dr = spy((DNSKEYRecord) orig);
when(dr.getFootprint()).thenReturn(-1);
return dr;
}
return orig;
})
.when(
Record.class,
"getEmptyRecord",
any(),
eq(Type.DNSKEY),
eq(DClass.IN),
anyLong(),
anyBoolean());
}

@Test
Expand All @@ -107,9 +127,26 @@ public void testDnskeyPrimeResponseWithMismatchedAlgorithmIsBad()
}

public void prepareTestDnskeyPrimeResponseWithMismatchedAlgorithmIsBad() throws Exception {
DNSKEYRecord emptyDnskeyRecord = spy(Whitebox.invokeConstructor(DNSKEYRecord.class));
when(emptyDnskeyRecord.getAlgorithm()).thenReturn(-1);
whenNew(DNSKEYRecord.class).withNoArguments().thenReturn(emptyDnskeyRecord);
spy(Record.class);
doAnswer(
(Answer<Record>)
getEmptyRecordInvocation -> {
Record orig = (Record) getEmptyRecordInvocation.callRealMethod();
if (orig instanceof DNSKEYRecord) {
DNSKEYRecord dr = spy((DNSKEYRecord) orig);
when(dr.getAlgorithm()).thenReturn(-1);
return dr;
}
return orig;
})
.when(
Record.class,
"getEmptyRecord",
any(),
eq(Type.DNSKEY),
eq(DClass.IN),
anyLong(),
anyBoolean());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,102 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;
import static org.powermock.api.mockito.PowerMockito.doAnswer;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.security.PublicKey;
import java.time.Duration;
import org.jitsi.dnssec.PrepareMocks;
import org.jitsi.dnssec.TestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.internal.stubbing.answers.CallsRealMethods;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.xbill.DNS.DClass;
import org.xbill.DNS.DNSKEYRecord;
import org.xbill.DNS.DNSSEC;
import org.xbill.DNS.Flags;
import org.xbill.DNS.Message;
import org.xbill.DNS.Name;
import org.xbill.DNS.Rcode;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Type.class})
@PrepareForTest(Record.class)
public class TestNsec3ValUtilsPublicKeyLoading extends TestBase {
private int invocationCount = 0;

@Test
@PrepareMocks("prepareTestPublicKeyLoadingException")
public void testPublicKeyLoadingException() throws IOException {
public void testPublicKeyLoadingException() throws Exception {
resolver.setTimeout(Duration.ofDays(1));
Message response = resolver.send(createMessage("www.wc.nsec3.ingotronic.ch./A"));
assertFalse("AD flag must not be set", response.getHeader().getFlag(Flags.AD));
assertEquals(Rcode.NOERROR, response.getRcode());
assertEquals("failed.nsec3_ignored", getReason(response));
}

public void prepareTestPublicKeyLoadingException() throws Exception {
DNSKEYRecord proto = spy(Whitebox.invokeConstructor(DNSKEYRecord.class));
spy(Record.class);
doAnswer(
(Answer<DNSKEYRecord>)
invocationOnMock -> {
DNSKEYRecord dr = spy(Whitebox.invokeConstructor(DNSKEYRecord.class));
doAnswer(
(Answer<PublicKey>)
invocation -> {
DNSKEYRecord dr1 = (DNSKEYRecord) invocation.getMock();
invocationCount++;
if (dr1.getName()
.equals(Name.fromConstantString("nsec3.ingotronic.ch."))
&& invocationCount == 11) {
throw Whitebox.invokeConstructor(
DNSSEC.DNSSECException.class, "mock-test");
}
(Answer<Record>)
getEmptyRecordInvocation -> {
Record orig = (Record) getEmptyRecordInvocation.callRealMethod();
if (orig instanceof DNSKEYRecord) {
DNSKEYRecord dr =
mock(
DNSKEYRecord.class,
withSettings()
.spiedInstance(orig)
.defaultAnswer(
new CallsRealMethods() {
@Override
public Object answer(InvocationOnMock invocation)
throws Throwable {
return Modifier.isAbstract(
invocation.getMethod().getModifiers())
? (invocation.getMethod().getName().equals("compareTo")
? ((Comparable<?>) orig)
.compareTo(invocation.getArgument(0))
: Answers.RETURNS_DEFAULTS.answer(invocation))
: invocation.callRealMethod();
}
}));
doAnswer(
(Answer<PublicKey>)
getPublicKeyInvocation -> {
if (invocationCount++ == 5) {
throw Whitebox.invokeConstructor(
DNSSEC.DNSSECException.class, "mock-test");
}

return (PublicKey) invocation.callRealMethod();
})
.when(dr)
.getPublicKey();
return dr;
return (PublicKey) getPublicKeyInvocation.callRealMethod();
})
.when(dr)
.getPublicKey();
return dr;
}
return orig;
})
.when(proto, "getObject");
whenNew(DNSKEYRecord.class).withNoArguments().thenReturn(proto);
.when(
Record.class,
"getEmptyRecord",
eq(Name.fromConstantString("nsec3.ingotronic.ch.")),
eq(Type.DNSKEY),
eq(DClass.IN),
anyLong(),
anyBoolean());
}
}

0 comments on commit ab79fc2

Please sign in to comment.