[libcamera-devel,v1,2/2] test: Replace "/proc/self/exe" with path to test binary
diff mbox series

Message ID 20211130012303.10574-3-laurent.pinchart@ideasonboard.com
State Accepted
Commit 59002a9e9d84417197999391c7d8c6af620fc1fb
Headers show
Series
  • libcamera: Fix valgrind usage with tests that fork process
Related show

Commit Message

Laurent Pinchart Nov. 30, 2021, 1:23 a.m. UTC
When tests are run under valgrind, /proc/self/exe points to valgrind,
not to the test binary. This results in failures for tests that need to
fork processes. Fix it by replacing "/proc/self/exe" with the path to
the test binary.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/file.cpp                 | 4 ++--
 test/ipc/unixsocket.cpp       | 5 ++---
 test/ipc/unixsocket_ipc.cpp   | 2 +-
 test/log/log_process.cpp      | 2 +-
 test/process/process_test.cpp | 2 +-
 5 files changed, 7 insertions(+), 8 deletions(-)

Comments

Kieran Bingham Nov. 30, 2021, 10:59 a.m. UTC | #1
Quoting Laurent Pinchart (2021-11-30 01:23:03)
> When tests are run under valgrind, /proc/self/exe points to valgrind,
> not to the test binary. This results in failures for tests that need to
> fork processes. Fix it by replacing "/proc/self/exe" with the path to
> the test binary.
> 

This looks fine to me.
Something almost grates on me that self() doesn't feel as obvious to me
as it should to what it expresses, but it does do the right thing, and I
don't think renaming it would help.

(I.e. does self mean the object? the class? is it like 'this'?)

But ... it's clear enough if the function is read.


Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  test/file.cpp                 | 4 ++--
>  test/ipc/unixsocket.cpp       | 5 ++---
>  test/ipc/unixsocket_ipc.cpp   | 2 +-
>  test/log/log_process.cpp      | 2 +-
>  test/process/process_test.cpp | 2 +-
>  5 files changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/test/file.cpp b/test/file.cpp
> index 9ac151c944b5..5c978ebfcada 100644
> --- a/test/file.cpp
> +++ b/test/file.cpp
> @@ -180,7 +180,7 @@ protected:
>                 }
>  
>                 /* Test size(). */
> -               file.setFileName("/proc/self/exe");
> +               file.setFileName(self());
>  
>                 if (file.size() >= 0) {
>                         cerr << "File has valid size before open" << endl;
> @@ -277,7 +277,7 @@ protected:
>                 file.close();
>  
>                 /* Test mapping and unmapping. */
> -               file.setFileName("/proc/self/exe");
> +               file.setFileName(self());
>                 file.open(File::OpenModeFlag::ReadOnly);
>  
>                 Span<uint8_t> data = file.map();
> diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp
> index 4fc1c10a2125..b3568c0684b0 100644
> --- a/test/ipc/unixsocket.cpp
> +++ b/test/ipc/unixsocket.cpp
> @@ -209,8 +209,7 @@ protected:
>  
>                 if (!pid_) {
>                         std::string arg = std::to_string(fd);
> -                       execl("/proc/self/exe", "/proc/self/exe",
> -                             arg.c_str(), nullptr);
> +                       execl(self().c_str(), self().c_str(), arg.c_str(), nullptr);
>  
>                         /* Only get here if exec fails. */
>                         exit(TestFail);
> @@ -464,7 +463,7 @@ private:
>  
>         int prepareFDs(IPCUnixSocket::Payload *message, unsigned int num)
>         {
> -               int fd = open("/proc/self/exe", O_RDONLY);
> +               int fd = open(self().c_str(), O_RDONLY);
>                 if (fd < 0)
>                         return fd;
>  
> diff --git a/test/ipc/unixsocket_ipc.cpp b/test/ipc/unixsocket_ipc.cpp
> index 2e3b52ca4d4b..1a8d06a12d5c 100644
> --- a/test/ipc/unixsocket_ipc.cpp
> +++ b/test/ipc/unixsocket_ipc.cpp
> @@ -173,7 +173,7 @@ protected:
>  
>         int run()
>         {
> -               ipc_ = std::make_unique<IPCPipeUnixSocket>("", "/proc/self/exe");
> +               ipc_ = std::make_unique<IPCPipeUnixSocket>("", self().c_str());
>                 if (!ipc_->isConnected()) {
>                         cerr << "Failed to create IPCPipe" << endl;
>                         return TestFail;
> diff --git a/test/log/log_process.cpp b/test/log/log_process.cpp
> index ca8351335f3a..2484c58f2fa9 100644
> --- a/test/log/log_process.cpp
> +++ b/test/log/log_process.cpp
> @@ -74,7 +74,7 @@ protected:
>                 vector<std::string> args;
>                 args.push_back(to_string(exitCode));
>                 args.push_back(to_string(num_));
> -               int ret = proc_.start("/proc/self/exe", args);
> +               int ret = proc_.start(self(), args);
>                 if (ret) {
>                         cerr << "failed to start process" << endl;
>                         return TestFail;
> diff --git a/test/process/process_test.cpp b/test/process/process_test.cpp
> index 96bea17f8dce..b410756b3288 100644
> --- a/test/process/process_test.cpp
> +++ b/test/process/process_test.cpp
> @@ -55,7 +55,7 @@ protected:
>                 proc_.kill();
>  
>                 /* Test starting the process and retrieving the exit code. */
> -               int ret = proc_.start("/proc/self/exe", args);
> +               int ret = proc_.start(self(), args);
>                 if (ret) {
>                         cerr << "failed to start process" << endl;
>                         return TestFail;
> -- 
> Regards,
> 
> Laurent Pinchart
>

Patch
diff mbox series

diff --git a/test/file.cpp b/test/file.cpp
index 9ac151c944b5..5c978ebfcada 100644
--- a/test/file.cpp
+++ b/test/file.cpp
@@ -180,7 +180,7 @@  protected:
 		}
 
 		/* Test size(). */
-		file.setFileName("/proc/self/exe");
+		file.setFileName(self());
 
 		if (file.size() >= 0) {
 			cerr << "File has valid size before open" << endl;
@@ -277,7 +277,7 @@  protected:
 		file.close();
 
 		/* Test mapping and unmapping. */
-		file.setFileName("/proc/self/exe");
+		file.setFileName(self());
 		file.open(File::OpenModeFlag::ReadOnly);
 
 		Span<uint8_t> data = file.map();
diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp
index 4fc1c10a2125..b3568c0684b0 100644
--- a/test/ipc/unixsocket.cpp
+++ b/test/ipc/unixsocket.cpp
@@ -209,8 +209,7 @@  protected:
 
 		if (!pid_) {
 			std::string arg = std::to_string(fd);
-			execl("/proc/self/exe", "/proc/self/exe",
-			      arg.c_str(), nullptr);
+			execl(self().c_str(), self().c_str(), arg.c_str(), nullptr);
 
 			/* Only get here if exec fails. */
 			exit(TestFail);
@@ -464,7 +463,7 @@  private:
 
 	int prepareFDs(IPCUnixSocket::Payload *message, unsigned int num)
 	{
-		int fd = open("/proc/self/exe", O_RDONLY);
+		int fd = open(self().c_str(), O_RDONLY);
 		if (fd < 0)
 			return fd;
 
diff --git a/test/ipc/unixsocket_ipc.cpp b/test/ipc/unixsocket_ipc.cpp
index 2e3b52ca4d4b..1a8d06a12d5c 100644
--- a/test/ipc/unixsocket_ipc.cpp
+++ b/test/ipc/unixsocket_ipc.cpp
@@ -173,7 +173,7 @@  protected:
 
 	int run()
 	{
-		ipc_ = std::make_unique<IPCPipeUnixSocket>("", "/proc/self/exe");
+		ipc_ = std::make_unique<IPCPipeUnixSocket>("", self().c_str());
 		if (!ipc_->isConnected()) {
 			cerr << "Failed to create IPCPipe" << endl;
 			return TestFail;
diff --git a/test/log/log_process.cpp b/test/log/log_process.cpp
index ca8351335f3a..2484c58f2fa9 100644
--- a/test/log/log_process.cpp
+++ b/test/log/log_process.cpp
@@ -74,7 +74,7 @@  protected:
 		vector<std::string> args;
 		args.push_back(to_string(exitCode));
 		args.push_back(to_string(num_));
-		int ret = proc_.start("/proc/self/exe", args);
+		int ret = proc_.start(self(), args);
 		if (ret) {
 			cerr << "failed to start process" << endl;
 			return TestFail;
diff --git a/test/process/process_test.cpp b/test/process/process_test.cpp
index 96bea17f8dce..b410756b3288 100644
--- a/test/process/process_test.cpp
+++ b/test/process/process_test.cpp
@@ -55,7 +55,7 @@  protected:
 		proc_.kill();
 
 		/* Test starting the process and retrieving the exit code. */
-		int ret = proc_.start("/proc/self/exe", args);
+		int ret = proc_.start(self(), args);
 		if (ret) {
 			cerr << "failed to start process" << endl;
 			return TestFail;