Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4559994
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:52:05+00:00 2026-05-21T17:52:05+00:00

I’m writing a C++ class to wrap sockets (I know that there are good

  • 0

I’m writing a C++ class to wrap sockets (I know that there are good libraries for this–I’m rolling my own for practise):

class Socket {
public:
  int init(void); // calls socket(2)
  // other stuff we don't care about for the sake of this code sample
};

This class is in turn used by several others, which I know I can unit test with googlemock by subclassing and mocking.

But I would like to develop this class test first, and am a bit stuck currently. I can’t use googlemock on the C standard library (i.e. socket.h, in this case), since a C++ class it is not. I could create a thin C++ wrapper class around the C standard library functions I need, e.g.

class LibcWrapper {
public:
   static int socket(int domain, int type, int protocol);
   static int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
   static int listen(int sockfd, int backlog);
   static int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
   static ssize_t write(int fd, const void *buf, size_t count);
   static int close(int fd);
};

Now I can mock that and unit test my Socket class (which might now need to be renamed Network or some such). LibcWrapper could come in handy for other classes as well, and would not itself need to be unit tested since it just provides a bunch of class methods.

This is starting to sound good to me. Have I answered my own question, or do there exist standard patterns for test driving this sort of development in C++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:52:05+00:00Added an answer on May 21, 2026 at 5:52 pm

    I would probably mock it by working through a socket interface (ie base class) and implementing a test version of that base class.

    You can do this a couple of ways, for example the simplest thing is to specify the entire socket API in terms of a C++ interface.

       class ISocket
       {
       public:
            virtual int socket(int domain, int type, int protocol) = 0;
            virtual int bind(int sockfd...) = 0;
            // listen, accept, write, etc            
       };
    

    Then provide a concrete implementation that worked through the BSD sockets library

       class CBsdSocketLib : public ISocket
       {
       public:
           // yadda, same stuff but actually call the BSD socket interface
       };
    
    
       class CTestSocketLib : public ISocket
       {
       public:
           // simulate the socket library
       };
    

    By coding against the interface you can create your test version to do whatever you like.

    However, we can clearly see that this first pass is rather weird. We’re wrapping an entire library, its not really a class in the sense that it describes objects.

    You’d rather think in terms of sockets and ways to manufacture sockets. This would be more object oriented. Along those lines I’d separate the functionality above into two classes.

       // responsible for socket creation/construction
       class ISocketFactory
       {
            virtual ISocket* createSocket(...) = 0; // perform socket() and maybe bind()
       };
    
       // a socket
       class ISocket
       {
             // pure virtual recv, send, listen, close, etc
       };
    

    For live use:

       class CBsdSocketFactory : public ISocketFactory
       {
          ...
       };
    
       class CBsdSocket : public ISocket
       { 
          ...
       };
    

    For testing:

       class CTestSocketFactory : public ISocketFactory
       {
       };
    
       class CTestSocket : public ISocket
       {
       };
    

    And separate the BSD library calls into those two distinct classes that have their own responsibilities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.