Mocking out libmm-glib for testing

Aleksander Morgado aleksander at aleksander.es
Wed Jun 2 08:57:41 UTC 2021


Hey!

> I have been working on a C++ project that uses libmm-glib, but I recently ran into an issue around testing it. I was hoping to be able to create an artificial MMSim (not based on a real card) and pass that to my code under test. My main goal is to avoid dbus dependencies so I can directly instrument the 'external' data types in my tests. It seemed like it was going to be reasonably easy:
>
> Make a directory to hold my wrappers.
> Create my own libmm-glib.h that imports the core enum and error header files.
> Create my own GObject based MMSim class for instrumentation.
> Do some clever stuff with include paths so my tests pull in my libmm-glib.h but my libmm-glib.h pulls in the ModemManager ModemManager-enums.h/etc files.
>
> But I realized I would lose access to all of libmm-glib's other types. I would have to reimplement types like MMBearerProperties (which is already a basic data type that needs no modification for testing).
>
> Does anyone know a better way to do this? I have found details on Aleksander's blog about how to test ModemManager, but I have not yet come up with much on how to test code that uses ModemManager. Any advice would be greatly appreciated.
>

What you're trying to accomplish is a bit difficult, unless you
implement custom DBus-less versions of all object types (modem, sim,
bearer...) and interfaces (modem3gpp, modemsimple...) in a custom fake
libmm-glib. See, with some clever setup you could even have your own
version of the gdbus-codegen program (the one that builds all
DBus-based object and interface types) so that it autogenerates
DBus-less GObjects. That could be a fun exercise, but not sure how
easy it would be.

Another approach you could take, which I know others are taking, is to
leave libmm-glib as it is, unmodified, based on D-Bus. And instead of
mocking the library, you would be mocking the ModemManager daemon with
a "fake" implementation that exposes the same DBus interface as the
daemon. See the current support for this here:
https://gitlab.freedesktop.org/mobile-broadband/ModemManager/-/blob/master/tools/test-modemmanager-service.py
https://gitlab.freedesktop.org/mobile-broadband/ModemManager/-/tree/master/tools/tests

With that setup above, you can have your program linked to the
official libmm-glib, then you run the service stub daemon, and then
you can instruct fake events like "AddModem" to add a fake modem with
a SIM. It's not extremely powerful, but it has a lot of potential for
growth I'd say.

-- 
Aleksander
https://aleksander.es


More information about the ModemManager-devel mailing list