1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/* -*- 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 "egg/egg-secure-memory.h"
#include "ssh-store/gkm-ssh-openssh.h"
#include "gkm/gkm-sexp.h"
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
static const gchar *PRIVATE_FILES[] = {
SRCDIR "/fixtures/id_rsa_encrypted",
SRCDIR "/fixtures/id_rsa_plain",
SRCDIR "/fixtures/id_dsa_encrypted",
SRCDIR "/fixtures/id_dsa_plain"
};
static const gchar *PUBLIC_FILES[] = {
SRCDIR "/fixtures/id_rsa_test.pub",
SRCDIR "/fixtures/id_dsa_test.pub"
};
#define COMMENT "A public key comment"
static void
test_parse_public (void)
{
gcry_sexp_t sexp;
gchar *comment;
gchar *data;
gsize n_data;
int algorithm;
gboolean is_private;
guint i;
GkmDataResult res;
for (i = 0; i < G_N_ELEMENTS (PUBLIC_FILES); ++i) {
if (!g_file_get_contents (PUBLIC_FILES[i], &data, &n_data, NULL))
g_assert_not_reached ();
res = gkm_ssh_openssh_parse_public_key (data, n_data, &sexp, &comment);
if (res != GKM_DATA_SUCCESS) {
g_warning ("couldn't parse public key: %s", PUBLIC_FILES[i]);
g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
}
if (!gkm_sexp_parse_key (sexp, &algorithm, &is_private, NULL))
g_assert_not_reached ();
g_assert_cmpstr (comment, ==, COMMENT);
g_assert_cmpint (algorithm, !=, 0);
g_assert (!is_private);
g_free (data);
g_free (comment);
gcry_sexp_release (sexp);
}
}
static void
test_parse_private (void)
{
gcry_sexp_t sexp;
gchar *data;
gsize n_data;
int algorithm;
gboolean is_private;
GBytes *bytes;
guint i;
GkmDataResult res;
for (i = 0; i < G_N_ELEMENTS (PRIVATE_FILES); ++i) {
if (!g_file_get_contents (PRIVATE_FILES[i], &data, &n_data, NULL))
g_assert_not_reached ();
bytes = g_bytes_new_take (data, n_data);
res = gkm_ssh_openssh_parse_private_key (bytes, "password", 8, &sexp);
g_bytes_unref (bytes);
if (res != GKM_DATA_SUCCESS) {
g_warning ("couldn't parse private key: %s", PRIVATE_FILES[i]);
g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
}
if (!gkm_sexp_parse_key (sexp, &algorithm, &is_private, NULL))
g_assert_not_reached ();
g_assert_cmpint (algorithm, !=, 0);
g_assert (is_private);
gcry_sexp_release (sexp);
}
}
int
main (int argc, char **argv)
{
#if !GLIB_CHECK_VERSION(2,35,0)
g_type_init ();
#endif
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/ssh-store/openssh/parse_private", test_parse_private);
g_test_add_func ("/ssh-store/openssh/parse_public", test_parse_public);
return g_test_run ();
}
|