Skip to content

Commit

Permalink
Added doxygen code documentation, Updated Markdown README with
Browse files Browse the repository at this point in the history
build information, API data and its use.
gtest has been updated with more test cases for IPv4 and IPv6.
  • Loading branch information
arunkumar-mourougappane committed Nov 14, 2019
1 parent 8abb5d1 commit a15d867
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ APPS = \
build/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities

GTEST_MINI_IP_UTILITIES_OBJS = \
src/app/gtest_mini_ip_utilities/gtest_ip_address.o \
src/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.o
build/app/gtest_mini_ip_utilities/gtest_ip_address.o \
build/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.o


all: gtest tree libs apps
Expand Down
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
# miniIPutils
# miniIPutils <img src="images/ip_address.png" width="30">

Ip Utility tools wrapper read, process and manipulate Ethernet interfaces.
[![Build Status](https://travis-ci.com/arunkumar-mourougappane/miniIPutils.svg?branch=master)](https://travis-ci.com/arunkumar-mourougappane/miniIPutils)

An advanced self managing IP Address Utility tool capable of managing both managing IPv4 and IPv6 Address.

[CIPAddress](https://github.com/arunkumar-mourougappane/miniIPutils/blob/master/src/lib/ip_address/ip_address.cpp "Source Code for CIPAddress API")

CIPAddress wrapper library can process any IP address format from one among the following:

- std::string
- const char*
- struct in_addr_t
- struct in_addr
- struct in6_addr
- struct sockaddr_storage

**APIs:**

- GetIPFamily() returns the IP Address family.
- IsValidIP() returns if IP is valid or not.
- GetString() returns a string for the IP Address.
- GetIPAddressBytes() returns a sockaddr_storage structure.
- GetIPAddress(struct in_addr & byteAddress ) returns 0 on success with valid IPv4 address populated to byte address.
- GetIPAddress(struct in6_addr & byteAddress ) returns 0 on success with valid IPv6 address populated to byte address.
- Compare(const CIPAddress& IpAddress) compare a CIPAddress instance with current insance.
- to_string() provides a string representation of a class.

**Example of Use:**

Here are some examples of the use for CIPAddress class implementation.

```cpp
#include <CIPAddress.h>

CIPAddress obj("127.0.01");
// Checking for validty
if (obj) // Also obj.IsValidIP() can be use to check validity.
{
std::cout << "Valid IP Address";
}
// to print ip Address string or send it to stream
stream_obj << obj;
// to get string value;
std::cout << obj.GetString();
```
Binary file added images/ip_address.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
167 changes: 166 additions & 1 deletion include/ip_address.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/**
* @file ip_address.h
* @author Arunkumar (amouroug@buffalo.edu)
* @brief A C++ IP Address Wrapper to handle both IPv4 as
* well as IPv6 Addresses without the need for
* separate APIs to handle them.
* @version 0.1
* @date 2019-11-13
*
* @copyright Copyright (c) 2019
*
*/
#include <sys/socket.h>
#include <iostream>
#include <arpa/inet.h>
Expand All @@ -7,6 +19,10 @@
#define IP_ADDRESS_H
namespace mini_ip_utilities
{
/**
* @brief CIPAddress wrapper to handle IPv4 and IPv6.
*
*/
class CIPAddress
{
private:
Expand All @@ -15,22 +31,171 @@ namespace mini_ip_utilities
bool mValidIP;
struct sockaddr_storage mIPAddressBytes;
public:
/**
* @brief Construct a new CIPAddress object
*
* @param ipAddress - a std::string of IPv4 or
* IPv6 Address. All instance variables are
* populated for a valid IP.
*/
CIPAddress(std::string ipAddress);

/**
* @brief Construct a new CIPAddress object
*
* @param byteAddress take a reference to byte
* address value and if valid populates
* all instance variables.
*/
CIPAddress(in_addr_t& byteAddress);

/**
* @brief Construct a new CIPAddress object
*
* @param byteAddressData a reference to the
* IPv6 in6_addr and if valid populates
* all the instance variables of the class
* with appropriate information.
*/
CIPAddress(const struct in6_addr & byteAddressData);

/**
* @brief Construct a new CIPAddress object
*
* @param byteAddressData a reference to the IPv4
* byte address is taken and if found valid is
* used to populate all instance variable
* information.
*/
CIPAddress(const struct in_addr & byteAddressData);

/**
* @brief Construct a new CIPAddress object
*
* @param byteAddressData a refernce to sockaddr_storage
* structure, the ip address is detected from the
* structure and is then used to populate all
* instance variable information.
*/
CIPAddress(const struct sockaddr_storage& byteAddressData);

/**
* @brief Construct a new CIPAddress object
*
*/
CIPAddress();


/**
* @brief GetIPFamily() gives the IP Address
* family of the IP Address saved in the
* instance.
*
* @return sa_family_t of values AF_INET,
* AF_INET6 or AF_UNSPEC
*/
sa_family_t GetIPFamily() const;

/**
* @brief IsValidIP() checks if IP from
* instance is valid or not.
*
* @return true if IP is valid
* @return false if IP is invalid
*/
bool IsValidIP() const;

/**
* @brief Get the String the IP Address instance.
*
* @return const std::string
*/
const std::string GetString() const;

/**
* @brief Get Byte Address information in the form of
* a sockaddr_storage structure.
*
* @return const struct sockaddr_storage
*/
const struct sockaddr_storage GetIPAddressBytes() const;

/**
* @brief Gets the byte address of IPv4 address from the
* instance and saves to argument reference.
*
* @param byteAddress a byte address for IPv4 is generated
* and populated to the reference provided as argument.
*
* @return int_least32_t 0 on success with the correct byte
* address and -1 for failure with IN_ADDR_ANY populated
* for reference argument.
*/
int_least32_t GetIPAddress(struct in_addr & byteAddress ) const;

/**
* @brief Gets the byte address of IPv6 address from the
* instance and saves to argument reference.
*
* @param byteAddress a byte address for IPv6 is generated
* and populated to the reference provided as argument.
*
* @return int_least32_t 0 on success with the correct byte
* address and -1 for failure with in6_addr_any populated
* for reference argument.
*/
int_least32_t GetIPAddress(struct in6_addr & byteAddress ) const;

/**
* @brief Compares current CIPAddess instance with another.
*
* @param IpAddress CIPAddress instance that needs to be comapred
* with.
* @return int_least32_t returns 0 on success -1 if they are not
* the same.
*/
int_least32_t Compare(const CIPAddress& IpAddress) const;

/**
* @brief operator overloading for equality.
*
* @param IpAddress to be compared with.
*
* @return true if the CIPAddress instance compared
* is the same.
* @return false if CIPAddress instances are
* different.
*/
bool operator== (const CIPAddress &IpAddress);

/**
* @brief Operator overloading for inequality
*
* @param IpAddress to be compared with.
*
* @return true f the CIPAddress instance compared
* is not the same.
* @return false if the CIPAddress instance compared
* is the same.
*/
bool operator!= (const CIPAddress &IpAddress);

/**
* @brief gives a string representation
*
* @return std::string a string representation of
* the IP Address.
*/
std::string to_string() const;
/**
* @brief Provides a boolean state of the instance
*
* @return true if IP Address is valid
* @return false if IP Address is invalid.
*/
explicit operator bool() const
{
return IsValidIP();
}
};
}
#endif
40 changes: 34 additions & 6 deletions src/app/gtest_mini_ip_utilities/gtest_ip_address.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
#include <gtest_ip_address.h>
#include <ip_address.h>

TEST_F(TestCIPAddress, CheckifValidIPIsAccepted)
TEST_F(TestCIPAddress, CheckifValidIPv4IsAccepted)
{
mini_ip_utilities::CIPAddress ipAddressObj(ipAddressString);
EXPECT_EQ(true, ipAddressObj.IsValidIP()) << "Failed to accept valid IP Addres argument, IsValidIP() returned false.";
EXPECT_EQ(std::string("127.0.0.1"), ipAddressObj.GetString()) << "Failed to accept valid IP Addres argument, ipAddr.GetString() returned empty string.";
EXPECT_EQ(AF_INET, ipAddressObj.GetIPFamily()) << "Failed to accept valid IP Addres argument, ipAddr.GetIPFamily() returned AF_UNSPEC";
}
mini_ip_utilities::CIPAddress ipAddressObj(ipv4AddressString);
ASSERT_EQ(true, ipAddressObj.IsValidIP()) << "Failed to accept valid IP Address argument, IsValidIP() returned false.";
EXPECT_EQ(std::string("127.0.0.1"), ipAddressObj.GetString()) << "Failed to accept valid IPv4 Address argument, ipAddr.GetString() returned empty string.";
EXPECT_EQ(AF_INET, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv4 Address argument, ipAddr.GetIPFamily() returned AF_UNSPEC";
EXPECT_TRUE(ipAddressObj) << "Failed to overload bool operator valid IPv4 Address argument,it returned false.";
}

TEST_F(TestCIPAddress, CheckifInvalidIPv4IsAccepted)
{
mini_ip_utilities::CIPAddress ipAddressObj(invalidIPv4AddressString);
ASSERT_EQ(false, ipAddressObj.IsValidIP()) << "Failed to accept invalid IPv4 Address argument, IsValidIP() returned true.";
EXPECT_EQ(std::string(""), ipAddressObj.GetString()) << "Failed to accept invalid IPv4 Addres argument, ipAddr.GetString() returned a string.";
EXPECT_EQ(AF_UNSPEC, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv4 Address argument, ipAddr.GetIPFamily() returned AF_INET";
EXPECT_FALSE(ipAddressObj) << "Failed to overload bool operator invalid IPv4 Address argument, it returned false.";
}

TEST_F(TestCIPAddress, CheckifValidIPv6IsAccepted)
{
mini_ip_utilities::CIPAddress ipAddressObj(ipv6AddressString);
ASSERT_EQ(true, ipAddressObj.IsValidIP()) << "Failed to accept valid IPv6 Address argument, IsValidIP() returned false.";
EXPECT_EQ(ipv6AddressString, ipAddressObj.GetString()) << "Failed to accept valid IPv6 Address argument, ipAddr.GetString() returned empty string.";
EXPECT_EQ(AF_INET6, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv6 Address argument, ipAddr.GetIPFamily() returned AF_UNSPEC or AF_INET";
EXPECT_TRUE(ipAddressObj) << "Failed to overload bool operator valid IPv6 Address argument,it returned false.";
}

TEST_F(TestCIPAddress, CheckifInvalidIPv6IsAccepted)
{
mini_ip_utilities::CIPAddress ipAddressObj(invalidIPv6AddressString);
ASSERT_EQ(false, ipAddressObj.IsValidIP()) << "Failed to accept invalid IPv6 Address argument, IsValidIP() returned true.";
EXPECT_EQ(std::string(""), ipAddressObj.GetString()) << "Failed to accept invalid IPv6 Address argument, ipAddr.GetString() returned a string.";
EXPECT_EQ(AF_UNSPEC, ipAddressObj.GetIPFamily()) << "Failed to accept valid IPv6 Address argument, ipAddr.GetIPFamily() returned AF_INET6 or AF_INET";
EXPECT_FALSE(ipAddressObj) << "Failed to overload bool operator invalid IPv6 Address argument, it returned false.";
}
8 changes: 7 additions & 1 deletion src/app/gtest_mini_ip_utilities/gtest_mini_ip_utilities.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@

#include <gtest/gtest.h>

/**
* @brief Main function for getests
*
* @param argc
* @param argv
* @return int returns 0 on success.
*/
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);

Expand Down
14 changes: 10 additions & 4 deletions src/app/include_app/gtest_ip_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
#include <cstring>
#include <gtest/gtest.h>


/**
* @brief TestCIPAddress to test for instance variables.
*
*/
class TestCIPAddress : public :: testing :: Test
{
public:
std::string ipAddressString, invalidIPAddressString;
std::string ipv4AddressString, invalidIPv4AddressString;
std::string ipv6AddressString, invalidIPv6AddressString;
virtual void SetUp()
{
ipAddressString = "127.0.0.1";
invalidIPAddressString = "123456A";
ipv4AddressString = "127.0.0.1";
invalidIPv4AddressString = "123456A";
ipv6AddressString="a060:9006:b0a2:5200:2a1c:d979:4276:836f";
invalidIPv6AddressString="a060:9006:b0a2:ghdiss:2a1c:d#979:42276:836f";
}

virtual void TearDown()
Expand Down
Loading

0 comments on commit a15d867

Please sign in to comment.