diff --git a/core/src/main/java/net/glxn/qrgen/core/scheme/EnterpriseWifi.java b/core/src/main/java/net/glxn/qrgen/core/scheme/EnterpriseWifi.java new file mode 100644 index 0000000..6eb4f92 --- /dev/null +++ b/core/src/main/java/net/glxn/qrgen/core/scheme/EnterpriseWifi.java @@ -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: + * WIFI:S:SSID;U:USER;P:PSK;E:EAP;PH:PHASE;; + */ +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 parameters = getParameters( + wifiCode.substring(WIFI_PROTOCOL_HEADER.length()), "(?WIFI:T:AUTHENTICATION;S:SSID;P:PSK;H:HIDDEN; */ 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() { } diff --git a/core/src/test/java/net/glxn/qrgen/core/scheme/EnterpriseWifiTest.java b/core/src/test/java/net/glxn/qrgen/core/scheme/EnterpriseWifiTest.java new file mode 100644 index 0000000..d6a94bd --- /dev/null +++ b/core/src/test/java/net/glxn/qrgen/core/scheme/EnterpriseWifiTest.java @@ -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()); + } + +} diff --git a/core/src/test/java/net/glxn/qrgen/core/scheme/WifiTest.java b/core/src/test/java/net/glxn/qrgen/core/scheme/WifiTest.java index 3eabe49..cf87e41 100644 --- a/core/src/test/java/net/glxn/qrgen/core/scheme/WifiTest.java +++ b/core/src/test/java/net/glxn/qrgen/core/scheme/WifiTest.java @@ -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()); @@ -77,4 +77,4 @@ public void testToStringEscapeSsidAndPassword() { wifi.toString()); } -} +} \ No newline at end of file