gcontini
2019-12-14 11a0f65bae425baf09508e2d57c830f5cad69df5
test/library/LicenseLocator_test.cpp
@@ -30,12 +30,9 @@
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;
      });
                     [&file_name](const directory_entry &e) { return e.path().filename() == file_name; });
   return it == end ? boost::optional<path>() : it->path();
}
/*****************************************************************************
 * Application Folder tests
@@ -51,7 +48,8 @@
   exeFileFound = exeLocation.has_value();
   if (exeFileFound) {
      referenceExeFileName = exeLocation.get().string();
      referenceLicenseFileName = referenceExeFileName.replace(referenceExeFileName.find(BOOST_TEST_MODULE ".exe"),
      referenceLicenseFileName =
         referenceExeFileName.replace(referenceExeFileName.find(BOOST_TEST_MODULE ".exe"),
         string(BOOST_TEST_MODULE ".exe").size(), BOOST_TEST_MODULE ".lic");
   }
#else
@@ -76,13 +74,10 @@
      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);
      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");
      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());
   }
}
@@ -93,13 +88,11 @@
BOOST_AUTO_TEST_CASE( external_definition ) {
   //an application can define multiple license locations separated by ';'
   const char *applicationDefinedString =
   MOCK_LICENSE ";/this/one/doesnt/exist";
   const char *applicationDefinedString = MOCK_LICENSE ";/this/one/doesnt/exist";
   //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;
   const LicenseLocation licLocation={applicationDefinedString,nullptr};
   ExternalDefinition externalDefinition(&licLocation);
@@ -107,12 +100,9 @@
   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.retrieve_license_content(
         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");
}
/**
@@ -125,15 +115,11 @@
   ExternalDefinition externalDefinition(&licLocation);
   vector<string> licenseInfos = externalDefinition.license_locations(registry);
   BOOST_CHECK_MESSAGE(registry.isGood(),
         "No fatal error for now, only warnings");
   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");
}
/*****************************************************************************
@@ -141,8 +127,7 @@
 *****************************************************************************/
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";
   const char *environment_variable_value = MOCK_LICENSE ";/this/one/doesnt/exist";
#ifdef _WIN32
   _putenv_s(LICENSE_LOCATION_ENV_VAR, environment_variable_value);
#else
@@ -150,22 +135,17 @@
#endif
   //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.license_locations(
         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.retrieve_license_content(
         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 = envVarLocationStrategy.retrieve_license_content(currentLocation);
   BOOST_CHECK_MESSAGE(referenceContent.compare(licenseRealContent) == 0, "File content is same");
   UNSETENV(LICENSE_LOCATION_ENV_VAR);
}
@@ -173,22 +153,17 @@
 * 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";
   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.license_locations(
         registry);
   BOOST_CHECK_MESSAGE(registry.isGood(),
         "No fatal error for now, only warnings");
   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");
   BOOST_CHECK_MESSAGE(registry.getLastFailure()->event_type == LICENSE_FILE_NOT_FOUND, "Error detected");
   UNSETENV(LICENSE_LOCATION_ENV_VAR);
}
@@ -199,18 +174,13 @@
   UNSETENV(LICENSE_LOCATION_ENV_VAR);
   license::EventRegistry registry;
   EnvironmentVarLocation environmentVarLocation;
   vector<string> licenseInfos = environmentVarLocation.license_locations(
         registry);
   vector<string> licenseInfos = environmentVarLocation.license_locations(registry);
   BOOST_CHECK_MESSAGE(registry.isGood(),
         "No fatal error for now, only warnings");
   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