Skip to content

Commit

Permalink
Merge branch 'master' into dagger-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Aug 26, 2015
2 parents 04a89b9 + 72154bc commit 0117f62
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/net/azib/ipscan/fetchers/MACFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public abstract class MACFetcher extends AbstractFetcher {
public static final String ID = "fetcher.mac";
static final Pattern macAddressPattern = Pattern.compile("([a-fA-F0-9]{1,2}(-|:)){5}[a-fA-F0-9]{1,2}");
static final Pattern leadingZeroesPattern = Pattern.compile("(?<=^|-|:)([A-F0-9])(?=-|:|$)");

@Override public String getId() {
return ID;
Expand All @@ -33,6 +34,10 @@ static String bytesToMAC(byte[] bytes) {

static String extractMAC(String line) {
Matcher m = macAddressPattern.matcher(line);
return m.find() ? m.group().toUpperCase() : null;
return m.find() ? addLeadingZeroes(m.group().toUpperCase()) : null;
}

private static String addLeadingZeroes(String mac) {
return leadingZeroesPattern.matcher(mac).replaceAll("0$1");
}
}
6 changes: 6 additions & 0 deletions test/net/azib/ipscan/fetchers/MACFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public void extractMAC() throws Exception {
assertEquals("E4:48:C7:EE:28:C2", UnixMACFetcher.extractMAC("? (192.168.0.1) at e4:48:c7:ee:28:c2 [ether] on wlan0"));
}

@Test
public void extractMACAddsLeadingZeroesOnOsX() throws Exception {
assertEquals("04:48:07:EE:28:02", UnixMACFetcher.extractMAC("? (192.168.0.1) at 4:48:7:ee:28:2 [ether] on wlan0"));
assertEquals("C4:2C:03:08:1E:89", UnixMACFetcher.extractMAC("? (10.10.10.96) at c4:2c:3:8:1e:89 on en0 ifscope permanent [ethernet]"));
}

@Test
public void bytesToMAC() throws Exception {
assertEquals("", WinMACFetcher.bytesToMAC(new byte[0]));
Expand Down

0 comments on commit 0117f62

Please sign in to comment.