~noskcaj/ubuntu/vivid/gnome-keyring/3.15.90

« back to all changes in this revision

Viewing changes to pkcs11/ssh-store/tests/test-ssh-openssh.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-14 22:13:02 UTC
  • mfrom: (1.3.1)
  • mto: (80.2.8 experimental) (1.1.77)
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: package-import@ubuntu.com-20120514221302-0l3gjmqpe6xopond
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
   Copyright (C) 2008 Stefan Walter
 
4
 
 
5
   The Gnome Keyring Library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public License as
 
7
   published by the Free Software Foundation; either version 2 of the
 
8
   License, or (at your option) any later version.
 
9
 
 
10
   The Gnome Keyring Library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public
 
16
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
 
17
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
 
 
20
   Author: Stef Walter <stef@memberwebs.com>
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "egg/egg-secure-memory.h"
 
26
 
 
27
#include "ssh-store/gkm-ssh-openssh.h"
 
28
 
 
29
#include "gkm/gkm-sexp.h"
 
30
 
 
31
#include <glib.h>
 
32
 
 
33
#include <stdlib.h>
 
34
#include <stdio.h>
 
35
#include <string.h>
 
36
 
 
37
EGG_SECURE_GLIB_DEFINITIONS ();
 
38
 
 
39
static const gchar *PRIVATE_FILES[] = {
 
40
        SRCDIR "/files/id_rsa_encrypted",
 
41
        SRCDIR "/files/id_rsa_plain",
 
42
        SRCDIR "/files/id_dsa_encrypted",
 
43
        SRCDIR "/files/id_dsa_plain"
 
44
};
 
45
 
 
46
static const gchar *PUBLIC_FILES[] = {
 
47
        SRCDIR "/files/id_rsa_test.pub",
 
48
        SRCDIR "/files/id_dsa_test.pub"
 
49
};
 
50
 
 
51
#define COMMENT "A public key comment"
 
52
 
 
53
static void
 
54
test_parse_public (void)
 
55
{
 
56
        gcry_sexp_t sexp;
 
57
        gchar *comment;
 
58
        gchar *data;
 
59
        gsize n_data;
 
60
        int algorithm;
 
61
        gboolean is_private;
 
62
        guint i;
 
63
        GkmDataResult res;
 
64
 
 
65
 
 
66
        for (i = 0; i < G_N_ELEMENTS (PUBLIC_FILES); ++i) {
 
67
 
 
68
                if (!g_file_get_contents (PUBLIC_FILES[i], &data, &n_data, NULL))
 
69
                        g_assert_not_reached ();
 
70
 
 
71
                res = gkm_ssh_openssh_parse_public_key (data, n_data, &sexp, &comment);
 
72
                if (res != GKM_DATA_SUCCESS) {
 
73
                        g_warning ("couldn't parse public key: %s", PUBLIC_FILES[i]);
 
74
                        g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
 
75
                }
 
76
 
 
77
                if (!gkm_sexp_parse_key (sexp, &algorithm, &is_private, NULL))
 
78
                        g_assert_not_reached ();
 
79
 
 
80
                g_assert_cmpstr (comment, ==, COMMENT);
 
81
                g_assert_cmpint (algorithm, !=, 0);
 
82
                g_assert (!is_private);
 
83
 
 
84
                g_free (data);
 
85
                g_free (comment);
 
86
                gcry_sexp_release (sexp);
 
87
        }
 
88
}
 
89
 
 
90
static void
 
91
test_parse_private (void)
 
92
{
 
93
        gcry_sexp_t sexp;
 
94
        gchar *data;
 
95
        gsize n_data;
 
96
        int algorithm;
 
97
        gboolean is_private;
 
98
        guint i;
 
99
        GkmDataResult res;
 
100
 
 
101
 
 
102
        for (i = 0; i < G_N_ELEMENTS (PRIVATE_FILES); ++i) {
 
103
 
 
104
                if (!g_file_get_contents (PRIVATE_FILES[i], &data, &n_data, NULL))
 
105
                        g_assert_not_reached ();
 
106
 
 
107
                res = gkm_ssh_openssh_parse_private_key (data, n_data, "password", 8, &sexp);
 
108
                if (res != GKM_DATA_SUCCESS) {
 
109
                        g_warning ("couldn't parse private key: %s", PRIVATE_FILES[i]);
 
110
                        g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
 
111
                }
 
112
 
 
113
                if (!gkm_sexp_parse_key (sexp, &algorithm, &is_private, NULL))
 
114
                        g_assert_not_reached ();
 
115
 
 
116
                g_assert_cmpint (algorithm, !=, 0);
 
117
                g_assert (is_private);
 
118
 
 
119
                g_free (data);
 
120
                gcry_sexp_release (sexp);
 
121
        }
 
122
}
 
123
 
 
124
int
 
125
main (int argc, char **argv)
 
126
{
 
127
        g_type_init ();
 
128
        g_test_init (&argc, &argv, NULL);
 
129
 
 
130
        g_test_add_func ("/ssh-store/openssh/parse_private", test_parse_private);
 
131
        g_test_add_func ("/ssh-store/openssh/parse_public", test_parse_public);
 
132
 
 
133
        return g_test_run ();
 
134
}