Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generating ipv6 #80

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
- [x] httpMethod
- [x] httpStatusCode
- [x] ipv4
- [ ] ipv6
- [x] ipv6
- [x] mac
- [ ] port
- [x] port
- [x] protocol
- [x] url
- [x] domainName
Expand Down
22 changes: 22 additions & 0 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ class Internet
*/
static std::string ipv4(const IPv4Address& baseIpv4Address, const IPv4Address& generationMask);

/**
* @brief Returns a string containing randomized ipv6 address.
*
* @return String representation of the ipv6 address.
*
* @code
* Internet::ipv6() // "269f:1230:73e3:318d:842b:daab:326d:897b"
* @endcode
*/
static std::string ipv6();

/**
* @brief Returns a generated random mac address.
*
Expand All @@ -218,6 +229,17 @@ class Internet
*/
static std::string mac(const std::string& sep = ":");

/**
* @brief Generates a random port.
*
* @return Port.
*
* @code
* Internet::port() // 5432
* @endcode
*/
static unsigned port();

/**
* @brief Generates a random url.
*
Expand Down
19 changes: 19 additions & 0 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "data/Emojis.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Person.h"
#include "faker-cxx/String.h"
#include "faker-cxx/Word.h"

namespace faker
Expand Down Expand Up @@ -210,6 +211,18 @@ std::string Internet::ipv4(const IPv4Address& baseIpv4Address, const IPv4Address
return std::format("{}.{}.{}.{}", sectors[0], sectors[1], sectors[2], sectors[3]);
}

std::string Internet::ipv6()
{
std::vector<std::string> ipv6Parts;

for (int i = 0; i < 8; i++)
{
ipv6Parts.push_back(String::hexadecimal(4, HexCasing::Lower, HexPrefix::None));
}

return StringHelper::join(ipv6Parts, ":");
}

std::string Internet::mac(const std::string& sep)
{
std::string mac;
Expand All @@ -232,6 +245,11 @@ std::string Internet::mac(const std::string& sep)
return mac;
}

unsigned Internet::port()
{
return Number::integer(65535u);
}

std::string Internet::url(WebProtocol webProtocol)
{
const auto protocol = webProtocol == WebProtocol::Https ? "https" : "http";
Expand All @@ -253,4 +271,5 @@ std::string Internet::domainSuffix()
{
return Helper::arrayElement<std::string>(domainSuffixes);
}

}
27 changes: 27 additions & 0 deletions src/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "../person/data/FirstNamesFemales.h"
#include "../person/data/FirstNamesMales.h"
#include "../person/data/LastNames.h"
#include "../string/data/Characters.h"
#include "../word/data/Adjectives.h"
#include "../word/data/Nouns.h"
#include "data/DomainSuffixes.h"
Expand Down Expand Up @@ -643,6 +644,24 @@ TEST_F(InternetTest, shouldGenerateIpv4KeepingTheMaskedPart)
ASSERT_TRUE((generatedAddress[1] & generationMask[1]) == expectedSecondSectorMaskedValue);
}

TEST_F(InternetTest, shouldGenerateIpv6)
{
const auto generatedIpv6 = Internet::ipv6();

const auto generatedIpv6Parts = StringHelper::split(generatedIpv6, ":");

ASSERT_EQ(generatedIpv6Parts.size(), 8);

ASSERT_TRUE(std::all_of(generatedIpv6Parts.begin(), generatedIpv6Parts.end(),
[](const std::string& generatedIpv6Part)
{
return std::all_of(
generatedIpv6Part.begin(), generatedIpv6Part.end(),
[](char hexCharacter)
{ return hexLowerCharacters.find(hexCharacter) != std::string::npos; });
}));
}

TEST_F(InternetTest, MacDefaultSeparator)
{
const auto mac = Internet::mac();
Expand Down Expand Up @@ -732,3 +751,11 @@ TEST_F(InternetTest, shouldGenerateHttpUrl)
{ return generatedDomainSuffix == domainSuffix; }));
ASSERT_EQ(generatedProtocol, "http");
}

TEST_F(InternetTest, shouldGeneratePort)
{
const auto generatedPort = Internet::port();

ASSERT_GE(generatedPort, 0);
ASSERT_LE(generatedPort, 65535);
}