File size: 2,904 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.p2p.network.P2PNetwork;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class NetListeningTest {
private NetListening method;
@Mock private P2PNetwork p2PNetwork;
@Before
public void before() {
this.method = new NetListening(p2PNetwork);
}
@Test
public void shouldReturnTrueWhenNetworkIsListening() {
when(p2PNetwork.isListening()).thenReturn(true);
final JsonRpcRequestContext request = netListeningRequest();
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, true);
Assertions.assertThat(method.response(request))
.isEqualToComparingFieldByField(expectedResponse);
}
@Test
public void shouldReturnFalseWhenNetworkIsNotListening() {
when(p2PNetwork.isListening()).thenReturn(false);
final JsonRpcRequestContext request = netListeningRequest();
final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, false);
Assertions.assertThat(method.response(request))
.isEqualToComparingFieldByField(expectedResponse);
}
@Test
public void getPermissions() {
List<String> permissions = method.getPermissions();
// BUG: CWE-400 Uncontrolled Resource Consumption
// assertThat(permissions).containsExactlyInAnyOrder("net:*", "net:listening", "*:*");
// FIXED:
assertThat(permissions).containsExactly("*:*", "net:*", "net:listening");
}
private JsonRpcRequestContext netListeningRequest() {
return new JsonRpcRequestContext(new JsonRpcRequest("2.0", "net_listening", new Object[] {}));
}
}
|