new file mode 100644
@@ -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)
+
new file mode 100644
@@ -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"
+}
new file mode 100644
@@ -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
+ }
+},
@@ -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'],
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