Fix language tag validation in config.xml parser 37/59237/2
authorTomasz Iwanek <t.iwanek@samsung.com>
Thu, 11 Feb 2016 13:47:11 +0000 (14:47 +0100)
committerTomasz Iwanek <t.iwanek@samsung.com>
Thu, 11 Feb 2016 16:19:34 +0000 (08:19 -0800)
From spec:
 - http://www.w3.org/TR/widgets/#rule-for-finding-a-file-within-a-widget-package-0
fixed:
 - behaviour from point 5.B
 - behaviour from point 6 (so that only locales/${valid_language_tag}/ is searched)

Change-Id: Ifc96d7b7d76578af7337dc4c36ca4aff1b7e7b91

src/manifest_parser/utils/language_tag_validator.cc
src/manifest_parser/utils/language_tag_validator.h
src/wgt_manifest_handlers/widget_config_parser.cc

index 5efb567..d134ef8 100644 (file)
@@ -7,7 +7,6 @@
 #include <boost/algorithm/string.hpp>
 #include <vector>
 
-#include "manifest_parser/utils/logging.h"
 #include "manifest_parser/utils/w3c_languages.h"
 
 namespace ba = boost::algorithm;
@@ -41,11 +40,12 @@ std::string ToUpper(const std::string& input) {
 namespace utils {
 namespace w3c_languages {
 
-bool ValidateLanguageTag(const std::string& tag) {
-  // algorithm based on http://www.w3.org/International/articles/language-tags/
+// algorithm based on http://www.w3.org/International/articles/language-tags/
+bool ValidateLanguageTag(const std::string& tag, std::string* error) {
   std::vector<std::string> splitted_tag;
   if (tag.empty()) {
-    LOG(ERROR) << "tag is empty";
+    if (error)
+      *error = "tag is empty";
     return false;
   }
   boost::split(splitted_tag, tag, boost::is_any_of(kTagDelimiter));
@@ -53,7 +53,8 @@ bool ValidateLanguageTag(const std::string& tag) {
   // main language validation
   if (current_item != splitted_tag.end() &&
       !lang_set::ValidateOnlyLanguage(*current_item)) {
-    LOG(ERROR) << "Invalid main language tag given";
+    if (error)
+      *error = "Invalid main language tag given";
     return false;
   }
   ++current_item;
@@ -68,7 +69,8 @@ bool ValidateLanguageTag(const std::string& tag) {
       if (current_item == splitted_tag.end())
         return true;
     } else {
-      LOG(ERROR) << "Extlang does not match language";
+      if (error)
+        *error = "Extlang does not match language";
       return false;
     }
   }
@@ -93,7 +95,9 @@ bool ValidateLanguageTag(const std::string& tag) {
   }
   // extension private tag validation
   if ((*current_item).size() != kSingletonTagSize) {
-    LOG(ERROR) << "Singletion subtag should be of size " << kSingletonTagSize;
+    if (error)
+      *error = std::string("Singletion subtag should be of size ") +
+        std::to_string(kSingletonTagSize);
     return false;
   }
   ++current_item;
@@ -102,8 +106,9 @@ bool ValidateLanguageTag(const std::string& tag) {
   for (auto it = current_item; it != splitted_tag.end(); ++current_item) {
     auto tag_length = (*current_item).size();
     if (tag_length > kMaximumExtensionTagSize) {
-      LOG(ERROR) << "Any extensions should be maximum "
-                 << kMaximumExtensionTagSize << "characters";
+      if (error)
+        *error = std::string("Any extensions should be maximum ") +
+            std::to_string(kMaximumExtensionTagSize) + "characters";
       return false;
     }
   }
index 0b6eedc..3c6361d 100644 (file)
@@ -18,9 +18,10 @@ namespace w3c_languages {
  *  - http://www.w3.org/International/articles/language-tags/
  *
  * @param language tag string
+ * @param error output parameter for error
  * @return true if tag is correct
  */
-bool ValidateLanguageTag(const std::string& tag);
+bool ValidateLanguageTag(const std::string& tag, std::string* error = nullptr);
 
 }  // namespace w3c_languages
 }  // namespace utils
index ed3f349..1f20b71 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "manifest_parser/manifest_handler.h"
 #include "manifest_parser/utils/iri_util.h"
+#include "manifest_parser/utils/language_tag_validator.h"
 #include "manifest_parser/utils/logging.h"
 #include "wgt_manifest_handlers/account_handler.h"
 #include "wgt_manifest_handlers/app_control_handler.h"
@@ -133,11 +134,11 @@ FindResult FindFileWithinWidget(const bf::path& widget_path,
   std::vector<std::string> path_components;
   ba::split(path_components, content_value, ba::is_any_of("/"));
   if (path_components.size() >= 1 && path_components[0] == kLocaleDirectory) {
-    if (path_components.size() == 1) {
+    if (path_components.size() == 1)
       return FindResult::NUL;
-    }
 
-    // TODO(t.iwanek): validate language tag
+    if (!utils::w3c_languages::ValidateLanguageTag(path_components[1]))
+      return FindResult::NUL;
 
     path_components.erase(path_components.begin(), ++++path_components.begin());
     content_value = ba::join(path_components, "/");
@@ -151,7 +152,8 @@ FindResult FindFileWithinWidget(const bf::path& widget_path,
          iter != bf::directory_iterator(); ++iter) {
       const bf::path& path = *iter;
 
-      // TODO(t.iwanek): validate language tag
+      if (!utils::w3c_languages::ValidateLanguageTag(path.filename().string()))
+        continue;
 
       bf::path candidate = path / content_value;
       if (bf::exists(candidate, error)) {