[libcamera-devel,RFC,7/8] test: Add configuration parser tests
diff mbox series

Message ID 20201123164319.152742-8-kieran.bingham@ideasonboard.com
State Changes Requested
Headers show
Series
  • Configuration files and parsing
Related show

Commit Message

Kieran Bingham Nov. 23, 2020, 4:43 p.m. UTC
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 test/configuration.cpp            | 105 ++++++++++++++++++++++++++++++
 test/data/test_configuration.json |  15 +++++
 test/data/unparsable.json         |   7 ++
 test/meson.build                  |   1 +
 4 files changed, 128 insertions(+)
 create mode 100644 test/configuration.cpp
 create mode 100644 test/data/test_configuration.json
 create mode 100644 test/data/unparsable.json

Patch
diff mbox series

diff --git a/test/configuration.cpp b/test/configuration.cpp
new file mode 100644
index 000000000000..bed70b1b1218
--- /dev/null
+++ b/test/configuration.cpp
@@ -0,0 +1,105 @@ 
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2020, Google Inc.
+ *
+ * configuration.cpp - Configuration parser tests
+ */
+
+#include "libcamera/internal/configuration.h"
+
+#include "test.h"
+
+#include <iostream>
+
+using namespace std;
+using namespace libcamera;
+
+class ConfigurationTest : public Test
+{
+protected:
+	int init()
+	{
+		return TestPass;
+	}
+
+	int run()
+	{
+		Configuration c;
+
+		/* Check access of json data before parsing */
+		if (!c.data().is_null()) {
+			std::cerr << "Empty json was not discarded"
+				  << std::endl;
+			return TestFail;
+		}
+
+		/* Failure tests */
+		int ret = c.open("CantFindMe.json");
+		if (ret != -ENOENT) {
+			std::cerr << "Found an invalid file" << std::endl;
+			return TestFail;
+		}
+
+		/*
+		 * When run from source, Configuration looks in data/
+		 * Find our test data relative to that.
+		 */
+		ret = c.open("../test/data/unparsable.json");
+		if (ret != -EINVAL) {
+			std::cerr << "Unparsable file parsed" << std::endl;
+			return TestFail;
+		}
+
+		/* We expect to find this in the data/ folder of the sources. */
+		ret = c.open("../test/data/test_configuration.json");
+		if (ret != 0) {
+			std::cerr << "Failed to open configuration data"
+				  << std::endl;
+			return TestFail;
+		}
+
+		/* Parse sample data */
+		json j = c.data();
+
+		for (auto &array : j) {
+			std::cout << array << std::endl;
+		}
+
+		for (auto &[key, value] : c.data().items()) {
+			std::cout << key << " : " << value << "\n";
+		}
+
+		std::string cameraName = "Not Found";
+
+		auto it = j.find("device");
+		if (it == j.end()) {
+			std::cerr << "Failed to find device key" << std::endl;
+			return TestFail;
+		}
+
+		std::cout << "Device: " << j["device"] << std::endl;
+		if (j["device"].contains("cameraName"))
+			cameraName = j["device"]["cameraName"];
+
+		for (auto &[key, value] : j["device"].items()) {
+			std::cout << key << " : " << value << "\n";
+		}
+
+		if (cameraName != "Test Camera Name") {
+			std::cerr << "Failed to find expected string" << std::endl;
+			return TestFail;
+		}
+
+		return TestPass;
+	}
+
+	void cleanup()
+	{
+	}
+
+private:
+	std::string fileName_;
+};
+
+TEST_REGISTER(ConfigurationTest)
+
diff --git a/test/data/test_configuration.json b/test/data/test_configuration.json
new file mode 100644
index 000000000000..926a14c23f17
--- /dev/null
+++ b/test/data/test_configuration.json
@@ -0,0 +1,15 @@ 
+{
+  "device": {
+	"cameraName": "Test Camera Name",
+	"manufacturer": "Test Manufacturer"
+  },
+
+  "/base/soc/i2c0mux/i2c@1/imx219@10": {
+	"properties": {
+		"Rotation" : 0,
+		"Location" : 1
+	}
+  },
+
+  "dummy_key": "Sample test string"
+}
diff --git a/test/data/unparsable.json b/test/data/unparsable.json
new file mode 100644
index 000000000000..5cf969511242
--- /dev/null
+++ b/test/data/unparsable.json
@@ -0,0 +1,7 @@ 
+{
+  "\_SB_.PCI0.XHC_.RHUB.HS05-5:1.0-0408:5251": {
+	"properties": {
+		"The ID above is invalidly escaped" : 0,
+		"This will not parse": 1
+	}
+},
diff --git a/test/meson.build b/test/meson.build
index 0a1d434e3996..6b2105295ddf 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -25,6 +25,7 @@  public_tests = [
 internal_tests = [
     ['byte-stream-buffer',              'byte-stream-buffer.cpp'],
     ['camera-sensor',                   'camera-sensor.cpp'],
+    ['configuration',                   'configuration.cpp'],
     ['event',                           'event.cpp'],
     ['event-dispatcher',                'event-dispatcher.cpp'],
     ['event-thread',                    'event-thread.cpp'],