Skip to content

Commit ed05676

Browse files
committed
Fixed #1 added locally check for reserved IPv4 Ranges
1 parent 4ed4044 commit ed05676

24 files changed

+1705
-111
lines changed

src.wsjcpp/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ set (WSJCPP_LIBRARIES "")
1414
set (WSJCPP_INCLUDE_DIRS "")
1515
set (WSJCPP_SOURCES "")
1616

17-
# wsjcpp-core:v0.0.4
17+
find_package(Threads REQUIRED)
18+
list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
19+
20+
# wsjcpp-core:v0.0.5
1821
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_core/")
1922
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp")
2023
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h")
@@ -23,6 +26,11 @@ list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h")
2326
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/nlohmann_json/")
2427
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/nlohmann_json/json.hpp")
2528

29+
# wsjcpp-validators:v0.0.3
30+
list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_validators/")
31+
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_validators/wsjcpp_validators.h")
32+
list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_validators/wsjcpp_validators.cpp")
33+
2634
# required-libraries
2735
list (APPEND WSJCPP_LIBRARIES "-lpthread")
2836

src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml

Lines changed: 17 additions & 17 deletions
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-core
6-
version: v0.0.4
6+
version: v0.0.5
77
description: Basic Utils for wsjcpp
88
issues: https://github.com/wsjcpp/wsjcpp-core/issues
99
repositories:
@@ -23,24 +23,24 @@ required-libraries:
2323
distribution:
2424
- source-file: src/wsjcpp_core.cpp
2525
target-file: wsjcpp_core.cpp
26-
sha1: "04a9e3c6db3c7190c6589514a4b5957dbb4c366d"
26+
sha1: "d892bfee196af88dcada57b39bc6bd65ce2ce075"
2727
type: "source-code"
2828
- source-file: src/wsjcpp_core.h
2929
target-file: wsjcpp_core.h
30-
sha1: "1d76447c6880f43f9afeca89111ec64d3574ccb7"
31-
type: "source-code"
32-
- source-file: "src/unit_tests.cpp"
33-
target-file: "unit_tests.cpp"
30+
sha1: "8f5e7b7ada06814ed123a08e49acffe2e12d398a"
31+
type: "source-code" # todo must be header-file
32+
- source-file: "src/wsjcpp_unit_tests.cpp"
33+
target-file: "wsjcpp_unit_tests.cpp"
3434
type: "unit-tests"
35-
sha1: "a36d71646ddd6041346909c0481a59299f01f898"
36-
- source-file: "src/unit_tests.h"
37-
target-file: "unit_tests.h"
35+
sha1: "4208e039ec2923636655b3ada79ec223cca7bae2"
36+
- source-file: "src/wsjcpp_unit_tests.h"
37+
target-file: "wsjcpp_unit_tests.h"
3838
type: "unit-tests"
39-
sha1: "a20248c9801c53519ad847aa19d80460289a5dd1"
40-
- source-file: "src/unit_tests_main.cpp"
41-
target-file: "unit_tests_main.cpp"
39+
sha1: "8d2ec886f23161a639bb2419bb5e4af48278f18b"
40+
- source-file: "src/wsjcpp_unit_tests_main.cpp"
41+
target-file: "wsjcpp_unit_tests_main.cpp"
4242
type: "unit-tests"
43-
sha1: "81a2b3d7328575c0087fa43000b05a96e278da07"
43+
sha1: "2c02fb58f51687eeac2076096b7df698cc246c9d"
4444

4545
unit-tests:
4646
cases:
@@ -50,7 +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
53+
- name: "ToUpper"
54+
description: "String to upper"
55+
- name: "CreateUuid"
56+
description: "Test generation uuids"

src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,14 @@ std::string& WSJCppCore::to_lower(std::string& str) {
389389
return str;
390390
}
391391

392+
// ---------------------------------------------------------------------
393+
// will worked only with latin
394+
395+
std::string WSJCppCore::toUpper(const std::string& str) {
396+
std::string sRet = str;
397+
std::transform(sRet.begin(), sRet.end(), sRet.begin(), ::toupper);
398+
return sRet;
399+
}
392400

393401
// ---------------------------------------------------------------------
394402

@@ -413,40 +421,30 @@ std::string WSJCppCore::createUuid() {
413421

414422
// ---------------------------------------------------------------------
415423

416-
bool WSJCppCore::isIPv4(const 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;
424+
unsigned long WSJCppCore::convertVoidToULong(void *p) {
425+
unsigned long ret = *(unsigned long *)p;
426+
return ret;
427+
}
428+
429+
// ---------------------------------------------------------------------
430+
431+
std::string WSJCppCore::getPointerAsHex(void *p) {
432+
std::uintptr_t i = reinterpret_cast<std::uintptr_t>(p);
433+
std::stringstream stream;
434+
stream << std::hex << i;
435+
return "0x" + std::string(stream.str());
442436
}
443437

444438
// ---------------------------------------------------------------------
445439

446-
bool WSJCppCore::isIPv6(const 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;
440+
std::string WSJCppCore::extractURLProtocol(const std::string& sValue) {
441+
std::string sRet = "";
442+
int nPosProtocol = sValue.find("://");
443+
if (nPosProtocol == std::string::npos) {
444+
return sRet;
445+
}
446+
sRet = sValue.substr(0, nPosProtocol);
447+
return sRet;
450448
}
451449

452450
// ---------------------------------------------------------------------
@@ -511,6 +509,18 @@ void WSJCppLog::ok(const std::string &sTag, const std::string &sMessage) {
511509

512510
// ---------------------------------------------------------------------
513511

512+
std::vector<std::string> WSJCppLog::getLastLogMessages() {
513+
WSJCppLog::initGlobalVariables();
514+
std::lock_guard<std::mutex> lock(*WSJCppLog::g_WSJCPP_LOG_MUTEX);
515+
std::vector<std::string> vRet;
516+
for (int i = 0; i < g_WSJCPP_LOG_LAST_MESSAGES->size(); i++) {
517+
vRet.push_back(g_WSJCPP_LOG_LAST_MESSAGES->at(i));
518+
}
519+
return vRet;
520+
}
521+
522+
// ---------------------------------------------------------------------
523+
514524
void WSJCppLog::setLogDirectory(const std::string &sDirectoryPath) {
515525
WSJCppLog::g_WSJCPP_LOG_DIR = sDirectoryPath;
516526
WSJCppLog::doLogRotateUpdateFilename(true);

src.wsjcpp/wsjcpp_core/wsjcpp_core.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ class WSJCppCore {
4545
static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
4646
static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
4747
static std::string& to_lower(std::string& str);
48+
static std::string toUpper(const std::string& str);
4849

4950
static void initRandom();
5051
static std::string createUuid();
51-
static bool isIPv4(const std::string& str);
52-
static bool isIPv6(const std::string& str);
52+
53+
static unsigned long convertVoidToULong(void *p);
54+
static std::string getPointerAsHex(void *p);
55+
static std::string extractURLProtocol(const std::string& sValue);
5356
};
5457

5558

@@ -96,6 +99,7 @@ class WSJCppLog {
9699
static void throw_err(const std::string &sTag, const std::string &sMessage);
97100
static void warn(const std::string &sTag, const std::string &sMessage);
98101
static void ok(const std::string &sTag, const std::string &sMessage);
102+
static std::vector<std::string> getLastLogMessages();
99103
static void setLogDirectory(const std::string &sDirectoryPath);
100104
static void setPrefixLogFile(const std::string &sPrefixLogFile);
101105
static void initGlobalVariables();

src.wsjcpp/wsjcpp_core/unit_tests.cpp renamed to src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
#include "unit_tests.h"
1+
#include "wsjcpp_unit_tests.h"
22

3-
UnitTestBase::UnitTestBase(const std::string &sTestName) {
3+
WSJCppUnitTestBase::WSJCppUnitTestBase(const std::string &sTestName) {
44
m_sTestName = sTestName;
55
TAG = m_sTestName;
6-
UnitTests::addUnitTest(sTestName, this);
6+
WSJCppUnitTests::addUnitTest(sTestName, this);
77
}
88

99
// ---------------------------------------------------------------------
1010

11-
std::string UnitTestBase::name() {
11+
std::string WSJCppUnitTestBase::name() {
1212
return m_sTestName;
1313
}
1414

1515
// ---------------------------------------------------------------------
1616

17-
void UnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint,
17+
void WSJCppUnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint,
1818
const std::string &sValue, const std::string &sExpected) {
1919
if (sValue != sExpected) {
2020
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + sExpected + "', but got '" + sValue + "'");
@@ -24,7 +24,7 @@ void UnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint,
2424

2525
// ---------------------------------------------------------------------
2626

27-
bool UnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected) {
27+
bool WSJCppUnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected) {
2828
if (nValue != nExpected) {
2929
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
3030
bTestSuccess = false;
@@ -35,7 +35,7 @@ bool UnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int n
3535

3636
// ---------------------------------------------------------------------
3737

38-
bool UnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected) {
38+
bool WSJCppUnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected) {
3939
if (nValue != nExpected) {
4040
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'");
4141
bTestSuccess = false;
@@ -46,7 +46,7 @@ bool UnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, doubl
4646

4747
// ---------------------------------------------------------------------
4848

49-
void UnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected) {
49+
void WSJCppUnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected) {
5050
if (bValue != bExpected) {
5151
WSJCppLog::err(TAG, " {" + sPoint + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'");
5252
bTestSuccess = false;
@@ -55,22 +55,22 @@ void UnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool
5555

5656
// ---------------------------------------------------------------------
5757

58-
std::vector<UnitTestBase*> *g_pUnitTests = NULL;
58+
std::vector<WSJCppUnitTestBase*> *g_pUnitTests = NULL;
5959

60-
void UnitTests::initGlobalVariables() {
60+
void WSJCppUnitTests::initGlobalVariables() {
6161
if (g_pUnitTests == NULL) {
6262
// Log::info(std::string(), "Create handlers map");
63-
g_pUnitTests = new std::vector<UnitTestBase*>();
63+
g_pUnitTests = new std::vector<WSJCppUnitTestBase*>();
6464
}
6565
}
6666

6767
// ---------------------------------------------------------------------
6868

69-
void UnitTests::addUnitTest(const std::string &sTestName, UnitTestBase* pUnitTest) {
70-
UnitTests::initGlobalVariables();
69+
void WSJCppUnitTests::addUnitTest(const std::string &sTestName, WSJCppUnitTestBase* pUnitTest) {
70+
WSJCppUnitTests::initGlobalVariables();
7171
bool bFound = false;
7272
for (int i = 0; i < g_pUnitTests->size(); i++) {
73-
UnitTestBase* p = g_pUnitTests->at(i);
73+
WSJCppUnitTestBase* p = g_pUnitTests->at(i);
7474
if (p->name() == sTestName) {
7575
bFound = true;
7676
}
@@ -86,13 +86,13 @@ void UnitTests::addUnitTest(const std::string &sTestName, UnitTestBase* pUnitTes
8686

8787
// ---------------------------------------------------------------------
8888

89-
bool UnitTests::runUnitTests() {
90-
UnitTests::initGlobalVariables();
89+
bool WSJCppUnitTests::runUnitTests() {
90+
WSJCppUnitTests::initGlobalVariables();
9191
int nAll = g_pUnitTests->size();
9292
WSJCppLog::info("runUnitTests", "All tests count " + std::to_string(nAll));
9393
int nSuccess = 0;
9494
for (int i = 0; i < g_pUnitTests->size(); i++) {
95-
UnitTestBase* pUnitTest = g_pUnitTests->at(i);
95+
WSJCppUnitTestBase* pUnitTest = g_pUnitTests->at(i);
9696
std::string sTestName = pUnitTest->name();
9797
WSJCppLog::info("runUnitTests", "Run test " + sTestName);
9898
if (pUnitTest->run()) {
@@ -102,7 +102,7 @@ bool UnitTests::runUnitTests() {
102102
WSJCppLog::err(sTestName, "Test failed");
103103
}
104104
}
105-
WSJCppLog::info("runUnitTests", "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll));
105+
WSJCppLog::info("WSJCpp::runUnitTests", "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll));
106106
return nSuccess == nAll;
107107
}
108108

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#ifndef UNIT_TESTS_H
2-
#define UNIT_TESTS_H
1+
#ifndef WSJCPP_UNIT_TESTS_H
2+
#define WSJCPP_UNIT_TESTS_H
33

44
#include <map>
55
#include <vector>
66
#include <wsjcpp_core.h>
77

8-
class UnitTestBase {
8+
class WSJCppUnitTestBase {
99
public:
10-
UnitTestBase(const std::string &sTestName);
10+
WSJCppUnitTestBase(const std::string &sTestName);
1111
std::string name();
1212
virtual void init() = 0;
1313
virtual bool run() = 0;
@@ -23,18 +23,18 @@ class UnitTestBase {
2323
std::string m_sTestName;
2424
};
2525

26-
extern std::vector<UnitTestBase*> *g_pUnitTests;
26+
extern std::vector<WSJCppUnitTestBase*> *g_pUnitTests;
2727

28-
class UnitTests {
28+
class WSJCppUnitTests {
2929
public:
3030
static void initGlobalVariables();
31-
static void addUnitTest(const std::string &sTestName, UnitTestBase* pCmdHandler);
31+
static void addUnitTest(const std::string &sTestName, WSJCppUnitTestBase* pCmdHandler);
3232
static bool runUnitTests();
3333
};
3434

3535
// RegistryCmdHandler
3636
#define REGISTRY_UNIT_TEST( classname ) \
37-
static classname * pRegistryUnitTest ## classname = new classname(); \
37+
static classname * pRegistryWSJCppUnitTest ## classname = new classname(); \
3838

3939

40-
#endif // UNIT_TESTS_H
40+
#endif // WSJCPP_UNIT_TESTS_H

src.wsjcpp/wsjcpp_core/unit_tests_main.cpp renamed to src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <string>
22
#include <wsjcpp_core.h>
3-
#include <unit_tests.h>
3+
#include <wsjcpp_unit_tests.h>
44

55
int main(int argc, char** argv) {
66
WSJCppCore::initRandom();
@@ -16,7 +16,7 @@ int main(int argc, char** argv) {
1616
return -1;
1717
}
1818

19-
if (!UnitTests::runUnitTests()) {
19+
if (!WSJCppUnitTests::runUnitTests()) {
2020
WSJCppLog::err(TAG, "Some unit tests failed");
2121
return -1;
2222
}

0 commit comments

Comments
 (0)