As the headline says, how would you test a client/server application, that is written in C/C++, that talks through a protocol over a network? Im a bit confused on how to do this. I have thought about making some mocking, but I have never tried mocking, so I dont know if this is the best way.
How should I do this? I have written many unit tests, but never tried to test something that interact over a network.
I use the command pattern in the unit test driver (client) to send test commands to the server. The advantage of this is that the test is coded in one place.
Example for testing a request timeout:
Client sends a sleep command to server and then the request. The request times out and the test case is passed.
Typically you'll want to use mocking to verify that each side reacts as required to messages from the other side (e.g., that when the client receives a response from the server that it processes that response correctly).
To test the network functionality itself, you can test both running on the same machine, and you can run one (or both) inside a virtual machine. If you have two network adapters, you can even dedicate each to a virtual machine so the network traffic actually goes out one, to a switch/router, and comes back in the other (particularly useful when/if you want to capture and verify packets).
I have some client/server code that I unit test through the loopback address. I use some mocks when I have to test error conditions. So, I test with the real code when I can and the mocks when I need to trigger very specific conditions.
Related
This is surely a weird one ... I'm doing some extreme integration style testing on a custom Java HTTP client for a backend service I'm working with. For reasons which don't matter here, the client has some specific quirks and a custom solution was the only real option.
For automated testing, I've built a "fake" version of the backend service by spinning up a Jetty server locally and having it behave in different ways e.g. return 500, wait e.g. 4 seconds before giving a response to simulate latency etc and firing off a battery of tests against it with the client on build time.
Given the nature of this client, there is an usual and specific scenario which I need to test and I'm trying to find a way to make my Jetty serve behave in the correct fashion. Basically, when returning HTTP response, I need to immediately return the HTTP Headers and the first few bytes of the HTTP body and then sleep. The goal is to trigger a socket timeout in the client specifically when reading the HTTP body.
Anyone know where in Jetty I could plug something in to force this behaviour? Was looking at the Connector interface but not so sure thats the right place.
Thanks for any suggestions.
Write a few bytes to the HttpServletResponse.getOutputStream(), then call HttpServletResponse.flushBuffer() to immediately commit the response.
Bonus tip: use HttpServletResponse.sendError(-1) to terminate the connection abruptly.
I have an app that connects to an API endpoint and displays a list of people. I want to write a unit test for the app. But I am not sure what to test here. There is no arithmetic operation happening It's just fetching the data from API and displaying it.
What should the unit test cover in such a scenario?
If a test can never fail, then it's not really testing anything. In your case though, a network api call is being made. And network calls can fail all the time. Depending on how you are making your network call you can either:
create a fake web server that can return a variety of error codes
create a mock api service that can return a variety of error codes
don't test anything
There are all kinds of tests you can use, behavioural, unit, functional, integration, black box, user acceptance testing.
What does testing do for you? Does it document code behaviour? Does it lock in the behavior of a function? Does it ensure that something works?
Depending on your needs, you may not need a test. Or, you may need a lot more. It's up to you.
Unit tests are designed to ensure that a behavior or set of behaviors occur(s) when you invoke a unit of code.
In this case, you have code that is fetching the data from an API and returning it. You might want to test the following:
Your code makes a network call to the API.
When the API returns a successful response, your app renders the data.
When the API returns a failed response, you gracefully handle the failure.
Of course these steps will probably vary depending on your use case. You can look into stubbing the API to understand how you can simulate API invocation failures.
I've developed a simple xmpp chat client (for Android, using asmack library). Now, I would like to test the client to see if it does what it is supposed to do (ie. fetch the list of contacts, refresh contact list, receive messages). Using smack library, I assume it is pretty much safe, but still...
How could I check if my fetched list of contacts is the one returned by the server? How to check if the presence status of certain contact is the correct one?
Regarding the usage of unit tests, I was thinking of mocking the server side and test the client side, but that doesn't seem of much use because I would like to test it with real server data.
Is there some automated tool for this? Or would it be enough just to distribute the application to my friends and tell them to use it for a while and report any misbehaviors?
You'll just have to trust aSmack. You could use logcat to investigate the XMPP stanzas returned by the server "by hand" and compare them to your client's behavior. You could also increase the verbosity on your server's log (if you have access) and compare that way. However, doing automated testing would require some sort of XMPP parser - but that's exactly what aSmack is. I'm sure the aSmack developers have already tested it thoroughly enough using their own methods.
I'm looking for a simple POP3 and/or IMAP server for unit testing my application.
Requirements are:
no root privileges required to make it fully functional,
may store it's data in whichever directory I choose,
compliant to RFCs,
possibility to add e-mails by hand.
I've tried Dovecot, but it seems too complicated and running it without special system account is fairly impossible.
I know Mozilla should have one for Thunderbird testing, but only one I have found was for newsgroups.
Why not use (or create) a mock server and use that to test the functionality? This will return the correct responses to the various commands so you can be sure that your code will work correctly when you connect it to a real server.
That way you're not reliant on a 3rd party service for this aspect of your testing.
http://quintanasoft.com/dumbster/
http://www.icegreen.com/greenmail/
And probably many more. You start them in Your test so You don't have to create any system accounts.
I'd like to create various network errors during testing. I'm using the Berkely sockets API directly in C++ on Linux. I'm running a mock server in another thread from within Boost.Test which listens on localhost.
For instance, I'd like to create a timeout during connect. So far I've tried not calling accept in my mock server and setting the backlog to 1, then making multiple connections, but all seem to successfully connect. I would think that if there wasn't room in the backlog queue I would at least get a connection refused error if not a timeout.
I'd like to do this all programatically if possible, but I'd consider using something external like IPchains to intentionally drop certain packets to certain ports during testing, but I'd need to automate creating and removing rules so I could do it from within my Boost.Test unit tests.
I suppose I could mock the various system calls involved, but I'd rather go through a real TCP stack if possible.
Ideas?
When I did some intensive protocol testing recently I used the click modular router. The advantage is it's quite powerful and relatively easy accessible. If you install click as a kernel module on a linux machine, you can easily reach network elements parameters for both setting and reading them. So you can for example change the loss rate of a drop element from 0 to 100%. While it is a little bit more difficult to get started with, you can simulate quite complex things with it. I personally used it (for example) this way to simulate varying bandwidth and packet loss circumstances to test an RTP video stream.
There was another question similar to this. My recommendation would be to use a network traffic generator like the IXIA. It will allow you to do many possible combinations of protocol testing in a repeatable way.