From patchwork Mon Nov 23 16:43:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 10478 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 4FE17BE08A for ; Mon, 23 Nov 2020 16:45:05 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 15E61633F6; Mon, 23 Nov 2020 17:45:05 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Fc2CrJSM"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0E407633F9 for ; Mon, 23 Nov 2020 17:45:00 +0100 (CET) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id AFF9171; Mon, 23 Nov 2020 17:44:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1606149899; bh=AWhmdsy7jYmfQVRscIK7cYnS6iEbSVhlB6iSzv6l2RI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fc2CrJSMFXH8cpRvLAVd6QyRwgipzhGxaZs3I4yos4NufH7ZOH8yRNIV2gZJ6Cq/s 90PI8JiH2oMd/JiETM920dL93RPRW3L9N+FC6zC0/4bRWbcRuBXJEyDZrESQhJjmKX okQYqzKjEh7wJvqU2xMwx9IHgvf7qOdOYm6JMY1c= From: Kieran Bingham To: libcamera devel Date: Mon, 23 Nov 2020 16:43:18 +0000 Message-Id: <20201123164319.152742-8-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201123164319.152742-1-kieran.bingham@ideasonboard.com> References: <20201123164319.152742-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [RFC PATCH 7/8] test: Add configuration parser tests X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Signed-off-by: Kieran Bingham --- 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 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 + +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'],