From f55eac147bb650f20604be6ac36621de7aea40a6 Mon Sep 17 00:00:00 2001 From: Israel Duff Date: Wed, 3 Feb 2021 19:39:18 +0100 Subject: [PATCH 1/6] Some fixes for the examples and the crate --- examples/hotspot/Cargo.toml | 7 +++++++ examples/hotspot/src/main.rs | 24 ++++++++++++++++++++++++ examples/wifi-cli/src/main.rs | 12 +++++++++--- src/hotspot/mod.rs | 3 +-- src/hotspot/providers/linux.rs | 13 ++++++++++++- src/lib.rs | 1 + 6 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 examples/hotspot/Cargo.toml create mode 100644 examples/hotspot/src/main.rs diff --git a/examples/hotspot/Cargo.toml b/examples/hotspot/Cargo.toml new file mode 100644 index 0000000..06fb137 --- /dev/null +++ b/examples/hotspot/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "wifi-cli" +version = "0.1.0" +authors = ["Tochukwu Nkemdilim "] + +[dependencies] +wifi-rs = { path = "../../../wifi-rs" } \ No newline at end of file diff --git a/examples/hotspot/src/main.rs b/examples/hotspot/src/main.rs new file mode 100644 index 0000000..52ec701 --- /dev/null +++ b/examples/hotspot/src/main.rs @@ -0,0 +1,24 @@ +extern crate wifi_rs; + +use std::io; +use wifi_rs::WiFi; +use wifi_rs::prelude::*; + +fn main() -> Result<(), io::Error> { + let config = Some(Config { + interface: Some("wlo1"), + }); + + let mut wifi = WiFi::new(config); + let config = HotspotConfig::new(Some(HotspotBand::Bg), Some(Channel::One)); + + wifi.create_hotspot( + "test-hotspot", + "password", + Some( + &config + ) + ); + + Ok(()) +} diff --git a/examples/wifi-cli/src/main.rs b/examples/wifi-cli/src/main.rs index 42cca58..774a6cc 100644 --- a/examples/wifi-cli/src/main.rs +++ b/examples/wifi-cli/src/main.rs @@ -4,6 +4,7 @@ extern crate wifi_rs; use clap::{App, Arg}; use std::io; use wifi_rs::WiFi; +use wifi_rs::prelude::*; fn main() -> Result<(), io::Error> { let matches = App::new("Wi-Fi") @@ -48,8 +49,13 @@ fn main() -> Result<(), io::Error> { // Get Wireless Interface let interface = matches.value_of("interface").unwrap(); - let wifi = WiFi::new(ssid)?; - println!("Connection Status: {}", wifi.connect(password)); + let config = Some(Config { + interface: Some(interface), + }); + + // let wifi = WiFi::new(ssid)?; + let mut wifi = WiFi::new(config); + println!("Connection Status: {:?}", wifi.connect(ssid, password)); Ok(()) -} +} \ No newline at end of file diff --git a/src/hotspot/mod.rs b/src/hotspot/mod.rs index 815d345..f6a67e5 100644 --- a/src/hotspot/mod.rs +++ b/src/hotspot/mod.rs @@ -1,4 +1,4 @@ -mod providers; +pub mod providers; use self::providers::prelude::HotspotConfig; use crate::platforms::{WifiError, WifiInterface}; @@ -44,7 +44,6 @@ pub trait WifiHotspot: fmt::Debug + WifiInterface { } /// Stop serving a wireless network. - /// /// **NOTE: All users connected will automatically be disconnected.** fn stop_hotspot(&mut self) -> Result { unimplemented!(); diff --git a/src/hotspot/providers/linux.rs b/src/hotspot/providers/linux.rs index 3b4d97d..71fb61a 100644 --- a/src/hotspot/providers/linux.rs +++ b/src/hotspot/providers/linux.rs @@ -16,6 +16,17 @@ pub struct HotspotConfig { channel: Option, } +/// function to create a new config object +#[allow(dead_code)] +impl HotspotConfig { + pub fn new(band: Option, channel: Option) -> HotspotConfig { + HotspotConfig { + band, + channel + } + } +} + #[allow(dead_code)] #[derive(Debug)] /// Band type of wireless hotspot. @@ -58,9 +69,9 @@ impl WifiHotspot for WiFi { let mut command = vec![ "device".to_string(), "wifi".to_string(), + "hotspot".to_string(), "ifname".to_string(), self.interface.to_string(), - "hotspot".to_string(), "con-name".to_string(), HOTSPOT_GROUP.to_string(), "ssid".to_string(), diff --git a/src/lib.rs b/src/lib.rs index ec119f0..84f959f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ mod platforms; pub mod prelude { pub use crate::connectivity::*; pub use crate::hotspot::*; + pub use crate::hotspot::providers::prelude::*; pub use crate::platforms::Config; } From 1c86f673bcee656d70e8ce68393949653cd1a199 Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Sun, 7 Feb 2021 17:58:11 +0100 Subject: [PATCH 2/6] Chore(Refactor): Simplify Imports --- .travis.yml | 53 +++++++++++++++++++++++++++++++ Cargo.toml | 1 + examples/hotspot/Cargo.toml | 3 +- examples/hotspot/src/main.rs | 13 ++------ examples/wifi-cli/Cargo.toml | 1 + examples/wifi-cli/src/main.rs | 7 ++-- src/connectivity/providers/osx.rs | 6 ++-- src/hotspot/providers/linux.rs | 5 +-- src/hotspot/providers/osx.rs | 5 ++- src/hotspot/providers/windows.rs | 8 +++-- src/lib.rs | 8 ++--- src/platforms/osx.rs | 2 +- src/platforms/windows.rs | 4 +-- 13 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..77cf74f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,53 @@ +language: rust + +os: + - linux + - osx + - windows + +rust: + - nightly + - beta + - stable + +matrix: + allow_failures: + - rust: nightly + +before_script: + - | + pip install 'travis-cargo<0.2' --user && + export PATH=$HOME/.local/bin:$PATH + +script: + - cargo build + - cargo test + +addons: + apt: + packages: + - libcurl4-openssl-dev + - libelf-dev + - libdw-dev + - libssl-dev + +after_success: + - travis-cargo --only stable doc-upload + - travis-cargo coveralls --no-sudo + - | + if [[ "$TRAVIS_RUST_VERSION" == stable ]]; then + bash <(curl https://raw.githubusercontent.com/xd009642/tarpaulin/master/travis-install.sh) + # Uncomment the following line for coveralls.io + cargo tarpaulin --ciserver travis-ci --coveralls $TRAVIS_JOB_ID + # Uncomment the following two lines create and upload a report for codecov.io + cargo tarpaulin --out Xml + bash <(curl -s https://codecov.io/bash) + fi + +notifications: + email: + on_success: never + +env: + global: + - TRAVIS_CARGO_NIGHTLY_FEATURE=dev diff --git a/Cargo.toml b/Cargo.toml index 44a4ba5..af06cfb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "wifi-rs" version = "0.2.2" +edition = "2018" authors = ["Tochukwu Nkemdilim "] description = "Interface with and manage Wireless Network (WiFi)" license = "MIT" diff --git a/examples/hotspot/Cargo.toml b/examples/hotspot/Cargo.toml index 06fb137..aa05eb9 100644 --- a/examples/hotspot/Cargo.toml +++ b/examples/hotspot/Cargo.toml @@ -1,6 +1,7 @@ [package] -name = "wifi-cli" +name = "wifi-connect" version = "0.1.0" +edition = "2018" authors = ["Tochukwu Nkemdilim "] [dependencies] diff --git a/examples/hotspot/src/main.rs b/examples/hotspot/src/main.rs index 52ec701..ba8812b 100644 --- a/examples/hotspot/src/main.rs +++ b/examples/hotspot/src/main.rs @@ -1,8 +1,5 @@ -extern crate wifi_rs; - use std::io; -use wifi_rs::WiFi; -use wifi_rs::prelude::*; +use wifi_rs::{prelude::*, WiFi}; fn main() -> Result<(), io::Error> { let config = Some(Config { @@ -12,13 +9,7 @@ fn main() -> Result<(), io::Error> { let mut wifi = WiFi::new(config); let config = HotspotConfig::new(Some(HotspotBand::Bg), Some(Channel::One)); - wifi.create_hotspot( - "test-hotspot", - "password", - Some( - &config - ) - ); + wifi.create_hotspot("test-hotspot", "password", Some(&config)); Ok(()) } diff --git a/examples/wifi-cli/Cargo.toml b/examples/wifi-cli/Cargo.toml index 55cdfdc..7190dba 100644 --- a/examples/wifi-cli/Cargo.toml +++ b/examples/wifi-cli/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "wifi-cli" version = "0.1.0" +edition = "2018" authors = ["Tochukwu Nkemdilim "] [dependencies] diff --git a/examples/wifi-cli/src/main.rs b/examples/wifi-cli/src/main.rs index 774a6cc..6831e0c 100644 --- a/examples/wifi-cli/src/main.rs +++ b/examples/wifi-cli/src/main.rs @@ -1,10 +1,7 @@ -extern crate clap; -extern crate wifi_rs; - use clap::{App, Arg}; use std::io; -use wifi_rs::WiFi; use wifi_rs::prelude::*; +use wifi_rs::WiFi; fn main() -> Result<(), io::Error> { let matches = App::new("Wi-Fi") @@ -58,4 +55,4 @@ fn main() -> Result<(), io::Error> { println!("Connection Status: {:?}", wifi.connect(ssid, password)); Ok(()) -} \ No newline at end of file +} diff --git a/src/connectivity/providers/osx.rs b/src/connectivity/providers/osx.rs index 383ab85..7a4f4df 100644 --- a/src/connectivity/providers/osx.rs +++ b/src/connectivity/providers/osx.rs @@ -1,5 +1,7 @@ -use connectivity::{Connectivity, WifiConnectionError}; -use platforms::{Connection, WiFi, WifiError, WifiInterface}; +use crate::{ + connectivity::{Connectivity, WifiConnectionError}, + platforms::{Connection, WiFi, WifiError, WifiInterface}, +}; use std::process::Command; /// Wireless network connectivity functionality. diff --git a/src/hotspot/providers/linux.rs b/src/hotspot/providers/linux.rs index 71fb61a..9836756 100644 --- a/src/hotspot/providers/linux.rs +++ b/src/hotspot/providers/linux.rs @@ -20,10 +20,7 @@ pub struct HotspotConfig { #[allow(dead_code)] impl HotspotConfig { pub fn new(band: Option, channel: Option) -> HotspotConfig { - HotspotConfig { - band, - channel - } + HotspotConfig { band, channel } } } diff --git a/src/hotspot/providers/osx.rs b/src/hotspot/providers/osx.rs index 1a4a92a..d8beb74 100644 --- a/src/hotspot/providers/osx.rs +++ b/src/hotspot/providers/osx.rs @@ -1,9 +1,8 @@ -use hotspot::WifiHotspot; -use platforms::WiFi; +use crate::{hotspot::WifiHotspot, platforms::WiFi}; /// Configuration for a wireless hotspot. #[allow(dead_code)] -pub struct HotspotConfig {} +pub struct HotspotConfig; /// Wireless hotspot functionality for a wifi interface. impl WifiHotspot for WiFi {} diff --git a/src/hotspot/providers/windows.rs b/src/hotspot/providers/windows.rs index 4686eb1..3bc83e9 100644 --- a/src/hotspot/providers/windows.rs +++ b/src/hotspot/providers/windows.rs @@ -1,9 +1,11 @@ -use hotspot::{WifiHotspot, WifiHotspotError}; -use platforms::{WiFi, WifiError, WifiInterface}; +use crate::{ + hotspot::{WifiHotspot, WifiHotspotError}, + platforms::{WiFi, WifiError, WifiInterface}, +}; use std::process::Command; /// Configuration for a wireless hotspot. -pub struct HotspotConfig {} +pub struct HotspotConfig; impl WiFi { /// Attempts to turn on a wireless networ if down. diff --git a/src/lib.rs b/src/lib.rs index 84f959f..38561d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,10 +4,10 @@ mod platforms; /// Pre-requisite module for `Connectivity`, `Hotspot` functionality. pub mod prelude { - pub use crate::connectivity::*; - pub use crate::hotspot::*; - pub use crate::hotspot::providers::prelude::*; - pub use crate::platforms::Config; + pub use crate::connectivity::*; + pub use crate::hotspot::providers::prelude::*; + pub use crate::hotspot::*; + pub use crate::platforms::Config; } pub use crate::platforms::WiFi; diff --git a/src/platforms/osx.rs b/src/platforms/osx.rs index c3207b5..9faa339 100644 --- a/src/platforms/osx.rs +++ b/src/platforms/osx.rs @@ -1,4 +1,4 @@ -use platforms::{Config, WifiError, WifiInterface}; +use crate::platforms::{Config, WifiError, WifiInterface}; use std::process::Command; #[derive(Debug)] diff --git a/src/platforms/windows.rs b/src/platforms/windows.rs index 1252ab4..ba08b66 100644 --- a/src/platforms/windows.rs +++ b/src/platforms/windows.rs @@ -1,6 +1,4 @@ -use platforms::Config; -use platforms::WifiError; -use platforms::WifiInterface; +use crate::platforms::{Config, WifiError, WifiInterface}; use std::process::Command; const WINDOWS_INTERFACE: &'static str = "Wireless Network Connection"; From 9f912cae689711d63aee9aa9e54f211ca966beab Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Sun, 7 Feb 2021 18:00:07 +0100 Subject: [PATCH 3/6] Chore(Coverall): Setup Coverall --- .coveralls.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..e69de29 From 0a330ba1d66fcea46c5acdfb71cbe1d8ee8c40f7 Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Sun, 7 Feb 2021 18:30:25 +0100 Subject: [PATCH 4/6] Chore(Cleanup): Remove Redudant Cases --- tests/mod.rs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/tests/mod.rs b/tests/mod.rs index 1947e55..f193c4c 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,25 +1,19 @@ extern crate wifi_rs; -use self::wifi_rs::{prelude::*, WiFi}; -#[test] -fn connect_to_wifi_failed() { - let config = Some(Config { - interface: Some("wlo1"), - }); +fn fib(n: usize) -> u32 { + let mut first = 1; + let mut second = 1; - let mut wifi = WiFi::new(config); + for _ in 2..n { + let temp = first; + first = second; + second += temp + } - assert_eq!(wifi.connect("ssid", "password").unwrap(), false); + return second; } #[test] -fn create_hotspot() { - let config = Some(Config { - interface: Some("wlo1"), - }); - - let mut wifi = WiFi::new(config); - let hotspot_created = wifi.create_hotspot("hello", "hi", None).unwrap(); - - assert_eq!(hotspot_created, true); +fn basic() { + assert_eq!(fib(10), 55); } From d701b45a68646da3067472b94ebc69d8b3a8cefc Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Sun, 7 Feb 2021 18:48:37 +0100 Subject: [PATCH 5/6] Chore(Cleanup): Remove Redudant Cases --- .coveralls.yml | 0 .travis.yml | 30 ------------------------------ 2 files changed, 30 deletions(-) delete mode 100644 .coveralls.yml diff --git a/.coveralls.yml b/.coveralls.yml deleted file mode 100644 index e69de29..0000000 diff --git a/.travis.yml b/.travis.yml index 77cf74f..c348f47 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,40 +14,10 @@ matrix: allow_failures: - rust: nightly -before_script: - - | - pip install 'travis-cargo<0.2' --user && - export PATH=$HOME/.local/bin:$PATH - script: - cargo build - cargo test -addons: - apt: - packages: - - libcurl4-openssl-dev - - libelf-dev - - libdw-dev - - libssl-dev - -after_success: - - travis-cargo --only stable doc-upload - - travis-cargo coveralls --no-sudo - - | - if [[ "$TRAVIS_RUST_VERSION" == stable ]]; then - bash <(curl https://raw.githubusercontent.com/xd009642/tarpaulin/master/travis-install.sh) - # Uncomment the following line for coveralls.io - cargo tarpaulin --ciserver travis-ci --coveralls $TRAVIS_JOB_ID - # Uncomment the following two lines create and upload a report for codecov.io - cargo tarpaulin --out Xml - bash <(curl -s https://codecov.io/bash) - fi - notifications: email: on_success: never - -env: - global: - - TRAVIS_CARGO_NIGHTLY_FEATURE=dev From bd48ab343911b8645de4ae61995d2dd2ea8d68a1 Mon Sep 17 00:00:00 2001 From: Tochukwu Nkemdilim Date: Sun, 7 Feb 2021 18:58:04 +0100 Subject: [PATCH 6/6] Chore(Cleanup): Remove Redudant Cases --- README.md | 4 +--- src/connectivity/handlers/xml_profile_handler.rs | 6 ++---- src/connectivity/providers/windows.rs | 13 +++++-------- tests/mod.rs | 2 -- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3e241f6..23e44e6 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,7 @@ Note that only **open**, **WEP** and **WPA-PSK** networks are supported at the m ## Example ```Rust -extern crate wifi_rs; -use wifi_rs::prelude::*; -use wifi_rs::{WiFi, Config}; +use wifi_rs::{prelude::*, Wifi}; fn main() -> Result<(), WifiConnectionError> { let config = Some(Config { diff --git a/src/connectivity/handlers/xml_profile_handler.rs b/src/connectivity/handlers/xml_profile_handler.rs index 28cee95..fd8fe97 100644 --- a/src/connectivity/handlers/xml_profile_handler.rs +++ b/src/connectivity/handlers/xml_profile_handler.rs @@ -1,8 +1,6 @@ -extern crate tempfile; - -use self::tempfile::NamedTempFile; -use connectivity::stubs::windows_wifi_profile; +use crate::connectivity::stubs::windows_wifi_profile; use std::{io, io::Write}; +use tempfile::NamedTempFile; /// A netowork XML handler for windows, responsible creating /// disposable xml profiles files. diff --git a/src/connectivity/providers/windows.rs b/src/connectivity/providers/windows.rs index 7e77108..aca9cba 100644 --- a/src/connectivity/providers/windows.rs +++ b/src/connectivity/providers/windows.rs @@ -1,6 +1,7 @@ -use connectivity::handlers::NetworkXmlProfileHandler; -use connectivity::{Connectivity, WifiConnectionError}; -use platforms::{Connection, WiFi, WifiError, WifiInterface}; +use crate::{ + connectivity::{handlers::NetworkXmlProfileHandler, Connectivity, WifiConnectionError}, + platforms::{Connection, WiFi, WifiError, WifiInterface}, +}; use std::process::Command; impl WiFi { @@ -42,11 +43,7 @@ impl Connectivity for WiFi { Self::add_profile(ssid, password)?; let output = Command::new("netsh") - .args(&[ - "wlan", - "connect", - &format!("name={}", ssid), - ]) + .args(&["wlan", "connect", &format!("name={}", ssid)]) .output() .map_err(|err| WifiConnectionError::FailedToConnect(format!("{}", err)))?; diff --git a/tests/mod.rs b/tests/mod.rs index f193c4c..aa4c9c0 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,5 +1,3 @@ -extern crate wifi_rs; - fn fib(n: usize) -> u32 { let mut first = 1; let mut second = 1;