From f77e1e4d2adf4422943c10a1906854c3027391d8 Mon Sep 17 00:00:00 2001
From: open-license-manager <rillf@maildrop.cc>
Date: 周一, 14 4月 2014 01:11:41 +0800
Subject: [PATCH] first roundtrip test

---
 src/library/CMakeLists.txt                |   15 -----
 src/library/os/linux/os-linux.cpp         |   12 ++-
 src/library/api/datatypes.h               |   11 ++-
 src/library/base/EventRegistry.cpp        |   27 +++++++-
 src/library/reader/LicenseReader.h        |    4 -
 test/library/reader/CMakeLists.txt        |    5 -
 src/license-generator/CMakeLists.txt      |    2 
 CMakeLists.txt                            |   12 ++-
 test/functional/CMakeLists.txt            |    2 
 src/bootstrap/bootstrap.cpp               |    2 
 test/functional/standard-license_test.cpp |    1 
 src/library/reader/CMakeLists.txt         |   13 ----
 src/library/reader/LicenseReader.cpp      |   42 +++++++++++---
 src/library/os/linux/CMakeLists.txt       |    3 
 test/CMakeLists.txt                       |    3 
 15 files changed, 84 insertions(+), 70 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5f1fe56..1c0e7c4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -29,7 +29,7 @@
     SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -fmessage-length=0 -Wall -Wuninitialized -fPIC") 
     SET(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -pipe" CACHE INTERNAL "Linker flags")
     SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS} -Wl,--strip-all")
-    #SET(CMAKE_FIND_LIBRARY_SUFFIXES .a) #.so for dynamic libraries
+    SET(CMAKE_FIND_LIBRARY_SUFFIXES .a) #.so for dynamic libraries
     #you need to "force" the change in cmake_install_prefix after the project keyword
 	IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
 		SET(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Install path prefix" FORCE)
@@ -49,10 +49,7 @@
 #include_directories(${CRYPTOPP_INCLUDE_DIRS})
 #link_directories ( ${Boost_LIBRARY_DIRS} )
 
-find_package(OpenSSL REQUIRED)
-include_directories(${OPENSSL_INCLUDE_DIR})
-list(APPEND EXTERNAL_LIBS ${OPENSSL_CRYPTO_LIBRARY})
-#link_directories ( ${OPENSSL_LIBRARIES} )
+
 
 SET(Boost_USE_STATIC_LIBS ON)
 # static runtime requires /MT 
@@ -62,6 +59,11 @@
 #include_directories(${Boost_INCLUDE_DIRS})
 #link_directories ( ${Boost_LIBRARY_DIRS} )
 
+#find a static version of openssl crypto library
+find_package(OpenSSL REQUIRED)
+include_directories(${OPENSSL_INCLUDE_DIR})
+list(APPEND EXTERNAL_LIBS ${OPENSSL_CRYPTO_LIBRARY})
+
 FIND_PACKAGE(Doxygen)
 
 IF (DOXYGEN_FOUND)
diff --git a/src/bootstrap/bootstrap.cpp b/src/bootstrap/bootstrap.cpp
index 055c792..360510a 100644
--- a/src/bootstrap/bootstrap.cpp
+++ b/src/bootstrap/bootstrap.cpp
@@ -44,7 +44,7 @@
 	char* pem_key = (char*) (calloc(keylen + 1, 1)); /* Null-terminate */
 	BIO_read(bio_private, pem_key, keylen);
 	std::string dest = replaceAll(string(pem_key), string("\n"),
-			string("\" \\\n\""));
+			string("\\n\" \\\n\""));
 	FILE* fp = fopen(public_fname.c_str(), "w");
 	fprintf(fp, "//file generated by bootstrap.c, do not edit.\n\n");
 	fprintf(fp, "#ifndef PUBLIC_KEY_H_\n#define PUBLIC_KEY_H_\n");
diff --git a/src/library/CMakeLists.txt b/src/library/CMakeLists.txt
index c841fef..57642dd 100644
--- a/src/library/CMakeLists.txt
+++ b/src/library/CMakeLists.txt
@@ -14,20 +14,5 @@
      license++_static
      reader
      os
-     ${EXTERNAL_LIBS}
 )
 
-ADD_LIBRARY(license++ SHARED
-    license++.cpp
-)
-
-SET_TARGET_PROPERTIES(license++ PROPERTIES LINK_SEARCH_START_STATIC OFF)
-SET_TARGET_PROPERTIES(license++ PROPERTIES LINK_SEARCH_END_STATIC OFF)
-
-target_link_libraries(
-     license++
-     reader_dynl
-     #os
-     #${EXTERNAL_LIBS}
-)
-set_target_properties(license++ PROPERTIES PREFIX "lib")
\ No newline at end of file
diff --git a/src/library/api/datatypes.h b/src/library/api/datatypes.h
index 3c75c4d..fb730c1 100644
--- a/src/library/api/datatypes.h
+++ b/src/library/api/datatypes.h
@@ -32,13 +32,14 @@
 	LICENSE_SERVER_NOT_FOUND = 2, 		//license server can't be contacted
 	ENVIRONMENT_VARIABLE_NOT_DEFINED = 3, //environment variable not defined
 	FILE_FORMAT_NOT_RECOGNIZED = 4,	//license file has invalid format (not .ini file)
-	LICENSE_MALFORMED = 5,//some mandatory field are missing, or data can't be fully read.
+	LICENSE_MALFORMED = 5,          //some mandatory field are missing, or data can't be fully read.
 	PRODUCT_NOT_LICENSED = 6,		//this product was not licensed
 	PRODUCT_EXPIRED = 7,
-	LICENSE_CORRUPTED = 8,		//License serial and calculated one didn't match
-	IDENTIFIERS_MISMATCH = 9, //Calculated identifier and the one provided in license didn't match
+	LICENSE_CORRUPTED = 8,		//License signature didn't match with current license
+	IDENTIFIERS_MISMATCH = 9,   //Calculated identifier and the one provided in license didn't match
 
-	LICENSE_FILE_FOUND = 100
+	LICENSE_FILE_FOUND = 100,
+	LICENSE_VERIFIED = 101
 
 };
 
@@ -47,7 +48,7 @@
 };
 
 enum SEVERITY {
-	INFO, WARN, SEVERITY_ERROR
+	INFO, SEVERITY_WARN, SEVERITY_ERROR
 };
 
 
diff --git a/src/library/base/EventRegistry.cpp b/src/library/base/EventRegistry.cpp
index 26bc337..8650b96 100644
--- a/src/library/base/EventRegistry.cpp
+++ b/src/library/base/EventRegistry.cpp
@@ -8,6 +8,7 @@
 #include "EventRegistry.h"
 #include <cstddef>
 #include <string.h>
+#include <algorithm>
 
 namespace license {
 EventRegistry::EventRegistry() {
@@ -49,11 +50,11 @@
 
 AuditEvent const * EventRegistry::getLastFailure() const {
 	const AuditEvent* result = NULL;
-	if(logs.size() == 0){
+	if (logs.size() == 0) {
 		return result;
 	}
 	auto it = logs.end();
-	do  {
+	do {
 		--it;
 		if (it->severity == SEVERITY_ERROR) {
 			result = &(*it);
@@ -75,8 +76,8 @@
 	return isGood;
 }
 
-void EventRegistry::addError(EVENT_TYPE event){
-	addEvent(event,SEVERITY_ERROR);
+void EventRegistry::addError(EVENT_TYPE event) {
+	addEvent(event, SEVERITY_ERROR);
 }
 void EventRegistry::addEvent(EVENT_TYPE event, SEVERITY severity) {
 	AuditEvent audit;
@@ -92,9 +93,25 @@
 	AuditEvent audit;
 	audit.severity = severity;
 	audit.event_type = event;
-	strncpy(audit.param1,eventParameter.c_str(),255);
+	strncpy(audit.param1, eventParameter.c_str(), 255);
 	audit.param2[0] = '\0';
 	logs.push_back(audit);
 }
 
+bool EventRegistry::turnErrosIntoWarnings() {
+	bool eventFound = false;
+	for (auto it = logs.begin(); it != logs.end(); ++it) {
+		if (it->severity == SEVERITY_ERROR) {
+			it->severity = SEVERITY_WARN;
+			eventFound = true;
+		}
+	}
+	return eventFound;
 }
+
+void EventRegistry::exportLastEvents(AuditEvent* auditEvents, int nlogs) {
+	int sizeToCopy = std::min(nlogs, (int) logs.size());
+	std::copy(logs.begin(), logs.begin() + sizeToCopy, auditEvents);
+}
+}
+
diff --git a/src/library/os/linux/CMakeLists.txt b/src/library/os/linux/CMakeLists.txt
index 2d30cf8..436f2fb 100644
--- a/src/library/os/linux/CMakeLists.txt
+++ b/src/library/os/linux/CMakeLists.txt
@@ -2,9 +2,8 @@
     os-linux.cpp
 )
 
-
 target_link_libraries(
      os
      base
      ${EXTERNAL_LIBS}
-)
\ No newline at end of file
+)
diff --git a/src/library/os/linux/os-linux.cpp b/src/library/os/linux/os-linux.cpp
index b8ad61c..2028d47 100644
--- a/src/library/os/linux/os-linux.cpp
+++ b/src/library/os/linux/os-linux.cpp
@@ -69,12 +69,16 @@
 	char *pubKey = PUBLIC_KEY
 	;
 
-	BIO* bio = BIO_new_mem_buf((void*) (pubKey), sizeof(pubKey));
-	EVP_PKEY *pktmp = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
+	BIO* bio = BIO_new_mem_buf((void*) (pubKey), strlen(pubKey));
+	RSA *rsa = PEM_read_bio_RSAPublicKey(bio, NULL,NULL,NULL);
 	BIO_free(bio);
-	if (pktmp == NULL) {
+	if (rsa == NULL) {
 		throw new logic_error("Error reading public key");
 	}
+	EVP_PKEY *pkey = EVP_PKEY_new();
+
+	EVP_PKEY_assign_RSA(pkey, rsa);
+
 	/*BIO* bo = BIO_new(BIO_s_mem());
 	 BIO_write(bo, pubKey, strlen(pubKey));
 	 RSA *key = 0;
@@ -87,7 +91,7 @@
 	if (!(mdctx = EVP_MD_CTX_create())) {
 		throw new logic_error("Error creating context");
 	}
-	if (1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pktmp)) {
+	if (1 != EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, pkey)) {
 		throw new logic_error("Error initializing digest");
 	}
 
diff --git a/src/library/reader/CMakeLists.txt b/src/library/reader/CMakeLists.txt
index d4dee76..d637498 100644
--- a/src/library/reader/CMakeLists.txt
+++ b/src/library/reader/CMakeLists.txt
@@ -11,16 +11,3 @@
      ${EXTERNAL_LIBS}
 )
 
-ADD_LIBRARY(reader_dynl STATIC
-    ConvertUTF.c
-    LicenseReader.cpp
-)
-
-SET_TARGET_PROPERTIES(reader_dynl PROPERTIES LINK_SEARCH_START_STATIC OFF)
-SET_TARGET_PROPERTIES(reader_dynl PROPERTIES LINK_SEARCH_END_STATIC OFF)
-target_link_libraries(
-     reader_dynl
-     os
-     #base
-     #${EXTERNAL_LIBS}
-)
\ No newline at end of file
diff --git a/src/library/reader/LicenseReader.cpp b/src/library/reader/LicenseReader.cpp
index 353cd0f..458fb75 100644
--- a/src/library/reader/LicenseReader.cpp
+++ b/src/library/reader/LicenseReader.cpp
@@ -50,6 +50,27 @@
 		extra_data(extra_data) {
 }
 
+EventRegistry FullLicenseInfo::validate(int sw_version) {
+	EventRegistry er;
+	bool sigVerified = OsFunctions::verifySignature(printForSign().c_str(),
+			license_signature.c_str());
+	if (sigVerified) {
+		er.addEvent(LICENSE_VERIFIED, INFO);
+	} else {
+		er.addEvent(LICENSE_CORRUPTED, SEVERITY_ERROR);
+	}
+	if(has_expiry){
+		time_t now = time(NULL);
+		if(this->to_date<now){
+			er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR, "");
+		}
+		if(this->from_date>now){
+			er.addEvent(PRODUCT_EXPIRED, SEVERITY_ERROR);
+		}
+	}
+	return er;
+}
+
 void FullLicenseInfo::toLicenseInfo(LicenseInfo* license) const {
 	if (license != NULL) {
 		strncpy(license->proprietary_data, extra_data.c_str(),
@@ -88,7 +109,7 @@
 		ini.Reset();
 		SI_Error rc = ini.LoadFile((*it).c_str());
 		if (rc < 0) {
-			result.addEvent(FILE_FORMAT_NOT_RECOGNIZED, WARN, *it);
+			result.addEvent(FILE_FORMAT_NOT_RECOGNIZED, SEVERITY_WARN, *it);
 			continue;
 		} else {
 			loadAtLeastOneFile = true;
@@ -96,7 +117,7 @@
 		const char* productNamePtr = product.c_str();
 		int sectionSize = ini.GetSectionSize(productNamePtr);
 		if (sectionSize <= 0) {
-			result.addEvent(PRODUCT_NOT_LICENSED, WARN, *it);
+			result.addEvent(PRODUCT_NOT_LICENSED, SEVERITY_WARN, *it);
 			continue;
 		} else {
 			atLeastOneProductLicensed = true;
@@ -120,7 +141,7 @@
 			licenseInfoOut.push_back(licInfo);
 			atLeastOneLicenseComplete = true;
 		} else {
-			result.addEvent(LICENSE_MALFORMED, WARN, *it);
+			result.addEvent(LICENSE_MALFORMED, SEVERITY_WARN, *it);
 		}
 	}
 	if (!loadAtLeastOneFile) {
@@ -155,7 +176,8 @@
 				}
 			}
 		} else {
-			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, WARN, varName);
+			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SEVERITY_WARN,
+					varName);
 		}
 	}
 	return licenseFoundWithExplicitLocation;
@@ -183,14 +205,16 @@
 						eventRegistry.addEvent(LICENSE_FILE_FOUND, INFO, *it);
 					}
 				} else {
-					eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, WARN,
-							env_var_value);
+					eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND,
+							SEVERITY_WARN, env_var_value);
 				}
 			} else {
-				eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED, WARN);
+				eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED,
+						SEVERITY_WARN);
 			}
 		} else {
-			eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED, WARN);
+			eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED,
+					SEVERITY_WARN);
 		}
 	}
 	return licenseFileFoundWithEnvVariable;
@@ -209,7 +233,7 @@
 			diskFiles.push_back(temptativeLicense);
 			eventRegistry.addEvent(LICENSE_FILE_FOUND, INFO, temptativeLicense);
 		} else {
-			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, WARN,
+			eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SEVERITY_WARN,
 					temptativeLicense);
 		}
 		f.close();
diff --git a/src/library/reader/LicenseReader.h b/src/library/reader/LicenseReader.h
index deed0ea..9ab92ca 100644
--- a/src/library/reader/LicenseReader.h
+++ b/src/library/reader/LicenseReader.h
@@ -46,9 +46,7 @@
 			const string& extra_data = "");
 	string printForSign() const;
 	void printAsIni(ostream & a_ostream) const;
-	inline bool signatureVerified() const {
-		return OsFunctions::verifySignature(printForSign().c_str(), license_signature.c_str());
-	}
+
 	void toLicenseInfo(LicenseInfo* license) const;
 	EventRegistry validate(int sw_version);
 };
diff --git a/src/license-generator/CMakeLists.txt b/src/license-generator/CMakeLists.txt
index b1d69e4..24b3f28 100644
--- a/src/license-generator/CMakeLists.txt
+++ b/src/license-generator/CMakeLists.txt
@@ -13,8 +13,6 @@
      ${Boost_LIBRARIES}
      ${EXTERNAL_LIBS}
 )
-#SET_TARGET_PROPERTIES(license_generator_lib PROPERTIES LINK_SEARCH_START_STATIC ON)
-#SET_TARGET_PROPERTIES(license_generator_lib PROPERTIES LINK_SEARCH_END_STATIC ON)
 
 add_executable(
  license_generator
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 1fc3729..fc1f859 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -4,4 +4,5 @@
 link_directories ( ${Boost_LIBRARY_DIRS} )
 
 add_subdirectory(library)
-add_subdirectory(license-generator)
\ No newline at end of file
+add_subdirectory(license-generator)
+add_subdirectory(functional)
\ No newline at end of file
diff --git a/test/functional/CMakeLists.txt b/test/functional/CMakeLists.txt
index ee89b80..4e3773d 100644
--- a/test/functional/CMakeLists.txt
+++ b/test/functional/CMakeLists.txt
@@ -6,8 +6,8 @@
 
 target_link_libraries(
  standard_license_test
- license_generator_lib
  license++_static
+ license_generator_lib
 )
 
 SET_TARGET_PROPERTIES(standard_license_test PROPERTIES LINK_SEARCH_START_STATIC ON)
diff --git a/test/functional/standard-license_test.cpp b/test/functional/standard-license_test.cpp
index d301d68..dc24a98 100644
--- a/test/functional/standard-license_test.cpp
+++ b/test/functional/standard-license_test.cpp
@@ -6,6 +6,7 @@
 #include "../../../src/library/api/license++.h"
 #include <build_properties.h>
 #include <boost/filesystem.hpp>
+#include "../../src/library/reader/SimpleIni.h"
 
 
 namespace fs = boost::filesystem;
diff --git a/test/library/reader/CMakeLists.txt b/test/library/reader/CMakeLists.txt
index d094bfa..5c0f908 100644
--- a/test/library/reader/CMakeLists.txt
+++ b/test/library/reader/CMakeLists.txt
@@ -7,11 +7,8 @@
 
 target_link_libraries(
  license_reader_test
- ${Boost_LIBRARIES}
  reader
- os
- base
- ${PLATFORM_LIBS}
+ ${Boost_LIBRARIES}
 )
 
 SET_TARGET_PROPERTIES(license_reader_test PROPERTIES LINK_SEARCH_START_STATIC ON)

--
Gitblit v1.9.1