Visual Studio Unit Testing an asynchronous event callback

Problem …
Long title … simple problem: I am writing automated unit test cases for an asynchronous method call which returns back via an event handler (a callback).

If I had architected the method that I am calling I would have used the standard Begin/End methods to handle the asynchronous calls but I am using a pre-written source code so I need to adhere to its methodology.

Solution …
Nonetheless, I needed a way to have a TestMethod make the asynchronous method call and then somehow get the results back from the event handler. I was not (am still not) aware of anything built-in to NUnit or Visual Studio Unit Testing to handle this
so I figured I was on my own.

I figured that all I really needed was a way to sleep the test method until the event handler is called and then signal it to continue and analyze the data returned from the event. Signal. I had heard that before when talking about Thread Synchronization … threading … that is what the event callback is … duh. I need a thread signal!

Enter AutoResetEvent …
Very fortunately for me, .NET has built-in just such a signal using a class called AutoResetEvent (System.Threading). Once I realized this, the code was frighteningly simple. One thread creates the signal object (initializing its state to un-signaled) and waits on it. The second thread sets the signal and the initial thread continues.

Here’s some code:

  1. using System.Threading;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. namespace MyTests
  4. {
  5. [TestClass]
  6. public class MyTestClass
  7. {
  8. AutoResetEvent _TestTrigger;
  9. DataType _MyData;
  10. [TestMethod]
  11. public void MyTestMethod()
  12. {
  13. this._TestTrigger = new AutoResetEvent(false);
  14. MyAsyncCall();
  15. this._TestTrigger.WaitOne();
  16. Assert.AreEqual()
  17. }
  18. private void My_EventHandler()
  19. {
  20. this._MyData = result from event handler;
  21. this._TestTrigger.Set();
  22. }
  23. }
  24. }

You can make modifications to this so that the calling method does not wait indefinitely as well as other handy mods. If you come up with anything cool please drop a comment here so everyone can share in your find!

Advertisements

Getting and Setting a SecureString in .NET 2.0

SecureString Class
A nice new addition to the .NET 2.0 Framework is the SecureString class making it safe to store sensitive information in memory (e.g. passwords, connection strings). This class takes care of encrypting this information but the class does not provide a very straightforward method for getting and setting its value.

Since the actual value of the string is NOT stored in the memory space of your process it is not really a "managed" value so a bit of marshaling is required to work with it.

Setting a SecureString’s value
Fortunately, it is rather easy to set the value of a SecureString … but it has to be character by character. I assume the reason for this is because you really should not be using any transient/temporary variable to load the data into the SecureString. That would pretty much defeat its purpose. However, there will come a time when you want to set the value of the SecureString FROM another string. That much is simple:

SecureString securePassword = new SecureString();
string insecurePassword = "password";

foreach(char passChar in insecurePassword.ToCharArray()) {
    securePassword.AppendChar(passChar);
}



The above code simply iterates through the characters in the string and appends them to the SecureString.

Getting a SecureString’s value
It is as difficult, however, to retrieve the value from a SecureString as it was simple to set it. Since the value of the SecureString is not in the application’s process space your code has to interact with it via a pointer to a BSTR:

IntPtr passwordBSTR = default(IntPtr);

try {
    passwordBSTR = Marshal.SecureStringToBSTR(securePassword);
    insecurePassword = Marshal.PtrToStringBSTR(passwordBSTR);
} catch {
    insecurePassword = "";
}

This code uses the Marshal static class to retrieve the value of the SecureString into a BTRS and returns its pointer. Next, again using the Marshal class to reads the BSTR into a managed string vairable to be used at will.

Is this secure?
No … not really. It should be apparent by now that you are taking the value out of a secure, encrypted memory location and putting it right back into an insecure, unencrypted location.