Bug 543710 - Logging for XUL template based on storage; The patch to commit.
authorLaurent Jouanneau <laurent@xulfr.org>
Tue, 09 Mar 2010 01:02:27 +0100
changeset 39127 1cb03235231e439eda4fb5e6f78d53f95a120650
parent 39126 93594759cfb8cabd446884aa5051c3cd0e148f75
child 39128 0905374b063d0980816b38fc8298186983c048e1
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs543710
milestone1.9.3a3pre
Bug 543710 - Logging for XUL template based on storage; The patch to commit. r=enndeakin sr=neil.
content/xul/templates/src/nsXULContentUtils.h
content/xul/templates/src/nsXULTemplateQueryProcessorStorage.cpp
content/xul/templates/tests/chrome/Makefile.in
content/xul/templates/tests/chrome/test_tmpl_storage_bad_parameters.xul
content/xul/templates/tests/chrome/test_tmpl_storage_bad_parameters_2.xul
content/xul/templates/tests/chrome/test_tmpl_storage_bad_parameters_3.xul
content/xul/templates/tests/chrome/test_tmpl_storage_baddatasource.xul
content/xul/templates/tests/chrome/test_tmpl_storage_badquery.xul
--- a/content/xul/templates/src/nsXULContentUtils.h
+++ b/content/xul/templates/src/nsXULContentUtils.h
@@ -101,16 +101,28 @@ class nsICollation;
 #define ERROR_TEMPLATE_TRIPLE_UNBOUND                                   \
         "neither subject or object variables of <triple> has a value"
 #define ERROR_TEMPLATE_BAD_XPATH                                        \
         "XPath expression in query could not be parsed"
 #define ERROR_TEMPLATE_BAD_ASSIGN_XPATH                                 \
         "XPath expression in <assign> could not be parsed"
 #define ERROR_TEMPLATE_BAD_BINDING_XPATH                                \
         "XPath expression in <binding> could not be parsed"
+#define ERROR_TEMPLATE_STORAGE_BAD_URI                                  \
+        "only profile: or file URI are allowed"
+#define ERROR_TEMPLATE_STORAGE_CANNOT_OPEN_DATABASE                     \
+        "cannot open given database"
+#define ERROR_TEMPLATE_STORAGE_BAD_QUERY                                \
+        "syntax error in the SQL query"
+#define ERROR_TEMPLATE_STORAGE_UNKNOWN_QUERY_PARAMETER                   \
+        "the given named parameter is unknown in the SQL query"
+#define ERROR_TEMPLATE_STORAGE_WRONG_TYPE_QUERY_PARAMETER               \
+        "the type of a query parameter is wrong"
+#define ERROR_TEMPLATE_STORAGE_QUERY_PARAMETER_NOT_BOUND                \
+        "a query parameter cannot be bound to the SQL query"
 
 class nsXULContentUtils
 {
 protected:
     static nsIRDFService* gRDF;
     static nsIDateTimeFormat* gFormat;
     static nsICollation *gCollation;
 
--- a/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.cpp
+++ b/content/xul/templates/src/nsXULTemplateQueryProcessorStorage.cpp
@@ -48,16 +48,17 @@
 #include "nsIIOService.h"
 #include "nsIFileChannel.h"
 #include "nsIFile.h"
 #include "nsGkAtoms.h"
 #include "nsContentUtils.h"
 
 #include "nsXULTemplateBuilder.h"
 #include "nsXULTemplateResultStorage.h"
+#include "nsXULContentUtils.h"
 
 #include "mozIStorageService.h"
 
 //----------------------------------------------------------------------
 //
 // nsXULTemplateResultSetStorage
 //
 
@@ -243,27 +244,33 @@ nsXULTemplateQueryProcessorStorage::GetD
         nsCOMPtr<nsIIOService> ioservice =
             do_GetService("@mozilla.org/network/io-service;1", &rv);
         NS_ENSURE_SUCCESS(rv, rv);
 
         rv = ioservice->NewChannelFromURI(uri, getter_AddRefs(channel));
         NS_ENSURE_SUCCESS(rv, rv);
 
         nsCOMPtr<nsIFileChannel> fileChannel = do_QueryInterface(channel, &rv);
-        NS_ENSURE_SUCCESS(rv, rv); // if it fails, not a file url
+        if (NS_FAILED(rv)) { // if it fails, not a file url
+            nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_BAD_URI);
+            return rv;
+        }
 
         nsCOMPtr<nsIFile> file;
         rv = fileChannel->GetFile(getter_AddRefs(databaseFile));
         NS_ENSURE_SUCCESS(rv, rv);
     }
 
     // ok now we have an URI of a sqlite file
     nsCOMPtr<mozIStorageConnection> connection;
     rv = storage->OpenDatabase(databaseFile, getter_AddRefs(connection));
-    NS_ENSURE_SUCCESS(rv, rv);
+    if (NS_FAILED(rv)) {
+        nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_CANNOT_OPEN_DATABASE);
+        return rv;
+    }
 
     NS_ADDREF(*aReturn = connection);
     return NS_OK;
 }
 
 
 
 NS_IMETHODIMP
@@ -309,17 +316,20 @@ nsXULTemplateQueryProcessorStorage::Comp
     nsCOMPtr<nsIContent> queryContent = do_QueryInterface(aQueryNode);
     nsAutoString sqlQuery;
 
     // Let's get all text nodes (which should be the query) 
     nsContentUtils::GetNodeTextContent(queryContent, PR_FALSE, sqlQuery);
 
     nsresult rv = mStorageConnection->CreateStatement(NS_ConvertUTF16toUTF8(sqlQuery),
                                                               getter_AddRefs(statement));
-    NS_ENSURE_SUCCESS(rv, rv);
+    if (NS_FAILED(rv)) {
+        nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_BAD_QUERY);
+        return rv;
+    }
 
     PRUint32 parameterCount = 0;
     PRUint32 count = queryContent->GetChildCount();
 
     for (PRUint32 i = 0; i < count; ++i) {
         nsIContent *child = queryContent->GetChildAt(i);
 
         if (child->NodeInfo()->Equals(nsGkAtoms::param, kNameSpaceID_XUL)) {
@@ -327,33 +337,37 @@ nsXULTemplateQueryProcessorStorage::Comp
             nsContentUtils::GetNodeTextContent(child, PR_FALSE, value);
 
             PRUint32 index = parameterCount;
             nsAutoString name, indexValue;
 
             if (child->GetAttr(kNameSpaceID_None, nsGkAtoms::name, name)) {
                 rv = statement->GetParameterIndex(NS_ConvertUTF16toUTF8(name),
                                                   &index);
-                NS_ENSURE_SUCCESS(rv, rv);
+                if (NS_FAILED(rv)) {
+                    nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_UNKNOWN_QUERY_PARAMETER);
+                    return rv;
+                }
                 parameterCount++;
             }
             else if (child->GetAttr(kNameSpaceID_None, nsGkAtoms::index, indexValue)) {
                 PR_sscanf(NS_ConvertUTF16toUTF8(indexValue).get(),"%d",&index);
                 if (index > 0)
                     index--;
             }
             else {
                 parameterCount++;
             }
 
             static nsIContent::AttrValuesArray sTypeValues[] =
                 { &nsGkAtoms::int32, &nsGkAtoms::integer, &nsGkAtoms::int64,
                   &nsGkAtoms::null, &nsGkAtoms::double_, &nsGkAtoms::string, nsnull };
 
-            PRInt32 typeError, typeValue = child->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type,
+            PRInt32 typeError = 1;
+            PRInt32 typeValue = child->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type,
                                                        sTypeValues, eCaseMatters);
             rv = NS_ERROR_ILLEGAL_VALUE;
             PRInt32 valInt32 = 0;
             PRInt64 valInt64 = 0;
             PRFloat64 valFloat = 0;
 
             switch (typeValue) {
               case 0:
@@ -374,18 +388,29 @@ nsXULTemplateQueryProcessorStorage::Comp
                 typeError = PR_sscanf(NS_ConvertUTF16toUTF8(value).get(),"%lf",&valFloat);
                 if (typeError > 0)
                     rv = statement->BindDoubleParameter(index, valFloat);
                 break;
               case 5:
               case nsIContent::ATTR_MISSING:
                 rv = statement->BindStringParameter(index, value);
                 break;
+              default:
+                typeError = 0;
             }
-            NS_ENSURE_SUCCESS(rv, rv);
+
+            if (typeError <= 0) {
+                nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_WRONG_TYPE_QUERY_PARAMETER);
+                return rv;
+            }
+
+            if (NS_FAILED(rv)) {
+                nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_STORAGE_QUERY_PARAMETER_NOT_BOUND);
+                return rv;
+            }
         }
     }
 
     *aReturn = statement;
     NS_IF_ADDREF(*aReturn);
 
     return NS_OK;
 }
--- a/content/xul/templates/tests/chrome/Makefile.in
+++ b/content/xul/templates/tests/chrome/Makefile.in
@@ -154,16 +154,19 @@ include $(topsrcdir)/config/rules.mk
 		test_tmpl_sorttwovariablesdescendingquerysyntax.xul \
 		test_tmpl_sortresource2descendingsimplesyntax.xul \
 		test_tmpl_sortascendingtworulesquerysyntax.xul \
 		test_tmpl_sortascendingtworuleswithcontainerquerysyntax.xul \
 		test_tmpl_sortascendingtworuleswithdifferentcontainerquerysyntax.xul \
 		test_tmpl_sortquerymemberandtwotriples.xul \
 		test_tmpl_storage_baddatasource.xul \
 		test_tmpl_storage_badquery.xul \
+		test_tmpl_storage_bad_parameters.xul \
+		test_tmpl_storage_bad_parameters_2.xul \
+		test_tmpl_storage_bad_parameters_3.xul \
 		test_tmpl_storage_dynamicparameters.xul \
 		test_tmpl_storage_listbox.xul \
 		test_tmpl_storage_multiqueries.xul \
 		test_tmpl_storage_parameters.xul \
 		test_tmpl_storage_rule.xul \
 		test_tmpl_storage_simple.xul \
 		test_tmpl_storage_sortintegerasc.xul \
 		test_tmpl_storage_sortintegerdesc.xul \
new file mode 100644
--- /dev/null
+++ b/content/xul/templates/tests/chrome/test_tmpl_storage_bad_parameters.xul
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
+
+<!--
+    storage listbox with bad query parameters
+-->
+
+<window title="XUL Template Tests" width="500" height="600"
+        onload="test_template();"
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript"
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+
+  <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
+
+<script src="templates_shared.js"/>
+
+<script>
+<![CDATA[
+SimpleTest.waitForExplicitFinish();
+
+var testid ="storage listbox with bad query parameters";
+var queryType = "storage";
+var isTreeBuilder = false;
+var needsOpen = false;
+var notWorkingYet = false;
+var notWorkingYetDynamic = false;
+var expectedOutput = <output></output>;
+
+
+Components.classes["@mozilla.org/consoleservice;1"]
+          .getService(Components.interfaces.nsIConsoleService)
+          .reset();
+
+expectedConsoleMessages.push("Error parsing template: the given named parameter is unknown in the SQL query");
+
+var changes = [];
+]]>
+</script>
+
+<listbox  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="root" 
+              flex="1"  datasources="animals.sqlite" ref="." querytype="storage">
+    <template>
+        <query>
+            SELECT * FROM animals WHERE species_id = ? ORDER BY name
+            <param name="bar">2</param>
+        </query>
+        <action>
+            <listitem uri="?" label="?name"/>
+        </action>
+    </template>
+</listbox>
+
+</window>
new file mode 100644
--- /dev/null
+++ b/content/xul/templates/tests/chrome/test_tmpl_storage_bad_parameters_2.xul
@@ -0,0 +1,57 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
+
+<!--
+    storage listbox with bad query parameters
+-->
+
+<window title="XUL Template Tests" width="500" height="600"
+        onload="test_template();"
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript"
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+
+  <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
+
+<script src="templates_shared.js"/>
+
+<script>
+<![CDATA[
+SimpleTest.waitForExplicitFinish();
+
+var testid ="storage listbox with bad query parameters";
+var queryType = "storage";
+var isTreeBuilder = false;
+var needsOpen = false;
+var notWorkingYet = false;
+var notWorkingYetDynamic = false;
+var expectedOutput = <output></output>;
+
+
+Components.classes["@mozilla.org/consoleservice;1"]
+          .getService(Components.interfaces.nsIConsoleService)
+          .reset();
+
+expectedConsoleMessages.push("Error parsing template: the type of a query parameter is wrong");
+
+var changes = [];
+]]>
+</script>
+
+<listbox  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="root" 
+              flex="1"  datasources="animals.sqlite" ref="." querytype="storage">
+    <template>
+        <query>
+            SELECT * FROM animals WHERE species_id = ? ORDER BY name
+            <param type="mysupertype">2</param>
+        </query>
+        <action>
+            <listitem uri="?" label="?name"/>
+        </action>
+    </template>
+</listbox>
+
+</window>
new file mode 100644
--- /dev/null
+++ b/content/xul/templates/tests/chrome/test_tmpl_storage_bad_parameters_3.xul
@@ -0,0 +1,58 @@
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
+
+<!--
+    storage listbox with bad query parameters
+-->
+
+<window title="XUL Template Tests" width="500" height="600"
+        onload="test_template();"
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript"
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+
+  <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>
+
+<script src="templates_shared.js"/>
+
+<script>
+<![CDATA[
+SimpleTest.waitForExplicitFinish();
+
+var testid ="storage listbox with bad query parameters";
+var queryType = "storage";
+var isTreeBuilder = false;
+var needsOpen = false;
+var notWorkingYet = false;
+var notWorkingYetDynamic = false;
+var expectedOutput = <output></output>;
+
+
+Components.classes["@mozilla.org/consoleservice;1"]
+          .getService(Components.interfaces.nsIConsoleService)
+          .reset();
+
+expectedConsoleMessages.push("Error parsing template: a query parameter cannot be bound to the SQL query");
+
+var changes = [];
+]]>
+</script>
+
+<listbox  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="root" 
+              flex="1"  datasources="animals.sqlite" ref="." querytype="storage">
+    <template>
+        <query>
+                SELECT * FROM animals WHERE species_id = :spec ORDER BY name
+            <param name="spec" type="int32">5</param>
+            <param>L%</param>
+        </query>
+        <action>
+            <listitem uri="?" label="?name"/>
+        </action>
+    </template>
+</listbox>
+
+</window>
--- a/content/xul/templates/tests/chrome/test_tmpl_storage_baddatasource.xul
+++ b/content/xul/templates/tests/chrome/test_tmpl_storage_baddatasource.xul
@@ -25,23 +25,30 @@ SimpleTest.waitForExplicitFinish();
 var testid ="storage bad datasource";
 var queryType = "storage";
 var isTreeBuilder = false;
 var needsOpen = false;
 var notWorkingYet = false;
 var notWorkingYetDynamic = false;
 var expectedOutput =<output></output>;
 
+Components.classes["@mozilla.org/consoleservice;1"]
+          .getService(Components.interfaces.nsIConsoleService)
+          .reset();
+
+expectedConsoleMessages.push("Error parsing template: only profile: or file URI are allowed");
+
+
 var changes = [];
 ]]>
 </script>
 
 <listbox  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="root" 
               flex="1" rows="8"
-              datasources="animalsqsdqsdqsdq.sqlite" ref="." querytype="storage">
+              datasources="http://foo.local/animals.sqlite" ref="." querytype="storage">
     <template>
         <query>
             SELECT * FROM animals WHERE species_id = 2 ORDER BY name
         </query>
         <action>
             <listitem uri="?" label="?name"  />
         </action>
     </template>
--- a/content/xul/templates/tests/chrome/test_tmpl_storage_badquery.xul
+++ b/content/xul/templates/tests/chrome/test_tmpl_storage_badquery.xul
@@ -25,16 +25,23 @@ SimpleTest.waitForExplicitFinish();
 var testid ="storage bad query";
 var queryType = "storage";
 var isTreeBuilder = false;
 var needsOpen = false;
 var notWorkingYet = false;
 var notWorkingYetDynamic = false;
 var expectedOutput =<output></output>;
 
+Components.classes["@mozilla.org/consoleservice;1"]
+          .getService(Components.interfaces.nsIConsoleService)
+          .reset();
+
+expectedConsoleMessages.push("Error parsing template: syntax error in the SQL query");
+
+
 var changes = [];
 ]]>
 </script>
 
 <listbox  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="root" 
               flex="1" rows="8"
               datasources="animals.sqlite" ref="." querytype="storage">
     <template>