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

Parsing large longs instead of crashing on Long.parseLong("0xffffffff") #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/main/java/org/altbeacon/beacon/BeaconParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,8 @@ private String byteArrayToFormattedString(byte[] byteBuffer, int startIndex, int


int length = endIndex-startIndex +1;
// We treat a 1-4 byte number as decimal string
if (length < 5) {
// We treat a 1-8 byte number as decimal string
if (length < 9) {
long number = 0l;
for (int i = 0; i < bytes.length; i++) {
long byteValue = (long) (bytes[bytes.length - i-1] & 0xff);
Expand All @@ -809,7 +809,7 @@ private String byteArrayToFormattedString(byte[] byteBuffer, int startIndex, int
return Long.toString(number);
}

// We treat a 7+ byte number as a hex string
// We treat a 9+ byte number as a hex string
String hexString = bytesToHex(bytes);

// And if it is a 12 byte number we add dashes to it to make it look like a standard UUID
Expand Down
36 changes: 22 additions & 14 deletions src/test/java/org/altbeacon/beacon/BeaconParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Parcel;

import static android.test.MoreAsserts.assertNotEqual;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.altbeacon.beacon.logging.LogManager;
import org.altbeacon.beacon.logging.Loggers;
import org.robolectric.RobolectricTestRunner;

import org.junit.runner.RunWith;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

@Config(sdk = 18)

@RunWith(RobolectricTestRunner.class)
Expand Down Expand Up @@ -159,7 +155,7 @@ public void testLittleEndianIdentifierParsing() {
assertEquals("id2 should be little endian", "0x0c0b0a090807", beacon.getIdentifier(1).toString());
assertEquals("id3 should be big endian", "0x0d0e0f1011121314", beacon.getIdentifier(2).toString());
assertEquals("txPower should be parsed", -59, beacon.getTxPower());
assertEquals("manufacturer should be parsed", 0x118 ,beacon.getManufacturer());
assertEquals("manufacturer should be parsed", 0x118, beacon.getManufacturer());
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
Expand All @@ -185,7 +181,7 @@ public void testRecognizeBeaconCapturedManufacturer() {
BeaconParser parser = new BeaconParser();
parser.setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");
Beacon beacon = parser.fromScanData(bytes, -55, null);
assertEquals("manufacturer should be parsed", "bbaa" ,String.format("%04x", beacon.getManufacturer()));
assertEquals("manufacturer should be parsed", "bbaa", String.format("%04x", beacon.getManufacturer()));
}


Expand Down Expand Up @@ -230,7 +226,6 @@ public void testParseProblematicBeaconFromIssue229() {
assertNotNull("beacon should be parsed", beacon);
}


@Test
public void testCanParseLocationBeacon() {
org.robolectric.shadows.ShadowLog.stream = System.err;
Expand Down Expand Up @@ -266,6 +261,7 @@ public void testCanParseLocationBeacon() {
assertEquals("longitude should be about right", longitude, parsedLongitude, 0.0001);

}

@Test
public void testCanGetAdvertisementDataForUrlBeacon() {
org.robolectric.shadows.ShadowLog.stream = System.err;
Expand All @@ -281,4 +277,16 @@ public void testCanGetAdvertisementDataForUrlBeacon() {
assertEquals("First byte of url should be in position 3", 0x02, bytes[2]);
}

@Test
public void testCanParseLargeLongValues() {
LogManager.setLogger(Loggers.verboseLogger());
org.robolectric.shadows.ShadowLog.stream = System.err;
byte[] bytes = hexStringToByteArray("02010609030318021804180f1803190002020a0609ff0e0aa000598f0001");
BeaconParser parser = new BeaconParser();
parser.setBeaconLayout("x,m:0-1=0e0a,d:0-5,d:6-6,d:7-7");
Beacon beacon = parser.fromScanData(bytes, -55, null);
assertEquals("mRssi should be as passed in", -55, beacon.getRssi());
assertEquals("manufacturer should be parsed", 0x0a0e ,beacon.getManufacturer());
assertEquals("data should be parsed", Long.parseLong("0e0aa000598f", 16), (long) beacon.getDataFields().get(0));
}
}