Skip to content

Commit

Permalink
test: improve test coverage of XdrOpaque
Browse files Browse the repository at this point in the history
Acked-by: Paul Millar
Target: master
  • Loading branch information
kofemann committed Jan 29, 2019
1 parent 3d89c3e commit d308cdc
Showing 1 changed file with 128 additions and 0 deletions.
128 changes: 128 additions & 0 deletions oncrpc4j-core/src/test/java/org/dcache/oncrpc4j/xdr/XdrOpaqueTest.java
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());
}
}

}

0 comments on commit d308cdc

Please sign in to comment.