Config.xml XML Schema validation
authorTomasz Iwanek <t.iwanek@samsung.com>
Mon, 6 May 2013 10:34:01 +0000 (12:34 +0200)
committerGerrit Code Review <gerrit2@kim11>
Wed, 29 May 2013 07:33:13 +0000 (16:33 +0900)
[Issue#]       LINUXWRT-314
[Feature]      Schema validation
[Cause]        N/A
[Solution]     Xml schema validation based on updated schema from related change.
Validation is turned on by SCHEMA_VALIDATION_SUPPORT option (default OFF). This is because of problem with w3c testcases.
[Verification] Build repository. Install some testcases.

Change-Id: Ia8d17c61d1faf96fbdbbc13a810302a39f9c2245

CMakeLists.txt
packaging/wrt-installer.spec
src/configuration_parser/parser_runner.cpp
src/configuration_parser/parser_runner.h
src/jobs/widget_install/task_widget_config.cpp

index b3933c5..666256f 100644 (file)
@@ -40,6 +40,7 @@ OPTION(MULTIPROCESS_SERVICE_SUPPORT "Process per service" OFF)
 OPTION(MULTIPROCESS_SERVICE_SUPPORT_INLINE "Process per service - inline mode support" OFF)
 OPTION(CSP_SUPPORT "Support for csp policy" ON)
 OPTION(ALLOW_NAVIGATION_SUPPORT "Support for allow-navigation" ON)
+OPTION(SCHEMA_VALIDATION_SUPPORT "Support for XML schema validation" OFF)
 
 ############################# compiler flags ##################################
 
@@ -70,6 +71,10 @@ ENDIF(CSP_SUPPORT)
 IF(ALLOW_NAVIGATION_SUPPORT)
     ADD_DEFINITIONS("-DALLOW_NAVIGATION_ENABLED")
 ENDIF(ALLOW_NAVIGATION_SUPPORT)
+IF(SCHEMA_VALIDATION_SUPPORT)
+    MESSAGE(STATUS "XML Schema validation of installed app enabled")
+    ADD_DEFINITIONS("-DSCHEMA_VALIDATION_ENABLED")
+ENDIF(SCHEMA_VALIDATION_SUPPORT)
 
 # If supported for the target machine, emit position-independent code,suitable
 # for dynamic linking and avoiding any limit on the size of the global offset
@@ -99,6 +104,7 @@ SET(TARGET_BACKEND_LIB "wgt")
 ############################# subdirectories ##################################
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(etc)
+ADD_SUBDIRECTORY(configuration)
 
 IF(WITH_TESTS)
     ADD_SUBDIRECTORY(tests)
index 86f88c7..2320d91 100644 (file)
@@ -103,6 +103,7 @@ mkdir -p /opt/share/icons/default/small
 %attr(755,root,root) %{_bindir}/wrt-installer
 %attr(775,root,root) %{_bindir}/wrt_preinstall_widgets.sh
 /usr/etc/package-manager/backendlib/libwgt.so
+%attr(644,root,root) /usr/etc/wrt-installer/*.xsd
 %{_datadir}/license/%{name}
 %{_libdir}/systemd/user/tizen-mobile-session.target.wants/wrt-preinstall-widgets.service
 %{_libdir}/systemd/user/wrt-preinstall-widgets.service
index cff02f7..cdc7caa 100644 (file)
@@ -22,6 +22,8 @@
 #include "parser_runner.h"
 #include "root_parser.h"
 
+#include "stdio.h"
+
 #include <stack>
 #include <libxml/xmlreader.h>
 #include <dpl/binary_queue.h>
 
 class ParserRunner::Impl
 {
+    static void logErrorLibxml2(void *, const char *msg, ...)
+    {
+        char buffer[300];
+        va_list args;
+        va_start(args, msg);
+        vsnprintf(buffer, 300, msg, args);
+        va_end(args);
+        LogError(buffer);
+    }
+
+    static void logWarningLibxml2(void *, const char *msg, ...)
+    {
+        char buffer[300];
+        va_list args;
+        va_start(args, msg);
+        vsnprintf(buffer, 300, msg, args);
+        va_end(args);
+        LogWarning(buffer);
+    }
+
   public:
+    bool Validate(const std::string& filename, const std::string& schema)
+    {
+        int ret = -1;
+        xmlSchemaParserCtxtPtr ctx;
+        xmlSchemaValidCtxtPtr vctx;
+        xmlSchemaPtr xschema;
+        ctx = xmlSchemaNewParserCtxt(schema.c_str());
+        if (ctx == NULL) {
+            LogError("xmlSchemaNewParserCtxt() Failed");
+            return false;
+        }
+        xschema = xmlSchemaParse(ctx);
+        if (xschema == NULL) {
+            LogError("xmlSchemaParse() Failed");
+            return false;
+        }
+        vctx = xmlSchemaNewValidCtxt(xschema);
+        if (vctx == NULL) {
+            LogError("xmlSchemaNewValidCtxt() Failed");
+            return false;
+        }
+        xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc)&logErrorLibxml2, (xmlSchemaValidityWarningFunc) &logWarningLibxml2, NULL);
+        ret = xmlSchemaValidateFile(vctx, filename.c_str(), 0);
+        if (ret == -1) {
+            LogError("xmlSchemaValidateFile() failed");
+            return false;
+        } else if (ret == 0) {
+            LogError("Config is Valid");
+            return true;
+        } else {
+            LogError("Config Validation Failed with error code " << ret);
+            return false;
+        }
+        return true;
+    }
+
     void Parse(const std::string& filename,
                const ElementParserPtr& root)
     {
@@ -385,6 +443,11 @@ ParserRunner::ParserRunner() :
     m_impl(new ParserRunner::Impl())
 {}
 
+bool ParserRunner::Validate(const std::string& filename, const std::string& schema)
+{
+    return m_impl->Validate(filename, schema);
+}
+
 void ParserRunner::Parse(const std::string& filename,
                          ElementParserPtr root)
 {
index 80a3208..c5c0714 100644 (file)
@@ -30,6 +30,8 @@
 class ParserRunner : private DPL::Noncopyable
 {
   public:
+    bool Validate(const std::string& filename, const std::string& schema);
+
     void Parse(const std::string& filename,
                ElementParserPtr root);
     void Parse(DPL::AbstractInput *input,
index 20a24a2..f77a8b7 100755 (executable)
@@ -60,6 +60,8 @@ const char *const DEFAULT_LANGUAGE = "default";
 const char *const WRT_WIDGET_CONFIG_FILE_NAME = "config.xml";
 
 const std::string WINDGET_INSTALL_NETWORK_ACCESS = "network access";
+
+const char WRT_WIDGETS_XML_SCHEMA[] = "/usr/etc/wrt-installer/widgets.xsd";
 }
 
 namespace Jobs {
@@ -723,6 +725,13 @@ bool TaskWidgetConfig::parseConfigurationFileWidget(
     Try
     {
         ParserRunner parser;
+#ifdef SCHEMA_VALIDATION_ENABLED
+        if(!parser.Validate(configFilePath, WRT_WIDGETS_XML_SCHEMA))
+        {
+            LogError("Invalid configuration file - schema validation failed");
+            return false;
+        }
+#endif
         parser.Parse(configFilePath,
                      ElementParserPtr(new RootParser<WidgetParser>(
                                           configInfo,