Skip to content
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

Fix TlvEncoder Date encoding for dates in a distant future (exceeding MAX_INT) #1630

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -19,7 +19,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.ByteBuffer;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.eclipse.leshan.core.tlv.Tlv.TlvType;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -57,15 +60,71 @@ 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 {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(ZoneId.of("UTC")));
cal.setTimeInMillis(utcTimeInSeconds * 1000);

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

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

// confirm decoded value
Date date = TlvDecoder.decodeDate(encoded);
assertEquals(cal.getTimeInMillis(), 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