Skip to content

Commit d318b39

Browse files
committed
Added functions isIPv4 and isIPv6
1 parent 65648d6 commit d318b39

File tree

9 files changed

+173
-6
lines changed

9 files changed

+173
-6
lines changed

src/unit_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ std::string UnitTestBase::name() {
1717
void UnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint,
1818
const std::string &sValue, const std::string &sExpected) {
1919
if (sValue != sExpected) {
20-
WSJCppLog::err(TAG, sPoint + ", expeceted '" + sExpected + "', but got '" + sValue + "'");
20+
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + sExpected + "', but got '" + sValue + "'");
2121
bTestSuccess = false;
2222
}
2323
}
@@ -26,7 +26,7 @@ void UnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint,
2626

2727
bool UnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected) {
2828
if (nValue != nExpected) {
29-
WSJCppLog::err(TAG, " {" + sPoint + "} Expeceted '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
29+
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
3030
bTestSuccess = false;
3131
return false;
3232
}
@@ -37,7 +37,7 @@ bool UnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int n
3737

3838
bool UnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected) {
3939
if (nValue != nExpected) {
40-
WSJCppLog::err(TAG, " {" + sPoint + "} Expeceted '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
40+
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
4141
bTestSuccess = false;
4242
return false;
4343
}
@@ -48,7 +48,7 @@ bool UnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, doubl
4848

4949
void UnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected) {
5050
if (bValue != bExpected) {
51-
WSJCppLog::err(TAG, sPoint + ", expeceted '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'");
51+
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'");
5252
bTestSuccess = false;
5353
}
5454
}

src/wsjcpp_core.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include <cstdint>
1717
#include <unistd.h>
1818
#include <streambuf>
19+
#include <sys/types.h>
20+
#include <sys/socket.h>
21+
#include <arpa/inet.h>
1922

2023
// ---------------------------------------------------------------------
2124

@@ -408,6 +411,44 @@ std::string WSJCppCore::createUuid() {
408411
return sRet;
409412
}
410413

414+
// ---------------------------------------------------------------------
415+
416+
bool WSJCppCore::isIPv4(std::string& str) {
417+
int n = 0;
418+
std::string s[4] = {"", "", "", ""};
419+
for (int i = 0; i < str.length(); i++) {
420+
char c = str[i];
421+
if (n > 3) {
422+
return false;
423+
}
424+
if (c >= '0' && c <= '9') {
425+
s[n] += c;
426+
} else if (c == '.') {
427+
n++;
428+
} else {
429+
return false;
430+
}
431+
}
432+
for (int i = 0; i < 4; i++) {
433+
if (s[i].length() > 3) {
434+
return false;
435+
}
436+
int p = std::stoi(s[i]);
437+
if (p > 255 || p < 0) {
438+
return false;
439+
}
440+
}
441+
return true;
442+
}
443+
444+
// ---------------------------------------------------------------------
445+
446+
bool WSJCppCore::isIPv6(std::string& str) {
447+
unsigned char buf[sizeof(struct in6_addr)];
448+
bool isValid = inet_pton(AF_INET6, str.c_str(), buf);
449+
return isValid;
450+
}
451+
411452
// ---------------------------------------------------------------------
412453
// WSJCppLog
413454

src/wsjcpp_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class WSJCppCore {
4848

4949
static void initRandom();
5050
static std::string createUuid();
51+
static bool isIPv4(std::string& str);
52+
static bool isIPv6(std::string& str);
5153
};
5254

5355

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ list (APPEND WSJCPP_SOURCES "./src/unit_test_core_normalize_path.h")
2929
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_normalize_path.cpp")
3030
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_uuid.h")
3131
list (APPEND WSJCPP_SOURCES "./src/unit_test_core_uuid.cpp")
32-
32+
list (APPEND WSJCPP_SOURCES "./src/unit_test_ip_v4.h")
33+
list (APPEND WSJCPP_SOURCES "./src/unit_test_ip_v4.cpp")
34+
list (APPEND WSJCPP_SOURCES "./src/unit_test_ip_v6.h")
35+
list (APPEND WSJCPP_SOURCES "./src/unit_test_ip_v6.cpp")
3336

3437
include_directories(${WSJCPP_INCLUDE_DIRS})
3538

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "unit_test_ip_v4.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <wsjcpp_core.h>
5+
6+
REGISTRY_UNIT_TEST(UnitTestIPv4)
7+
8+
9+
UnitTestIPv4::UnitTestIPv4()
10+
: UnitTestBase("UnitTestIPv4") {
11+
//
12+
}
13+
14+
void UnitTestIPv4::init() {
15+
// nothing
16+
}
17+
18+
bool UnitTestIPv4::run() {
19+
struct TestIPv4 {
20+
TestIPv4(const std::string &sIPv4, bool bYes) :
21+
m_sIPv4(sIPv4), m_bYes(bYes) {
22+
23+
};
24+
std::string m_sIPv4;
25+
bool m_bYes;
26+
27+
};
28+
std::vector<TestIPv4> vIPv4s;
29+
vIPv4s.push_back(TestIPv4("127.0.0.1", true));
30+
vIPv4s.push_back(TestIPv4("10.0.0.1", true));
31+
vIPv4s.push_back(TestIPv4("0.0.0.0", true));
32+
vIPv4s.push_back(TestIPv4("300.200.100.100", false));
33+
vIPv4s.push_back(TestIPv4("aaaa", false));
34+
vIPv4s.push_back(TestIPv4("100.0.0.x", false));
35+
vIPv4s.push_back(TestIPv4("10033.1289.0.0", false));
36+
vIPv4s.push_back(TestIPv4("10033.1289.0.0.0.0.0", false));
37+
38+
bool bTestSuccess = true;
39+
for (int i = 0; i < vIPv4s.size(); i++) {
40+
TestIPv4 t = vIPv4s[i];
41+
compareB(bTestSuccess, "Test[" + t.m_sIPv4 + "]", WSJCppCore::isIPv4(t.m_sIPv4), t.m_bYes);
42+
}
43+
return bTestSuccess;
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_IP_V4_H
2+
#define UNIT_TEST_IP_V4_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestIPv4 : public UnitTestBase {
7+
public:
8+
UnitTestIPv4();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_IP_V4_H
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "unit_test_ip_v6.h"
2+
#include <vector>
3+
#include <iostream>
4+
#include <wsjcpp_core.h>
5+
6+
REGISTRY_UNIT_TEST(UnitTestIPv6)
7+
8+
9+
UnitTestIPv6::UnitTestIPv6()
10+
: UnitTestBase("UnitTestIPv6") {
11+
//
12+
}
13+
14+
void UnitTestIPv6::init() {
15+
// nothing
16+
}
17+
18+
bool UnitTestIPv6::run() {
19+
struct TestIPv6 {
20+
TestIPv6(const std::string &sIPv6, bool bYes) :
21+
m_sIPv6(sIPv6), m_bYes(bYes) {
22+
23+
};
24+
std::string m_sIPv6;
25+
bool m_bYes;
26+
27+
};
28+
std::vector<TestIPv6> vIPv6s;
29+
vIPv6s.push_back(TestIPv6("3FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", true));
30+
vIPv6s.push_back(TestIPv6("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", true));
31+
vIPv6s.push_back(TestIPv6("2607:f8b0:4002:c08::8b", true));
32+
vIPv6s.push_back(TestIPv6("2607:f8b0:4002:c08:0:0:0:8b", true));
33+
vIPv6s.push_back(TestIPv6("2000::", true));
34+
vIPv6s.push_back(TestIPv6("::1", true));
35+
vIPv6s.push_back(TestIPv6("aaaa", false));
36+
vIPv6s.push_back(TestIPv6("100.0.0.1", false));
37+
vIPv6s.push_back(TestIPv6("100.0.0.x", false));
38+
vIPv6s.push_back(TestIPv6("10033.1289.0.0", false));
39+
vIPv6s.push_back(TestIPv6("10033.1289.0.0.0.0.0", false));
40+
41+
bool bTestSuccess = true;
42+
for (int i = 0; i < vIPv6s.size(); i++) {
43+
TestIPv6 t = vIPv6s[i];
44+
compareB(bTestSuccess, "Test[" + t.m_sIPv6 + "]", WSJCppCore::isIPv6(t.m_sIPv6), t.m_bYes);
45+
}
46+
return bTestSuccess;
47+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef UNIT_TEST_IP_V6_H
2+
#define UNIT_TEST_IP_V6_H
3+
4+
#include <unit_tests.h>
5+
6+
class UnitTestIPv6 : public UnitTestBase {
7+
public:
8+
UnitTestIPv6();
9+
virtual void init();
10+
virtual bool run();
11+
};
12+
13+
#endif // UNIT_TEST_IP_V6_H

wsjcpp.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cmake_cxx_standard: 11
33
cmake_minimum_required: 3.0
44

55
name: wsjcpp/wsjcpp-core
6-
version: v0.0.3
6+
version: v0.0.4
77
description: Basic Utils for wsjcpp
88
issues: https://github.com/wsjcpp/wsjcpp-core/issues
99
repositories:
@@ -50,3 +50,7 @@ unit-tests:
5050
description: Check test generate uuid function
5151
- name: CoreExtractFilename
5252
description: Check function extract filenane from path
53+
- name: IPv4
54+
description: Check function isIPv4
55+
- name: IPv6
56+
description: Check function isIPv6

0 commit comments

Comments
 (0)