Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Add unit tests for wpa_supplicant parser
Browse files Browse the repository at this point in the history
Issue: #34
  • Loading branch information
bparmentier committed Oct 28, 2018
1 parent 97b55fc commit 3470c92
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.net.wifi.WifiConfiguration;

import java.io.Serializable;
import java.util.Objects;

public class WifiNetwork implements Serializable {
private String ssid;
Expand Down Expand Up @@ -112,6 +113,22 @@ public String toString() {
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WifiNetwork that = (WifiNetwork) o;
return isHidden == that.isHidden &&
Objects.equals(ssid, that.ssid) &&
Objects.equals(key, that.key) &&
authType == that.authType;
}

@Override
public int hashCode() {
return Objects.hash(ssid, key, authType, isHidden);
}

public void setKey(String key) {
this.key = key;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* WiFiKeyShare. Share Wi-Fi passwords with QR codes or NFC tags.
* Copyright (C) 2018 Bruno Parmentier <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package be.brunoparmentier.wifikeyshare.utils;

import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import be.brunoparmentier.wifikeyshare.model.WifiAuthType;
import be.brunoparmentier.wifikeyshare.model.WifiNetwork;

import static org.junit.Assert.assertEquals;

public class WpaSupplicantParserTest {

private static final String TAG = WpaSupplicantParserTest.class.getSimpleName();

private final List<WifiNetwork> expectedWifiNetworks = new ArrayList<>();

@Before
public void setUp() {
expectedWifiNetworks.add(new WifiNetwork(
"test1", WifiAuthType.OPEN, "", false));
expectedWifiNetworks.add(new WifiNetwork(
"test2", WifiAuthType.WEP, "test", false));
expectedWifiNetworks.add(new WifiNetwork(
"test3", WifiAuthType.WPA2_PSK, "test1234", false));
expectedWifiNetworks.add(new WifiNetwork(
"test4", WifiAuthType.WPA2_PSK, "ABCDEFGHI0123456789JKLMNOP", false));
expectedWifiNetworks.add(new WifiNetwork(
"test5", WifiAuthType.WPA2_PSK, "\";/\\=#}`,\\\"$4<d)=%", false));
expectedWifiNetworks.add(new WifiNetwork(
"test6", WifiAuthType.WPA2_EAP, "", false));
expectedWifiNetworks.add(new WifiNetwork(
"test7", WifiAuthType.WPA2_EAP, "", false));
expectedWifiNetworks.add(new WifiNetwork(
"a\"=$\\o(#=>™•,k8", WifiAuthType.OPEN, "", false));
}

@Test
public void parse() {
InputStream wpaSupplicantFileStream =
getClass().getClassLoader().getResourceAsStream("test_wpa_supplicant.conf");
Scanner s = new Scanner(wpaSupplicantFileStream).useDelimiter("\\A");
String wpaSupplicantString = s.hasNext() ? s.next() : "";

List<WifiNetwork> parsedWifiNetworks = WpaSupplicantParser.parse(wpaSupplicantString);
assertEquals(expectedWifiNetworks.size(), parsedWifiNetworks.size());
for (int i = 0; i < expectedWifiNetworks.size(); i++) {
assertEquals(expectedWifiNetworks.get(i), parsedWifiNetworks.get(i));
}
}
}

74 changes: 74 additions & 0 deletions app/src/test/resources/test_wpa_supplicant.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
disable_scan_offload=1
driver_param=use_p2p_group_interface=1
update_config=1
device_name=testdev
manufacturer=TestCo
model_name=TestDevice
model_number=TestDevice
serial_number=ABCDEFGHI
device_type=10-0050F204-5
config_methods=physical_display virtual_push_button
p2p_disabled=1
pmf=1
external_sim=1
tdls_external_control=1

network={
ssid="test1"
scan_ssid=1
key_mgmt=NONE
}

network={
ssid="test2"
scan_ssid=1
key_mgmt=NONE
auth_alg=OPEN SHARED
wep_key0="test"
}

network={
ssid="test3"
scan_ssid=1
psk="test1234"
key_mgmt=WPA-PSK
}

network={
ssid="test4"
psk="ABCDEFGHI0123456789JKLMNOP"
key_mgmt=WPA-PSK
priority=4289
}

network={
ssid="test5"
scan_ssid=1
psk="";/\=#}`,\"$4<d)=%"
key_mgmt=WPA-PSK
}

network={
ssid="test6"
scan_ssid=1
key_mgmt=WPA-EAP IEEE8021X
eap=PEAP
proactive_key_caching=1
}

network={
ssid="test7"
key_mgmt=WPA-EAP IEEE8021X
eap=PEAP
identity="abcdef"
password="qwertyuiop"
priority=24139
proactive_key_caching=1
disabled=1
}

network={
ssid=61223d245c6f28233d3ee284a2e280a22c6b38
scan_ssid=1
key_mgmt=NONE
}

0 comments on commit 3470c92

Please sign in to comment.