Ejemplo n.º 1
0
        public void SetMetadata(Dictionary <PhysicalAddress, List <IPAddress> > metadata)
        {
            foreach (KeyValuePair <PhysicalAddress, List <IPAddress> > entry in metadata)
            {
                List <IPAddress> addresses   = entry.Value;
                PhysicalAddress  mac         = entry.Key;
                string           registryKey = mac.ToString();

                List <string>    registryKeys         = registryWriter.GetMultiStringValue(registryKey);
                List <IPAddress> registryForwardedIps = registryKeys.ConvertAll <IPAddress>(ConvertStringToIpAddress);
                List <IPAddress> addressesConfigured  = AddressSystemReader.GetAddresses();
                List <IPAddress> toAdd                = new List <IPAddress>(addresses.Except(addressesConfigured));
                List <IPAddress> toRemove             = new List <IPAddress>(registryForwardedIps.Except(addresses));
                List <string>    metadataStrings      = addresses.ConvertAll <string>(ip => ip.ToString());
                List <string>    toRemoveFromRegistry = new List <string>(registryKeys.Except(metadataStrings));
                List <string>    toAddToRegistry      = new List <string>(metadataStrings.Except(registryKeys));
                LogForwardedIpsChanges(mac, addressesConfigured, addresses, toAdd, toRemove);
                AddAddresses(toAdd, mac);
                RemoveAddresses(toRemove, mac);
                registryWriter.RemoveMultiStringValues(registryKey, toRemoveFromRegistry);
                foreach (string address in toAddToRegistry)
                {
                    registryWriter.AddMultiStringValue(mac.ToString(), address.ToString());
                }
            }
        }
        private void AddUserAccounts(List <WindowsKey> userAccounts)
        {
            foreach (WindowsKey userAccount in userAccounts)
            {
                while (true)
                {
                    try
                    {
                        CreateAccount(userAccount);
                        break;
                    }
                    catch (Win32Exception e)
                    {
                        if ((int)NativeMethods.NetUserRetEnum.NerrPasswordTooShort == e.NativeErrorCode)
                        {
                            Logger.Info("Password reset failed. Retrying...");
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                // The user account was created so add a multi-string value to
                // the registry.
                registryWriter.AddMultiStringValue(RegistryKey, userAccount.ToString());
            }
        }
 private void AddAddresses(IEnumerable <IPAddress> toAdd)
 {
     foreach (IPAddress address in toAdd)
     {
         AddAddress(address);
         registryWriter.AddMultiStringValue(RegistryKey, address.ToString());
     }
 }
Ejemplo n.º 4
0
        public void AddRegistryKeyTest()
        {
            string         registryKey    = "AddRegistryKeyTest";
            RegistryWriter registryWriter = GetRegistryWriter();

            // Empty string.
            string registryValueEmpty = string.Empty;

            // Long value.
            string registryValueLong = new string('x', 2048);

            // Special characters.
            string registryValueRandom = @"{}}12345\n\n\nhello\ncd C:\.\n!@#$%^&*()\n-=";

            // Many registry values.
            string[] registryValueList = Enumerable.Range(0, 10000).Select(num => num.ToString()).ToArray();

            List <string> expected = new List <string>();

            registryWriter.AddMultiStringValue(registryKey, registryValueEmpty);
            expected.Add(registryValueEmpty);
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);

            registryWriter.AddMultiStringValue(registryKey, registryValueLong);
            expected.Add(registryValueLong);
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);

            registryWriter.AddMultiStringValue(registryKey, registryValueRandom);
            expected.Add(registryValueRandom);
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);

            foreach (string value in registryValueList)
            {
                registryWriter.AddMultiStringValue(registryKey, value);
                expected.Add(value);
                RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);
            }
        }
Ejemplo n.º 5
0
        public void RegistryKeysConcurrentTest()
        {
            string         registryKeyFoo = "RegistryKeysConcurrentTestFoo";
            string         registryKeyBar = "RegistryKeysConcurrentTestBar";
            RegistryWriter writerFoo      = GetRegistryWriter();
            RegistryWriter writerBar      = new RegistryWriter(RegistryKeyPath);

            // List of Foo's  registry keys to test removal on: [0, 100).
            List <int> registryValuesFoo = Enumerable.Range(0, 100).ToList();

            // List of Bar's registry keys to test removal on: [50, 150).
            List <int> registryValuesBar = Enumerable.Range(50, 100).ToList();

            foreach (int value in registryValuesFoo)
            {
                writerFoo.AddMultiStringValue(registryKeyFoo, value.ToString());
            }

            foreach (int value in registryValuesBar)
            {
                writerBar.AddMultiStringValue(registryKeyBar, value.ToString());
            }

            // Remove elements from Foo that also exist in Bar: [90, 100).
            RemoveRegistryKeysCompareTest(
                remove: Enumerable.Range(90, 10).ToList(),
                registryValues: registryValuesFoo,
                registryWriter: writerFoo,
                registryKey: registryKeyFoo);

            // Remove elements from Bar that also exist in Foo: [50, 60).
            RemoveRegistryKeysCompareTest(
                remove: Enumerable.Range(50, 10).ToList(),
                registryValues: registryValuesBar,
                registryWriter: writerBar,
                registryKey: registryKeyBar);

            // Test that Foo and Bar contain the appropriate elements.
            RegistryKeysCompareTest(
                expected: Enumerable.Range(0, 90).Select(value => value.ToString()).ToList(),
                registryWriter: writerFoo,
                registryKey: registryKeyFoo);

            RegistryKeysCompareTest(
                expected: Enumerable.Range(60, 90).Select(value => value.ToString()).ToList(),
                registryWriter: writerBar,
                registryKey: registryKeyBar);
        }
Ejemplo n.º 6
0
        public void AddRegistryKeyEmptyTest()
        {
            string         registryKey    = "AddRegistryKeyEmptyTest";
            RegistryWriter registryWriter = GetRegistryWriter();

            // The registry key should not exist.
            Assert.Null(GetRegistryKey());

            // Adding a null value should leave the list of registry keys
            // unchanged.
            registryWriter.AddMultiStringValue(registryKey, registryValue: null);
            RegistryKeysCompareTest(
                expected: new List <string>(),
                registryWriter: registryWriter,
                registryKey: registryKey);

            // Any call to add a registry key should set up the registry key.
            Assert.NotNull(GetRegistryKey());
        }
Ejemplo n.º 7
0
        public void RemoveRegistryKeysEmptyTest()
        {
            string         registryKey    = "RemoveRegistryKeysEmptyTest";
            RegistryWriter registryWriter = GetRegistryWriter();
            List <string>  expected       = new List <string>();

            // The registry key should not exist.
            Assert.Null(GetRegistryKey());

            // Adding a null value should leave the list of registry keys
            // unchanged.
            registryWriter.RemoveMultiStringValues(registryKey, null);
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);

            // Calling remove should not create a registry key that doesn't
            // already exist.
            Assert.Null(GetRegistryKey());

            string testValue = "Test value.";

            registryWriter.AddMultiStringValue(registryKey, testValue);
            expected.Add(testValue);
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);

            // Removing null should not modify the list.
            registryWriter.RemoveMultiStringValues(registryKey, null);
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);

            // Removing the empty list should not modify the list.
            registryWriter.RemoveMultiStringValues(registryKey, new List <string>());
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);

            // Removing the empty list containing null should not modify the
            // list.
            registryWriter.RemoveMultiStringValues(registryKey, new List <string> {
                null
            });
            RegistryKeysCompareTest(expected: expected, registryWriter: registryWriter, registryKey: registryKey);
        }
Ejemplo n.º 8
0
        public void RemoveRegistryKeysTest()
        {
            string         registryKey    = "RemoveRegistryKeysTest";
            RegistryWriter registryWriter = GetRegistryWriter();

            // List of registry keys to test removal on.
            List <int> registryValues = Enumerable.Range(0, 100).ToList();

            // List of registry keys to remove.
            List <int> registryRemoveList = new List <int>();

            foreach (int value in registryValues)
            {
                registryWriter.AddMultiStringValue(registryKey, value.ToString());
            }

            RemoveRegistryKeysCompareTest(
                remove: registryRemoveList,
                registryValues: registryValues,
                registryWriter: registryWriter,
                registryKey: registryKey);

            // Try removing the first element.
            RemoveRegistryKeysCompareTest(
                remove: new List <int> {
                1
            },
                registryValues: registryValues,
                registryWriter: registryWriter,
                registryKey: registryKey);

            // Remove all elements less than 10.
            RemoveRegistryKeysCompareTest(
                remove: Enumerable.Range(0, 10).ToList(),
                registryValues: registryValues,
                registryWriter: registryWriter,
                registryKey: registryKey);

            // Remove some elements that do not exist: [0, 50).
            RemoveRegistryKeysCompareTest(
                remove: Enumerable.Range(0, 50).ToList(),
                registryValues: registryValues,
                registryWriter: registryWriter,
                registryKey: registryKey);

            registryValues = Enumerable.Range(50, 50).ToList();

            // Remove only elements that do not exist: [0, 25).
            RemoveRegistryKeysCompareTest(
                remove: Enumerable.Range(0, 25).ToList(),
                registryValues: registryValues,
                registryWriter: registryWriter,
                registryKey: registryKey);

            // Remove a superset of the elements that exist: [50, 150).
            registryRemoveList = Enumerable.Range(0, 50).ToList();
            RemoveRegistryKeysCompareTest(
                remove: Enumerable.Range(50, 100).ToList(),
                registryValues: registryValues,
                registryWriter: registryWriter,
                registryKey: registryKey);

            // Check there are no registry keys left.
            RegistryKeysCompareTest(
                expected: new List <string>(),
                registryWriter: registryWriter,
                registryKey: registryKey);
        }