Simulate a network failure

I have written an application using a webservice and I want to simulate a network failure for testing purposes. I know I can turn off the network manually, but it would be great if it were automatic.

I tried the solution: How to simulate a network failure for test purposes (in C #)? from Larsenal, but doesn't recognize 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 am I doing wrong)

It should work something like this:

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

      

+3


source to share


1 answer


You can use WMI for it.

First make sure you add the reference: System.Management

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

Now I need to check if the device got DHCPLeaseObtained.
Therefore, I use foreach to check each network device in the
finder : String Check = Convert.ToString (queryObj ["DHCPLeaseObtained"]);



If the device does not have DHCPLeaseObtained, the line will be emty. So I check if the line is emty:
if (String.IsNullOrEmpty (Check))

Then you can use ReleaseDHCPLease and RenewDHCPLease in the other.
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);
        }
}

      

+1


source







All Articles