mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-03 04:12:31 +01:00
Fix findbugs findings in cloudstack-service-console-proxy-rdpclient
Findings: - 32 int shifted by an amount not in range -31..31: The shifts by 32 bits don't actually have any effect on the value (as shown by the tests) - possible null pointer dereference - repeated conditional test - field only ever set to null All other uses of the field were to check if it was null, which it was, so it was removed Other actions: - Upgrade jUnit to version 4 - Add PowerMock dependency Signed-off-by: Hugo Trippaers <htrippaers@schubergphilis.com>
This commit is contained in:
parent
ef2ced7564
commit
1237bf3222
12
pom.xml
12
pom.xml
@ -75,6 +75,7 @@
|
||||
<cs.vmware.api.version>5.1</cs.vmware.api.version>
|
||||
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
|
||||
<cs.mockito.version>1.9.5</cs.mockito.version>
|
||||
<cs.powermock.version>1.5.3</cs.powermock.version>
|
||||
<cs.aws.sdk.version>1.3.22</cs.aws.sdk.version>
|
||||
<cs.lang.version>2.6</cs.lang.version>
|
||||
<cs.commons-io.version>1.4</cs.commons-io.version>
|
||||
@ -417,6 +418,17 @@
|
||||
<version>1.9.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>${cs.powermock.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>${cs.powermock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
|
||||
@ -58,7 +58,7 @@
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<version>${cs.junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Apache Portable Runtime implementation of SSL protocol, which is compatible with broken MS RDP SSL suport.
|
||||
|
||||
@ -34,6 +34,7 @@ import streamer.apr.AprSocketWrapperImpl;
|
||||
import streamer.bco.BcoSocketWrapperImpl;
|
||||
import streamer.ssl.SSLState;
|
||||
import vncclient.VncClient;
|
||||
|
||||
import common.opt.IntOption;
|
||||
import common.opt.Option;
|
||||
import common.opt.OptionParser;
|
||||
@ -47,28 +48,28 @@ public class Client {
|
||||
}
|
||||
|
||||
// Common options
|
||||
private Option help = new Option() {
|
||||
private final Option help = new Option() {
|
||||
{
|
||||
name = "--help";
|
||||
alias = "-h";
|
||||
description = "Show this help text.";
|
||||
}
|
||||
};
|
||||
private Option debugLink = new Option() {
|
||||
private final Option debugLink = new Option() {
|
||||
{
|
||||
name = "--debug-link";
|
||||
alias = "-DL";
|
||||
description = "Print debugging messages when packets are trasnferred via links.";
|
||||
}
|
||||
};
|
||||
private Option debugElement = new Option() {
|
||||
private final Option debugElement = new Option() {
|
||||
{
|
||||
name = "--debug-element";
|
||||
alias = "-DE";
|
||||
description = "Print debugging messages when packets are received or sended by elements.";
|
||||
}
|
||||
};
|
||||
private Option debugPipeline = new Option() {
|
||||
private final Option debugPipeline = new Option() {
|
||||
{
|
||||
name = "--debug-pipeline";
|
||||
alias = "-DP";
|
||||
@ -76,7 +77,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private StringOption hostName = new StringOption() {
|
||||
private final StringOption hostName = new StringOption() {
|
||||
{
|
||||
name = "--host";
|
||||
alias = "-n";
|
||||
@ -85,7 +86,7 @@ public class Client {
|
||||
description = "Name or IP address of host to connect to.";
|
||||
}
|
||||
};
|
||||
private IntOption canvasWidth = new IntOption() {
|
||||
private final IntOption canvasWidth = new IntOption() {
|
||||
{
|
||||
name = "--width";
|
||||
alias = "-W";
|
||||
@ -94,7 +95,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private IntOption canvasHeight = new IntOption() {
|
||||
private final IntOption canvasHeight = new IntOption() {
|
||||
{
|
||||
name = "--height";
|
||||
alias = "-H";
|
||||
@ -105,7 +106,7 @@ public class Client {
|
||||
|
||||
// Protocol specific options
|
||||
|
||||
private IntOption vncPort = new IntOption() {
|
||||
private final IntOption vncPort = new IntOption() {
|
||||
{
|
||||
name = "--port";
|
||||
alias = "-p";
|
||||
@ -114,7 +115,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private IntOption rdpPort = new IntOption() {
|
||||
private final IntOption rdpPort = new IntOption() {
|
||||
{
|
||||
name = "--port";
|
||||
alias = "-p";
|
||||
@ -123,7 +124,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private IntOption hyperVPort = new IntOption() {
|
||||
private final IntOption hyperVPort = new IntOption() {
|
||||
{
|
||||
name = "--port";
|
||||
alias = "-p";
|
||||
@ -132,7 +133,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private StringOption password = new StringOption() {
|
||||
private final StringOption password = new StringOption() {
|
||||
{
|
||||
name = "--password";
|
||||
alias = "-P";
|
||||
@ -141,7 +142,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private StringOption rdpPassword = new StringOption() {
|
||||
private final StringOption rdpPassword = new StringOption() {
|
||||
{
|
||||
name = "--password";
|
||||
alias = "-P";
|
||||
@ -150,7 +151,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private StringOption userName = new StringOption() {
|
||||
private final StringOption userName = new StringOption() {
|
||||
{
|
||||
name = "--user";
|
||||
alias = "-U";
|
||||
@ -159,7 +160,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private StringOption domain = new StringOption() {
|
||||
private final StringOption domain = new StringOption() {
|
||||
{
|
||||
name = "--domain";
|
||||
alias = "-D";
|
||||
@ -168,7 +169,7 @@ public class Client {
|
||||
}
|
||||
};
|
||||
|
||||
private StringOption hyperVInstanceId = new StringOption() {
|
||||
private final StringOption hyperVInstanceId = new StringOption() {
|
||||
{
|
||||
name = "--instance";
|
||||
alias = "-i";
|
||||
@ -176,7 +177,7 @@ public class Client {
|
||||
description = "HyperV instance ID to use.";
|
||||
}
|
||||
};
|
||||
private StringEnumerationOption sslImplementation = new StringEnumerationOption() {
|
||||
private final StringEnumerationOption sslImplementation = new StringEnumerationOption() {
|
||||
{
|
||||
name = "--ssl-implementation";
|
||||
alias = "-j";
|
||||
@ -196,6 +197,7 @@ public class Client {
|
||||
private static ScrollPane scroller;
|
||||
private static ScreenDescription screen;
|
||||
private static BufferedImageCanvas canvas;
|
||||
private InetSocketAddress address;
|
||||
|
||||
private void help() {
|
||||
System.out.println("Usage: \n java common.Client vnc|rdp|hyperv OPTIONS\n");
|
||||
@ -243,32 +245,7 @@ public class Client {
|
||||
}
|
||||
});
|
||||
|
||||
// Assemble pipeline
|
||||
InetSocketAddress address;
|
||||
Element main;
|
||||
switch (protocol) {
|
||||
case VNC:
|
||||
address = new InetSocketAddress(hostName.value, vncPort.value);
|
||||
main = new VncClient("client", password.value, screen, canvas);
|
||||
break;
|
||||
case RDP:
|
||||
address = new InetSocketAddress(hostName.value, rdpPort.value);
|
||||
main = new RdpClient("client", hostName.value, domain.value, userName.value, rdpPassword.value, null, screen, canvas, sslState);
|
||||
break;
|
||||
case HYPERV:
|
||||
address = new InetSocketAddress(hostName.value, hyperVPort.value);
|
||||
main = new RdpClient("client", hostName.value, domain.value, userName.value, password.value, hyperVInstanceId.value, screen, canvas, sslState);
|
||||
break;
|
||||
default:
|
||||
address = null;
|
||||
main = null;
|
||||
}
|
||||
|
||||
Pipeline pipeline = new PipelineImpl("Client");
|
||||
pipeline.add(socket, main);
|
||||
pipeline.link("socket", main.getId(), "socket");
|
||||
|
||||
pipeline.validate();
|
||||
assemblePipeline(setMainElementAndAddressBasedOnProtocol(protocol, sslState));
|
||||
|
||||
frame = createVncClientMainWindow(canvas, protocol.toString() + " " + hostName.value, new WindowAdapter() {
|
||||
@Override
|
||||
@ -289,6 +266,37 @@ public class Client {
|
||||
}
|
||||
}
|
||||
|
||||
protected static void assemblePipeline(Element main) {
|
||||
Pipeline pipeline = new PipelineImpl("Client");
|
||||
pipeline.add(socket, main);
|
||||
pipeline.link("socket", main.getId(), "socket");
|
||||
|
||||
pipeline.validate();
|
||||
}
|
||||
|
||||
private Element setMainElementAndAddressBasedOnProtocol(Protocol protocol, SSLState sslState) {
|
||||
Element main;
|
||||
switch (protocol) {
|
||||
case VNC:
|
||||
address = new InetSocketAddress(hostName.value, vncPort.value);
|
||||
main = new VncClient("client", password.value, screen, canvas);
|
||||
break;
|
||||
case RDP:
|
||||
address = new InetSocketAddress(hostName.value, rdpPort.value);
|
||||
main = new RdpClient("client", hostName.value, domain.value, userName.value, rdpPassword.value, null, screen, canvas, sslState);
|
||||
break;
|
||||
case HYPERV:
|
||||
address = new InetSocketAddress(hostName.value, hyperVPort.value);
|
||||
main = new RdpClient("client", hostName.value, domain.value, userName.value, password.value, hyperVInstanceId.value, screen, canvas, sslState);
|
||||
break;
|
||||
default:
|
||||
address = null;
|
||||
main = null;
|
||||
}
|
||||
|
||||
return main;
|
||||
}
|
||||
|
||||
private Protocol parseOptions(String[] args) {
|
||||
String protocolName = (args.length > 0) ? args[0] : "";
|
||||
Protocol protocol = Protocol.NONE;
|
||||
|
||||
@ -36,7 +36,6 @@ public class ByteBuffer {
|
||||
public int cursor = 0;
|
||||
|
||||
private int refCount = 1;
|
||||
private ByteBuffer parentByteBuffer = null;
|
||||
|
||||
private Order order;
|
||||
|
||||
@ -159,8 +158,23 @@ public class ByteBuffer {
|
||||
*/
|
||||
public String dump() {
|
||||
StringBuilder builder = new StringBuilder(length * 4);
|
||||
int i = addBytesToBuilder(builder);
|
||||
int end = i - 1;
|
||||
if (end % 16 != 15) {
|
||||
int begin = end & ~0xf;
|
||||
for (int j = 0; j < (15 - (end % 16)); j++) {
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append(' ');
|
||||
builder.append(toASCIIString(begin, end));
|
||||
builder.append('\n');
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
protected int addBytesToBuilder(StringBuilder builder) {
|
||||
int i = 0;
|
||||
for (; i < length && i < length; i++) {
|
||||
for (; i < length; i++) {
|
||||
if (i % 16 == 0) {
|
||||
builder.append(String.format("%04x", i));
|
||||
}
|
||||
@ -175,17 +189,7 @@ public class ByteBuffer {
|
||||
builder.append('\n');
|
||||
}
|
||||
}
|
||||
int end = i - 1;
|
||||
if (end % 16 != 15) {
|
||||
int begin = end & ~0xf;
|
||||
for (int j = 0; j < (15 - (end % 16)); j++) {
|
||||
builder.append(" ");
|
||||
}
|
||||
builder.append(' ');
|
||||
builder.append(toASCIIString(begin, end));
|
||||
builder.append('\n');
|
||||
}
|
||||
return builder.toString();
|
||||
return i;
|
||||
}
|
||||
|
||||
private String toASCIIString(int start, int finish) {
|
||||
@ -222,14 +226,8 @@ public class ByteBuffer {
|
||||
refCount--;
|
||||
|
||||
if (refCount == 0) {
|
||||
|
||||
if (parentByteBuffer != null) {
|
||||
parentByteBuffer.unref();
|
||||
parentByteBuffer = null;
|
||||
} else {
|
||||
// Return buffer to buffer pool
|
||||
BufferPool.recycleBuffer(data);
|
||||
}
|
||||
// Return buffer to buffer pool
|
||||
BufferPool.recycleBuffer(data);
|
||||
|
||||
data = null;
|
||||
}
|
||||
@ -237,7 +235,7 @@ public class ByteBuffer {
|
||||
}
|
||||
|
||||
public boolean isSoleOwner() {
|
||||
return refCount == 1 && (parentByteBuffer == null);
|
||||
return refCount == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -247,8 +245,8 @@ public class ByteBuffer {
|
||||
ref();
|
||||
|
||||
if (this.length < (offset + length))
|
||||
throw new RuntimeException("Length of region is larger that length of this buffer. Buffer length: " + this.length + ", offset: " + offset
|
||||
+ ", new region length: " + length + ".");
|
||||
throw new RuntimeException("Length of region is larger that length of this buffer. Buffer length: " + this.length + ", offset: " + offset + ", new region length: "
|
||||
+ length + ".");
|
||||
|
||||
ByteBuffer slice = new ByteBuffer(data, this.offset + offset, length);
|
||||
|
||||
@ -308,8 +306,8 @@ public class ByteBuffer {
|
||||
|
||||
public short[] toShortArray() {
|
||||
if (length % 2 != 0)
|
||||
throw new ArrayIndexOutOfBoundsException("Length of byte array must be dividable by 2 without remainder. Array length: " + length + ", remainder: "
|
||||
+ (length % 2) + ".");
|
||||
throw new ArrayIndexOutOfBoundsException("Length of byte array must be dividable by 2 without remainder. Array length: " + length + ", remainder: " + (length % 2)
|
||||
+ ".");
|
||||
|
||||
short[] buf = new short[length / 2];
|
||||
|
||||
@ -324,8 +322,8 @@ public class ByteBuffer {
|
||||
*/
|
||||
public int[] toIntLEArray() {
|
||||
if (length % 4 != 0)
|
||||
throw new ArrayIndexOutOfBoundsException("Length of byte array must be dividable by 4 without remainder. Array length: " + length + ", remainder: "
|
||||
+ (length % 4) + ".");
|
||||
throw new ArrayIndexOutOfBoundsException("Length of byte array must be dividable by 4 without remainder. Array length: " + length + ", remainder: " + (length % 4)
|
||||
+ ".");
|
||||
|
||||
int[] buf = new int[length / 4];
|
||||
|
||||
@ -341,8 +339,8 @@ public class ByteBuffer {
|
||||
*/
|
||||
public int[] toInt3LEArray() {
|
||||
if (length % 3 != 0)
|
||||
throw new ArrayIndexOutOfBoundsException("Length of byte array must be dividable by 3 without remainder. Array length: " + length + ", remainder: "
|
||||
+ (length % 3) + ".");
|
||||
throw new ArrayIndexOutOfBoundsException("Length of byte array must be dividable by 3 without remainder. Array length: " + length + ", remainder: " + (length % 3)
|
||||
+ ".");
|
||||
|
||||
int[] buf = new int[length / 3];
|
||||
|
||||
@ -373,8 +371,7 @@ public class ByteBuffer {
|
||||
if (cursor + 4 > length)
|
||||
throw new ArrayIndexOutOfBoundsException("Cannot read 4 bytes from this buffer: " + this + ".");
|
||||
|
||||
int result = (((data[offset + cursor] & 0xff) << 24) + ((data[offset + cursor + 1] & 0xff) << 16) + ((data[offset + cursor + 2] & 0xff) << 8) + (data[offset
|
||||
+ cursor + 3] & 0xff));
|
||||
int result = (((data[offset + cursor] & 0xff) << 24) + ((data[offset + cursor + 1] & 0xff) << 16) + ((data[offset + cursor + 2] & 0xff) << 8) + (data[offset + cursor + 3] & 0xff));
|
||||
cursor += 4;
|
||||
return result;
|
||||
}
|
||||
@ -386,8 +383,7 @@ public class ByteBuffer {
|
||||
if (cursor + 4 > length)
|
||||
throw new ArrayIndexOutOfBoundsException("Cannot read 4 bytes from this buffer: " + this + ".");
|
||||
|
||||
int result = (((data[offset + cursor + 3] & 0xff) << 24) + ((data[offset + cursor + 2] & 0xff) << 16) + ((data[offset + cursor + 1] & 0xff) << 8) + (data[offset
|
||||
+ cursor] & 0xff));
|
||||
int result = (((data[offset + cursor + 3] & 0xff) << 24) + ((data[offset + cursor + 2] & 0xff) << 16) + ((data[offset + cursor + 1] & 0xff) << 8) + (data[offset + cursor] & 0xff));
|
||||
cursor += 4;
|
||||
return result;
|
||||
}
|
||||
@ -399,8 +395,8 @@ public class ByteBuffer {
|
||||
if (cursor + 4 > length)
|
||||
throw new ArrayIndexOutOfBoundsException("Cannot read 4 bytes from this buffer: " + this + ".");
|
||||
|
||||
long result = (((long)(data[offset + cursor + 3] & 0xff) << 24) + ((long)(data[offset + cursor + 2] & 0xff) << 16)
|
||||
+ ((long)(data[offset + cursor + 1] & 0xff) << 8) + (data[offset + cursor + 0] & 0xff));
|
||||
long result = (((long)(data[offset + cursor + 3] & 0xff) << 24) + ((long)(data[offset + cursor + 2] & 0xff) << 16) + ((long)(data[offset + cursor + 1] & 0xff) << 8) + (data[offset
|
||||
+ cursor + 0] & 0xff));
|
||||
cursor += 4;
|
||||
return result;
|
||||
}
|
||||
@ -412,12 +408,19 @@ public class ByteBuffer {
|
||||
if (cursor + 4 > length)
|
||||
throw new ArrayIndexOutOfBoundsException("Cannot read 4 bytes from this buffer: " + this + ".");
|
||||
|
||||
long result = (((long)(data[offset + cursor + 0] & 0xff) << 24) + ((long)(data[offset + cursor + 1] & 0xff) << 16)
|
||||
+ ((long)(data[offset + cursor + 2] & 0xff) << 8) + (data[offset + cursor + 3] & 0xff));
|
||||
byte value1 = data[offset + cursor + 0];
|
||||
byte value2 = data[offset + cursor + 1];
|
||||
byte value3 = data[offset + cursor + 2];
|
||||
byte value4 = data[offset + cursor + 3];
|
||||
long result = calculateUnsignedInt(value1, value2, value3, value4);
|
||||
cursor += 4;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected static long calculateUnsignedInt(byte value1, byte value2, byte value3, byte value4) {
|
||||
return (calculateUnsignedByte(value1) << 24) + (calculateUnsignedByte(value2) << 16) + (calculateUnsignedByte(value3) << 8) + calculateUnsignedByte(value4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read signed int in variable length format. Top most bit of each byte
|
||||
* indicates that next byte contains additional bits. Cursor is advanced by
|
||||
@ -474,11 +477,16 @@ public class ByteBuffer {
|
||||
if (cursor + 1 > length)
|
||||
throw new ArrayIndexOutOfBoundsException("Cannot read 1 byte from this buffer: " + this + ".");
|
||||
|
||||
int b = data[offset + cursor] & 0xff;
|
||||
byte value = data[offset + cursor];
|
||||
int b = calculateUnsignedByte(value);
|
||||
cursor += 1;
|
||||
return b;
|
||||
}
|
||||
|
||||
protected static int calculateUnsignedByte(byte value) {
|
||||
return value & 0xff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read signed byte. Cursor is advanced by 1.
|
||||
*/
|
||||
@ -498,11 +506,17 @@ public class ByteBuffer {
|
||||
if (cursor + 2 > length)
|
||||
throw new ArrayIndexOutOfBoundsException("Cannot read 2 bytes from this buffer: " + this + ".");
|
||||
|
||||
int result = (((data[offset + cursor] & 0xff) << 8) | (data[offset + cursor + 1] & 0xff));
|
||||
byte value1 = data[offset + cursor];
|
||||
byte value2 = data[offset + cursor + 1];
|
||||
int result = calculateUnsignedShort(value1, value2);
|
||||
cursor += 2;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected static int calculateUnsignedShort(byte value1, byte value2) {
|
||||
return (calculateUnsignedByte(value1) << 8) | calculateUnsignedByte(value2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read signed short in little endian order. Cursor is advanced by 2.
|
||||
*/
|
||||
@ -522,11 +536,17 @@ public class ByteBuffer {
|
||||
if (cursor + 2 > length)
|
||||
throw new ArrayIndexOutOfBoundsException("Cannot read 2 bytes from this buffer: " + this + ".");
|
||||
|
||||
short result = (short)(((data[offset + cursor + 0] & 0xff) << 8) | (data[offset + cursor + 1] & 0xff));
|
||||
byte value1 = data[offset + cursor + 0];
|
||||
byte value2 = data[offset + cursor + 1];
|
||||
short result = calculateSignedShort(value1, value2);
|
||||
cursor += 2;
|
||||
return result;
|
||||
}
|
||||
|
||||
protected static short calculateSignedShort(byte value1, byte value2) {
|
||||
return (short)calculateUnsignedShort(value1, value2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read unsigned short in network order in variable length format. Cursor is
|
||||
* advanced by 1 or 2 bytes.
|
||||
@ -653,13 +673,13 @@ public class ByteBuffer {
|
||||
value = readSignedInt();
|
||||
break;
|
||||
case 5:
|
||||
value = (readSignedByte() << 32) | readUnsignedInt();
|
||||
value = readSignedByte() | readUnsignedInt();
|
||||
break;
|
||||
case 6:
|
||||
value = (readSignedShort() << 32) | readUnsignedInt();
|
||||
value = readSignedShort() | readUnsignedInt();
|
||||
break;
|
||||
case 7:
|
||||
value = (readSignedByte() << 32 + 24) | (readUnsignedShort() << 32) | readUnsignedInt();
|
||||
value = (readSignedByte() << 24) | readUnsignedShort() | readUnsignedInt();
|
||||
break;
|
||||
case 8:
|
||||
value = readSignedLong();
|
||||
@ -694,20 +714,19 @@ public class ByteBuffer {
|
||||
value = readUnsignedInt();
|
||||
break;
|
||||
case 5:
|
||||
value = (readUnsignedByte() << 32) | readUnsignedInt();
|
||||
value = readUnsignedByte() | readUnsignedInt();
|
||||
break;
|
||||
case 6:
|
||||
value = (readUnsignedShort() << 32) | readUnsignedInt();
|
||||
value = readUnsignedShort() | readUnsignedInt();
|
||||
break;
|
||||
case 7:
|
||||
value = (readUnsignedByte() << 32 + 16) | (readUnsignedShort() << 32) | readUnsignedInt();
|
||||
value = (readUnsignedByte() << 16) | readUnsignedShort() | readUnsignedInt();
|
||||
break;
|
||||
case 8:
|
||||
value = readSignedLong();
|
||||
if (value < 0)
|
||||
throw new RuntimeException(
|
||||
"Cannot read 64 bit integers which are larger than 0x7FffFFffFFffFFff, because of lack of unsinged long type in Java. Value: " + value + ". Data: "
|
||||
+ this + ".");
|
||||
throw new RuntimeException("Cannot read 64 bit integers which are larger than 0x7FffFFffFFffFFff, because of lack of unsinged long type in Java. Value: " + value
|
||||
+ ". Data: " + this + ".");
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("Cannot read integers which are more than 8 bytes long. Length: " + len + ". Data: " + this + ".");
|
||||
@ -971,8 +990,7 @@ public class ByteBuffer {
|
||||
*/
|
||||
public void prepend(byte[] data, int offset, int length) {
|
||||
if (!isSoleOwner()) {
|
||||
throw new RuntimeException("Create full copy of this byte buffer data for modification. refCount: " + refCount + ", parentByteBuffer: "
|
||||
+ parentByteBuffer + ".");
|
||||
throw new RuntimeException("Create full copy of this byte buffer data for modification. refCount: " + refCount + ".");
|
||||
}
|
||||
|
||||
// If there is no enough space for header to prepend
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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.
|
||||
package common;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import streamer.Element;
|
||||
import streamer.SocketWrapper;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
public class ClientTest {
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testAssemblePipelineWhenMainElementIsNull() throws Exception {
|
||||
SocketWrapper socketMock = mock(SocketWrapper.class);
|
||||
when(socketMock.getId()).thenReturn("socket");
|
||||
Whitebox.setInternalState(Client.class, "socket", socketMock);
|
||||
Element main = null;
|
||||
|
||||
Client.assemblePipeline(main);
|
||||
}
|
||||
|
||||
}
|
||||
@ -30,11 +30,15 @@ import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import streamer.debug.MockServer;
|
||||
import streamer.debug.MockServer.Packet;
|
||||
|
||||
public class MockServerTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testIsMockServerCanRespond() throws Exception {
|
||||
|
||||
final byte[] mockClientData = new byte[] {0x01, 0x02, 0x03};
|
||||
@ -87,6 +91,7 @@ public class MockServerTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsMockServerCanUpgradeConnectionToSsl() throws Exception {
|
||||
|
||||
final byte[] mockClientData1 = new byte[] {0x01, 0x02, 0x03};
|
||||
|
||||
@ -0,0 +1,150 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you 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.
|
||||
package streamer;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class ByteBufferTest {
|
||||
|
||||
private static final Random random = new Random(System.currentTimeMillis());
|
||||
|
||||
private final byte[] data;
|
||||
|
||||
public ByteBufferTest(byte[] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
private static byte[] getRandomByteArray() {
|
||||
return new byte[] {(byte)random.nextInt(), (byte)random.nextInt(), (byte)random.nextInt(), (byte)random.nextInt(), (byte)random.nextInt(), (byte)random.nextInt(),
|
||||
(byte)random.nextInt()};
|
||||
}
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
int parameterCount = 50;
|
||||
List<Object[]> parameters = new ArrayList<Object[]>(parameterCount);
|
||||
|
||||
for (int i = 0; i < parameterCount; i++) {
|
||||
parameters.add(new Object[] {getRandomByteArray()});
|
||||
}
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
// This findbugs finding is meant to show that the shift by 32 does nothing
|
||||
// and was used to remove these cases from the production code.
|
||||
// Don't try to fix it
|
||||
@Test
|
||||
public void testShiftByteBy32BitsDoesNothing() throws Exception {
|
||||
for (byte b : data) {
|
||||
assertEquals(b, b << 32);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadSignedVarIntWhenLenIs5() throws Exception {
|
||||
int len = 5;
|
||||
ByteBuffer byteBuffer = new ByteBuffer(data);
|
||||
|
||||
long expected = data[0] | ByteBuffer.calculateUnsignedInt(data[1], data[2], data[3], data[4]);
|
||||
long actual = byteBuffer.readSignedVarInt(len);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadSignedVarIntWhenLenIs6() throws Exception {
|
||||
int len = 6;
|
||||
ByteBuffer byteBuffer = new ByteBuffer(data);
|
||||
|
||||
long expected = ByteBuffer.calculateSignedShort(data[0], data[1]) | ByteBuffer.calculateUnsignedInt(data[2], data[3], data[4], data[5]);
|
||||
long actual = byteBuffer.readSignedVarInt(len);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadSignedVarIntWhenLenIs7() throws Exception {
|
||||
int len = 7;
|
||||
ByteBuffer byteBuffer = new ByteBuffer(data);
|
||||
|
||||
long expected = data[0] << 24 | ByteBuffer.calculateUnsignedShort(data[1], data[2]) | ByteBuffer.calculateUnsignedInt(data[3], data[4], data[5], data[6]);
|
||||
long actual = byteBuffer.readSignedVarInt(len);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUnsignedVarIntWhenLenIs5() throws Exception {
|
||||
int len = 5;
|
||||
ByteBuffer byteBuffer = new ByteBuffer(data);
|
||||
|
||||
long expected = ByteBuffer.calculateUnsignedByte(data[0]) | ByteBuffer.calculateUnsignedInt(data[1], data[2], data[3], data[4]);
|
||||
long actual = byteBuffer.readUnsignedVarInt(len);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUnsignedVarIntWhenLenIs6() throws Exception {
|
||||
int len = 6;
|
||||
ByteBuffer byteBuffer = new ByteBuffer(data);
|
||||
|
||||
long expected = ByteBuffer.calculateUnsignedShort(data[0], data[1]) | ByteBuffer.calculateUnsignedInt(data[2], data[3], data[4], data[5]);
|
||||
long actual = byteBuffer.readUnsignedVarInt(len);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadUnsignedVarIntWhenLenIs7() throws Exception {
|
||||
int len = 7;
|
||||
ByteBuffer byteBuffer = new ByteBuffer(data);
|
||||
|
||||
long expected = (ByteBuffer.calculateUnsignedByte(data[0]) << 16) | ByteBuffer.calculateUnsignedShort(data[1], data[2])
|
||||
| ByteBuffer.calculateUnsignedInt(data[3], data[4], data[5], data[6]);
|
||||
long actual = byteBuffer.readUnsignedVarInt(len);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddBytesToBuilder() throws Exception {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
byte[] data = new byte[] {(byte)1, (byte)2};
|
||||
ByteBuffer byteBuffer = new ByteBuffer(data);
|
||||
|
||||
int expected = 2;
|
||||
int actual = byteBuffer.addBytesToBuilder(builder);
|
||||
|
||||
assertNotNull(builder);
|
||||
assertFalse(builder.toString().isEmpty());
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user