From bd879d3c9f5fef0107bc3036be3893edab14e89c Mon Sep 17 00:00:00 2001
From: Gabriele Contini <contini.mailing@gmail.com>
Date: 周三, 01 1月 2020 20:26:43 +0800
Subject: [PATCH] cleanup & new api

---
 test/library/LicenseLocator_test.cpp |  237 ++++++++++++++++++++++++++--------------------------------
 1 files changed, 107 insertions(+), 130 deletions(-)

diff --git a/test/library/LicenseLocator_test.cpp b/test/library/LicenseLocator_test.cpp
index 1fed1ee..65c225a 100644
--- a/test/library/LicenseLocator_test.cpp
+++ b/test/library/LicenseLocator_test.cpp
@@ -1,17 +1,21 @@
-#define BOOST_TEST_MODULE "license_locator_test"
+#define BOOST_TEST_MODULE "test_license_locator"
+#define __STDC_WANT_LIB_EXT1__ 1
+#include <string.h>
 
 #include <iostream>
 #include <iterator>
-
+#include <cstdio>
 #include <fstream>
-#include <string>
 #include <vector>
-
+#include <boost/filesystem.hpp>
+#include <boost/optional.hpp>
 #include <boost/test/unit_test.hpp>
 #include <stdlib.h>
-#include <cstdio>
 
-#include <build_properties.h>
+#include <licensecc_properties.h>
+#include <licensecc_properties_test.h>
+
+#include "../../src/library/os/os.h"
 #include "../../src/library/base/EventRegistry.h"
 #include "../../src/library/locate/ApplicationFolder.hpp"
 #include "../../src/library/locate/EnvironmentVarLocation.hpp"
@@ -22,191 +26,164 @@
 namespace test {
 using namespace license::locate;
 using namespace std;
+using namespace boost::filesystem;
+
+static boost::optional<path> find_file(const path &dir_path, const path &file_name) {
+	const recursive_directory_iterator end;
+	const auto it = find_if(recursive_directory_iterator(dir_path), end,
+							[&file_name](const directory_entry &e) { return e.path().filename() == file_name; });
+	return it == end ? boost::optional<path>() : it->path();
+}
 
 /*****************************************************************************
  * Application Folder tests
  *****************************************************************************/
-BOOST_AUTO_TEST_CASE( read_license_near_module ) {
-#ifdef _WIN32
-#ifdef _DEBUG
-	const string testExeFolder = PROJECT_BINARY_DIR "/test/library/Debug";
-#else
-	const string testExeFolder = PROJECT_BINARY_DIR "/test/library/Release";
-#endif
-	const string testExe = testExeFolder + "/" + BOOST_TEST_MODULE ".exe";
-#else
+BOOST_AUTO_TEST_CASE(read_license_near_module) {
 	const string testExeFolder = PROJECT_BINARY_DIR "/test/library";
-	const string testExe = testExeFolder + "/" + BOOST_TEST_MODULE;
-#endif	
-	const string referenceLicenseFileName = testExeFolder + "/"
-			+ BOOST_TEST_MODULE ".lic";
+	bool exeFileFound = false;
+	string referenceExeFileName;
+	string referenceLicenseFileName;
+	// Verify we're pointing the correct executable, in windows isn't clear where it's built
+#ifdef _WIN32
+	boost::optional<path> exeLocation(find_file(path(testExeFolder), path(BOOST_TEST_MODULE ".exe")));
+	exeFileFound = exeLocation.has_value();
+	if (exeFileFound) {
+		referenceExeFileName = exeLocation.get().string();
+		referenceLicenseFileName =
+			referenceExeFileName.replace(referenceExeFileName.find(BOOST_TEST_MODULE ".exe"),
+										 string(BOOST_TEST_MODULE ".exe").size(), BOOST_TEST_MODULE ".lic");
+	}
+#else
+	referenceExeFileName = testExeFolder + "/" + BOOST_TEST_MODULE;
+	std::ifstream f(referenceExeFileName.c_str());
+	exeFileFound = f.good();
+	referenceLicenseFileName = testExeFolder + "/" + BOOST_TEST_MODULE ".lic";
+#endif
+	BOOST_WARN_MESSAGE(!exeFileFound, "File [" + referenceExeFileName + "] NOT found");
+	if (exeFileFound) {
+		// copy test license near module
+		std::ifstream src(MOCK_LICENSE, std::ios::binary);
+		std::ofstream dst(referenceLicenseFileName, std::ios::binary);
+		dst << src.rdbuf();
+		dst.close();
 
-	//Verify we're pointing the correct executable
-	ifstream f(testExe.c_str());
-	BOOST_REQUIRE_MESSAGE(f.good(), "File [" + testExe + "] NOT found");
-
-	//copy test license near module
-	std::ifstream src(MOCK_LICENSE, std::ios::binary);
-	std::ofstream dst(referenceLicenseFileName, std::ios::binary);
-	dst << src.rdbuf();
-	dst.close();
-
-	license::EventRegistry registry;
-	ApplicationFolder applicationFolder;
-	vector<string> licenseInfos = applicationFolder.licenseLocations(registry);
-	BOOST_CHECK(registry.isGood());
-	BOOST_REQUIRE_EQUAL(1, licenseInfos.size());
-	string currentLocation = licenseInfos[0];
-	BOOST_CHECK_MESSAGE(referenceLicenseFileName.compare(currentLocation) == 0,
-			"file found at expected location");
-	string licenseRealContent = applicationFolder.retrieveLicense(
-			currentLocation);
-	src.seekg(0, ios::beg);
-	std::string referenceContent((std::istreambuf_iterator<char>(src)),
-			std::istreambuf_iterator<char>());
-	BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
-			"File content is same");
-	remove(referenceLicenseFileName.c_str());
+		license::EventRegistry registry;
+		ApplicationFolder applicationFolder;
+		vector<string> licenseInfos = applicationFolder.license_locations(registry);
+		BOOST_CHECK(registry.isGood());
+		BOOST_REQUIRE_EQUAL(1, licenseInfos.size());
+		string currentLocation = licenseInfos[0];
+		BOOST_CHECK_MESSAGE(equivalent(path(referenceLicenseFileName), path(currentLocation)),
+							"file " + currentLocation + "found at expected location");
+		string licenseRealContent = applicationFolder.retrieve_license_content(currentLocation);
+		src.seekg(0, ios::beg);
+		std::string referenceContent((std::istreambuf_iterator<char>(src)), std::istreambuf_iterator<char>());
+		BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0, "File content is same");
+		remove(referenceLicenseFileName.c_str());
+	}
 }
 
 /*****************************************************************************
  * External_Definition tests
  *****************************************************************************/
 
-BOOST_AUTO_TEST_CASE( external_definition ) {
-	//an application can define multiple license locations separated by ';'
-	const char *applicationDefinedString =
-	MOCK_LICENSE ";/this/one/doesnt/exist";
+BOOST_AUTO_TEST_CASE(external_definition) {
+	// an application can define multiple license locations separated by ';'
+	string applicationDefinedString = MOCK_LICENSE ";/this/one/doesnt/exist";
 
-	//read test license
+	// read test license
 	std::ifstream src(MOCK_LICENSE, std::ios::binary);
-	std::string referenceContent((std::istreambuf_iterator<char>(src)),
-			std::istreambuf_iterator<char>());
+	std::string referenceContent((std::istreambuf_iterator<char>(src)), std::istreambuf_iterator<char>());
 	license::EventRegistry registry;
-	ExternalDefinition externalDefinition(applicationDefinedString);
-	vector<string> licenseInfos = externalDefinition.licenseLocations(registry);
+	LicenseLocation licLocation = {LICENSE_PATH};
+	std::copy(applicationDefinedString.begin(), applicationDefinedString.end(), licLocation.licenseData);
+	ExternalDefinition externalDefinition(&licLocation);
+	vector<string> licenseInfos = externalDefinition.license_locations(registry);
 	BOOST_CHECK(registry.isGood());
 	BOOST_CHECK_EQUAL(1, licenseInfos.size());
 	string currentLocation = licenseInfos[0];
-	BOOST_CHECK_MESSAGE(string(MOCK_LICENSE).compare(currentLocation) == 0,
-			"file found at expected location");
-	string licenseRealContent = externalDefinition.retrieveLicense(
-			currentLocation);
-	BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
-			"File content is same");
+	BOOST_CHECK_MESSAGE(string(MOCK_LICENSE).compare(currentLocation) == 0, "file found at expected location");
+	string licenseRealContent = externalDefinition.retrieve_license_content(currentLocation);
+	BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0, "File content is same");
 }
 
 /**
- * The license file doesn't exist. Chech that the locator reports the right error
+ * The license file doesn't exist. Check that the locator reports the right error
  */
-BOOST_AUTO_TEST_CASE( external_definition_not_found ) {
-	const char *applicationDefinedString = PROJECT_TEST_SRC_DIR "/this/file/doesnt/exist";
+BOOST_AUTO_TEST_CASE(external_definition_not_found) {
+	string applicationDefinedString = PROJECT_TEST_SRC_DIR "/this/file/doesnt/exist";
 	license::EventRegistry registry;
-	ExternalDefinition externalDefinition(applicationDefinedString);
-	vector<string> licenseInfos = externalDefinition.licenseLocations(registry);
+	LicenseLocation licLocation = {LICENSE_PATH};
+	std::copy(applicationDefinedString.begin(), applicationDefinedString.end(), licLocation.licenseData);
+	ExternalDefinition externalDefinition(&licLocation);
+	vector<string> licenseInfos = externalDefinition.license_locations(registry);
 
-	BOOST_CHECK_MESSAGE(registry.isGood(),
-			"No fatal error for now, only warnings");
-	registry.turnEventIntoError(LICENSE_FILE_NOT_FOUND);
+	BOOST_CHECK_MESSAGE(registry.isGood(), "No fatal error for now, only warnings");
+	registry.turnWarningsIntoErrors();
 	BOOST_REQUIRE_MESSAGE(!registry.isGood(), "Error detected");
 	BOOST_CHECK_EQUAL(0, licenseInfos.size());
-	BOOST_CHECK_MESSAGE(
-			registry.getLastFailure()->event_type == LICENSE_FILE_NOT_FOUND,
-			"Error detected");
-
+	BOOST_CHECK_MESSAGE(registry.getLastFailure()->event_type == LICENSE_FILE_NOT_FOUND, "Error detected");
 }
 
 /*****************************************************************************
  * EnvironmentVarLocation tests
  *****************************************************************************/
-BOOST_AUTO_TEST_CASE( environment_var_location ) {
-	//an application can define multiple license locations separated by ';'
-	const char *environment_variable_value =
-	MOCK_LICENSE ";/this/one/doesnt/exist";
+BOOST_AUTO_TEST_CASE(environment_var_location) {
+	// an application can define multiple license locations separated by ';'
+	const char *environment_variable_value = MOCK_LICENSE ";/this/one/doesnt/exist";
 #ifdef _WIN32
 	_putenv_s(LICENSE_LOCATION_ENV_VAR, environment_variable_value);
 #else
 	setenv(LICENSE_LOCATION_ENV_VAR, environment_variable_value, 1);
 #endif
-	//read test license
+	// read test license
 	std::ifstream src(MOCK_LICENSE, std::ios::binary);
-	std::string referenceContent((std::istreambuf_iterator<char>(src)),
-			std::istreambuf_iterator<char>());
+	std::string referenceContent((std::istreambuf_iterator<char>(src)), std::istreambuf_iterator<char>());
 	license::EventRegistry registry;
 
 	EnvironmentVarLocation envVarLocationStrategy;
-	vector<string> licenseInfos = envVarLocationStrategy.licenseLocations(
-			registry);
+	vector<string> licenseInfos = envVarLocationStrategy.license_locations(registry);
 	BOOST_CHECK(registry.isGood());
 	BOOST_CHECK_EQUAL(1, licenseInfos.size());
 	string currentLocation = licenseInfos[0];
-	BOOST_CHECK_MESSAGE(string(MOCK_LICENSE).compare(currentLocation) == 0,
-			"file found at expected location");
-	string licenseRealContent = envVarLocationStrategy.retrieveLicense(
-			currentLocation);
-	BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0,
-			"File content is same");
-#ifdef _WIN32
-	_putenv_s(LICENSE_LOCATION_ENV_VAR, "");
-#else
-	unsetenv(LICENSE_LOCATION_ENV_VAR);
-#endif
-	
+	BOOST_CHECK_MESSAGE(string(MOCK_LICENSE).compare(currentLocation) == 0, "file found at expected location");
+	string licenseRealContent = envVarLocationStrategy.retrieve_license_content(currentLocation);
+	BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0, "File content is same");
+	UNSETENV(LICENSE_LOCATION_ENV_VAR);
 }
 
 /**
  * The license file doesn't exist. Check that the locator reports the right error
  */
-BOOST_AUTO_TEST_CASE( environment_var_location_not_found ) {
-	const char *environment_variable_value =
-	PROJECT_TEST_SRC_DIR "/this/file/doesnt/exist";
-#ifdef _WIN32
-	_putenv_s(LICENSE_LOCATION_ENV_VAR, environment_variable_value);
-#else
-	setenv(LICENSE_LOCATION_ENV_VAR, environment_variable_value, 1);
-#endif
+BOOST_AUTO_TEST_CASE(environment_var_location_not_found) {
+	const char *environment_variable_value = PROJECT_TEST_SRC_DIR "/this/file/doesnt/exist";
+	SETENV(LICENSE_LOCATION_ENV_VAR, environment_variable_value);
+
 	license::EventRegistry registry;
 	EnvironmentVarLocation envVarLocationStrategy;
-	vector<string> licenseInfos = envVarLocationStrategy.licenseLocations(
-			registry);
-	BOOST_CHECK_MESSAGE(registry.isGood(),
-			"No fatal error for now, only warnings");
-	registry.turnEventIntoError(LICENSE_FILE_NOT_FOUND);
+	vector<string> licenseInfos = envVarLocationStrategy.license_locations(registry);
+	BOOST_CHECK_MESSAGE(registry.isGood(), "No fatal error for now, only warnings");
+	registry.turnWarningsIntoErrors();
 	BOOST_REQUIRE_MESSAGE(!registry.isGood(), "Error detected");
 	BOOST_CHECK_EQUAL(0, licenseInfos.size());
-	BOOST_CHECK_MESSAGE(
-			registry.getLastFailure()->event_type == LICENSE_FILE_NOT_FOUND,
-			"Error detected");
-#ifdef _WIN32
-	_putenv_s(LICENSE_LOCATION_ENV_VAR, "");
-#else
-	unsetenv(LICENSE_LOCATION_ENV_VAR);
-#endif
+	BOOST_CHECK_MESSAGE(registry.getLastFailure()->event_type == LICENSE_FILE_NOT_FOUND, "Error detected");
+	UNSETENV(LICENSE_LOCATION_ENV_VAR);
 }
 
 /**
  * The license file doesn't exist. Check that the locator reports the right error
  */
-BOOST_AUTO_TEST_CASE( environment_var_location_not_defined ) {
-#ifdef _WIN32
-	_putenv_s(LICENSE_LOCATION_ENV_VAR, "");
-#else
-	setenv(LICENSE_LOCATION_ENV_VAR, environment_variable_value, 1);
-#endif
+BOOST_AUTO_TEST_CASE(environment_var_location_not_defined) {
+	UNSETENV(LICENSE_LOCATION_ENV_VAR);
 	license::EventRegistry registry;
 	EnvironmentVarLocation environmentVarLocation;
-	vector<string> licenseInfos = environmentVarLocation.licenseLocations(
-			registry);
+	vector<string> licenseInfos = environmentVarLocation.license_locations(registry);
 
-	BOOST_CHECK_MESSAGE(registry.isGood(),
-			"No fatal error for now, only warnings");
-	registry.turnEventIntoError(ENVIRONMENT_VARIABLE_NOT_DEFINED);
+	BOOST_CHECK_MESSAGE(registry.isGood(), "No fatal error for now, only warnings");
+	registry.turnWarningsIntoErrors();
 	BOOST_REQUIRE_MESSAGE(!registry.isGood(), "Error detected");
 	BOOST_CHECK_EQUAL(0, licenseInfos.size());
-	BOOST_CHECK_MESSAGE(
-			registry.getLastFailure()->event_type
-					== ENVIRONMENT_VARIABLE_NOT_DEFINED, "Error detected");
-
+	BOOST_CHECK_MESSAGE(registry.getLastFailure()->event_type == ENVIRONMENT_VARIABLE_NOT_DEFINED, "Error detected");
 }
 
-}  //namespace test
+}  // namespace test

--
Gitblit v1.9.1