-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve test coverage of XdrOpaque
Acked-by: Paul Millar Target: master
- Loading branch information
Showing
1 changed file
with
128 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrOpaqueTest.java
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,128 @@ | ||
package org.dcache.oncrpc4j.xdr; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class XdrOpaqueTest { | ||
|
||
@Test | ||
public void testEncode() throws IOException { | ||
|
||
XdrOpaque opaque = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8)); | ||
|
||
try(Xdr xdr = new Xdr(128)) { | ||
|
||
xdr.beginEncoding(); | ||
opaque.xdrEncode(xdr); | ||
xdr.endEncoding(); | ||
|
||
xdr.beginDecoding(); | ||
|
||
assertArrayEquals("invalid opaque value", opaque.getOpaque(), xdr.xdrDecodeDynamicOpaque()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDecode() throws IOException { | ||
|
||
byte[] value = "some bytes".getBytes(StandardCharsets.UTF_8); | ||
|
||
try (Xdr xdr = new Xdr(128)) { | ||
|
||
xdr.beginEncoding(); | ||
xdr.xdrEncodeDynamicOpaque(value); | ||
xdr.endEncoding(); | ||
|
||
xdr.beginDecoding(); | ||
|
||
XdrOpaque opaque = new XdrOpaque(xdr); | ||
|
||
assertArrayEquals("invalid opaque value", value, opaque.getOpaque()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSameMustBeEqual() throws IOException { | ||
|
||
XdrOpaque opaque = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8)); | ||
|
||
assertTrue("opaques with equal values must be equal", opaque.equals(opaque)); | ||
} | ||
|
||
@Test | ||
public void testEqualsByValue() throws IOException { | ||
|
||
XdrOpaque opaque1 = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8)); | ||
XdrOpaque opaque2 = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8)); | ||
|
||
assertTrue("opaques with equal values must be equal", opaque1.equals(opaque2)); | ||
assertTrue("equal objects must have the same hashcode", opaque1.hashCode() == opaque2.hashCode()); | ||
} | ||
|
||
@Test | ||
public void testDifferentTypesCantBeEqual() throws IOException { | ||
|
||
byte[] data = "some bytes".getBytes(StandardCharsets.UTF_8); | ||
XdrOpaque opaque = new XdrOpaque(data); | ||
|
||
assertFalse("opaques with equal values must be equal", opaque.equals(data)); | ||
} | ||
|
||
|
||
@Test | ||
public void testDifferentObjectDifferentStrings() { | ||
XdrOpaque opaque1 = new XdrOpaque("some bytes".getBytes(StandardCharsets.UTF_8)); | ||
XdrOpaque opaque2 = new XdrOpaque("some other bytes".getBytes(StandardCharsets.UTF_8)); | ||
assertNotNull("toString should not return null", opaque1.toString()); | ||
assertNotNull("toString should not return null", opaque2.toString()); | ||
|
||
assertNotEquals("different by value must return different strings", | ||
opaque1.toString(), opaque2.toString()); | ||
} | ||
|
||
@Test | ||
public void testDecodeWellKnown() throws IOException { | ||
|
||
byte[] data = new byte[]{ | ||
0x0, 0x0, 0x0, 0x08, // size | ||
0x0C, 0x0A, 0x0F, 0x0E, 0x0B, 0x0A, 0x0B, 0x0E // data | ||
}; | ||
|
||
try (Xdr xdr = new Xdr(data)) { | ||
xdr.beginDecoding(); | ||
|
||
XdrOpaque opaque = new XdrOpaque(xdr); | ||
// the data part must match (e.g without size) | ||
assertArrayEquals(Arrays.copyOfRange(data, 4, 12), opaque.getOpaque()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEncodeWellKnown() throws IOException { | ||
|
||
byte[] data = new byte[]{ | ||
0x0C, 0x0A, 0x0F, 0x0E, 0x0B, 0x0A, 0x0B, 0x0E // data | ||
}; | ||
|
||
byte[] expected = new byte[]{ | ||
0x0, 0x0, 0x0, 0x08, // size | ||
0x0C, 0x0A, 0x0F, 0x0E, 0x0B, 0x0A, 0x0B, 0x0E // data | ||
}; | ||
|
||
try (Xdr xdr = new Xdr(16)) { | ||
xdr.beginEncoding(); | ||
|
||
XdrOpaque opaque = new XdrOpaque(data); | ||
opaque.xdrEncode(xdr); | ||
|
||
xdr.endEncoding(); | ||
|
||
// the data part must match (e.g without size) | ||
assertArrayEquals(expected, xdr.getBytes()); | ||
} | ||
} | ||
|
||
} |