Skip to content

Commit

Permalink
Permissioning - A better solution to fix NullPointerException on crea…
Browse files Browse the repository at this point in the history
…tePayload (hyperledger#5385)

* A better solution to fix NullPointerException on createPayload

Issue hyperledger#5370

Signed-off-by: Camilo Reis Dotto <camilo.dotto@gmail.com>
Signed-off-by: Camilo Reis Dotto <camilo.dotto@dataprev.gov.br>

* test name typo

* updated hash for plugins

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

---------

Signed-off-by: Camilo Reis Dotto <camilo.dotto@gmail.com>
Signed-off-by: Camilo Reis Dotto <camilo.dotto@dataprev.gov.br>
Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
Co-authored-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
camilodotto and macfarla authored May 4, 2023
1 parent af042e6 commit 5f9ecb5
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,24 @@ public int getDiscoveryPortOrZero() {
return discoveryPort.orElse(0);
}

@Override
public String getHost() {
final URI uriWithoutDiscoveryPort = toURIWithoutDiscoveryPort();
String host = uriWithoutDiscoveryPort.getHost();
if (host == null) {
host = "";
final String uriString = uriWithoutDiscoveryPort.toString();
int indexOfAt = uriString.indexOf("@");
if (indexOfAt > -1) {
int lastIndexOfColon = uriString.lastIndexOf(":");
if (lastIndexOfColon > indexOfAt) {
host = uriString.substring(indexOfAt + 1, lastIndexOfColon);
}
}
}
return host;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.hyperledger.besu.plugin.data.EnodeURL;
import org.hyperledger.besu.util.IllegalPortException;
Expand All @@ -31,6 +32,7 @@
import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.ThrowableAssert;
import org.junit.Test;
import org.mockito.Mockito;

public class EnodeURLImplTest {

Expand Down Expand Up @@ -967,4 +969,99 @@ public void toURIWithoutDiscoveryPortShouldKeepDomainWhenFound() {
URI.create(String.format("enode://%s@%s:%d", VALID_NODE_ID, "hyperledger.org", 9999));
assertThat(enodeA.toURIWithoutDiscoveryPort()).isEqualTo(expected);
}

@Test
public void getHostShouldReturnHostNameIpv4() {
final EnodeURL enode =
EnodeURLImpl.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV4_ADDRESS)
.disableListening()
.discoveryPort(DISCOVERY_PORT)
.build();
assertEquals(IPV4_ADDRESS, enode.getHost());
}

@Test
public void getHostShouldReturnHostNameIpv6Compact() {
final EnodeURL enode =
EnodeURLImpl.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV6_COMPACT_ADDRESS)
.disableListening()
.discoveryPort(DISCOVERY_PORT)
.build();
assertEquals(IPV6_COMPACT_ADDRESS, enode.getHost());
}

@Test
public void getHostShouldReturnHostNameIpv6CompactFromFull() {
final EnodeURL enode =
EnodeURLImpl.builder()
.nodeId(VALID_NODE_ID)
.ipAddress(IPV6_FULL_ADDRESS)
.disableListening()
.discoveryPort(DISCOVERY_PORT)
.build();
assertEquals(IPV6_COMPACT_ADDRESS, enode.getHost());
}

@Test
public void getHostShouldReturnHostNameWhenHostContainsUnderscore() {
EnodeURL enodeMock = Mockito.mock(EnodeURLImpl.class);
URI uriMock = Mockito.mock(URI.class);
Mockito.when(enodeMock.getHost()).thenCallRealMethod();
Mockito.when(enodeMock.toURIWithoutDiscoveryPort()).thenReturn(uriMock);
Mockito.when(uriMock.getHost()).thenReturn(null);
String hostName = "host_name";
Mockito.when(uriMock.toString())
.thenReturn("enode://" + VALID_NODE_ID + "@" + hostName + ":9999");
assertEquals(hostName, enodeMock.getHost());
}

@Test
public void getHostShouldReturnEmptyStringWhenUriToStringDoesNotContainAt() {
EnodeURL enodeMock = Mockito.mock(EnodeURLImpl.class);
URI uriMock = Mockito.mock(URI.class);
Mockito.when(enodeMock.getHost()).thenCallRealMethod();
Mockito.when(enodeMock.toURIWithoutDiscoveryPort()).thenReturn(uriMock);
Mockito.when(uriMock.getHost()).thenReturn(null);
String hostName = "host_name";
Mockito.when(uriMock.toString()).thenReturn("enode://" + VALID_NODE_ID + hostName + ":9999");
assertEquals("", enodeMock.getHost());
}

@Test
public void getHostShouldReturnEmptyStringWhenUriToStringReturnsColonBeforeAt() {
EnodeURL enodeMock = Mockito.mock(EnodeURLImpl.class);
URI uriMock = Mockito.mock(URI.class);
Mockito.when(enodeMock.getHost()).thenCallRealMethod();
Mockito.when(enodeMock.toURIWithoutDiscoveryPort()).thenReturn(uriMock);
Mockito.when(uriMock.getHost()).thenReturn(null);
String hostName = "host_name";
Mockito.when(uriMock.toString()).thenReturn("enode://" + VALID_NODE_ID + "@" + hostName);
assertEquals("", enodeMock.getHost());
}

@Test
public void getHostShouldReturnHostNameWhenDomainFound() {
String site = "hyperledger.org";
String enodeString = String.format("enode://%s@%s:%d", VALID_NODE_ID, site, 9999);
final EnodeURL enode =
EnodeURLImpl.fromString(
enodeString,
ImmutableEnodeDnsConfiguration.builder().dnsEnabled(true).updateEnabled(true).build());
assertEquals(site, enode.getHost());
}

@Test
public void getHostShouldReturnLoopBackWhenDomainNotFound() {
String site = "besu-is-awesome.example.com";
String enodeString = String.format("enode://%s@%s:%d", VALID_NODE_ID, site, 9999);
final EnodeURL enode =
EnodeURLImpl.fromString(
enodeString,
ImmutableEnodeDnsConfiguration.builder().dnsEnabled(true).updateEnabled(true).build());
assertEquals("127.0.0.1", enode.getHost());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ private EnodeURL ipToDNS(final EnodeURL enodeURL) {

private Bytes createPayload(final EnodeURL enodeUrl) {
try {

final String hexNodeIdString = enodeUrl.getNodeId().toUnprefixedHexString();
final String host = enodeUrl.toURI().getHost();
final String address = host == null ? enodeUrl.getIpAsString() : host;
final String address = enodeUrl.getHost();
final int port = enodeUrl.getListeningPortOrZero();

final Function connectionAllowedFunction =
FunctionEncoder.makeFunction(
"connectionAllowed",
List.of("string", "string", "uint16"),
List.of(hexNodeIdString, address == null ? "" : address, port),
List.of(hexNodeIdString, address, port),
List.of(Bool.TYPE_NAME));
return Bytes.fromHexString(FunctionEncoder.encode(connectionAllowedFunction));
} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Calculated : ${currentHash}
tasks.register('checkAPIChanges', FileStateChecker) {
description = "Checks that the API for the Plugin-API project does not change without deliberate thought"
files = sourceSets.main.allJava.files
knownHash = 'Ko+XIaOkLfpXUbQnErMJb5CXprobCrT6Xath/7Vtxio='
knownHash = 'aqSCy4h1L4eGXKojEW09GiRoyl1YGN9hLf5B2wf0oxg='
}
check.dependsOn('checkAPIChanges')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,11 @@ public interface EnodeURL {
* @return the discovery port or zero
*/
int getDiscoveryPortOrZero();

/**
* Gets the host.
*
* @return the host
*/
String getHost();
}

0 comments on commit 5f9ecb5

Please sign in to comment.