/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2022, Google Inc. * * yaml-parser.cpp - YAML parser operations tests */ #include #include #include #include #include #include #include #include "libcamera/internal/yaml_parser.h" #include "test.h" using namespace libcamera; using namespace std; static const string testYaml = "string: libcamera\n" "double: 3.14159\n" "int8_t: -100\n" "uint8_t: 100\n" "int16_t: -1000\n" "uint16_t: 1000\n" "int32_t: -100000\n" "uint32_t: 100000\n" "size: [1920, 1080]\n" "list:\n" " - James\n" " - Mary\n" "dictionary:\n" " a: 1\n" " c: 3\n" " b: 2\n" "level1:\n" " level2:\n" " - [1, 2]\n" " - {one: 1, two: 2}\n"; static const string invalidYaml = "Invalid : - YAML : - Content"; class YamlParserTest : public Test { protected: bool createFile(const string &content, string &filename) { filename = "/tmp/libcamera.test.XXXXXX"; int fd = mkstemp(&filename.front()); if (fd == -1) return false; int ret = write(fd, content.c_str(), content.size()); close(fd); if (ret != static_cast(content.size())) return false; return true; } int init() { if (!createFile(testYaml, testYamlFile_)) return TestFail; if (!createFile(invalidYaml, invalidYamlFile_)) return TestFail; return TestPass; } enum class Type { String, Int8, UInt8, Int16, UInt16, Int32, UInt32, Double, Size, List, Dictionary, }; int testObjectType(const YamlObject &obj, const char *name, Type type) { bool isList = type == Type::List || type == Type::Size; bool isScalar = !isList && type != Type::Dictionary; bool isInteger8 = type == Type::Int8 || type == Type::UInt8; bool isInteger16 = type == Type::Int16 || type == Type::UInt16; bool isInteger32 = type == Type::Int32 || type == Type::UInt32; bool isIntegerUpTo16 = isInteger8 || isInteger16; bool isIntegerUpTo32 = isIntegerUpTo16 || isInteger32; bool isSigned = type == Type::Int8 || type == Type::Int16 || type == Type::Int32; if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) { std::cerr << "Object " << name << " type mismatch when compared to " << "value" << std::endl; return TestFail; } if ((isList && !obj.isList()) || (!isList && obj.isList())) { std::cerr << "Object " << name << " type mismatch when compared to " << "list" << std::endl; return TestFail; } if ((type == Type::Dictionary && !obj.isDictionary()) || (type != Type::Dictionary && obj.isDictionary())) { std::cerr << "Object " << name << " type mismatch when compared to " << "dictionary" << std::endl; return TestFail; } if (!isScalar && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "string" << std::endl; return TestFail; } if (!isInteger8 && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "int8_t" << std::endl; return TestFail; } if ((!isInteger8 || isSigned) && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "uint8_t" << std::endl; return TestFail; } if (!isIntegerUpTo16 && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "int16_t" << std::endl; return TestFail; } if ((!isIntegerUpTo16 || isSigned) && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "uint16_t" << std::endl; return TestFail; } if (!isIntegerUpTo32 && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "int32_t" << std::endl; return TestFail; } if ((!isIntegerUpTo32 || isSigned) && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "uint32_t" << std::endl; return TestFail; } if (!isIntegerUpTo32 && type != Type::Double && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "double" << std::endl; return TestFail; } if (type != Type::Size && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " << "Size" << std::endl; return TestFail; } return TestPass; } int testIntegerObject(const YamlObject &obj, const char *name, Type type, int64_t value) { uint64_t unsignedValue = static_cast(value); std::string strValue = std::to_string(value); bool isInteger8 = type == Type::Int8 || type == Type::UInt8; bool isInteger16 = type == Type::Int16 || type == Type::UInt16; bool isSigned = type == Type::Int8 || type == Type::Int16 || type == Type::Int32; /* All integers can be parsed as strings or double. */ if (obj.get().value_or("") != strValue || obj.get("") != strValue) { std::cerr << "Object " << name << " failed to parse as " << "string" << std::endl; return TestFail; } if (obj.get().value_or(0.0) != value || obj.get(0.0) != value) { std::cerr << "Object " << name << " failed to parse as " << "double" << std::endl; return TestFail; } if (isInteger8) { if (obj.get().value_or(0) != value || obj.get(0) != value) { std::cerr << "Object " << name << " failed to parse as " << "int8_t" << std::endl; return TestFail; } } if (isInteger8 && !isSigned) { if (obj.get().value_or(0) != unsignedValue || obj.get(0) != unsignedValue) { std::cerr << "Object " << name << " failed to parse as " << "uint8_t" << std::endl; return TestFail; } } if (isInteger8 || isInteger16) { if (obj.get().value_or(0) != value || obj.get(0) != value) { std::cerr << "Object " << name << " failed to parse as " << "int16_t" << std::endl; return TestFail; } } if ((isInteger8 || isInteger16) && !isSigned) { if (obj.get().value_or(0) != unsignedValue || obj.get(0) != unsignedValue) { std::cerr << "Object " << name << " failed to parse as " << "uint16_t" << std::endl; return TestFail; } } if (obj.get().value_or(0) != value || obj.get(0) != value) { std::cerr << "Object " << name << " failed to parse as " << "int32_t" << std::endl; return TestFail; } if (!isSigned) { if (obj.get().value_or(0) != unsignedValue || obj.get(0) != unsignedValue) { std::cerr << "Object " << name << " failed to parse as " << "uint32_t" << std::endl; return TestFail; } } return TestPass; } int run() { /* Test invalid YAML file */ File file{ invalidYamlFile_ }; if (!file.open(File::OpenModeFlag::ReadOnly)) { cerr << "Fail to open invalid YAML file" << std::endl; return TestFail; } std::unique_ptr root = YamlParser::parse(file); if (root) { cerr << "Invalid YAML file parse successfully" << std::endl; return TestFail; } /* Test YAML file */ file.close(); file.setFileName(testYamlFile_); if (!file.open(File::OpenModeFlag::ReadOnly)) { cerr << "Fail to open test YAML file" << std::endl; return TestFail; } root = YamlParser::parse(file); if (!root) { cerr << "Fail to parse test YAML file: " << std::endl; return TestFail; } if (!root->isDictionary()) { cerr << "YAML root is not dictionary" << std::endl; return TestFail; } std::vector rootElemNames = { "string", "double", "int8_t", "uint8_t", "int16_t", "uint16_t", "int32_t", "uint32_t", "size", "list", "dictionary", "level1", }; for (const char *name : rootElemNames) { if (!root->contains(name)) { cerr << "Missing " << name << " object in YAML root" << std::endl; return TestFail; } } /* Test string obje/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * log.cpp - log API test */ #include <algorithm> #include <fcntl.h> #include <iostream> #include <list> #include <sstream> #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <libcamera/base/log.h> #include <libcamera/logging.h> #include "test.h" using namespace std; using namespace libcamera; LOG_DEFINE_CATEGORY(LogAPITest) class LogAPITest : public Test { protected: void doLogging() { logSetLevel("LogAPITest", "DEBUG"); LOG(LogAPITest, Info) << "good 1"; logSetLevel("LogAPITest", "WARN"); LOG(LogAPITest, Info) << "bad"; logSetLevel("LogAPITest", "ERROR"); LOG(LogAPITest, Error) << "good 3"; LOG(LogAPITest, Info) << "bad"; logSetLevel("LogAPITest", "WARN"); LOG(LogAPITest, Warning) << "good 5"; LOG(LogAPITest, Info) << "bad"; } int verifyOutput(istream &is) { list<int> goodList = { 1, 3, 5 }; string line; while (getline(is, line)) { if (goodList.empty()) { cout << "Too many log lines" << endl; return TestFail; } unsigned int digit = line.back() - '0'; unsigned int expect = goodList.front(); goodList.pop_front(); if (digit != expect) { cout << "Incorrect log line" << endl; return TestFail; } } if (!goodList.empty()) { cout << "Too few log lines" << endl; return TestFail; } return TestPass; } int testFile() { int fd = open("/tmp", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); if (fd < 0) { cerr << "Failed to open tmp log file" << endl; return TestFail; } char path[32]; snprintf(path, sizeof(path), "/proc/self/fd/%u", fd); if (logSetFile(path) < 0) { cerr << "Failed to set log file" << endl; close(fd); return TestFail; } doLogging(); char buf[1000]; memset(buf, 0, sizeof(buf)); lseek(fd, 0, SEEK_SET); if (read(fd, buf, sizeof(buf)) < 0) { cerr << "Failed to read tmp log file" << endl; close(fd); return TestFail; } close(fd); istringstream iss(buf); return verifyOutput(iss); } int testStream() { stringstream log; /* Never fails, so no need to check return value */ logSetStream(&log); doLogging(); return verifyOutput(log); } int testTarget() { logSetTarget(LoggingTargetNone); logSetLevel("LogAPITest", "DEBUG"); LOG(LogAPITest, Info) << "don't crash please"; if (!logSetTarget(LoggingTargetFile)) return TestFail; if (!logSetTarget(LoggingTargetStream)) return TestFail; return TestPass; } int run() override { int ret = testFile(); if (ret != TestPass) return TestFail; ret = testStream(); if (ret != TestPass) return TestFail; ret = testTarget(); if (ret != TestPass) return TestFail; return TestPass; } }; TEST_REGISTER(LogAPITest)