How to set up a mock server with GoogleMock - c++

I am converting old C code to C++, and I am testing it as I rewrite with GoogleTest. Part of my code uses sockets to connect to a remote host, with an IPV4 address. I am having a lot of trouble understanding how I can use GoogleTest and GoogleMock to test the socket part of my code.
My object takes in a host IP address and number of seconds to wait before trying to connect to a host again. There is a function called "conn()" that actually tries to connect to the host. That is the function I want to test. So in my test, I want to do something like:
TEST(CONNECTION_TEST, Connection)
{
MyObject obj("###.###.###.###", 1);
ASSERT_TRUE(obj.conn());
}
I read on here that I needed to mock up a server since testing a remote host isn't really advisable. I am brand new to GoogleMock, so I read the official documentation and read the GoogleTest primer, GoogleMock For Dummies, the GoogleMock Cheat Sheet, and the GoogleMock Cookbook. I watched CodesBay's tutorial. I looked at several posts on SO, including What is wrong with my attempts to mock a simple C++ method with googlemock?, Patterns for unit testing a C++ method that makes a standard library call, and How to unit test BSD sockets.
For my mock server, I did the following. I made a Socket class that wraps socket.h, and then I extended MockSocket from it. In my TEST, I set up the mock socket to accept connections, like so:
TEST(CONNECTION_TEST, Connection)
{
// bind to local host?
MyObject obj("0.0.0.0", 1);
// create the host
MockSocket mock_sock;
MockSocket new_sock;
EXPECT_CALL(mock_sock, create()).Times(1);
EXPECT_CALL(mock_sock, bind(_));
EXPECT_CALL(mock_sock, listen());
EXPECT_CALL(mock_sock, accept(_));
mock_sock.create();
mock_sock.bind(123);
mock_sock.listen();
mock_sock.accept(new_sock);
ASSERT_TRUE(obj.conn());
}
As you can tell, I don't know what I am doing. My test results fail and I think it is because I just didn't do this correctly. Please, can someone guide me in the right direction? Is the mock server the correct way to test my object?

What do you want to test?
Do you want to test if the TCP/IP socket class (assuming there is one), can actually connect and communicate with some TCP/IP service? This is more an end-to-end test. You can use Google Test to do this, but you need a running TCP/IP service to communicate with. This is not where the Google mocking framework is for. This is similar to when you want to test a class that reads a file: you need to have a file that it can read and Google mocking framework will not help you here.
Do you want to test if some other class, that uses your TCP/IP socket class, works? Here you can use Google mocking framework to create a mock for your socket class. This mock will act as the socket class to your other class but will not do any TCP/IP communication. This way you can unit-test your other class.

Related

Testing client/server in gTest

I'm trying to write tests for my chat program based on client/server interaction. After reading some sources I came up with idea of using Google Mock but the whole concept seems a little bit abstract to me. Let's say I want to test client. In order to do that, should i write a server mock that imitates the work of an actual server or I got something wrong?

Simulate traffic over virtual com port

I'm having a project with Arduino(s) and I would like to build a testing environment so I will be able to do end-to-end integration before testing on the real stuff.
I have a C# program that controls the actions sent to an Arduino, using serial port. The Arduino do some calculations and send back the results back to the C# controller.
I do not want to change the C# program. I do have to implement a program that runs the arudino code and transmit it to a virtual com port I've already created, so the C# program could read it.
Can you please give me some guidance how to write to the virtual com port ?
The Arduino is written in C++, so I guess the solution should be implemented in C++ also.
I noticed to the existance of the tcomport, but it's written in Delphi, so I can't see how I can use it with the C++ code.
Thanks
This question will be closed pretty quickly, since you're looking for a tool or library, which is technically off-topic.
That being said, you might want to check out com0com. This creates a couple fake comport that are "connected together". You would have your program use one, then write another program that simulates the Arduino connected to the other port.
Another solution is to have an interface abstraction between your code and the serial port code. You can then implement that interface to pretend to accept data written to the fake "com port" and produce whatever data you wish in response. This way, you can have the "loopback" and "simulator" contained within your own code.

What is Proxy that is created at the client side

This is the Documentation from Oracle Docs. I want to clarify certain jargon based questions.
On the server side, the developer specifies the web service operations by defining methods in an interface written in the Java programming language. The developer also codes one or more classes that implement those methods. Client programs are also easy to code. A client creates a proxy (a local object representing the service) and then simply invokes methods on the proxy.
In The first Bold lettered sentence,
Are these classes, the Implementation classes of the Web service ?
Second Bold lettered,
will the client create the object of those service implementation classes? If yes, how come? Will JAX WS transport the complete service implementation class code which is # server to the client?
I am very new to the concept of web services. if my doubt is silly please bear with me. Thanks!
First point. Yes you code the implementation of the web service. However this is just limited to the business logic you wish to execute you don't have to go code the low level boiler plate code like creating a HTTP socket etc.
A simple class that is exposed as a web service will look like this:
#Webservice()
public class CalculatorWS()
{
#WebMethod(#operationame="add")
public int add(#WebParam(name="i") int i, WebParam(name="j") int j)
{
//this is where you code your implementation
return i+ j;
}
}
A client proxy class does NOT transfer the implementation across the wire. It just creates a proxy that you can use to call the implementation.
You can learn all about it step by step by following this tutorial. It is easy one to understand and follow and will answer all of your questions.

Faking SerialPort Communication

I am looking to mock a Serial Port so I can test communication and OS flushing.
Things I am hoping for out of the mock serial port. (in order of importance to the project)
Pass istty
Able to be used with an automated test suite (no gui)
Language Agnostic
Able to run on TravisCI
Cross Platform
I don't know if all those options are possible. But doing so would be nice. I know Unix treats everything as a file, so if I could just create a file that passes istty, that would be a good solution, but probably not windows compatible.
Anyway any ideas for testing SerialPort communication would be amazing.
Thanks Everyone.
I believe if you use CMock you can pass a .h file and get a mock generated.
Failing that, I'd write one by hand. Nothing fancy, just the stubs and some basic known return values. Once the mock (and your tests/unit under test) is sufficiently developed, that's when I'd switch to an integration test with the real socket API

TCPStream Class for multithreaded TCP server

I'm currently working on transitioning a small console application to a TCP server / client application. The client will connect to the server via any Telnet client, and the server will replicate the standard console interface for each Telnet connection.
I started looking into doing this using the techniques I've learned from Beej's guide to network programming -- accepting the connection and then using fork() to separate it into its own process.
However, I would prefer to maintain my use of streaming IO (the original console application uses cin / cout, using similar functions for the networking logic would make the conversion process much simpler).
I've discovered the TCPStream class, hiding within sockets.h (http://www.gnutelephony.org/doxy/bayonne2/a00215.html)
It appears this class will allow me to use the server with streaming IO. However, I can't find a single example of using this class, nor can I find an explanation as to how to use fork() with it.
Any ideas? Thanks in advance for any help.
I think you are confusing the trees for the forest. One socket class is such a small part of what you need to do overall that it is not worth focusing on that.
If your objective is just to get your project working then just use an existing framework rather than trying to pull individual classes out of a large project. POCO has a TCPServer class that will do 90% of the work for you. QT, ACE and others have similar classes. There is not a huge amount of documentation on POCO but they do cover TCPServer pretty well and you can learn a lot from reading the source code if that is really where your interest lies.