minimal build
[platform/upstream/gcr.git] / gcr / tests / test-openssh.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3    Copyright (C) 2011 Collabora Ltd
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 <stefw@collabora.co.uk>
21 */
22
23 #include "config.h"
24
25 #include "gcr/gcr-base.h"
26 #include "gcr/gcr-openssh.h"
27
28 #include "egg/egg-testing.h"
29
30 #include <gcrypt.h>
31 #include <glib.h>
32 #include <string.h>
33
34 typedef struct {
35         const gchar *expected_label;
36         const gchar *expected_options;
37 } Test;
38
39 #define OPENSSH_PUBLIC_RSA1 \
40         "2048 65537 19574029774826276058535216798260123376543523095248321838931" \
41         "8476099051534660565418100376122247153936738716140984293302866595208305" \
42         "7124376564328644357957081508003798389808113087527047927841196160520784" \
43         "3971799891833860159372766201922902824211581515042106928142039998651198" \
44         "7806024885997262427984841536983221992403267030558391252672804492615887" \
45         "9294713324466630490990131504557923061505441555447586185019409756877006" \
46         "5871190731807718592844942425524851665039303855329966512492845780563670" \
47         "0617451083369174928502647995734856960603065454655489558179113130210712" \
48         "74638931037011169213563881172297734240201883475566393175838117784693 r" \
49         "sa-key@example.com\n"
50
51 #define OPENSSH_PUBLIC_RSA2 \
52         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCs8z2y0cCPYRAPkq8tAt6FC/kdfnR/p" \
53         "8B2ZoY0oiLNt7kQEwJfexgwLqTxWYd2fSDUSSDPrsqAxZAwLLS/eF04kXiJO2VfqAWFpTL" \
54         "NToERHpFF1yZQe26ELTlNNfna7LqfCRvpNDwu6AqndsT3eFt7DWvBDXbbEiTLW21Z2OFAA" \
55         "H/J2iCFn4c0a8Myf7IaMYcy5GG3mpk39kEO4aNV/67U7kfooek24ObwD0vlXzlsi5VZIUF" \
56         "OIUi0UdkNEMCtUWpfkZ1STUlmwp9HVM7xb7/9PESQKDnZdxpB09S9cIjdpDecpDlMDDEbE" \
57         "UECM1PIas3ndhB7gAN1i2JsPHTcXZ1 rsa-key@example.com\r\n" \
58         "# Comment\n"
59
60 #define OPENSSH_PUBLIC_DSA2 \
61         "ssh-dss AAAAB3NzaC1kc3MAAACBAL4z+ad0ZJYzMOQuGp00UJ+AijKhrPVUEYLcxBmFQo" \
62         "nb/KIlLSWJua4Rl9DB4tDj30Y9c/oApqC4n+FIYlUZMSnxmpvcLF6aeXOiHHPvm0EDYjjy" \
63         "VubyYQWI7CROrrzSc+x++ha3TuJEvF3PlKlZmTKKVYEkZNjwFqYysGyPxPalAAAAFQDtDS" \
64         "EF9Gvnv5fQtSbbsp7j78uVBwAAAIAtNpAg/Mbd/E2241enedB9AxAbJWZ5QYnoPe6/zx5d" \
65         "OmU7+qz8mG6tgvF8F7IgXPabuAKslzTDGS3zgaEhWicDS3CIYik2UR8hXdxfovIEqZKZe7" \
66         "u02FCEoXYCEiFUAdzDGzjI7PswgtEJWWNqKeNis3HmDDha9lMkqz/3fLZGXwAAAIEAiaRP" \
67         "YKZDMoJG+aVZ5A3R/m2gl+mYE2MsjPKXuBKcrZ6ItA9BMe4G/An0/+E3A+DuoGxdeNNMF8" \
68         "U9Dy2N8Sch/Ngtg2E/FBo5geljWobJXd1jxmPtF2WAliYJXDdIt6RBVPGL9H/KSjDmBMsV" \
69         "d42wxVJywawzypklVZjSUuWuBMI= dsa-key@example.com \n"
70
71 #define EXTRA_LINES_WITHOUT_KEY \
72         "\n# Comment\n\n" \
73         "20aa3\n" \
74         "not a key\n"
75
76 static void
77 setup (Test *test,
78        gconstpointer unused)
79 {
80
81 }
82
83 static void
84 teardown (Test *test,
85           gconstpointer unused)
86 {
87
88 }
89
90 static void
91 on_openssh_pub_parse (GckAttributes *attrs,
92                       const gchar *label,
93                       const gchar *options,
94                       GBytes *outer,
95                       gpointer user_data)
96 {
97         Test *test = user_data;
98         guint keys;
99
100         if (test->expected_label)
101                 g_assert_cmpstr (label, ==, test->expected_label);
102         if (test->expected_options)
103                 g_assert_cmpstr (options, ==, test->expected_options);
104
105         /* The block should parse properly */
106         keys = _gcr_openssh_pub_parse (outer, NULL, NULL);
107         g_assert_cmpuint (keys, ==, 1);
108 }
109
110 static void
111 test_parse_v1_rsa (Test *test,
112                    gconstpointer unused)
113 {
114         const gchar *data = OPENSSH_PUBLIC_RSA1 EXTRA_LINES_WITHOUT_KEY;
115         GBytes *bytes;
116         gint keys;
117
118         test->expected_label = "rsa-key@example.com";
119
120         bytes = g_bytes_new_static (data, strlen (data));
121         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
122         g_assert_cmpint (keys, ==, 1);
123
124         g_bytes_unref (bytes);
125 }
126
127 static void
128 test_parse_v2_rsa (Test *test,
129                    gconstpointer unused)
130 {
131         const gchar *data = OPENSSH_PUBLIC_RSA2 EXTRA_LINES_WITHOUT_KEY;
132         GBytes *bytes;
133         gint keys;
134
135         test->expected_label = "rsa-key@example.com";
136
137         bytes = g_bytes_new_static (data, strlen (data));
138         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
139         g_assert_cmpint (keys, ==, 1);
140
141         g_bytes_unref (bytes);
142 }
143
144 static void
145 test_parse_v2_dsa (Test *test,
146                gconstpointer unused)
147 {
148         const gchar *data = OPENSSH_PUBLIC_DSA2 EXTRA_LINES_WITHOUT_KEY;
149         GBytes *bytes;
150         gint keys;
151
152         test->expected_label = "dsa-key@example.com";
153
154         bytes = g_bytes_new_static (data, strlen (data));
155         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
156         g_assert_cmpint (keys, ==, 1);
157
158         g_bytes_unref (bytes);
159 }
160
161 static void
162 test_parse_v1_options (Test *test,
163                        gconstpointer unused)
164 {
165         const gchar *data = "option1,option2=\"value 2\",option3 " OPENSSH_PUBLIC_RSA1;
166         GBytes *bytes;
167         gint keys;
168
169         test->expected_options = "option1,option2=\"value 2\",option3";
170
171         bytes = g_bytes_new_static (data, strlen (data));
172         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
173         g_assert_cmpint (keys, ==, 1);
174
175         g_bytes_unref (bytes);
176 }
177
178 static void
179 test_parse_v2_options (Test *test,
180                        gconstpointer unused)
181 {
182         const gchar *data = "option1,option2=\"value 2\",option3 " OPENSSH_PUBLIC_RSA2;
183         GBytes *bytes;
184         gint keys;
185
186         test->expected_options = "option1,option2=\"value 2\",option3";
187
188         bytes = g_bytes_new_static (data, strlen (data));
189         keys = _gcr_openssh_pub_parse (bytes, on_openssh_pub_parse, test);
190         g_assert_cmpint (keys, ==, 1);
191
192         g_bytes_unref (bytes);
193 }
194
195 int
196 main (int argc, char **argv)
197 {
198 #if !GLIB_CHECK_VERSION(2,35,0)
199         g_type_init ();
200 #endif
201         g_test_init (&argc, &argv, NULL);
202         g_set_prgname ("test-gnupg-process");
203
204         g_test_add ("/gcr/openssh/parse_v1_rsa", Test, NULL, setup, test_parse_v1_rsa, teardown);
205         g_test_add ("/gcr/openssh/parse_v2_rsa", Test, NULL, setup, test_parse_v2_rsa, teardown);
206         g_test_add ("/gcr/openssh/parse_v2_dsa", Test, NULL, setup, test_parse_v2_dsa, teardown);
207         g_test_add ("/gcr/openssh/parse_v1_options", Test, NULL, setup, test_parse_v1_options, teardown);
208         g_test_add ("/gcr/openssh/parse_v2_options", Test, NULL, setup, test_parse_v2_options, teardown);
209
210         return egg_tests_run_with_loop ();
211 }