[gnome-keyring/wip/dueno/ssh-agent: 1/2] ssh-agent: add test for ssh-agent interaction



commit e8aabea3e1c73732113efd0b0bfa8bd20e3dbacc
Author: Daiki Ueno <dueno src gnome org>
Date:   Mon Feb 12 17:59:16 2018 +0100

    ssh-agent: add test for ssh-agent interaction

 daemon/ssh-agent/Makefile.am                  |   12 +-
 daemon/ssh-agent/gkd-ssh-agent-process.c      |  126 ++++++++++-
 daemon/ssh-agent/gkd-ssh-agent.c              |   92 --------
 daemon/ssh-agent/test-gkd-ssh-agent-process.c |  276 +++++++++++++++++++++++++
 4 files changed, 399 insertions(+), 107 deletions(-)
---
diff --git a/daemon/ssh-agent/Makefile.am b/daemon/ssh-agent/Makefile.am
index 73a2edf..b58d465 100644
--- a/daemon/ssh-agent/Makefile.am
+++ b/daemon/ssh-agent/Makefile.am
@@ -28,15 +28,21 @@ ssh_agent_CFLAGS = \
        $(DAEMON_CFLAGS)
 
 ssh_agent_LIBS = \
-       $(DAEMON_LIBS) \
-       libgkd-ssh-agent.la
+       libgkd-ssh-agent.la \
+       libegg-buffer.la \
+       $(DAEMON_LIBS)
 
 ssh_agent_TESTS = \
-       test-gkd-ssh-openssh
+       test-gkd-ssh-openssh \
+       test-gkd-ssh-agent-process
 
 test_gkd_ssh_openssh_SOURCES = daemon/ssh-agent/test-gkd-ssh-openssh.c
 test_gkd_ssh_openssh_CFLAGS = $(ssh_agent_CFLAGS)
 test_gkd_ssh_openssh_LDADD = $(ssh_agent_LIBS)
 
+test_gkd_ssh_agent_process_SOURCES = daemon/ssh-agent/test-gkd-ssh-agent-process.c
+test_gkd_ssh_agent_process_CFLAGS = $(ssh_agent_CFLAGS)
+test_gkd_ssh_agent_process_LDADD = $(ssh_agent_LIBS)
+
 check_PROGRAMS += $(ssh_agent_TESTS)
 TESTS += $(ssh_agent_TESTS)
diff --git a/daemon/ssh-agent/gkd-ssh-agent-process.c b/daemon/ssh-agent/gkd-ssh-agent-process.c
index 52ddc2e..03a9846 100644
--- a/daemon/ssh-agent/gkd-ssh-agent-process.c
+++ b/daemon/ssh-agent/gkd-ssh-agent-process.c
@@ -119,6 +119,65 @@ gkd_ssh_agent_process_class_init (GkdSshAgentProcessClass *klass)
                                                              G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
 }
 
+static gboolean
+read_all (int fd, guchar *buf, int len)
+{
+       int all = len;
+       int res;
+
+       while (len > 0) {
+
+               res = read (fd, buf, len);
+
+               if (res < 0) {
+                       if (errno == EAGAIN || errno == EINTR)
+                               continue;
+                       g_warning ("couldn't read %u bytes from client: %s", all,
+                                  g_strerror (errno));
+                       return FALSE;
+               } else if (res == 0) {
+                       return FALSE;
+               } else  {
+                       len -= res;
+                       buf += res;
+               }
+       }
+
+       return TRUE;
+}
+
+static gboolean
+write_all (int fd,
+          const guchar *buf,
+          int len,
+          const gchar *where)
+{
+       int all = len;
+       int res;
+
+       while (len > 0) {
+
+               res = write (fd, buf, len);
+               if (res < 0) {
+                       if (errno == EAGAIN || errno == EINTR)
+                               continue;
+                       if (errno != EPIPE) {
+                               g_warning ("couldn't write %u bytes to %s: %s", all,
+                                          where, g_strerror (errno));
+                       }
+                       return FALSE;
+               } else if (res == 0) {
+                       g_warning ("couldn't write %u bytes to %s", all, where);
+                       return FALSE;
+               } else  {
+                       len -= res;
+                       buf += res;
+               }
+       }
+
+       return TRUE;
+}
+
 static void
 on_child_watch (GPid pid,
                 gint status,
@@ -160,7 +219,7 @@ on_output_watch (gint fd,
                                g_message ("couldn't read from ssh-agent stdout: %m");
                        condition |= G_IO_ERR;
                } else if (len > 0) {
-                       gkd_ssh_agent_write_all (1, buf, len, "stdout");
+                       write_all (1, buf, len, "stdout");
                }
        }
 
@@ -204,7 +263,7 @@ on_timeout (gpointer user_data)
 }
 
 gboolean
-gkd_ssh_agent_process_connect (GkdSshAgentProcess *self)
+gkd_ssh_agent_process_connect (GkdSshAgentProcess  *self)
 {
        gboolean started = FALSE;
        struct sockaddr_un addr;
@@ -214,11 +273,13 @@ gkd_ssh_agent_process_connect (GkdSshAgentProcess *self)
 
        g_mutex_lock (&self->lock);
 
-       if (self->pid == 0 || kill (self->pid, 0) != 0)
-               started = agent_start_inlock (self);
-
-       addr.sun_family = AF_UNIX;
-       g_strlcpy (addr.sun_path, self->path, sizeof (addr.sun_path));
+       if (self->pid == 0 || kill (self->pid, 0) != 0) {
+               if (!agent_start_inlock (self)) {
+                       g_mutex_unlock (&self->lock);
+                       return FALSE;
+               }
+               started = TRUE;
+       }
 
        if (started && !self->ready) {
                source = g_timeout_add_seconds (5, on_timeout, &timedout);
@@ -227,24 +288,65 @@ gkd_ssh_agent_process_connect (GkdSshAgentProcess *self)
                g_source_remove (source);
        }
 
-       if (!self->ready)
+       if (!self->ready) {
+               g_mutex_unlock (&self->lock);
                return FALSE;
+       }
 
        sock = socket (AF_UNIX, SOCK_STREAM, 0);
-       g_return_val_if_fail (sock >= 0, -1);
+       if (sock < 0) {
+               g_mutex_unlock (&self->lock);
+               return FALSE;
+       }
+
+       addr.sun_family = AF_UNIX;
+       g_strlcpy (addr.sun_path, self->path, sizeof (addr.sun_path));
 
        if (connect (sock, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
                g_message ("couldn't connect to ssh-agent socket at: %s: %s",
                           addr.sun_path, g_strerror (errno));
                close (sock);
-               sock = -1;
+               g_mutex_unlock (&self->lock);
+               return FALSE;
        }
 
        self->socket_fd = sock;
-
        g_mutex_unlock (&self->lock);
 
-       return sock != -1;
+       return TRUE;
+}
+
+gboolean
+gkd_ssh_agent_read_packet (gint fd,
+                           EggBuffer *buffer)
+{
+       guint32 packet_size;
+
+       egg_buffer_reset (buffer);
+       egg_buffer_resize (buffer, 4);
+       if (!read_all (fd, buffer->buf, 4))
+               return FALSE;
+
+       if (!egg_buffer_get_uint32 (buffer, 0, NULL, &packet_size) ||
+           packet_size < 1) {
+               g_warning ("invalid packet size from client");
+               return FALSE;
+       }
+
+       egg_buffer_resize (buffer, packet_size + 4);
+       if (!read_all (fd, buffer->buf + 4, packet_size))
+               return FALSE;
+
+       return TRUE;
+}
+
+gboolean
+gkd_ssh_agent_write_packet (gint fd,
+                            EggBuffer *buffer)
+{
+       if (!egg_buffer_set_uint32 (buffer, 0, buffer->len - 4))
+               g_return_val_if_reached (FALSE);
+       return write_all (fd, buffer->buf, buffer->len, "client");
 }
 
 gboolean
diff --git a/daemon/ssh-agent/gkd-ssh-agent.c b/daemon/ssh-agent/gkd-ssh-agent.c
index 6e31bc6..765ddf9 100644
--- a/daemon/ssh-agent/gkd-ssh-agent.c
+++ b/daemon/ssh-agent/gkd-ssh-agent.c
@@ -53,98 +53,6 @@ static char process_path[1024] = { 0, };
 static GkdSshAgentProcess *process = NULL;
 
 static gboolean
-read_all (int fd, guchar *buf, int len)
-{
-       int all = len;
-       int res;
-
-       while (len > 0) {
-
-               res = read (fd, buf, len);
-
-               if (res < 0) {
-                       if (errno == EAGAIN || errno == EINTR)
-                               continue;
-                       g_warning ("couldn't read %u bytes from client: %s", all,
-                                  g_strerror (errno));
-                       return FALSE;
-               } else if (res == 0) {
-                       return FALSE;
-               } else  {
-                       len -= res;
-                       buf += res;
-               }
-       }
-
-       return TRUE;
-}
-
-gboolean
-gkd_ssh_agent_write_all (int fd,
-                         const guchar *buf,
-                         int len,
-                         const gchar *where)
-{
-       int all = len;
-       int res;
-
-       while (len > 0) {
-
-               res = write (fd, buf, len);
-               if (res < 0) {
-                       if (errno == EAGAIN || errno == EINTR)
-                               continue;
-                       if (errno != EPIPE) {
-                               g_warning ("couldn't write %u bytes to %s: %s", all,
-                                          where, g_strerror (errno));
-                       }
-                       return FALSE;
-               } else if (res == 0) {
-                       g_warning ("couldn't write %u bytes to %s", all, where);
-                       return FALSE;
-               } else  {
-                       len -= res;
-                       buf += res;
-               }
-       }
-
-       return TRUE;
-}
-
-gboolean
-gkd_ssh_agent_read_packet (gint fd,
-                           EggBuffer *buffer)
-{
-       guint32 packet_size;
-
-       egg_buffer_reset (buffer);
-       egg_buffer_resize (buffer, 4);
-       if (!read_all (fd, buffer->buf, 4))
-               return FALSE;
-
-       if (!egg_buffer_get_uint32 (buffer, 0, NULL, &packet_size) ||
-           packet_size < 1) {
-               g_warning ("invalid packet size from client");
-               return FALSE;
-       }
-
-       egg_buffer_resize (buffer, packet_size + 4);
-       if (!read_all (fd, buffer->buf + 4, packet_size))
-               return FALSE;
-
-       return TRUE;
-}
-
-gboolean
-gkd_ssh_agent_write_packet (gint fd,
-                            EggBuffer *buffer)
-{
-       if (!egg_buffer_set_uint32 (buffer, 0, buffer->len - 4))
-               g_return_val_if_reached (FALSE);
-       return gkd_ssh_agent_write_all (fd, buffer->buf, buffer->len, "client");
-}
-
-static gboolean
 agent_relay (GkdSshAgentCall *call)
 {
        return gkd_ssh_agent_process_call (call->process, call->req, call->resp);
diff --git a/daemon/ssh-agent/test-gkd-ssh-agent-process.c b/daemon/ssh-agent/test-gkd-ssh-agent-process.c
new file mode 100644
index 0000000..5013c37
--- /dev/null
+++ b/daemon/ssh-agent/test-gkd-ssh-agent-process.c
@@ -0,0 +1,276 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+   Copyright (C) 2008 Stefan Walter
+
+   The Gnome Keyring Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Keyring Library 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   <http://www.gnu.org/licenses/>.
+
+   Author: Stef Walter <stef memberwebs com>
+*/
+
+#include "config.h"
+
+#include "daemon/ssh-agent/gkd-ssh-agent-private.h"
+
+#include <glib.h>
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+typedef struct {
+       EggBuffer req;
+       EggBuffer resp;
+       GkdSshAgentProcess *process;
+} Test;
+
+/* RSA private key blob decoded from pkcs11/ssh-store/fixtures/id_rsa_plain */
+static guint8 blob[4*6 + 0x101 + 0x1 + 0x101 + 0x80 + 0x81 + 0x81] = {
+       /* n */
+       0x00, 0x00, 0x01, 0x01, 0x00, 0xa0, 0x3e, 0x95, 0x2a, 0xa9, 0x21, 0x6b,
+       0x2e, 0xa9, 0x28, 0x74, 0x91, 0x8c, 0x01, 0x96, 0x59, 0xf1, 0x4f, 0x53,
+       0xcc, 0x5f, 0xb2, 0x2d, 0xa0, 0x9c, 0xec, 0x0f, 0xfc, 0x1d, 0x54, 0x1c,
+       0x3a, 0x33, 0xb7, 0x1d, 0xdc, 0xce, 0x13, 0xbe, 0xa7, 0x2f, 0xdf, 0x4e,
+       0x58, 0x42, 0x9d, 0x23, 0xf5, 0x8e, 0xc8, 0xe4, 0xad, 0x52, 0x19, 0x72,
+       0x7c, 0xda, 0x87, 0x67, 0xd4, 0x34, 0x51, 0x51, 0x81, 0x2e, 0x3e, 0x8d,
+       0x13, 0x81, 0xb6, 0xf6, 0xe0, 0x1e, 0xc4, 0xbb, 0xd9, 0x5d, 0x44, 0xeb,
+       0xe6, 0x68, 0x81, 0x5f, 0xa6, 0x04, 0x95, 0x96, 0x02, 0x1c, 0x34, 0x88,
+       0xfa, 0xe6, 0x43, 0x72, 0xaf, 0x9b, 0x7f, 0x03, 0xdc, 0xf0, 0x72, 0xa3,
+       0x96, 0x3b, 0xc8, 0xa3, 0xb9, 0x90, 0x81, 0xb6, 0x2e, 0x5a, 0x18, 0x2e,
+       0x3a, 0x2c, 0x27, 0x91, 0x78, 0xb3, 0x1d, 0xb1, 0x87, 0x4b, 0xb3, 0xdb,
+       0x05, 0xcd, 0xb6, 0x76, 0x35, 0x6f, 0x9c, 0x61, 0x7b, 0x6f, 0x95, 0x12,
+       0x4b, 0x26, 0xf4, 0xe0, 0x7e, 0x15, 0x76, 0x94, 0x91, 0x90, 0xb6, 0x7d,
+       0x0a, 0xd3, 0x36, 0x8f, 0x19, 0x18, 0x52, 0x50, 0x48, 0x57, 0x7c, 0x91,
+       0x48, 0x48, 0x7d, 0xb5, 0x03, 0x26, 0x69, 0x58, 0xb9, 0x9f, 0xaf, 0xbc,
+       0x73, 0x3e, 0x03, 0x72, 0xdc, 0xf6, 0xb1, 0xf2, 0x5b, 0x82, 0x0f, 0x69,
+       0x1c, 0xb1, 0x15, 0x07, 0x22, 0x46, 0x66, 0xfe, 0x65, 0x0a, 0x94, 0xda,
+       0xe4, 0x9d, 0x39, 0x70, 0x21, 0x83, 0x5e, 0xe5, 0xb2, 0x4b, 0x97, 0xfe,
+       0xaf, 0x32, 0x08, 0x8e, 0x47, 0xcb, 0x97, 0x83, 0x89, 0xc0, 0xb6, 0xdb,
+       0x6a, 0x14, 0x31, 0xd2, 0x53, 0xb5, 0x88, 0x30, 0x5f, 0x87, 0x50, 0x09,
+       0x4f, 0x13, 0x20, 0x25, 0xa1, 0xc5, 0xbd, 0xf1, 0xe1, 0x10, 0x95, 0xfa,
+       0x0e, 0xc3, 0xf7, 0xdf, 0xad, 0x90, 0x8b, 0xef, 0xfb,
+       /* e */
+       0x00, 0x00, 0x00, 0x01, 0x23,
+       /* d */
+       0x00, 0x00, 0x01, 0x01, 0x00, 0x9b, 0xaa, 0x82, 0x46, 0xb2, 0xed, 0x43,
+       0x8c, 0x69, 0xcf, 0x87, 0x2e, 0x4d, 0x7d, 0xe2, 0x83, 0x42, 0x2f, 0xcd,
+       0xbf, 0x38, 0x63, 0xf1, 0xcf, 0x39, 0x5a, 0x58, 0xab, 0xc4, 0xb8, 0x1b,
+       0x6b, 0xbd, 0x35, 0x8a, 0xb9, 0x3d, 0x37, 0xc0, 0x85, 0x27, 0x30, 0xb2,
+       0x81, 0x9f, 0xcb, 0xd9, 0xc9, 0xf8, 0x6b, 0x61, 0xcc, 0xf0, 0xab, 0x01,
+       0x80, 0x99, 0xc5, 0x5d, 0x8c, 0x50, 0x14, 0x7b, 0x0f, 0xc6, 0x85, 0xe8,
+       0x21, 0x93, 0xf3, 0x90, 0xbc, 0x75, 0xa9, 0x2b, 0x82, 0xb2, 0x60, 0x35,
+       0x9d, 0xff, 0x1e, 0x97, 0x6e, 0x13, 0x14, 0xf8, 0x1f, 0x4e, 0x99, 0x6f,
+       0x1f, 0x9d, 0xdb, 0x1e, 0xf3, 0xbb, 0x9f, 0xf5, 0x1f, 0xc5, 0x01, 0xa6,
+       0x3a, 0x2b, 0x72, 0x73, 0x29, 0x4a, 0x8c, 0xa2, 0x58, 0xe9, 0xce, 0x58,
+       0xca, 0xcb, 0xce, 0xaa, 0x92, 0x82, 0x1c, 0xd8, 0x57, 0x8b, 0x5e, 0x42,
+       0x79, 0x21, 0x0e, 0x63, 0x13, 0x0e, 0x03, 0xff, 0x2f, 0x7f, 0x64, 0xf6,
+       0x82, 0xe1, 0xfe, 0x0b, 0xc3, 0x1e, 0x4c, 0x50, 0x11, 0x3f, 0xc8, 0x8a,
+       0xba, 0xcc, 0xde, 0x24, 0xf7, 0xae, 0x96, 0x6c, 0x5e, 0x3b, 0x00, 0xfa,
+       0xf0, 0x0e, 0xac, 0x3a, 0xeb, 0xb1, 0xab, 0x8f, 0x3f, 0xdb, 0x80, 0xb3,
+       0x06, 0x91, 0x18, 0xe1, 0xf5, 0x3b, 0xec, 0x5d, 0x01, 0xcf, 0xd0, 0x1f,
+       0xaf, 0xe3, 0xd9, 0x12, 0xba, 0x7b, 0x0f, 0xee, 0x20, 0x29, 0x74, 0x57,
+       0xdc, 0x58, 0x75, 0xd4, 0xb0, 0xf4, 0xb4, 0xa4, 0x93, 0x48, 0x2b, 0x7b,
+       0x6b, 0x1d, 0x77, 0xbc, 0xf3, 0xfe, 0xbd, 0xad, 0xd6, 0x83, 0x05, 0x16,
+       0xca, 0xbe, 0x31, 0xa4, 0x39, 0x53, 0x29, 0xf3, 0xd3, 0x39, 0xb0, 0xa5,
+       0xef, 0xf0, 0xc9, 0x08, 0xd6, 0x63, 0x52, 0x0b, 0xcb, 0xfc, 0x1c, 0x21,
+       0xd3, 0xa9, 0x2f, 0x23, 0x92, 0x3d, 0x46, 0x8c, 0x4b,
+       /* iqmp */
+       0x00, 0x00, 0x00, 0x80, 0x15, 0x40, 0xcc, 0xa4, 0x83, 0xdf, 0x26, 0xbe,
+       0x55, 0x82, 0x85, 0x0f, 0x71, 0x3c, 0x19, 0xa8, 0x8b, 0x42, 0x80, 0xa5,
+       0x24, 0x5d, 0xad, 0xf5, 0x99, 0x33, 0xaf, 0x7c, 0xb2, 0x27, 0xae, 0x7b,
+       0x0b, 0x0b, 0xa0, 0x03, 0xfd, 0xae, 0x53, 0x6f, 0xf1, 0xdd, 0x83, 0x54,
+       0xde, 0xf2, 0xbd, 0x87, 0x2c, 0xa9, 0x4d, 0x7b, 0xa5, 0x6e, 0xdb, 0x5e,
+       0x89, 0xf4, 0x5c, 0x79, 0x22, 0xc3, 0xc4, 0x40, 0x50, 0xeb, 0xb7, 0xf4,
+       0x17, 0x78, 0x2f, 0x06, 0xa5, 0x3a, 0x65, 0x4d, 0x85, 0x98, 0x3e, 0xd8,
+       0x4d, 0x3b, 0xfc, 0xd8, 0x9b, 0xe5, 0xd1, 0x47, 0xb6, 0xe3, 0xda, 0x2e,
+       0xc5, 0x18, 0xce, 0x37, 0xd9, 0xd7, 0x9a, 0xbf, 0xba, 0xa9, 0xef, 0xf2,
+       0xaf, 0x9b, 0xc8, 0x46, 0x57, 0x11, 0x8c, 0xa9, 0x5f, 0x68, 0x8c, 0x43,
+       0x2f, 0xb5, 0x7a, 0x39, 0x38, 0x30, 0x79, 0xd5, 0x30, 0xa8, 0x2b, 0x98,
+       /* p */
+       0x00, 0x00, 0x00, 0x81, 0x00, 0xcc, 0x50, 0xb1, 0x2c, 0x5f, 0xe4, 0x02,
+       0x85, 0x7d, 0xce, 0x77, 0xd8, 0x27, 0xc1, 0xf6, 0xee, 0xe2, 0x2b, 0x7b,
+       0x29, 0x83, 0x95, 0xf1, 0x5e, 0x3d, 0xe5, 0xa9, 0x75, 0x62, 0xc6, 0x84,
+       0xc9, 0x97, 0x26, 0x70, 0xf4, 0x0d, 0x28, 0x6a, 0xc6, 0x88, 0x7c, 0xa3,
+       0x0d, 0x35, 0xa3, 0x8f, 0xdc, 0x34, 0x4c, 0x78, 0x6b, 0xcc, 0x5d, 0x99,
+       0x7e, 0x45, 0xb0, 0xdf, 0xe3, 0x77, 0x48, 0x77, 0xd8, 0xa9, 0x1c, 0x74,
+       0xf9, 0xbc, 0xcc, 0x82, 0xdb, 0x44, 0x10, 0x96, 0xda, 0x00, 0x23, 0xaa,
+       0x04, 0x93, 0xcc, 0x98, 0xec, 0x26, 0x8b, 0x7d, 0x08, 0xf4, 0x82, 0xdc,
+       0x9a, 0xc4, 0x8c, 0xc8, 0xe9, 0x3e, 0x5b, 0xd6, 0xc7, 0x28, 0xf4, 0x38,
+       0x3a, 0x3c, 0x08, 0x56, 0xbb, 0xa2, 0xca, 0xfb, 0x05, 0xa0, 0xb7, 0xe1,
+       0x70, 0x59, 0xb4, 0x86, 0x2b, 0x29, 0x89, 0xb5, 0x82, 0x2a, 0x79, 0x61,
+       0x51,
+       /* q */
+       0x00, 0x00, 0x00, 0x81, 0x00, 0xc8, 0xc7, 0xe6, 0x93, 0x90, 0x59, 0xe7,
+       0x54, 0x1b, 0xcf, 0x9c, 0xb0, 0x07, 0x80, 0x37, 0xcd, 0xdf, 0x65, 0xf4,
+       0x29, 0x1e, 0x4a, 0x93, 0x73, 0xd1, 0x7b, 0x47, 0x1d, 0x36, 0x87, 0x89,
+       0x1d, 0xbf, 0xd5, 0x1e, 0x02, 0xc2, 0xd1, 0x2b, 0xb3, 0x67, 0x07, 0x65,
+       0xf9, 0xbc, 0xcb, 0x74, 0x4c, 0x83, 0x68, 0xa8, 0x6d, 0x30, 0x68, 0x8f,
+       0xb5, 0xb9, 0x44, 0x86, 0xb8, 0xde, 0x4e, 0xfc, 0x02, 0x1e, 0x9c, 0x05,
+       0x3b, 0x23, 0x1b, 0xdf, 0x79, 0x58, 0x73, 0x51, 0x27, 0xf0, 0xbd, 0x83,
+       0x34, 0x38, 0xcb, 0xd0, 0x20, 0x12, 0xcd, 0x1a, 0x07, 0x6e, 0xf7, 0x0a,
+       0x92, 0x29, 0xff, 0x2f, 0xbf, 0x30, 0x2a, 0x69, 0x15, 0x4d, 0x8e, 0x6e,
+       0x17, 0x26, 0x7b, 0x43, 0xfe, 0x52, 0xd1, 0x83, 0x65, 0x19, 0x22, 0x8b,
+       0xd3, 0x6f, 0x97, 0x51, 0x11, 0x3f, 0x17, 0xfe, 0x05, 0xcc, 0xa4, 0x49,
+       0x8b
+};
+
+static void
+setup (Test *test, gconstpointer unused)
+{
+       egg_buffer_init_full (&test->req, 128, (EggBufferAllocator)g_realloc);
+       egg_buffer_init_full (&test->resp, 128, (EggBufferAllocator)g_realloc);
+
+       unlink (BUILDDIR "/.ssh.sock");
+       test->process = gkd_ssh_agent_process_new (BUILDDIR "/.ssh.sock");
+       g_assert_nonnull (test->process);
+}
+
+static void
+teardown (Test *test, gconstpointer unused)
+{
+       g_clear_object (&test->process);
+       unlink (BUILDDIR "/.ssh.sock");
+
+       egg_buffer_uninit (&test->req);
+       egg_buffer_uninit (&test->resp);
+}
+
+static void
+test_connect (Test *test, gconstpointer unused)
+{
+       gboolean ret = gkd_ssh_agent_process_connect (test->process);
+       g_assert_true (ret);
+}
+
+static void
+call_request_identities (Test *test, gsize count)
+{
+       uint32_t length;
+       unsigned char code;
+       size_t offset;
+       gboolean ret;
+
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       egg_buffer_add_uint32 (&test->req, 1);
+       egg_buffer_add_byte (&test->req, GKD_SSH_OP_REQUEST_IDENTITIES);
+       ret = gkd_ssh_agent_process_call (test->process, &test->req, &test->resp);
+       g_assert_true (ret);
+
+       offset = 0;
+       egg_buffer_get_uint32 (&test->resp, offset, &offset, &length);
+       g_assert_cmpint (length, >, 0);
+
+       code = 0;
+       egg_buffer_get_byte (&test->resp, offset, &offset, &code);
+       g_assert_cmpint (code, ==, GKD_SSH_RES_IDENTITIES_ANSWER);
+
+       egg_buffer_get_uint32 (&test->resp, offset, &offset, &length);
+       g_assert_cmpint (length, ==, count);
+}
+
+static void
+call_add_identity (Test *test)
+{
+       uint32_t length;
+       unsigned char code;
+       size_t offset;
+       gboolean ret;
+
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       egg_buffer_add_uint32 (&test->req, 0);
+       egg_buffer_add_byte (&test->req, GKD_SSH_OP_ADD_IDENTITY);
+       egg_buffer_add_string (&test->req, "ssh-rsa");
+       egg_buffer_append (&test->req, blob, G_N_ELEMENTS(blob));
+       egg_buffer_add_string (&test->req, "comment");
+       egg_buffer_set_uint32 (&test->req, 0, test->req.len);
+       ret = gkd_ssh_agent_process_call (test->process, &test->req, &test->resp);
+       g_assert_true (ret);
+
+       offset = 0;
+       egg_buffer_get_uint32 (&test->resp, offset, &offset, &length);
+       g_assert_cmpint (length, ==, 1);
+
+       code = 0;
+       egg_buffer_get_byte (&test->resp, offset, &offset, &code);
+       g_assert_cmpint (code, ==, GKD_SSH_RES_SUCCESS);
+}
+
+static void
+call_remove_all_identities (Test *test)
+{
+       uint32_t length;
+       unsigned char code;
+       size_t offset;
+       gboolean ret;
+
+       egg_buffer_reset (&test->req);
+       egg_buffer_reset (&test->resp);
+
+       egg_buffer_add_uint32 (&test->req, 1);
+       egg_buffer_add_byte (&test->req, GKD_SSH_OP_REMOVE_ALL_IDENTITIES);
+       ret = gkd_ssh_agent_process_call (test->process, &test->req, &test->resp);
+       g_assert_true (ret);
+
+       offset = 0;
+       egg_buffer_get_uint32 (&test->resp, offset, &offset, &length);
+       g_assert_cmpint (length, >, 0);
+
+       code = 0;
+       egg_buffer_get_byte (&test->resp, offset, &offset, &code);
+       g_assert_cmpint (code, ==, GKD_SSH_RES_SUCCESS);
+}
+
+static void
+test_list (Test *test, gconstpointer unused)
+{
+       gboolean ret;
+
+       ret = gkd_ssh_agent_process_connect (test->process);
+       g_assert_true (ret);
+
+       call_request_identities(test, 0);
+}
+
+static void
+test_load (Test *test, gconstpointer unused)
+{
+       gboolean ret;
+
+       ret = gkd_ssh_agent_process_connect (test->process);
+       g_assert_true (ret);
+
+       call_add_identity (test);
+       call_request_identities (test, 1);
+
+       call_remove_all_identities (test);
+       call_request_identities (test, 0);
+}
+
+int
+main (int argc, char **argv)
+{
+       g_test_init (&argc, &argv, NULL);
+
+       g_test_add ("/ssh-agent/process/connect", Test, NULL, setup, test_connect, teardown);
+       g_test_add ("/ssh-agent/process/list", Test, NULL, setup, test_list, teardown);
+       g_test_add ("/ssh-agent/process/load", Test, NULL, setup, test_load, teardown);
+
+       return g_test_run ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]