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

Added extended EnterpriseWifi class #83

Merged
merged 9 commits into from
Oct 30, 2016
123 changes: 123 additions & 0 deletions core/src/main/java/net/glxn/qrgen/core/scheme/EnterpriseWifi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package net.glxn.qrgen.core.scheme;

import java.util.Map;

import static net.glxn.qrgen.core.scheme.SchemeUtil.getParameters;

/**
* Encodes a Wifi connection containing a username / password scheme for authentication, format is:
* <code>WIFI:S:SSID;U:USER;P:PSK;E:EAP;PH:PHASE;;</code>
*/
public class EnterpriseWifi extends Wifi {
public static final String USER = "U";
public static final String EAP = "E";
public static final String PHASE = "PH";
private String user;
private String eap;
private String phase;

public EnterpriseWifi() {
}

public static EnterpriseWifi parse(final String wifiCode) {
if (wifiCode == null || !wifiCode.startsWith(WIFI_PROTOCOL_HEADER)) {
throw new IllegalArgumentException(
"this is not a valid WIFI code: " + wifiCode);
}
EnterpriseWifi wifi = new EnterpriseWifi();
Map<String, String> parameters = getParameters(
wifiCode.substring(WIFI_PROTOCOL_HEADER.length()), "(?<!\\\\);");
if (parameters.containsKey(SSID)) {
wifi.setSsid(unescape(parameters.get(SSID)));
}
if (parameters.containsKey(PSK)) {
wifi.setPsk(unescape(parameters.get(PSK)));
}
if (parameters.containsKey(USER)) {
wifi.setUser(unescape(parameters.get(USER)));
}
if (parameters.containsKey(EAP)) {
wifi.setEap(unescape(parameters.get(EAP)));
}
if (parameters.containsKey(PHASE)) {
wifi.setPhase(unescape(parameters.get(PHASE)));
}
if (parameters.containsKey(HIDDEN)) {
wifi.setHidden(parameters.get(HIDDEN));
}
return wifi;
}

public static String escape(final String text) {
return text.replace("\\", "\\\\").replace(",", "\\,")
.replace(";", "\\;").replace(".", "\\.")
.replace("\"", "\\\"").replace("'", "\\'");
}

public static String unescape(final String text) {
return text.replace("\\\\", "\\").replace("\\,", ",")
.replace("\\;", ";").replace("\\.", ".")
.replace("\\\"", "\"").replace("\\'", "'");
}

public EnterpriseWifi withUser(String user) {
this.user = user;
return this;
}

public void setUser(String user) {
withUser(user);
}

public String getUser() {
return user;
}

public EnterpriseWifi withEap(String eap) {
this.eap = eap;
return this;
}

public void setEap(String eap) {
withEap(eap);
}

public String getEap() {
return eap;
}

public EnterpriseWifi withPhase(String phase) {
this.phase = phase;
return this;
}

public void setPhase(String phase) {
withPhase(phase);
}

public String getPhase() {
return phase;
}

@Override
public String toString() {
StringBuilder bob = new StringBuilder(WIFI_PROTOCOL_HEADER);
if (getSsid() != null) {
bob.append(SSID).append(":").append(escape(getSsid())).append(";");
}
if (getUser() != null) {
bob.append(USER).append(":").append(escape(getUser())).append(";");
}
if (getPsk() != null) {
bob.append(PSK).append(":").append(escape(getPsk())).append(";");
}
if (getEap() != null) {
bob.append(EAP).append(":").append(escape(getEap())).append(";");
}
if (getPhase() != null) {
bob.append(PHASE).append(":").append(escape(getPhase())).append(";");
}
bob.append(HIDDEN).append(":").append(isHidden()).append(";");
return bob.toString();
}
}
12 changes: 6 additions & 6 deletions core/src/main/java/net/glxn/qrgen/core/scheme/Wifi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
* <code>WIFI:T:AUTHENTICATION;S:SSID;P:PSK;H:HIDDEN;</code>
*/
public class Wifi {

private static final String WIFI_PROTOCOL_HEADER = "WIFI:";
private static final String AUTHENTICATION = "T";
private static final String SSID = "S";
private static final String PSK = "P";
private static final String HIDDEN = "H";
public static final String WIFI_PROTOCOL_HEADER = "WIFI:";
public static final String AUTHENTICATION = "T";
public static final String SSID = "S";
public static final String PSK = "P";
public static final String HIDDEN = "H";
private String authentication;
private String ssid;
private String psk;
private boolean hidden = false;

public Wifi() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package net.glxn.qrgen.core.scheme;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import net.glxn.qrgen.core.scheme.Wifi.Authentication;

import org.junit.Test;

public class EnterpriseWifiTest {

@Test
public void parse() {
EnterpriseWifi wifi = EnterpriseWifi.parse(
"WIFI:S:some weird SSID;U:Spock;P:aintNoSecret;E:PEAP;PH:MS-CHAPv2;H:true;");
assertEquals("some weird SSID", wifi.getSsid());
assertEquals("Spock", wifi.getUser());
assertEquals("aintNoSecret", wifi.getPsk());
assertEquals("PEAP", wifi.getEap());
assertEquals("MS-CHAPv2", wifi.getPhase());
assertEquals(true, wifi.isHidden());
}

/**
* The following characters need to be escaped with a backslash (\) in the
* SSID and PSK strings: backslash (\), single-quote ('), double-quote ("),
* dot (.), colon (:), comma (,), and semicolon (;)
*/
@Test
public void parseEscapeSsidAndAuth() {
EnterpriseWifi wifi = EnterpriseWifi.parse(
"WIFI:S:s\\;o\\,\\\"me \\'wei\\\\rd\\. SSID\\;;U:Sp\\;ock;P:\\;a\\,\\\"intNo\\,Sec\\\\ret;E:PEAP;PH:MS-CHAPv2;false;");

assertEquals("s;o,\"me 'wei\\rd. SSID;", wifi.getSsid());
assertEquals("Sp;ock", wifi.getUser());
assertEquals(";a,\"intNo,Sec\\ret", wifi.getPsk());
assertEquals("PEAP", wifi.getEap());
assertEquals("MS-CHAPv2", wifi.getPhase());
assertEquals(false, wifi.isHidden());
}

@Test(expected = IllegalArgumentException.class)
public void parseNull() {
EnterpriseWifi.parse(null);
}

@Test(expected = IllegalArgumentException.class)
public void parseEmptyString() {
EnterpriseWifi.parse("");
}

@Test
public void parseHeaderOnly() {
EnterpriseWifi wifi = EnterpriseWifi.parse("WIFI:");
assertNull(null, wifi.getSsid());
assertNull(null, wifi.getUser());
assertNull(null, wifi.getPsk());
assertNull(null, wifi.getEap());
assertNull(null, wifi.getPhase());
assertEquals(false, wifi.isHidden());
}

@Test
public void testToString() {
EnterpriseWifi wifi = new EnterpriseWifi();
wifi.setSsid("some weird SSID");
wifi.setUser("Spock");
wifi.setPsk("aintNoSecret");
wifi.setEap("PEAP");
wifi.setPhase("MS-CHAPv2");
wifi.setHidden(true);

assertEquals("WIFI:S:some weird SSID;U:Spock;P:aintNoSecret;E:PEAP;PH:MS-CHAPv2;H:true;",
wifi.toString());
}

@Test
public void testToStringEscapeUsernameAndPassword() {
EnterpriseWifi wifi = new EnterpriseWifi();
wifi.setSsid("s;o,\"me 'wei\\rd. SSID;");
wifi.setUser("Sp;ock");
wifi.setPsk(";a,\"intNo,Sec\\ret");
wifi.setEap("PEAP");
wifi.setPhase("MS-CHAPv2");
wifi.setHidden(false);

assertEquals("WIFI:S:s\\;o\\,\\\"me \\'wei\\\\rd\\. SSID\\;;U:Sp\\;ock;P:\\;a\\,\\\"intNo\\,Sec\\\\ret;E:PEAP;PH:MS-CHAPv2;H:false;",
wifi.toString());
}

}
4 changes: 2 additions & 2 deletions core/src/test/java/net/glxn/qrgen/core/scheme/WifiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void parse() {
public void parseEscapeSsidAndPassword() {
Wifi wifi = Wifi.parse(
"WIFI:S:s\\;o\\,\\\"me \\'wei\\\\rd\\. SSID\\;;T:WPA;P:\\;a\\,\\\"intNo\\,Sec\\\\ret;false;");

assertEquals("s;o,\"me 'wei\\rd. SSID;", wifi.getSsid());
assertEquals("WPA", wifi.getAuthentication());
assertEquals(";a,\"intNo,Sec\\ret", wifi.getPsk());
Expand Down Expand Up @@ -77,4 +77,4 @@ public void testToStringEscapeSsidAndPassword() {
wifi.toString());
}

}
}