Archive for testing

Testing COM1 ports using a virtual machine and putty

A few days ago a friend of mine asked me a few questions about how to test a COM1 device. The question sounds simple but think about that, sometimes you don’t have the needed COM1 device to test or your computer doesn’t have a COM1 port at all (for example, a laptop).

I think this is the kind of job for a Virtual Machine… Let’s try.

We would need a few things:

The secret is to redirect the COM1 port from the virtual machine to a file (possible in VMWare) or a named pipe, for starters, a named pipe is an IPC mechanism for process intercommunication.

In the virtual machine settings add a new hardware device and then select Serial Port, Add to named pipe, Finish. Write somewhere the name of the named pipe, in my case would be \.\pipe\com_1

com_port_named_pipe

Now, just for this experiment, I will use putty in the virtual machine to connect to COM1 and in my guest to connect to the named pipe, in that way I will send messages from the virtual machine to COM1 (as any other application would do) and I would be able to receive those messages in the guest through putty, and of course I can answer back from the guest.

In your putty host session remember to select “serial” as the connection type and instead of COM1 select the named pipe created by vmware

putty_guest

In the putty guest session everything is normal, just select “serial” and the expected COM* port, in my case would be COM2:

putty_guest_001

Putty attention in vmware notification icons, even when your named pipe is “com_1” could be possible that in your virtual machine that virtual port is COM2 (not COM1 as you would expect).

virtual_session

Now when I type “hello” in my virtual machine putty instance it will echo in the host putty session and the same backwards. Now, the question is simple, how do I emulate correctly my weird device?

Well, a nice and easy way would be to “record” a common communication session between your device and a machine with a COM1 installed, and then “replay” that session back in your virtual environment using putty or a home made program. You know what, I found a program to do exactly that in CodeProject (Application to Debug Serial Port communication and Basic Serial Port Listening Application), go, give a try =)

I hope this would help you someway my friend.

Moq e Indexers

Mi framework favorita de mocking es Moq, es flexible, rápida, intuitiva, (dije flexible ya verdad?). Otro día hablaré más detallado de Moq y sus destrezas. Hoy un compañero de trabajo se topó con algo interesante en un test, el mock de una propiedad indexer. Como recordaran, un indexer no es más que una propiedad de una clase que se comporta como la “llave” de esa propiedad, “a-la” diccionario.

La buena noticia es que Moq me permite fácilmente crear mocks de indexers.

Digamos que tenemos la siguiente clase e interface en cuestión:

interface ISimpleStorage {
    string this[string key] { get; set; }
}

interface ISearchDataFiller {
    void FillData(SearchData data);
}

class ClientIPSearchFiller : ISearchDataFiller {
    public readonly string HttpRemoteHostKey = "HTTP_REMOTE_HOST";
    public readonly string RemoteHostKey = "REMOTE_HOST";
    private readonly ISimpleStorage _storage;

    public ClientIPSearchDataFiller(ISimpleStorage storage) {
        _storage = storage;
    }

    public void FillData(SearchData data) {
          var ip = _storage[HttpRemoteHostKey] ?? string.Empty;
                if (string.IsNullOrEmpty(ip))
                    ip = _storage[RemoteHostKey] ?? string.Empty;
                data.IpAddress = ip;
    }
}

Gracias a Moq nuestro test sería algo tan simple como esto (faltan facts, pero ya ustedes se hicieron a la idea :P )

class ClientIPSearchFillerFacts {
    private readonly ClientIPSearchFiller _filler;
    private readonly Mock<ISimpleStorage> _storage;

    public ClientIPSearchFillerFacts() {
        _storage = new Mock<ISimpleStorage>();
        _filler = new ClientIPSearchFiller(_storage.Object);
    }

    [Fact]
    public void When_http_remote_host_appears_use_it() {
        const string ip = "192.168.1.1";
        var data = new SearchData();
        _storage.SetupGet(x => x[data.HttpRemoteHostKey]).Returns(ip);
        _filler.FillData(data);

        _storage.VerifyAll();
        data.IpAddres.Should().Be.EqualTo(ip);
    }

    [Fact]
    public void When_http_remote_host_is_empty_use_remote_host() {
        const string ip = "192.168.1.1";
        var data = new SearchData();
        _storage.SetupGet(x => x[data.HttpRemoteHostKey]).Returns(string.Empty);
        _storage.SetupGet(x => x[data.RemoteHostKey]).Returns(ip)
        _filler.FillData(data);

        _storage.VerifyAll();
        data.IpAddres.Should().Be.EqualTo(ip);
    }
}

Fácil, rápido, simple.

Hasta la próxima!