forked from scream3r/java-simple-serial-connector
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unreachable native calls 107 #118
Draft
pietrygamat
wants to merge
2
commits into
java-native:master
Choose a base branch
from
pietrygamat:unreachable_native_calls_107
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package jssc; | ||
|
||
import junit.framework.TestCase; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.reflect.Whitebox; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* Tests if Java logic around native invocations does not prevent | ||
* critical calls from happening when opening and closing ports | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(fullyQualifiedNames = "jssc.*") | ||
public class NativeMethodInvocationTest extends TestCase { | ||
|
||
@Mock(name = "serialInterface") | ||
private SerialNativeInterface serialInterface; | ||
|
||
private final long mockHandle = 0xDeadDeefL; | ||
|
||
@Test | ||
public void nativeMethodIsNotCalledWhenAttemptingToOpenAlreadyOpenPort() { | ||
// given | ||
SerialPort serialPort = newSerialPort(); | ||
|
||
// when | ||
try{ | ||
serialPort.openPort(); | ||
serialPort.openPort(); | ||
Assert.fail("Expected to throw a SerialPortException"); | ||
} catch (SerialPortException expected) { | ||
// TODO: Is it really expected or should this method return false as javadoc states? | ||
} | ||
|
||
// then | ||
verify(serialInterface, times(1)).openPort(anyString(), anyBoolean()); | ||
} | ||
|
||
@Test | ||
public void nativeMethodIsNotCalledWhenAttemptingToClosePortThatIsNotYetOpen() { | ||
// given | ||
SerialPort serialPort = newSerialPort(); | ||
|
||
// when | ||
try{ | ||
serialPort.closePort(); | ||
Assert.fail("Expected to throw a SerialPortException"); | ||
} catch (SerialPortException expected) { | ||
// TODO: Is it really expected or should this method return false as javadoc states? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe either could occur. |
||
} | ||
|
||
// then | ||
verify(serialInterface, never()).setEventsMask(anyLong(), anyInt()); | ||
verify(serialInterface, never()).closePort(anyLong()); | ||
} | ||
|
||
@Test | ||
public void nativeMethodIsNotCalledWhenAttemptingCloseAlreadyClosedPort() { | ||
// given | ||
SerialPort serialPort = newSerialPort(); | ||
|
||
// when | ||
try{ | ||
serialPort.openPort(); | ||
serialPort.closePort(); | ||
serialPort.closePort(); | ||
Assert.fail("Expected to throw a SerialPortException"); | ||
} catch (SerialPortException expected) { | ||
// TODO: Is it really expected or should this method return false as javadoc states? | ||
} | ||
|
||
// then | ||
verify(serialInterface, times(1)).openPort(anyString(), anyBoolean()); | ||
verify(serialInterface, times(1)).closePort(anyLong()); | ||
} | ||
|
||
@Test | ||
public void nativeMethodIsCalledWhenClosingOpenAndReopeningClosedPort() throws SerialPortException { | ||
// given | ||
SerialPort serialPort = newSerialPort(); | ||
|
||
// when | ||
serialPort.openPort(); | ||
serialPort.closePort(); | ||
serialPort.openPort(); | ||
serialPort.closePort(); | ||
|
||
// then | ||
verify(serialInterface, times(2)).openPort(anyString(), anyBoolean()); | ||
verify(serialInterface, times(2)).closePort(mockHandle); | ||
} | ||
|
||
@Test | ||
public void nativeMethodIsCalledWhenClosingOpenPortWithEventListenerOnPosix() throws SerialPortException { | ||
// given | ||
setOs(SerialNativeInterface.OS_LINUX); | ||
SerialPort serialPort = newSerialPort(); | ||
when(serialInterface.setEventsMask(anyLong(), anyInt())).thenReturn(true); | ||
|
||
// when | ||
serialPort.openPort(); | ||
serialPort.addEventListener(someListener(), SerialPort.MASK_RXCHAR); | ||
serialPort.closePort(); | ||
|
||
// then | ||
verify(serialInterface, times(1)).openPort(anyString(), anyBoolean()); | ||
verify(serialInterface, never()).setEventsMask(anyLong(), anyInt()); | ||
verify(serialInterface, times(1)).closePort(mockHandle); | ||
} | ||
|
||
@Test | ||
public void nativeMethodIsCalledWhenClosingOpenPortWithEventListenerOnWindows() throws SerialPortException { | ||
// given | ||
setOs(SerialNativeInterface.OS_WINDOWS); | ||
SerialPort serialPort = newSerialPort(); | ||
when(serialInterface.setEventsMask(anyLong(), anyInt())).thenReturn(true); | ||
|
||
// when | ||
serialPort.openPort(); | ||
serialPort.addEventListener(someListener(), SerialPort.MASK_RXCHAR); | ||
serialPort.closePort(); | ||
|
||
// then | ||
verify(serialInterface, times(1)).openPort(anyString(), anyBoolean()); | ||
verify(serialInterface, times(1)).setEventsMask(mockHandle, SerialPort.MASK_RXCHAR); | ||
verify(serialInterface, times(1)).setEventsMask(mockHandle, 0); | ||
verify(serialInterface, times(1)).closePort(mockHandle); | ||
} | ||
|
||
/** | ||
* Simulates conditions as described in issue #107: user attempts to close the serial port that has been removed | ||
* from the system (e.g. unplugged usb serial adapter) without notifying the java code about it. | ||
*/ | ||
@Test | ||
public void nativeMethodIsCalledWhenClosingOpenPortWithEventListenerIfSetMaskFails() { | ||
// given | ||
setOs(SerialNativeInterface.OS_WINDOWS); | ||
SerialPort serialPort = newSerialPort(); | ||
when(serialInterface.setEventsMask(anyLong(), anyInt())).thenReturn(true); | ||
|
||
// when | ||
try { | ||
serialPort.openPort(); | ||
serialPort.addEventListener(someListener()); | ||
when(serialInterface.setEventsMask(anyLong(), anyInt())).thenReturn(false); | ||
serialPort.closePort(); | ||
} catch (SerialPortException expected) { | ||
// TODO: Is it really expected or should this method return false as javadoc states? | ||
} | ||
|
||
// then | ||
verify(serialInterface, times(1)).openPort(anyString(), anyBoolean()); | ||
verify(serialInterface, times(1)).setEventsMask(mockHandle, SerialPort.MASK_RXCHAR); | ||
verify(serialInterface, times(1)).setEventsMask(mockHandle, 0); | ||
verify(serialInterface, times(1)).closePort(mockHandle); | ||
} | ||
|
||
private void setOs(int os) { | ||
PowerMockito.mockStatic(SerialNativeInterface.class); | ||
when(SerialNativeInterface.getOsType()).thenReturn(os); | ||
} | ||
|
||
private SerialPort newSerialPort() { | ||
String portName = "dummy"; | ||
SerialPort serialPort = new SerialPort(portName); | ||
Whitebox.setInternalState(serialPort, "serialInterface", serialInterface); | ||
when(serialInterface.openPort(anyString(), anyBoolean())).thenReturn(mockHandle); | ||
when(serialInterface.closePort(anyLong())).thenReturn(true); | ||
when(serialInterface.waitEvents(anyLong())).thenReturn(new int[][]{}); | ||
return serialPort; | ||
} | ||
|
||
private SerialPortEventListener someListener() { | ||
return new SerialPortEventListener() { | ||
@Override | ||
public void serialEvent(SerialPortEvent serialPortEvent) { | ||
|
||
} | ||
}; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API currently either throws the exception or returns
true
.