MrApfelstrudel
MrApfelstrudel

Reputation: 157

Simulate a network failure

I wrote an application using a webservice and I want to simulate a network failure for test purposes. I know I could turn off the network manually, but it would be awesome if it would be automatically.

I tried the solution from: How to simulate network failure for test purposes (in C#)? from Larsenal but it doesn't recognize the ManagementClass/ObjectCollection/... and I don't know why (i used System.Managment.Man... and it still didn't work. I imported the required references - didn't work. I have no idea what I am doing wrong)

It should work something like this:

[TestMethod]
public void Service_Login_NoInternetConnection()
{
  // Some code...
  TurnOffNetworkConnection();
  // More code...
  TurnOnNetworkConnection();
  // Blablabla code...
}

Upvotes: 3

Views: 1947

Answers (1)

Creator
Creator

Reputation: 1512

You can use WMI for it.

First make sure you add reference : System.Management

Then I get all devices with :
"ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration");"

Now i need to check if a device got DHCPLeaseObtained.
So I use foreach to check every network device in the searcher :
String Check = Convert.ToString(queryObj["DHCPLeaseObtained"]);

If the device has no DHCPLeaseObtained the string will be emty. So I check if the string is emty :
if (String.IsNullOrEmpty(Check))

Then you can use ReleaseDHCPLease and RenewDHCPLease in the else.
ManagementBaseObject outParams = queryObj.InvokeMethod("ReleaseDHCPLease", null, null);
or
ManagementBaseObject outParams = queryObj.InvokeMethod("RenewDHCPLease", null, null);

using System.Management;


    public void TurnOnNetworkConnection()
{

        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration");

               foreach (ManagementObject queryObj in searcher.Get())
               {
                  String Check = Convert.ToString(queryObj["DHCPLeaseObtained"]);
                  if (String.IsNullOrEmpty(Check))
                    {
                    }
                    else
                    {
                    ManagementBaseObject outParams = queryObj.InvokeMethod("RenewDHCPLease", null, null);
                    }
               }
         }
           catch (ManagementException e)
           {
           MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
           }
}

    public void TurnOffNetworkConnection()
{
        try
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration"); 

            foreach (ManagementObject queryObj in searcher.Get())
            {
                String Check = Convert.ToString(queryObj["DHCPLeaseObtained"]);
                if (String.IsNullOrEmpty(Check)) 
                {
                }
                else
                {
                    ManagementBaseObject outParams = queryObj.InvokeMethod("ReleaseDHCPLease", null, null);
                }
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
        }
}

Upvotes: 1

Related Questions