Skip to content

Commit

Permalink
GH-1630: Fix TlvEncoder - date (TIME) should be encoded like integer.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubsobolewskisag authored and sbernard31 committed Jul 25, 2024
1 parent e673ac7 commit 1b11956
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,21 @@ public static byte[] encodeString(String value) {
* Encodes a date value.
*/
public static byte[] encodeDate(Date value) {
ByteBuffer tBuf = ByteBuffer.allocate(4);
tBuf.putInt((int) (value.getTime() / 1000L));
ByteBuffer tBuf;
long lValue = value.getTime() / 1000L;
if (lValue <= Byte.MAX_VALUE && lValue >= Byte.MIN_VALUE) {
tBuf = ByteBuffer.allocate(1);
tBuf.put((byte) lValue);
} else if (lValue <= Short.MAX_VALUE && lValue >= Short.MIN_VALUE) {
tBuf = ByteBuffer.allocate(2);
tBuf.putShort((short) lValue);
} else if (lValue <= Integer.MAX_VALUE && lValue >= Integer.MIN_VALUE) {
tBuf = ByteBuffer.allocate(4);
tBuf.putInt((int) lValue);
} else {
tBuf = ByteBuffer.allocate(8);
tBuf.putLong(lValue);
}
return tBuf.array();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,70 @@ public void encode_long() {
assertEquals(0, bb.remaining());
}

@Test
public void encode_date() {
long timestamp = System.currentTimeMillis();
byte[] encoded = TlvEncoder.encodeDate(new Date(timestamp));
private void shouldEncodeDateAsNumberOfBytes(long utcTimeInSeconds, int expectedNumberOfBytes) throws TlvException {
Date dateToEncode = new Date(utcTimeInSeconds * 1000);

// check value
// encode
byte[] encoded = TlvEncoder.encodeDate(dateToEncode);

// check byte array value
ByteBuffer bb = ByteBuffer.wrap(encoded);
assertEquals((int) (timestamp / 1000), bb.getInt());
assertEquals(0, bb.remaining());
if (expectedNumberOfBytes == 1) {
assertEquals((byte) (dateToEncode.getTime() / 1000), bb.get());
assertEquals(0, bb.remaining());
} else if (expectedNumberOfBytes == 2) {
assertEquals((short) (dateToEncode.getTime() / 1000), bb.getShort());
assertEquals(0, bb.remaining());
} else if (expectedNumberOfBytes == 4) {
assertEquals((int) (dateToEncode.getTime() / 1000), bb.getInt());
assertEquals(0, bb.remaining());
} else {
assertEquals(dateToEncode.getTime() / 1000, bb.getLong());
assertEquals(0, bb.remaining());
}

// confirm decoded value
Date date = TlvDecoder.decodeDate(encoded);
assertEquals(dateToEncode.getTime(), date.getTime());
}

@Test
public void encode_date_byte() throws TlvException {
shouldEncodeDateAsNumberOfBytes(Byte.MAX_VALUE, 1);
shouldEncodeDateAsNumberOfBytes(Byte.MIN_VALUE, 1);
shouldEncodeDateAsNumberOfBytes(0, 1);
shouldEncodeDateAsNumberOfBytes(100, 1);
shouldEncodeDateAsNumberOfBytes(-100, 1);
}

@Test
public void encode_date_short() throws TlvException {
shouldEncodeDateAsNumberOfBytes(Byte.MAX_VALUE + 1, 2);
shouldEncodeDateAsNumberOfBytes(Byte.MIN_VALUE - 1, 2);
shouldEncodeDateAsNumberOfBytes(Short.MAX_VALUE, 2);
shouldEncodeDateAsNumberOfBytes(Short.MIN_VALUE, 2);
shouldEncodeDateAsNumberOfBytes(32000, 2);
shouldEncodeDateAsNumberOfBytes(-32000, 2);
}

@Test
public void encode_date_int() throws TlvException {
shouldEncodeDateAsNumberOfBytes(Short.MAX_VALUE + 1, 4);
shouldEncodeDateAsNumberOfBytes(Short.MIN_VALUE - 1, 4);
shouldEncodeDateAsNumberOfBytes(Integer.MAX_VALUE, 4);
shouldEncodeDateAsNumberOfBytes(Integer.MIN_VALUE, 4);
shouldEncodeDateAsNumberOfBytes(Integer.MAX_VALUE - 100, 4);
shouldEncodeDateAsNumberOfBytes(Integer.MIN_VALUE + 100, 4);
}

@Test
public void encode_date_long() throws TlvException {
shouldEncodeDateAsNumberOfBytes((long) Integer.MAX_VALUE + 1, 8);
shouldEncodeDateAsNumberOfBytes((long) Integer.MIN_VALUE - 1, 8);
shouldEncodeDateAsNumberOfBytes(Long.MAX_VALUE / 1000, 8);
shouldEncodeDateAsNumberOfBytes(Long.MIN_VALUE / 1000, 8);
shouldEncodeDateAsNumberOfBytes(Long.MAX_VALUE / 1000 - 100, 8);
shouldEncodeDateAsNumberOfBytes(Long.MIN_VALUE / 1000 + 100, 8);
}

@Test
Expand Down

0 comments on commit 1b11956

Please sign in to comment.