Browse Source

more tests, data_def.cc is not covered as much as possible

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@710 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
732ace0910

+ 3 - 1
src/lib/cc/cpp/data.h

@@ -96,7 +96,9 @@ protected:
     Element(int t) { type = t; }
 
 public:
-    enum types { integer, real, boolean, string, list, map };
+    // any is a special type used in list specifications, specifying
+    // that the elements can be of any type
+    enum types { integer, real, boolean, string, list, map, any };
     // base class; make dtor virtual
     virtual ~Element() {};
 

+ 1 - 0
src/lib/cc/cpp/data_unittests.cc

@@ -61,6 +61,7 @@ TEST(Element, from_and_to_str) {
     sv.push_back("\"asdf\"");
     sv.push_back("[ 1, 2, 3, 4 ]");
     sv.push_back("{\"name\": \"foo\", \"value\": 47806}");
+    sv.push_back("[ {\"a\": 1, \"b\": \"c\"}, {\"a\": 2, \"b\": \"d\"} ]");
 
     BOOST_FOREACH(std::string s, sv) {
         // also test << operator, which uses Element::str()

+ 12 - 7
src/lib/config/cpp/data_def.cc

@@ -48,6 +48,8 @@ getType_value(const std::string& type_name) {
         return Element::list;
     } else if (type_name == "map") {
         return Element::map;
+    } else if (type_name == "any") {
+        return Element::any;
     } else {
         throw DataDefinitionError(type_name + " is not a valid type name");
     }
@@ -97,7 +99,6 @@ check_config_item(const ElementPtr& spec) {
 static void
 check_config_item_list(const ElementPtr& spec) {
     if (spec->getType() != Element::list) {
-        std::cout << "[XX] ERROR IN: " << spec << std::endl;
         throw DataDefinitionError("config_data is not a list of elements");
     }
     BOOST_FOREACH(ElementPtr item, spec->listValue()) {
@@ -210,17 +211,23 @@ check_type(ElementPtr spec, ElementPtr element)
 
 bool
 DataDefinition::validate_item(const ElementPtr spec, const ElementPtr data) {
-    std::cout << "Validating type of " << data << std::endl;
     if (!check_type(spec, data)) {
-        std::cout << "type mismatch; not " << spec->get("item_type") << ": " << data << std::endl;
-        std::cout << spec << std::endl;
+        // we should do some proper error feedback here
+        // std::cout << "type mismatch; not " << spec->get("item_type") << ": " << data << std::endl;
+        // std::cout << spec << std::endl;
         return false;
     }
     if (data->getType() == Element::list) {
+        ElementPtr list_spec = spec->get("list_item_spec");
         BOOST_FOREACH(ElementPtr list_el, data->listValue()) {
-            if (!validate_spec(spec->get("list_item_spec"), list_el)) {
+            if (!check_type(list_spec, list_el)) {
                 return false;
             }
+            if (list_spec->get("item_type")->stringValue() == "map") {
+                if (!validate_item(list_spec, list_el)) {
+                    return false;
+                }
+            }
         }
     }
     if (data->getType() == Element::map) {
@@ -238,7 +245,6 @@ DataDefinition::validate_spec(const ElementPtr spec, const ElementPtr data) {
     bool optional = spec->get("item_optional")->boolValue();
     ElementPtr data_el;
     
-    std::cout << "check for item with name " << item_name << std::endl;
     data_el = data->get(item_name);
     if (data_el) {
         if (!validate_item(spec, data_el)) {
@@ -246,7 +252,6 @@ DataDefinition::validate_spec(const ElementPtr spec, const ElementPtr data) {
         }
     } else {
         if (!optional) {
-            std::cout << "non-optional value not found" << std::endl;
             return false;
         }
     }

+ 76 - 1
src/lib/config/cpp/data_def_unittests.cc

@@ -18,6 +18,8 @@
 
 #include <data_def.h>
 
+#include <fstream>
+
 #include "data_def_unittests_config.h"
 
 using namespace isc::data;
@@ -41,7 +43,7 @@ data_def_error(const std::string& file,
     }
 }
 
-TEST(DataDefinition, Specfiles) {
+TEST(DataDefinition, ReadingSpecfiles) {
     // Tests whether we can open specfiles and if we get the
     // right parse errors
     DataDefinition dd = DataDefinition(specfile("spec1.spec"));
@@ -55,6 +57,16 @@ TEST(DataDefinition, Specfiles) {
                    "Error opening ",
                    specfile("doesnotexist"),
                    ": No such file or directory");
+
+    std::ifstream file;
+    file.open(specfile("spec1.spec").c_str());
+    dd = DataDefinition(file);
+    EXPECT_EQ(dd.getDefinition()->get("data_specification")
+                                ->get("module_name")
+                                ->stringValue(), "Spec1");
+}
+
+TEST(DataDefinition, SpecfileItems) {
     data_def_error("spec3.spec",
                    "item_name missing in {\"item_default\": 1, \"item_optional\": False, \"item_type\": \"integer\"}");
     data_def_error("spec4.spec",
@@ -63,8 +75,71 @@ TEST(DataDefinition, Specfiles) {
                    "item_optional missing in {\"item_default\": 1, \"item_name\": \"item1\", \"item_type\": \"integer\"}");
     data_def_error("spec6.spec",
                    "item_default missing in {\"item_name\": \"item1\", \"item_optional\": False, \"item_type\": \"integer\"}");
+    data_def_error("spec9.spec",
+                   "item_default not of type integer");
+    data_def_error("spec10.spec",
+                   "item_default not of type real");
+    data_def_error("spec11.spec",
+                   "item_default not of type boolean");
+    data_def_error("spec12.spec",
+                   "item_default not of type string");
+    data_def_error("spec13.spec",
+                   "item_default not of type list");
+    data_def_error("spec14.spec",
+                   "item_default not of type map");
+    data_def_error("spec15.spec",
+                   "badname is not a valid type name");
+}
+
+TEST(DataDefinition, SpecfileConfigData)
+{
     data_def_error("spec7.spec",
                    "module_name missing in {}");
     data_def_error("spec8.spec",
                    "Data specification does not contain data_specification element");
+    data_def_error("spec16.spec",
+                   "config_data is not a list of elements");
+    data_def_error("spec21.spec",
+                   "commands is not a list of elements");
+}
+
+TEST(DataDefinition, SpecfileCommands)
+{
+    data_def_error("spec17.spec",
+                   "command_name missing in {\"command_args\": [ {\"item_default\": \"\", \"item_name\": \"message\", \"item_optional\": False, \"item_type\": \"string\"} ], \"command_description\": \"Print the given message to stdout\"}");
+    data_def_error("spec18.spec",
+                   "command_args missing in {\"command_description\": \"Print the given message to stdout\", \"command_name\": \"print_message\"}");
+    data_def_error("spec19.spec",
+                   "command_args not of type list");
+    data_def_error("spec20.spec",
+                   "somethingbad is not a valid type name");
+/*
+    data_def_error("spec22.spec",
+                   "");
+*/
+}
+
+bool
+data_test(DataDefinition dd, const std::string& data_file_name)
+{
+    std::ifstream data_file;
+
+    data_file.open(specfile(data_file_name).c_str());
+    ElementPtr data = Element::createFromString(data_file, data_file_name);
+    data_file.close();
+
+    return dd.validate(data);
+}
+
+TEST(DataDefinition, DataValidation) {
+    DataDefinition dd = DataDefinition(specfile("spec22.spec"));
+
+    EXPECT_TRUE(data_test(dd, "data22_1.data"));
+    EXPECT_FALSE(data_test(dd, "data22_2.data"));
+    EXPECT_FALSE(data_test(dd, "data22_3.data"));
+    EXPECT_FALSE(data_test(dd, "data22_4.data"));
+    EXPECT_FALSE(data_test(dd, "data22_5.data"));
+    EXPECT_TRUE(data_test(dd, "data22_6.data"));
+    EXPECT_TRUE(data_test(dd, "data22_7.data"));
+    EXPECT_FALSE(data_test(dd, "data22_8.data"));
 }

+ 8 - 0
src/lib/config/testdata/data22_1.data

@@ -0,0 +1,8 @@
+{
+    "value1": 1,
+    "value2": 2.3,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, 3 ],
+    "value6": { "v61": "bar", "v62": True }
+}

+ 8 - 0
src/lib/config/testdata/data22_2.data

@@ -0,0 +1,8 @@
+{
+    "value1": "asdf",
+    "value2": 2.3,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, 3 ],
+    "value6": { "v61": "bar", "v62": True }
+}

+ 8 - 0
src/lib/config/testdata/data22_3.data

@@ -0,0 +1,8 @@
+{
+    "value1": 1,
+    "value2": False,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, 3 ],
+    "value6": { "v61": "bar", "v62": True }
+}

+ 8 - 0
src/lib/config/testdata/data22_4.data

@@ -0,0 +1,8 @@
+{
+    "value1": 1,
+    "value2": 2.3,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, "a" ],
+    "value6": { "v61": "bar", "v62": True }
+}

+ 8 - 0
src/lib/config/testdata/data22_5.data

@@ -0,0 +1,8 @@
+{
+    "value1": 1,
+    "value2": 2.3,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, 3 ],
+    "value6": { "v61": "bar", "v62": "Break" }
+}

+ 9 - 0
src/lib/config/testdata/data22_6.data

@@ -0,0 +1,9 @@
+{
+    "value1": 1,
+    "value2": 2.3,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, 3 ],
+    "value6": { "v61": "bar", "v62": True },
+    "value7": [ 1, 2.2, "str", True ]
+}

+ 9 - 0
src/lib/config/testdata/data22_7.data

@@ -0,0 +1,9 @@
+{
+    "value1": 1,
+    "value2": 2.3,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, 3 ],
+    "value6": { "v61": "bar", "v62": True },
+    "value8": [ { "a": "d" }, { "a": "e" } ]
+}

+ 9 - 0
src/lib/config/testdata/data22_8.data

@@ -0,0 +1,9 @@
+{
+    "value1": 1,
+    "value2": 2.3,
+    "value3": True,
+    "value4": "foo",
+    "value5": [ 1, 2, 3 ],
+    "value6": { "v61": "bar", "v62": True },
+    "value8": [ { "a": "d" }, { "a": 1 } ]
+}

+ 13 - 0
src/lib/config/testdata/spec10.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "item1",
+        "item_type": "real",
+        "item_optional": False,
+        "item_default": 1
+      }
+    ]
+  }
+}
+

+ 13 - 0
src/lib/config/testdata/spec11.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "item1",
+        "item_type": "boolean",
+        "item_optional": False,
+        "item_default": 1
+      }
+    ]
+  }
+}
+

+ 13 - 0
src/lib/config/testdata/spec12.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "item1",
+        "item_type": "string",
+        "item_optional": False,
+        "item_default": 1
+      }
+    ]
+  }
+}
+

+ 13 - 0
src/lib/config/testdata/spec13.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "item1",
+        "item_type": "list",
+        "item_optional": False,
+        "item_default": 1
+      }
+    ]
+  }
+}
+

+ 13 - 0
src/lib/config/testdata/spec14.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "item1",
+        "item_type": "map",
+        "item_optional": False,
+        "item_default": 1
+      }
+    ]
+  }
+}
+

+ 13 - 0
src/lib/config/testdata/spec15.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "item1",
+        "item_type": "badname",
+        "item_optional": False,
+        "item_default": 1
+      }
+    ]
+  }
+}
+

+ 7 - 0
src/lib/config/testdata/spec16.spec

@@ -0,0 +1,7 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": 1
+  }
+}
+

+ 17 - 0
src/lib/config/testdata/spec17.spec

@@ -0,0 +1,17 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "commands": [
+      {
+        "command_description": "Print the given message to stdout",
+        "command_args": [ {
+          "item_name": "message",
+          "item_type": "string",
+          "item_optional": False,
+          "item_default": ""
+        } ]
+      }
+    ]
+  }
+}
+

+ 12 - 0
src/lib/config/testdata/spec18.spec

@@ -0,0 +1,12 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "commands": [
+      {
+        "command_name": "print_message",
+        "command_description": "Print the given message to stdout"
+      }
+    ]
+  }
+}
+

+ 13 - 0
src/lib/config/testdata/spec19.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "commands": [
+      {
+        "command_name": "print_message",
+        "command_description": "Print the given message to stdout",
+        "command_args": 1
+      }
+    ]
+  }
+}
+

+ 18 - 0
src/lib/config/testdata/spec20.spec

@@ -0,0 +1,18 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "commands": [
+      {
+        "command_name": "print_message",
+        "command_description": "Print the given message to stdout",
+        "command_args": [ {
+          "item_name": "message",
+          "item_type": "somethingbad",
+          "item_optional": False,
+          "item_default": ""
+        } ]
+      }
+    ]
+  }
+}
+

+ 7 - 0
src/lib/config/testdata/spec21.spec

@@ -0,0 +1,7 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "commands": 1
+  }
+}
+

+ 84 - 0
src/lib/config/testdata/spec22.spec

@@ -0,0 +1,84 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "value1",
+        "item_type": "integer",
+        "item_optional": False,
+        "item_default": 9
+      },
+      { "item_name": "value2",
+        "item_type": "real",
+        "item_optional": False,
+        "item_default": 9.9
+      },
+      { "item_name": "value3",
+        "item_type": "boolean",
+        "item_optional": False,
+        "item_default": False
+      },
+      { "item_name": "value4",
+        "item_type": "string",
+        "item_optional": False,
+        "item_default": "default_string"
+      },
+      { "item_name": "value5",
+        "item_type": "list",
+        "item_optional": False,
+        "item_default": [ ],
+        "list_item_spec": {
+          "item_name": "list_element",
+          "item_type": "integer",
+          "item_optional": False,
+          "item_default": 8
+        }
+      },
+      { "item_name": "value6",
+        "item_type": "map",
+        "item_optional": False,
+        "item_default": {},
+        "map_item_spec": [
+          { "item_name": "v61",
+            "item_type": "string",
+            "item_optional": False,
+            "item_default": "def"
+          },
+          { "item_name": "v62",
+            "item_type": "boolean",
+            "item_optional": False,
+            "item_default": False
+          }
+        ]
+      },
+      { "item_name": "value7",
+        "item_type": "list",
+        "item_optional": True,
+        "item_default": [ ],
+        "list_item_spec": {
+          "item_name": "list_element",
+          "item_type": "any",
+          "item_optional": True
+        }
+      },
+      { "item_name": "value8",
+        "item_type": "list",
+        "item_optional": True,
+        "item_default": [ ],
+        "list_item_spec": {
+          "item_name": "list_element",
+          "item_type": "map",
+          "item_optional": True,
+          "item_default": { "a": "b" },
+          "map_item_spec": [
+            { "item_name": "a",
+              "item_type": "string",
+              "item_optional": True,
+              "item_default": "empty"
+            }
+          ]
+        }
+      },
+    ]
+  }
+}
+

+ 18 - 0
src/lib/config/testdata/spec23.spec

@@ -0,0 +1,18 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "commands": [
+      {
+        "command_name": "print_message",
+        "command_description": "Print the given message to stdout",
+        "command_args": [ {
+          "item_name": "message",
+          "item_type": "string",
+          "item_optional": False,
+          "item_default": ""
+        } ]
+      }
+    ]
+  }
+}
+

+ 13 - 0
src/lib/config/testdata/spec9.spec

@@ -0,0 +1,13 @@
+{
+  "data_specification": {
+    "module_name": "Spec2",
+    "config_data": [
+      { "item_name": "item1",
+        "item_type": "integer",
+        "item_optional": False,
+        "item_default": "asdf"
+      }
+    ]
+  }
+}
+