Exemple #1
0
        /// <summary>
        /// Validates the profile creation process for different encryption and authentication types.
        /// </summary>
        /// <param name="authentication">Authentication type of access point.</param>
        /// <param name="encryption">Encryption type of access point.</param>
        /// <param name="ssid">Ssid of access point.</param>
        /// <param name="keyType">KeyType of access point.</param>
        /// <param name="keyIsEncrypted">Key/password specified in profile is encrypted or not.</param>
        /// <param name="password">Password needed to connect to access point, null if open access point.</param>
        public void CreateAndValidateProfile(WiFiAuthentication authentication,
                                             WiFiEncryptionType encryption, string ssid, WiFiKeyType keyType,
                                             bool keyIsEncrypted, string password)
        {
            /* Create access point, needed in order to create connection profile. */
            AccessPoint accessPoint = new AccessPoint()
            {
                Ssid           = ssid,
                Authentication = authentication,
                Encryption     = encryption,
                BssType        = WiFiBssType.Infrastructure
            };

            string profileXml = _profileService.CreateProfileXml(accessPoint, password);

            Assert.IsNotNull(profileXml, "Generated connection profile is null.");
            Assert.IsNotEmpty(profileXml, "Generated connection profile is string.Empty.");

            /* Attributes of generated profile are returned in profile object. */
            Profile profile = new Profile();
            bool    result  = _profileService.Parse(profileXml, ref profile);

            Assert.IsTrue(result, "Failed to parse generated profile.");
            /* Validate attributes of generated profile. */
            Assert.AreEqual(authentication, profile.Authentication, "Failed to validate authentication.");
            Assert.AreEqual(encryption, profile.Encryption, "Failed to validate encryption.");
            Assert.AreEqual(ssid, profile.Ssid, "Failed to validate ssid.");
            Assert.AreEqual(ssid, profile.ProfileName, "Failed to validate profile name.");
            Assert.AreEqual(WiFiBssType.Infrastructure, profile.BssType, "Failed to validate profile BSS type.");
            Assert.IsFalse(profile.IsAutoConnectEnabled, "Wireless profile automatic connection is enabled.");
            Assert.IsFalse(profile.IsAutoSwitchEnabled, "Wireless profile automatic switch is enabled.");
            Assert.AreEqual(keyType, profile.KeyType, "Failed to validate wireless profile KeyType.");
            Assert.AreEqual(keyIsEncrypted, profile.KeyIsEncrypted, "Failed to validate profile property KeyIsEncrypted.");
            Assert.AreEqual(password, profile.KeyValue, "Wireless profile password is not equal to expected password.");
        }
        /// <summary>
        /// Converts enum EncryptionType (ManagedNativeWifi) to internal type WiFiEncryptionType.
        /// </summary>
        private WiFiEncryptionType EncryptionTypeConverter(EncryptionType encryptionType)
        {
            WiFiEncryptionType wiFiEncryptionType = default(WiFiEncryptionType);

            switch (encryptionType)
            {
            case EncryptionType.None:
                wiFiEncryptionType = WiFiEncryptionType.None;
                break;

            case EncryptionType.WEP:
                wiFiEncryptionType = WiFiEncryptionType.WEP;
                break;

            case EncryptionType.TKIP:
                wiFiEncryptionType = WiFiEncryptionType.TKIP;
                break;

            case EncryptionType.AES:
                wiFiEncryptionType = WiFiEncryptionType.AES;
                break;
            }

            return(wiFiEncryptionType);
        }
        private WiFiEncryptionType GetEncryptionType(string source)
        {
            WiFiEncryptionType wiFiEncryptionType = default(WiFiEncryptionType);

            switch (source)
            {
            case "WEP":
                wiFiEncryptionType = WiFiEncryptionType.WEP;
                break;

            case "TKIP":
                wiFiEncryptionType = WiFiEncryptionType.TKIP;
                break;

            case "AES":
                wiFiEncryptionType = WiFiEncryptionType.AES;
                break;

            case "none":
                wiFiEncryptionType = WiFiEncryptionType.None;
                break;
            }

            return(wiFiEncryptionType);
        }
Exemple #4
0
        /// <summary>
        /// Validates the password rules for different encryption types.
        /// </summary>
        /// <param name="password">Password to validate.</param>
        /// <param name="encryption">Encryption type.</param>
        /// <param name="isValid">Is password expected to be valid.</param>
        /// <param name="expectedErrorMsg">Expected error message if password invalid.</param>
        public void ValidatePasswordRules(string password, WiFiEncryptionType encryption, bool isValid,
                                          string expectedErrorMsg)
        {
            string errorMsg = null;

            if (isValid)
            {
                Assert.IsTrue(PasswordHelper.IsValid(password, encryption, ref errorMsg),
                              "Password was expected to be valid but was actually invalid.");
                Assert.AreEqual(errorMsg, expectedErrorMsg, "Error message contained an unexpected value.");
            }
            else
            {
                Assert.IsFalse(PasswordHelper.IsValid(password, encryption, ref errorMsg),
                               "Password was expected to be invalid but was actually valid.");
                Assert.AreEqual(errorMsg, expectedErrorMsg, "Error message contained an unexpected value.");
            }
        }
 public AccessPoint(Guid id, string ssid, WiFiBssType bssType, bool isSecurityEnabled,
                    string profileName, bool networkConnectable, string wlanNotConnectableReason,
                    WiFiAuthentication authentication, WiFiEncryptionType encryption,
                    bool isConnected, int linkQuality, int frequency, float band, int channel)
 {
     Id                       = id;
     Ssid                     = ssid;
     BssType                  = bssType;
     IsSecurityEnabled        = isSecurityEnabled;
     ProfileName              = profileName;
     NetworkConnectable       = networkConnectable;
     WlanNotConnectableReason = wlanNotConnectableReason;
     Authentication           = authentication;
     Encryption               = encryption;
     IsConnected              = isConnected;
     LinkQuality              = linkQuality;
     Frequency                = frequency;
     Band                     = band;
     Channel                  = channel;
 }
 public Profile(Guid id, bool isConnected, string profileName, string ssid,
                WiFiProfileType profileType, WiFiBssType bssType, WiFiAuthentication authentication,
                WiFiEncryptionType encryption, WiFiKeyType keyType, bool keyIsEncrypted, string keyValue,
                bool isAutoConnectEnabled, bool isAutoSwitchEnabled, string xml, int position)
 {
     Id                   = id;
     IsConnected          = isConnected;
     ProfileName          = profileName;
     Ssid                 = ssid;
     ProfileType          = profileType;
     BssType              = bssType;
     Authentication       = authentication;
     Encryption           = encryption;
     KeyType              = keyType;
     KeyIsEncrypted       = keyIsEncrypted;
     KeyValue             = keyValue;
     IsAutoConnectEnabled = isAutoConnectEnabled;
     IsAutoSwitchEnabled  = isAutoSwitchEnabled;
     Xml                  = xml;
     Position             = position;
 }
        /// <summary>
        /// Checks if a password is valid for the specified encryption type.
        /// </summary>
        /// <param name="password">Password to validate.</param>
        /// <param name="encryption">Encryption type used by the WiFi access point.</param>
        /// <param name="errorMsg">Not valid reason.</param>
        /// <returns>True if password complies with encryption rules, false otherwise.</returns>
        public static bool IsValid(string password, WiFiEncryptionType encryption, ref string errorMsg)
        {
            bool isValid = false;

            if (encryption == WiFiEncryptionType.None)
            {
                isValid = true;
            }
            // WEP key is 10, 26 or 40 hex digits long.
            else if (encryption == WiFiEncryptionType.WEP &&
                     !string.IsNullOrEmpty(password) &&
                     (password.Length == 10 || password.Length == 26 || password.Length == 40) &&
                     new Regex("^[0-9A-Fa-f]+$").IsMatch(password))
            {
                isValid = true;
            }
            //WPA2-PSK/WPA-PSK 8 to 63 ASCII characters
            else if ((encryption == WiFiEncryptionType.AES ||
                      encryption == WiFiEncryptionType.TKIP) &&
                     !string.IsNullOrEmpty(password) &&
                     password.Length >= 8 &&
                     password.Length <= 63)
            {
                isValid = true;
            }

            /* If password not valid set error message. */
            if (!isValid && encryption == WiFiEncryptionType.WEP)
            {
                errorMsg = "WEP key is 10, 26 or 40 hex digits long.";
            }
            else if (!isValid && (encryption == WiFiEncryptionType.AES ||
                                  encryption == WiFiEncryptionType.TKIP))
            {
                errorMsg = "AES/TKIP password is 8 to 63 ASCII characters long.";
            }

            return(isValid);
        }
Exemple #8
0
        /// <summary>
        /// Validates the profile Parser method.
        /// </summary>
        /// <param name="index">Index of testcase see ProfileTestCases.</param>
        /// <param name="profileXml">Example profile used to call Parser method.</param>
        /// <param name="authentication">Authentication type of access point.</param>
        /// <param name="encryption">Encryption type of access point.</param>
        /// <param name="ssid">Ssid of access point.</param>
        /// <param name="bssType">BssType of access point.</param>
        /// <param name="IsAutoConnectEnabled">Is profile auto connectable.</param>
        /// <param name="IsAutoSwitchEnabled">Is auto switch enabled.</param>
        /// <param name="keyType">KeyType of access point.</param>
        /// <param name="keyIsEncrypted">Key/password specified in profile is encrypted or not.</param>
        /// <param name="password">Password needed to connect to access point, null if open access point.</param>
        public void ValidateProfileParser(int index, string profileXml,
                                          WiFiAuthentication authentication, WiFiEncryptionType encryption,
                                          string ssid, WiFiBssType bssType, bool IsAutoConnectEnabled,
                                          bool IsAutoSwitchEnabled, WiFiKeyType keyType, bool keyIsEncrypted, string password)
        {
            /* Attributes of generated profile are returned in profile object. */
            Profile profile = new Profile();
            bool    result  = _profileService.Parse(profileXml, ref profile);

            Assert.IsTrue(result, $"Testcase[{index}]: failed to parse example profile.");
            /* Validate attributes of generated profile. */
            Assert.AreEqual(authentication, profile.Authentication, $"Testcase[{index}]: failed to validate authentication.");
            Assert.AreEqual(encryption, profile.Encryption, $"Testcase[{index}]: failed to validate encryption.");
            Assert.AreEqual(ssid, profile.Ssid, $"Testcase[{index}]: failed to validate ssid.");
            Assert.AreEqual(ssid, profile.ProfileName, $"Testcase[{index}]: failed to validate profile name.");
            Assert.AreEqual(bssType, profile.BssType, $"Testcase[{index}]: failed to validate profile BSS type.");
            Assert.AreEqual(IsAutoConnectEnabled, profile.IsAutoConnectEnabled, $"Testcase[{index}]: failed to validate profile automatic connection property.");
            Assert.AreEqual(IsAutoSwitchEnabled, profile.IsAutoSwitchEnabled, $"Testcase[{index}]: failed to validate profile automatic switch property.");
            Assert.AreEqual(keyType, profile.KeyType, $"Testcase[{index}]: failed to validate wireless profile KeyType.");
            Assert.AreEqual(keyIsEncrypted, profile.KeyIsEncrypted, $"Testcase[{index}]: failed to validate profile property KeyIsEncrypted.");
            Assert.AreEqual(password, profile.KeyValue, $"Testcase[{index}]: wireless profile password is not equal to expected password.");
        }
Exemple #9
0
        /// <summary>
        /// Validates the access point types that are not supported.
        /// </summary>
        /// <param name="authentication">Authentication type of access point.</param>
        /// <param name="encryption">Encryption type of access point.</param>
        /// <param name="ssid">Ssid of access point.</param>
        /// <param name="bssType">BssType of access point, only infrastructure is supported by Microsoft.</param>
        /// <param name="exceptionType">Type of exception thrown when creating (not supported) connection profile.</param>
        /// <param name="expectedErrorMsg">Expected error message when creating (not supported) connection profile.</param>
        public void CreateProfileExceptions(WiFiAuthentication authentication,
                                            WiFiEncryptionType encryption,
                                            string ssid,
                                            WiFiBssType bssType,
                                            Type exceptionType,
                                            string expectedErrorMsg)
        {
            AccessPoint accessPoint = new AccessPoint()
            {
                Ssid           = ssid,
                Authentication = authentication,
                Encryption     = encryption,
                BssType        = bssType
            };

            /* Create profile with parameters that result in an exception. */
            Exception ex = Assert.Throws(exceptionType, () => _profileService.CreateProfileXml(accessPoint, null),
                                         "CreateProfileXml does not throw an exception as expected.");

            /* Check error message */
            Assert.AreEqual(expectedErrorMsg, ex.Message);
        }
        /// <summary>
        /// Validates connection profile and retrieves the attributes from the connection profile.
        /// </summary>
        /// <param name="profileXml">Connection profile to validate.</param>
        /// <param name="profile">Profile object contains attributes retrieved from profile.</param>
        /// <returns>True if profile is valide, false otherwise.</returns>
        public bool Parse(string profileXml, ref Profile profile)
        {
            bool isValid = false;

            if (string.IsNullOrWhiteSpace(profileXml))
            {
                throw new ArgumentNullException(nameof(profileXml));
            }

            XDocument document = XDocument.Parse(profileXml);

            if (!string.Equals(document.Root.Name.NamespaceName, Namespace))
            {
                throw new ArgumentException("The namespace in the Xml does not indicate a WiFi profile.",
                                            nameof(profileXml));
            }

            string   name           = document.Elements().First().Elements(XName.Get("name", Namespace)).FirstOrDefault()?.Value;
            XElement ssidElement    = document.Descendants(XName.Get("SSID", Namespace)).FirstOrDefault();
            string   ssidName       = ssidElement?.Descendants(XName.Get("name", Namespace)).FirstOrDefault()?.Value;
            string   ssidHex        = ssidElement?.Descendants(XName.Get("hex", Namespace)).FirstOrDefault()?.Value;
            string   connectionType = document.Descendants(XName.Get("connectionType", Namespace)).FirstOrDefault()?.Value;
            string   authentication = document.Descendants(XName.Get("authentication", Namespace)).FirstOrDefault()?.Value;
            string   encryption     = document.Descendants(XName.Get("encryption", Namespace)).FirstOrDefault()?.Value;

            /* Retrieve password data, optional data. */
            string   keyType = document.Descendants(XName.Get("keyType", Namespace)).FirstOrDefault()?.Value;
            XElement keyIsEncryptedElement = document.Descendants(XName.Get("protected", Namespace)).FirstOrDefault();
            string   keyMaterial           = document.Descendants(XName.Get("keyMaterial", Namespace)).FirstOrDefault()?.Value;

            /* ConnectionMode and autoSwitchElement are optional data. */
            XElement connectionModeElement = document.Descendants(XName.Get("connectionMode", Namespace)).FirstOrDefault();
            XElement autoSwitchElement     = document.Descendants(XName.Get("autoSwitch", Namespace)).FirstOrDefault();

            if (!string.IsNullOrEmpty(name) &&
                (!string.IsNullOrEmpty(ssidName) ||
                 !string.IsNullOrEmpty(ssidHex)) &&
                !string.IsNullOrEmpty(connectionType) &&
                !string.IsNullOrEmpty(authentication) &&
                !string.IsNullOrEmpty(encryption))
            {
                string ssid = !string.IsNullOrEmpty(ssidHex) ?
                              HexConverter.HexToString(ssidHex) : ssidName;
                WiFiBssType        wiFiBssType        = GetBssType(connectionType);
                WiFiAuthentication wiFiAuthentication = GetAuthentication(authentication);
                WiFiEncryptionType wiFiEncryptionType = GetEncryptionType(encryption);

                if (wiFiBssType != default(WiFiBssType) &&
                    wiFiAuthentication != default(WiFiAuthentication) &&
                    wiFiEncryptionType != default(WiFiEncryptionType))
                {
                    if (!ReferenceEquals(profile, null))
                    {
                        profile.ProfileName          = name;
                        profile.Ssid                 = ssid;
                        profile.BssType              = wiFiBssType;
                        profile.Authentication       = wiFiAuthentication;
                        profile.Encryption           = wiFiEncryptionType;
                        profile.KeyType              = GetKeyType(keyType);
                        profile.KeyIsEncrypted       = ((bool?)keyIsEncryptedElement).GetValueOrDefault();
                        profile.KeyValue             = keyMaterial;
                        profile.IsAutoConnectEnabled = IsAutoConnectEnabled(connectionModeElement?.Value);
                        profile.IsAutoSwitchEnabled  = ((bool?)autoSwitchElement).GetValueOrDefault();
                        profile.Xml = profileXml;
                    }

                    isValid = true;
                }
            }

            return(isValid);
        }