1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 Copyright (C) 2011 Collabora Ltd
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.
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.
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.
20 Author: Stef Walter <stefw@collabora.co.uk>
25 #include "gcr/gcr-base.h"
26 #include "gcr/gcr-openssh.h"
28 #include "egg/egg-testing.h"
35 const gchar *expected_label;
36 const gchar *expected_options;
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"
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" \
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"
71 #define EXTRA_LINES_WITHOUT_KEY \
91 on_openssh_pub_parse (GckAttributes *attrs,
97 Test *test = user_data;
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);
105 /* The block should parse properly */
106 keys = _gcr_openssh_pub_parse (outer, NULL, NULL);
107 g_assert_cmpuint (keys, ==, 1);
111 test_parse_v1_rsa (Test *test,
112 gconstpointer unused)
114 const gchar *data = OPENSSH_PUBLIC_RSA1 EXTRA_LINES_WITHOUT_KEY;
118 test->expected_label = "rsa-key@example.com";
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);
124 g_bytes_unref (bytes);
128 test_parse_v2_rsa (Test *test,
129 gconstpointer unused)
131 const gchar *data = OPENSSH_PUBLIC_RSA2 EXTRA_LINES_WITHOUT_KEY;
135 test->expected_label = "rsa-key@example.com";
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);
141 g_bytes_unref (bytes);
145 test_parse_v2_dsa (Test *test,
146 gconstpointer unused)
148 const gchar *data = OPENSSH_PUBLIC_DSA2 EXTRA_LINES_WITHOUT_KEY;
152 test->expected_label = "dsa-key@example.com";
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);
158 g_bytes_unref (bytes);
162 test_parse_v1_options (Test *test,
163 gconstpointer unused)
165 const gchar *data = "option1,option2=\"value 2\",option3 " OPENSSH_PUBLIC_RSA1;
169 test->expected_options = "option1,option2=\"value 2\",option3";
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);
175 g_bytes_unref (bytes);
179 test_parse_v2_options (Test *test,
180 gconstpointer unused)
182 const gchar *data = "option1,option2=\"value 2\",option3 " OPENSSH_PUBLIC_RSA2;
186 test->expected_options = "option1,option2=\"value 2\",option3";
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);
192 g_bytes_unref (bytes);
196 main (int argc, char **argv)
198 #if !GLIB_CHECK_VERSION(2,35,0)
201 g_test_init (&argc, &argv, NULL);
202 g_set_prgname ("test-gnupg-process");
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);
210 return egg_tests_run_with_loop ();