tests: ipa_data_serializer_test: Test serializing fds
diff mbox series

Message ID 20260708042237.297547-1-paul.elder@ideasonboard.com
State New
Headers show
Series
  • tests: ipa_data_serializer_test: Test serializing fds
Related show

Commit Message

Paul Elder July 8, 2026, 4:22 a.m. UTC
Add tests to test IPADataSerializer serializing SharedFDs.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
 .../ipa_data_serializer_test.cpp              | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)

Comments

Kieran Bingham July 8, 2026, 8:29 a.m. UTC | #1
Quoting Paul Elder (2026-07-08 05:22:37)
> Add tests to test IPADataSerializer serializing SharedFDs.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
> ---
>  .../ipa_data_serializer_test.cpp              | 43 +++++++++++++++++++
>  1 file changed, 43 insertions(+)
> 
> diff --git a/test/serialization/ipa_data_serializer_test.cpp b/test/serialization/ipa_data_serializer_test.cpp
> index afea93a6c24d..9b16383c4d5f 100644
> --- a/test/serialization/ipa_data_serializer_test.cpp
> +++ b/test/serialization/ipa_data_serializer_test.cpp
> @@ -11,6 +11,7 @@
>  #include <iostream>
>  #include <limits>
>  #include <stdlib.h>
> +#include <sys/mman.h>
>  #include <sys/stat.h>
>  #include <sys/types.h>
>  #include <tuple>
> @@ -140,6 +141,10 @@ protected:
>                 if (ret != TestPass)
>                         return ret;
>  
> +               ret = testFd();
> +               if (ret != TestPass)
> +                       return ret;
> +
>                 return TestPass;
>         }
>  
> @@ -431,6 +436,44 @@ private:
>  
>                 return TestPass;
>         }
> +
> +       int testFd()
> +       {
> +               /* Test serdes of single fd */
> +               int memfd = memfd_create("test", 0);
> +               if (memfd < 0) {
> +                       cerr << "Failed to create memfd" << endl;
> +                       return TestFail;
> +               }
> +
> +               SharedFD fd = SharedFD(std::move(memfd));
> +               if (memfd != -1) {
> +                       cerr << "SharedFD move constructor failed" << endl;
> +                       return TestFail;
> +               }
> +
> +               if (testPodSerdes(fd) != TestPass)
> +                       return TestFail;

This tests the objects are reconstructed correctly, but doesn't check
it's still a valid file descriptor which will work after crossing an IPC
boundary?

Is there anything we can do in the existing infrastructure to validate
that bit ? Otherwise as this at least ensures the type is correct, and
given the content will be the same - and we're in the same process the
FD will be valid so:


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


> +
> +               /* Test serdes of vector of fds */
> +               std::vector<SharedFD> vecFds;
> +
> +               for (unsigned int i = 0; i < 10; i++) {
> +                       std::string name = "test" + std::to_string(i);
> +                       int mfd = memfd_create(name.c_str(), 0);
> +                       if (mfd < 0)
> +                               return TestFail;
> +
> +                       vecFds.push_back(SharedFD(std::move(mfd)));
> +                       if (mfd != -1)
> +                               return TestFail;
> +               }
> +
> +               if (testVectorSerdes(vecFds) != TestPass)
> +                       return TestFail;
> +
> +               return TestPass;
> +       }
>  };
>  
>  TEST_REGISTER(IPADataSerializerTest)
> -- 
> 2.47.2
>
Paul Elder July 8, 2026, 10:41 a.m. UTC | #2
Quoting Kieran Bingham (2026-07-08 17:29:49)
> Quoting Paul Elder (2026-07-08 05:22:37)
> > Add tests to test IPADataSerializer serializing SharedFDs.
> > 
> > Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
> > ---
> >  .../ipa_data_serializer_test.cpp              | 43 +++++++++++++++++++
> >  1 file changed, 43 insertions(+)
> > 
> > diff --git a/test/serialization/ipa_data_serializer_test.cpp b/test/serialization/ipa_data_serializer_test.cpp
> > index afea93a6c24d..9b16383c4d5f 100644
> > --- a/test/serialization/ipa_data_serializer_test.cpp
> > +++ b/test/serialization/ipa_data_serializer_test.cpp
> > @@ -11,6 +11,7 @@
> >  #include <iostream>
> >  #include <limits>
> >  #include <stdlib.h>
> > +#include <sys/mman.h>
> >  #include <sys/stat.h>
> >  #include <sys/types.h>
> >  #include <tuple>
> > @@ -140,6 +141,10 @@ protected:
> >                 if (ret != TestPass)
> >                         return ret;
> >  
> > +               ret = testFd();
> > +               if (ret != TestPass)
> > +                       return ret;
> > +
> >                 return TestPass;
> >         }
> >  
> > @@ -431,6 +436,44 @@ private:
> >  
> >                 return TestPass;
> >         }
> > +
> > +       int testFd()
> > +       {
> > +               /* Test serdes of single fd */
> > +               int memfd = memfd_create("test", 0);
> > +               if (memfd < 0) {
> > +                       cerr << "Failed to create memfd" << endl;
> > +                       return TestFail;
> > +               }
> > +
> > +               SharedFD fd = SharedFD(std::move(memfd));
> > +               if (memfd != -1) {
> > +                       cerr << "SharedFD move constructor failed" << endl;
> > +                       return TestFail;
> > +               }
> > +
> > +               if (testPodSerdes(fd) != TestPass)
> > +                       return TestFail;
> 
> This tests the objects are reconstructed correctly, but doesn't check
> it's still a valid file descriptor which will work after crossing an IPC
> boundary?

Yes, this only tests the serialization and deserialization.

> 
> Is there anything we can do in the existing infrastructure to validate
> that bit ? Otherwise as this at least ensures the type is correct, and

I think we can add it to unixsocket_ipc.

(This patch was just a few-year-old patch that I unearthed while cleaning my
branches)

> given the content will be the same - and we're in the same process the
> FD will be valid so:
> 
> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>


Thanks,

Paul

> 
> 
> > +
> > +               /* Test serdes of vector of fds */
> > +               std::vector<SharedFD> vecFds;
> > +
> > +               for (unsigned int i = 0; i < 10; i++) {
> > +                       std::string name = "test" + std::to_string(i);
> > +                       int mfd = memfd_create(name.c_str(), 0);
> > +                       if (mfd < 0)
> > +                               return TestFail;
> > +
> > +                       vecFds.push_back(SharedFD(std::move(mfd)));
> > +                       if (mfd != -1)
> > +                               return TestFail;
> > +               }
> > +
> > +               if (testVectorSerdes(vecFds) != TestPass)
> > +                       return TestFail;
> > +
> > +               return TestPass;
> > +       }
> >  };
> >  
> >  TEST_REGISTER(IPADataSerializerTest)
> > -- 
> > 2.47.2
> >

Patch
diff mbox series

diff --git a/test/serialization/ipa_data_serializer_test.cpp b/test/serialization/ipa_data_serializer_test.cpp
index afea93a6c24d..9b16383c4d5f 100644
--- a/test/serialization/ipa_data_serializer_test.cpp
+++ b/test/serialization/ipa_data_serializer_test.cpp
@@ -11,6 +11,7 @@ 
 #include <iostream>
 #include <limits>
 #include <stdlib.h>
+#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <tuple>
@@ -140,6 +141,10 @@  protected:
 		if (ret != TestPass)
 			return ret;
 
+		ret = testFd();
+		if (ret != TestPass)
+			return ret;
+
 		return TestPass;
 	}
 
@@ -431,6 +436,44 @@  private:
 
 		return TestPass;
 	}
+
+	int testFd()
+	{
+		/* Test serdes of single fd */
+		int memfd = memfd_create("test", 0);
+		if (memfd < 0) {
+			cerr << "Failed to create memfd" << endl;
+			return TestFail;
+		}
+
+		SharedFD fd = SharedFD(std::move(memfd));
+		if (memfd != -1) {
+			cerr << "SharedFD move constructor failed" << endl;
+			return TestFail;
+		}
+
+		if (testPodSerdes(fd) != TestPass)
+			return TestFail;
+
+		/* Test serdes of vector of fds */
+		std::vector<SharedFD> vecFds;
+
+		for (unsigned int i = 0; i < 10; i++) {
+			std::string name = "test" + std::to_string(i);
+			int mfd = memfd_create(name.c_str(), 0);
+			if (mfd < 0)
+				return TestFail;
+
+			vecFds.push_back(SharedFD(std::move(mfd)));
+			if (mfd != -1)
+				return TestFail;
+		}
+
+		if (testVectorSerdes(vecFds) != TestPass)
+			return TestFail;
+
+		return TestPass;
+	}
 };
 
 TEST_REGISTER(IPADataSerializerTest)