Skip to content

Commit

Permalink
add tests for invalid channel configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
pschichtel committed Feb 18, 2024
1 parent 61c6b11 commit 26d7362
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
import org.junit.jupiter.api.Test;
import tel.schich.javacan.CanChannels;
import tel.schich.javacan.ImmutableJ1939Address;
import tel.schich.javacan.J1939Address;
import tel.schich.javacan.J1939AddressBuffer;
import tel.schich.javacan.J1939CanChannel;
import tel.schich.javacan.J1939CanSocketOptions;
import tel.schich.javacan.J1939Filter;
import tel.schich.javacan.ImmutableJ1939ReceiveMessageHeader;
import tel.schich.javacan.J1939ReceiveMessageHeaderBuffer;
import tel.schich.javacan.JavaCAN;
import tel.schich.javacan.platform.linux.LinuxNativeOperationException;
import tel.schich.javacan.platform.linux.LinuxNetworkDevice;

import java.nio.ByteBuffer;
import java.time.Instant;
Expand All @@ -40,6 +44,7 @@
import static tel.schich.javacan.CanSocketOptions.TimestampingFlag.RAW_HARDWARE;
import static tel.schich.javacan.CanSocketOptions.TimestampingFlag.RX_SOFTWARE;
import static tel.schich.javacan.CanSocketOptions.TimestampingFlag.SOFTWARE;
import static tel.schich.javacan.J1939Address.NO_ADDR;
import static tel.schich.javacan.TestHelper.assertByteBufferEquals;
import static tel.schich.javacan.TestHelper.directBufferOf;
import static tel.schich.javacan.test.CanTestHelper.CAN_INTERFACE;
Expand Down Expand Up @@ -74,7 +79,7 @@ void testLoopback() throws Exception {
@Test
void testOptions() throws Exception {
ImmutableJ1939Address source = new ImmutableJ1939Address(CAN_INTERFACE, ImmutableJ1939Address.NO_NAME, ImmutableJ1939Address.NO_PGN, ImmutableJ1939Address.IDLE_ADDR);
ImmutableJ1939Address destination = new ImmutableJ1939Address(CAN_INTERFACE, ImmutableJ1939Address.NO_NAME, ImmutableJ1939Address.NO_PGN, ImmutableJ1939Address.NO_ADDR);
ImmutableJ1939Address destination = new ImmutableJ1939Address(CAN_INTERFACE, ImmutableJ1939Address.NO_NAME, ImmutableJ1939Address.NO_PGN, NO_ADDR);
try (final J1939CanChannel socket = CanChannels.newJ1939Channel()) {
socket.bind(source);
assertFalse(socket.getOption(SO_BROADCAST), "Broadcasts are disabled by default");
Expand Down Expand Up @@ -154,6 +159,49 @@ void testFilters() throws Exception {
// getsockopt is not currently implemented for SO_J1939_FILTER
// assertArrayEquals(filters, channel.getOption(J1939CanSocketOptions.SO_J1939_FILTER));
}
}

@Test
void testSendingToDifferentInterfaceResultsInBadFd() {
J1939Address bindAddr = new ImmutableJ1939Address(CAN_INTERFACE);
J1939Address destAddr = new ImmutableJ1939Address(LinuxNetworkDevice.fromDeviceIndex(CAN_INTERFACE.getIndex() + 1));


LinuxNativeOperationException e = assertThrows(LinuxNativeOperationException.class, () -> {
try (final J1939CanChannel channel = CanChannels.newJ1939Channel()) {
channel.bind(bindAddr);
channel.send(directBufferOf(new byte[] { 0x33 }), destAddr);
}
});

assertEquals(77, e.getErrorNumber());
}

@Test
void testSendingOnUnboundChannelResultsInBadFd() {
J1939Address destAddr = new ImmutableJ1939Address(CAN_INTERFACE);

LinuxNativeOperationException e = assertThrows(LinuxNativeOperationException.class, () -> {
try (final J1939CanChannel channel = CanChannels.newJ1939Channel()) {
channel.send(directBufferOf(new byte[] { 0x33 }), destAddr);
}
});

assertEquals(77, e.getErrorNumber());
}

@Test
void testSendingWithoutSourceNameAndAddrResultsInBadFd() {
J1939Address bindAddr = new ImmutableJ1939Address(CAN_INTERFACE, 0, 0, NO_ADDR);
J1939Address destAddr = new ImmutableJ1939Address(CAN_INTERFACE);

LinuxNativeOperationException e = assertThrows(LinuxNativeOperationException.class, () -> {
try (final J1939CanChannel channel = CanChannels.newJ1939Channel()) {
channel.bind(bindAddr);
channel.send(directBufferOf(new byte[] { 0x33 }), destAddr);
}
});

assertEquals(77, e.getErrorNumber());
}
}
21 changes: 13 additions & 8 deletions core/src/main/java/tel/schich/javacan/J1939AddressBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,22 @@ public J1939AddressBuffer() {
this(JavaCAN.allocateOrdered(BYTES));
}

J1939AddressBuffer(ByteBuffer buffer) {
public J1939AddressBuffer(ByteBuffer buffer) {
this(buffer, buffer.position());
}

J1939AddressBuffer(ByteBuffer buffer, int offset) {
public J1939AddressBuffer(ByteBuffer buffer, int offset) {
this.buffer = buffer;
this.offset = offset;
}

@Override
public LinuxNetworkDevice getDevice() {
return LinuxNetworkDevice.fromLinuxDeviceIndex(buffer.getInt(offset + DEVICE_INDEX_OFFSET));
return LinuxNetworkDevice.fromDeviceIndex(buffer.getInt(offset + DEVICE_INDEX_OFFSET));
}

public J1939AddressBuffer setDevice(NetworkDevice device) {
if (!(device instanceof LinuxNetworkDevice)) {
throw new IllegalArgumentException("Unsupported network device given!");
}
buffer.putInt(offset + DEVICE_INDEX_OFFSET, ((LinuxNetworkDevice) device).getIndex());
public J1939AddressBuffer setDevice(LinuxNetworkDevice device) {
buffer.putInt(offset + DEVICE_INDEX_OFFSET, device.getIndex());
return this;
}

Expand Down Expand Up @@ -102,6 +99,14 @@ public J1939AddressBuffer setAddress(byte address) {
return this;
}

public J1939AddressBuffer set(J1939Address other) {
setDevice(other.getDevice());
setName(other.getName());
setParameterGroupNumber(other.getParameterGroupNumber());
setAddress(other.getAddress());
return this;
}

@Override
public ImmutableJ1939Address copy() {
return new ImmutableJ1939Address(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public J1939ReceiveMessageHeaderBuffer() {
this(JavaCAN.allocateOrdered(BYTES));
}

private J1939ReceiveMessageHeaderBuffer(ByteBuffer buffer) {
public J1939ReceiveMessageHeaderBuffer(ByteBuffer buffer) {
this(buffer, buffer.position());
}

private J1939ReceiveMessageHeaderBuffer(ByteBuffer buffer, int offset) {
public J1939ReceiveMessageHeaderBuffer(ByteBuffer buffer, int offset) {
this.buffer = buffer;
this.offset = offset;
this.sourceAddressBuffer = new J1939AddressBuffer(buffer, SOURCE_ADDRESS_OFFSET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public int hashCode() {

private static native String findDeviceNameByIndex(int index) throws LinuxNativeOperationException;

public static LinuxNetworkDevice fromLinuxDeviceIndex(int index) {
public static LinuxNetworkDevice fromDeviceIndex(int index) {
return new LinuxNetworkDevice(null, index);
}
}

0 comments on commit 26d7362

Please sign in to comment.