utils: Move test binaries into utils

These utilities are generally useful beyond only testing purproses. And,
since it is desirable to have automated tests inside the tests
subdirecty, it makes sense to move them elsewhere.
This commit is contained in:
Benjamin Berg
2019-10-02 14:07:42 +02:00
parent b2ff316e20
commit 3db69c2c2f
7 changed files with 2 additions and 2 deletions

35
utils/Makefile.am Normal file
View File

@ -0,0 +1,35 @@
BUILT_SOURCES = manager-dbus-glue.h device-dbus-glue.h $(MARSHALFILES)
noinst_HEADERS = $(BUILT_SOURCES)
CLEANFILES = $(BUILT_SOURCES)
bin_PROGRAMS = fprintd-verify fprintd-enroll fprintd-list fprintd-delete
fprintd_verify_SOURCES = verify.c $(MARSHALFILES)
fprintd_verify_CFLAGS = $(WARN_CFLAGS) $(GLIB_CFLAGS)
fprintd_verify_LDADD = $(GLIB_LIBS)
fprintd_enroll_SOURCES = enroll.c $(MARSHALFILES)
fprintd_enroll_CFLAGS = $(WARN_CFLAGS) $(GLIB_CFLAGS) -I$(top_srcdir)/pam
fprintd_enroll_LDADD = $(GLIB_LIBS)
fprintd_list_SOURCES = list.c
fprintd_list_CFLAGS = $(WARN_CFLAGS) $(GLIB_CFLAGS)
fprintd_list_LDADD = $(GLIB_LIBS)
fprintd_delete_SOURCES = delete.c
fprintd_delete_CFLAGS = $(WARN_CFLAGS) $(GLIB_CFLAGS)
fprintd_delete_LDADD = $(GLIB_LIBS)
manager-dbus-glue.h: ../src/manager.xml
dbus-binding-tool --prefix=fprint_manager --mode=glib-client $< --output=$@
device-dbus-glue.h: ../src/device.xml
dbus-binding-tool --prefix=fprint_device --mode=glib-client $< --output=$@
MARSHALFILES = marshal.c marshal.h
GLIB_GENMARSHAL=`pkg-config --variable=glib_genmarshal glib-2.0`
marshal.h: $(top_srcdir)/src/fprintd-marshal.list
( $(GLIB_GENMARSHAL) --prefix=fprintd_marshal $(top_srcdir)/src/fprintd-marshal.list --header > marshal.h )
marshal.c: marshal.h
( $(GLIB_GENMARSHAL) --prefix=fprintd_marshal $(top_srcdir)/src/fprintd-marshal.list --body --header > marshal.c )

132
utils/delete.c Normal file
View File

@ -0,0 +1,132 @@
/*
* fprintd example to delete fingerprints
* Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus-glib-bindings.h>
#include "manager-dbus-glue.h"
#include "device-dbus-glue.h"
static DBusGProxy *manager = NULL;
static DBusGConnection *connection = NULL;
static void create_manager(void)
{
GError *error = NULL;
connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
if (connection == NULL) {
g_print("Failed to connect to session bus: %s\n", error->message);
exit (1);
}
manager = dbus_g_proxy_new_for_name(connection,
"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
"net.reactivated.Fprint.Manager");
}
static void delete_fingerprints(DBusGProxy *dev, const char *username)
{
GError *error = NULL;
GHashTable *props;
DBusGProxy *p;
p = dbus_g_proxy_new_from_proxy(dev, "org.freedesktop.DBus.Properties", NULL);
if (!dbus_g_proxy_call (p, "GetAll", &error, G_TYPE_STRING, "net.reactivated.Fprint.Device", G_TYPE_INVALID,
dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props, G_TYPE_INVALID)) {
g_print("GetAll on the Properties interface failed: %s\n", error->message);
exit (1);
}
if (!net_reactivated_Fprint_Device_delete_enrolled_fingers(dev, username, &error)) {
if (dbus_g_error_has_name (error, "net.reactivated.Fprint.Error.NoEnrolledPrints") == FALSE) {
g_print("ListEnrolledFingers failed: %s\n", error->message);
exit (1);
} else {
g_print ("No fingerprints to delete on %s\n", g_value_get_string (g_hash_table_lookup (props, "name")));
}
} else {
g_print ("Fingerprints deleted on %s\n", g_value_get_string (g_hash_table_lookup (props, "name")));
}
g_hash_table_destroy (props);
g_object_unref (p);
}
static void process_devices(char **argv)
{
GError *error = NULL;
GPtrArray *devices;
char *path;
guint i;
if (!net_reactivated_Fprint_Manager_get_devices(manager, &devices, &error)) {
g_print("list_devices failed: %s\n", error->message);
exit (1);
}
if (devices->len == 0) {
g_print("No devices found\n");
exit(1);
}
g_print("found %d devices\n", devices->len);
for (i = 0; i < devices->len; i++) {
path = g_ptr_array_index(devices, i);
g_print("Device at %s\n", path);
}
for (i = 0; i < devices->len; i++) {
guint j;
DBusGProxy *dev;
path = g_ptr_array_index(devices, i);
g_print("Using device %s\n", path);
/* FIXME use for_name_owner?? */
dev = dbus_g_proxy_new_for_name(connection, "net.reactivated.Fprint",
path, "net.reactivated.Fprint.Device");
for (j = 1; argv[j] != NULL; j++)
delete_fingerprints (dev, argv[j]);
g_object_unref (dev);
}
g_ptr_array_foreach(devices, (GFunc) g_free, NULL);
g_ptr_array_free(devices, TRUE);
}
int main(int argc, char **argv)
{
#if !GLIB_CHECK_VERSION (2, 36, 0)
g_type_init();
#endif
create_manager();
if (argc < 2) {
g_print ("Usage: %s <username> [usernames...]\n", argv[0]);
return 1;
}
process_devices (argv);
return 0;
}

185
utils/enroll.c Normal file
View File

@ -0,0 +1,185 @@
/*
* fprintd example to enroll right index finger
* Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib-bindings.h>
#include "manager-dbus-glue.h"
#include "device-dbus-glue.h"
#include "marshal.h"
#define N_(x) x
#define TR(x) x
#include "fingerprint-strings.h"
static DBusGProxy *manager = NULL;
static DBusGConnection *connection = NULL;
static char *finger_name = "right-index-finger";
static char **usernames = NULL;
static void create_manager(void)
{
GError *error = NULL;
connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
if (connection == NULL) {
g_print("Failed to connect to session bus: %s\n", error->message);
exit (1);
}
manager = dbus_g_proxy_new_for_name(connection,
"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
"net.reactivated.Fprint.Manager");
}
static DBusGProxy *open_device(const char *username)
{
GError *error = NULL;
gchar *path;
DBusGProxy *dev;
if (!net_reactivated_Fprint_Manager_get_default_device(manager, &path, &error)) {
g_print("list_devices failed: %s\n", error->message);
exit (1);
}
if (path == NULL) {
g_print("No devices found\n");
exit(1);
}
g_print("Using device %s\n", path);
/* FIXME use for_name_owner?? */
dev = dbus_g_proxy_new_for_name(connection, "net.reactivated.Fprint",
path, "net.reactivated.Fprint.Device");
g_free (path);
if (!net_reactivated_Fprint_Device_claim(dev, username, &error)) {
g_print("failed to claim device: %s\n", error->message);
exit (1);
}
return dev;
}
static void enroll_result(GObject *object, const char *result, gboolean done, void *user_data)
{
gboolean *enroll_completed = user_data;
g_print("Enroll result: %s\n", result);
if (done != FALSE)
*enroll_completed = TRUE;
}
static void do_enroll(DBusGProxy *dev)
{
GError *error = NULL;
gboolean enroll_completed = FALSE;
gboolean found;
guint i;
dbus_g_proxy_add_signal(dev, "EnrollStatus", G_TYPE_STRING, G_TYPE_BOOLEAN, NULL);
dbus_g_proxy_connect_signal(dev, "EnrollStatus", G_CALLBACK(enroll_result),
&enroll_completed, NULL);
found = FALSE;
for (i = 0; fingers[i].dbus_name != NULL; i++) {
if (g_strcmp0 (fingers[i].dbus_name, finger_name) == 0) {
found = TRUE;
break;
}
}
if (!found) {
GString *s;
s = g_string_new (NULL);
g_string_append_printf (s, "Invalid finger name '%s'. Name must be one of ", finger_name);
for (i = 0; fingers[i].dbus_name != NULL; i++) {
g_string_append_printf (s, "%s", fingers[i].dbus_name);
if (fingers[i + 1].dbus_name != NULL)
g_string_append (s, ", ");
}
g_warning ("%s", s->str);
g_string_free (s, TRUE);
exit (1);
}
g_print("Enrolling %s finger.\n", finger_name);
if (!net_reactivated_Fprint_Device_enroll_start(dev, finger_name, &error)) {
g_print("EnrollStart failed: %s\n", error->message);
exit (1);
}
while (!enroll_completed)
g_main_context_iteration(NULL, TRUE);
dbus_g_proxy_disconnect_signal(dev, "EnrollStatus",
G_CALLBACK(enroll_result), &enroll_completed);
if (!net_reactivated_Fprint_Device_enroll_stop(dev, &error)) {
g_print("VerifyStop failed: %s\n", error->message);
exit(1);
}
}
static void release_device(DBusGProxy *dev)
{
GError *error = NULL;
if (!net_reactivated_Fprint_Device_release(dev, &error)) {
g_print("ReleaseDevice failed: %s\n", error->message);
exit (1);
}
}
static const GOptionEntry entries[] = {
{ "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL },
{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" },
{ NULL }
};
int main(int argc, char **argv)
{
GOptionContext *context;
GError *err = NULL;
DBusGProxy *dev;
#if !GLIB_CHECK_VERSION (2, 36, 0)
g_type_init();
#endif
dbus_g_object_register_marshaller (fprintd_marshal_VOID__STRING_BOOLEAN,
G_TYPE_NONE, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INVALID);
context = g_option_context_new ("Enroll a fingerprint");
g_option_context_add_main_entries (context, entries, NULL);
if (g_option_context_parse (context, &argc, &argv, &err) == FALSE) {
g_print ("couldn't parse command-line options: %s\n", err->message);
g_error_free (err);
return 1;
}
create_manager();
dev = open_device(usernames ? usernames[0] : NULL);
do_enroll(dev);
release_device(dev);
return 0;
}

148
utils/list.c Normal file
View File

@ -0,0 +1,148 @@
/*
* fprintd example to list enrolled fingerprints
* Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus-glib-bindings.h>
#include "manager-dbus-glue.h"
#include "device-dbus-glue.h"
static DBusGProxy *manager = NULL;
static DBusGConnection *connection = NULL;
static void create_manager(void)
{
GError *error = NULL;
connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
if (connection == NULL) {
g_print("Failed to connect to session bus: %s\n", error->message);
exit (1);
}
manager = dbus_g_proxy_new_for_name(connection,
"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
"net.reactivated.Fprint.Manager");
}
static void list_fingerprints(DBusGProxy *dev, const char *username)
{
GError *error = NULL;
char **fingers;
GHashTable *props;
DBusGProxy *p;
guint i;
if (!net_reactivated_Fprint_Device_list_enrolled_fingers(dev, username, &fingers, &error)) {
if (dbus_g_error_has_name (error, "net.reactivated.Fprint.Error.NoEnrolledPrints") == FALSE) {
g_print("ListEnrolledFingers failed: %s\n", error->message);
exit (1);
} else {
fingers = NULL;
}
}
p = dbus_g_proxy_new_from_proxy(dev, "org.freedesktop.DBus.Properties", NULL);
if (!dbus_g_proxy_call (p, "GetAll", &error, G_TYPE_STRING, "net.reactivated.Fprint.Device", G_TYPE_INVALID,
dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &props, G_TYPE_INVALID)) {
g_print("GetAll on the Properties interface failed: %s\n", error->message);
exit (1);
}
if (fingers == NULL || g_strv_length (fingers) == 0) {
g_print("User %s has no fingers enrolled for %s.\n", username, g_value_get_string (g_hash_table_lookup (props, "name")));
return;
}
g_print("Fingerprints for user %s on %s (%s):\n",
username,
g_value_get_string (g_hash_table_lookup (props, "name")),
g_value_get_string (g_hash_table_lookup (props, "scan-type")));
g_hash_table_destroy (props);
g_object_unref (p);
for (i = 0; fingers[i] != NULL; i++) {
g_print(" - #%d: %s\n", i, fingers[i]);
}
g_strfreev (fingers);
}
static void process_devices(char **argv)
{
GError *error = NULL;
GPtrArray *devices;
char *path;
guint i;
if (!net_reactivated_Fprint_Manager_get_devices(manager, &devices, &error)) {
g_print("list_devices failed: %s\n", error->message);
exit (1);
}
if (devices->len == 0) {
g_print("No devices found\n");
exit(1);
}
g_print("found %d devices\n", devices->len);
for (i = 0; i < devices->len; i++) {
path = g_ptr_array_index(devices, i);
g_print("Device at %s\n", path);
}
for (i = 0; i < devices->len; i++) {
guint j;
DBusGProxy *dev;
path = g_ptr_array_index(devices, i);
g_print("Using device %s\n", path);
/* FIXME use for_name_owner?? */
dev = dbus_g_proxy_new_for_name(connection, "net.reactivated.Fprint",
path, "net.reactivated.Fprint.Device");
for (j = 1; argv[j] != NULL; j++)
list_fingerprints (dev, argv[j]);
g_object_unref (dev);
}
g_ptr_array_foreach(devices, (GFunc) g_free, NULL);
g_ptr_array_free(devices, TRUE);
}
int main(int argc, char **argv)
{
#if !GLIB_CHECK_VERSION (2, 36, 0)
g_type_init();
#endif
create_manager();
if (argc < 2) {
g_print ("Usage: %s <username> [usernames...]\n", argv[0]);
return 1;
}
process_devices (argv);
return 0;
}

213
utils/verify.c Normal file
View File

@ -0,0 +1,213 @@
/*
* fprintd example to verify a fingerprint
* Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus-glib-bindings.h>
#include "manager-dbus-glue.h"
#include "device-dbus-glue.h"
#include "marshal.h"
static DBusGProxy *manager = NULL;
static DBusGConnection *connection = NULL;
static char *finger_name = NULL;
static gboolean g_fatal_warnings = FALSE;
static char **usernames = NULL;
static void create_manager(void)
{
GError *error = NULL;
connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
if (connection == NULL) {
g_print("Failed to connect to session bus: %s\n", error->message);
exit (1);
}
manager = dbus_g_proxy_new_for_name(connection,
"net.reactivated.Fprint", "/net/reactivated/Fprint/Manager",
"net.reactivated.Fprint.Manager");
}
static DBusGProxy *open_device(const char *username)
{
GError *error = NULL;
gchar *path;
DBusGProxy *dev;
if (!net_reactivated_Fprint_Manager_get_default_device(manager, &path, &error)) {
g_print("list_devices failed: %s\n", error->message);
exit (1);
}
if (path == NULL) {
g_print("No devices found\n");
exit(1);
}
g_print("Using device %s\n", path);
/* FIXME use for_name_owner?? */
dev = dbus_g_proxy_new_for_name(connection, "net.reactivated.Fprint",
path, "net.reactivated.Fprint.Device");
g_free (path);
if (!net_reactivated_Fprint_Device_claim(dev, username, &error)) {
g_print("failed to claim device: %s\n", error->message);
exit (1);
}
return dev;
}
static void find_finger(DBusGProxy *dev, const char *username)
{
GError *error = NULL;
char **fingers;
guint i;
if (!net_reactivated_Fprint_Device_list_enrolled_fingers(dev, username, &fingers, &error)) {
g_print("ListEnrolledFingers failed: %s\n", error->message);
exit (1);
}
if (fingers == NULL || g_strv_length (fingers) == 0) {
g_print("No fingers enrolled for this device.\n");
exit(1);
}
g_print("Listing enrolled fingers:\n");
for (i = 0; fingers[i] != NULL; i++) {
g_print(" - #%d: %s\n", i, fingers[i]);
}
if (finger_name == NULL || strcmp (finger_name, "any") == 0) {
g_free (finger_name);
finger_name = g_strdup (fingers[0]);
}
g_strfreev (fingers);
}
static void verify_result(GObject *object, const char *result, gboolean done, void *user_data)
{
gboolean *verify_completed = user_data;
g_print("Verify result: %s (%s)\n", result, done ? "done" : "not done");
if (done != FALSE)
*verify_completed = TRUE;
}
static void verify_finger_selected(GObject *object, const char *name, void *user_data)
{
g_print("Verifying: %s\n", name);
}
static void do_verify(DBusGProxy *dev)
{
GError *error = NULL;
gboolean verify_completed = FALSE;
dbus_g_proxy_add_signal(dev, "VerifyStatus", G_TYPE_STRING, G_TYPE_BOOLEAN, NULL);
dbus_g_proxy_add_signal(dev, "VerifyFingerSelected", G_TYPE_INT, NULL);
dbus_g_proxy_connect_signal(dev, "VerifyStatus", G_CALLBACK(verify_result),
&verify_completed, NULL);
dbus_g_proxy_connect_signal(dev, "VerifyFingerSelected", G_CALLBACK(verify_finger_selected),
NULL, NULL);
if (!net_reactivated_Fprint_Device_verify_start(dev, finger_name, &error)) {
g_print("VerifyStart failed: %s\n", error->message);
exit (1);
}
while (!verify_completed)
g_main_context_iteration(NULL, TRUE);
dbus_g_proxy_disconnect_signal(dev, "VerifyStatus", G_CALLBACK(verify_result), &verify_completed);
dbus_g_proxy_disconnect_signal(dev, "VerifyFingerSelected", G_CALLBACK(verify_finger_selected), NULL);
if (!net_reactivated_Fprint_Device_verify_stop(dev, &error)) {
g_print("VerifyStop failed: %s\n", error->message);
exit (1);
}
}
static void release_device(DBusGProxy *dev)
{
GError *error = NULL;
if (!net_reactivated_Fprint_Device_release(dev, &error)) {
g_print("ReleaseDevice failed: %s\n", error->message);
exit (1);
}
}
static const GOptionEntry entries[] = {
{ "finger", 'f', 0, G_OPTION_ARG_STRING, &finger_name, "Finger selected to verify (default is automatic)", NULL },
{"g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL},
{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &usernames, NULL, "[username]" },
{ NULL }
};
int main(int argc, char **argv)
{
GOptionContext *context;
GError *err = NULL;
DBusGProxy *dev;
char *username;
#if !GLIB_CHECK_VERSION (2, 36, 0)
g_type_init();
#endif
dbus_g_object_register_marshaller (fprintd_marshal_VOID__STRING_BOOLEAN,
G_TYPE_NONE, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INVALID);
context = g_option_context_new ("Verify a fingerprint");
g_option_context_add_main_entries (context, entries, NULL);
if (g_option_context_parse (context, &argc, &argv, &err) == FALSE) {
g_print ("couldn't parse command-line options: %s\n", err->message);
g_error_free (err);
return 1;
}
if (usernames == NULL) {
username = "";
} else {
username = usernames[0];
}
if (g_fatal_warnings) {
GLogLevelFlags fatal_mask;
fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
g_log_set_always_fatal (fatal_mask);
}
create_manager();
dev = open_device(username);
find_finger(dev, username);
do_verify(dev);
release_device(dev);
return 0;
}