/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2021, Google Inc. * * Test: * - Multiple reconfigurations of the Camera without stopping the CameraManager * - Validate there are no file descriptor leaks when using IPC */ #include #include #include #include #include #include #include #include #include "camera_test.h" #include "test.h" using namespace libcamera; using namespace std; using namespace std::chrono_literals; namespace { class CameraReconfigure : public CameraTest, public Test { public: /* Initialize CameraTest with isolated IPA */ CameraReconfigure() : CameraTest(kCamId_, true) { } private: static constexpr const char *kCamId_ = "platform/vimc.0 Sensor B"; static constexpr const char *kIpaProxyName_ = "vimc_ipa_proxy"; static constexpr unsigned int kNumOfReconfigures_ = 10; void requestComplete(Request *request) { if (request->status() != Request::RequestComplete) return; const Request::BufferMap &buffers = request->buffers(); const Stream *stream = buffers.begin()->first; FrameBuffer *buffer = buffers.begin()->second; /* Reuse the request and re-queue it with the same buffers. */ request->reuse(); request->addBuffer(stream, buffer); camera_->queueRequest(request); } int startAndStop() { StreamConfiguration &cfg = config_->at(0); if (camera_->acquire()) { cerr << "Failed to acquire the camera" << endl; return TestFail; } if (camera_->configure(config_.get())) { cerr << "Failed to set default configuration" << endl; return TestFail; } Stream *stream = cfg.stream(); /* * The configuration is consistent so we can re-use the * same buffer allocation for each run. */ if (!allocated_) { int ret = allocator_->allocate(stream); if (ret < 0) { cerr << "Failed to allocate buffers" << endl; return TestFail; } allocated_ = true; } for (const unique_ptr &buffer : allocator_->buffers(stream)) { unique_ptr request = camera_->createRequest(); if (!request) { cerr << "Failed to create request" << endl; return TestFail; } if (request->addBuffer(stream, buffer.get())) { cerr << "Failed to associate buffer with request" << endl; return TestFail; } requests_.push_back(std::move(request)); } camera_->requestCompleted.connect(this, &CameraReconfigure::requestComplete); if (camera_->start()) { cerr << "Failed to start camera" << endl; return TestFail; } for (unique_ptr &request : requests_) { if (camera_->queueRequest(request.get())) { cerr << "Failed to queue request" << endl; return TestFail; } } EventDispatcher *dispatcher = Thread::current()->eventDispatcher(); Timer timer; timer.start(100ms); while (timer.isRunning()) dispatcher->processEvents(); if (camera_->stop()) { cerr << "Failed to stop camera" << endl; return TestFail; } if (camera_->release()) { cerr << "Failed to release camera" << endl; return TestFail; } camera_->requestCompleted.disconnect(this); requests_.clear(); return 0; } int fdsOpen(pid_t pid) { string proxyFdPath = "/proc/" + to_string(pid) + "/fd"; DIR *dir; struct dirent *ptr; unsigned int openFds = 0; dir = opendir(proxyFdPath.c_str()); if (dir == nullptr) { int err = errno; cerr << "Error opening " << proxyFdPath << ": " << strerror(-err) << endl; return 0; } while ((ptr = readdir(dir)) != nullptr) { if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) continue; openFds++; } closedir(dir); return openFds; } pid_t findProxyPid() { string proxyPid; string proxyName(kIpaProxyName_); DIR *dir; struct dirent *ptr; dir = opendir("/proc"); while ((ptr = readdir(dir)) != nullptr) { if (ptr->d_type != DT_DIR) continue; string pname("/proc/" + string(ptr->d_name) + "/comm"); if (File::exists(pname)) { ifstream pfile(pname.c_str()); string comm; getline(pfile, comm); pfile.close(); proxyPid = comm == proxyName ? string(ptr->d_name) : ""; } if (!proxyPid.empty()) break; } closedir(dir); if (!proxyPid.empty()) return atoi(proxyPid.c_str()); return -1; } int init() override { if (status_ != TestPass) return status_; config_ = camera_->generateConfiguration({ StreamRole::StillCapture }); if (!config_ || config_->size() != 1) { cerr << "Failed to generate default configuration" << endl; return TestFail; } allocator_ = make_unique(camera_); allocated_ = false; return TestPass; } int run() override { unsigned int openFdsAtStart = 0; unsigned int openFds = 0; pid_t proxyPid = findProxyPid(); if (proxyPid < 0) { cerr << "Cannot find " << kIpaProxyName_ << " pid, exiting" << endl; return TestFail; } openFdsAtStart = fdsOpen(proxyPid); for (unsigned int i = 0; i < kNumOfReconfigures_; i++) { startAndStop(); openFds = fdsOpen(proxyPid); if (openFds == 0) { cerr << "No open fds found whereas " << "open fds at start: " << openFdsAtStart << endl; return TestFail; } if (openFds != openFdsAtStart) { cerr << "Leaking fds for " << kIpaProxyName_ << " - Open fds: " << openFds << " vs " << "Open fds at start: " << openFdsAtStart << endl; return TestFail; } } return TestPass; } bool allocated_; vector> requests_; unique_ptr config_; unique_ptr allocator_; }; } /* namespace */ TEST_REGISTER(CameraReconfigure) a> 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2018, Google Inc.
 *
 * utils.cpp - Miscellaneous utility tests
 */

#include <iostream>
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <vector>

#include <libcamera/base/span.h>
#include <libcamera/base/utils.h>

#include <libcamera/geometry.h>

#include "test.h"

using namespace std;
using namespace libcamera;
using namespace std::literals::chrono_literals;

class UtilsTest : public Test
{
protected:
	int testDirname()
	{
		static const std::vector<std::string> paths = {
			"",
			"///",
			"/bin",
			"/usr/bin",
			"//etc////",
			"//tmp//d//",
			"current_file",
			"./current_file",
			"./current_dir/",
			"current_dir/",
		};

		static const std::vector<std::string> expected = {
			".",
			"/",
			"/",
			"/usr",
			"/",
			"//tmp",
			".",
			".",
			".",
			".",
		};

		std::vector<std::string> results;

		for (const auto &path : paths)
			results.push_back(utils::dirname(path));

		if (results != expected) {
			cerr << "utils::dirname() tests failed" << endl;

			cerr << "expected: " << endl;
			for (const auto &path : expected)
				cerr << "\t" << path << endl;

			cerr << "results: " << endl;
			for (const auto &path : results)
				cerr << "\t" << path << endl;

			return TestFail;
		}

		return TestPass;
	}

	int testEnumerate()
	{