Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-armor.c: Test PEM and Armor parsing
3 :
4 : Copyright (C) 2012 Red Hat Inc.
5 :
6 : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : The Gnome Keyring Library is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public
17 : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stefw@gnome.org>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "egg/egg-armor.h"
26 : #include "egg/egg-symkey.h"
27 : #include "egg/egg-openssl.h"
28 : #include "egg/egg-secure-memory.h"
29 : #include "egg/egg-testing.h"
30 :
31 : #include <glib.h>
32 :
33 : #include <stdlib.h>
34 : #include <stdio.h>
35 : #include <string.h>
36 : #include <unistd.h>
37 :
38 8 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
39 :
40 : static void
41 2 : on_pem_get_contents (GQuark type,
42 : GBytes *data,
43 : GBytes *outer,
44 : GHashTable *headers,
45 : gpointer user_data)
46 : {
47 2 : GBytes **contents = user_data;
48 :
49 2 : g_assert_cmpstr (g_quark_to_string (type), ==, "TEST");
50 2 : g_assert (*contents == NULL);
51 2 : *contents = g_bytes_ref (data);
52 2 : }
53 :
54 :
55 : static void
56 1 : test_armor_parse (void)
57 : {
58 1 : const char *pem_data = "-----BEGIN TEST-----\n"
59 : "Z29vZCBtb3JuaW5nIGV2ZXJ5b25lCg==\n"
60 : "-----END TEST-----\n";
61 :
62 1 : GBytes *contents = NULL;
63 : GBytes *check;
64 : GBytes *bytes;
65 : guint num;
66 :
67 1 : bytes = g_bytes_new_static (pem_data, strlen (pem_data));
68 :
69 1 : num = egg_armor_parse (bytes, on_pem_get_contents, &contents);
70 1 : g_assert_cmpint (num, ==, 1);
71 1 : g_assert (contents != NULL);
72 :
73 1 : check = g_bytes_new ("good morning everyone\n", 22);
74 1 : g_assert (g_bytes_equal (check, contents));
75 :
76 1 : g_bytes_unref (check);
77 1 : g_bytes_unref (contents);
78 1 : g_bytes_unref (bytes);
79 1 : }
80 :
81 : static void
82 1 : test_armor_skip_checksum (void)
83 : {
84 1 : const char *pem_data = "-----BEGIN TEST-----\n"
85 : "Z29vZCBtb3JuaW5nIGV2ZXJ5b25lCg==\n"
86 : "=checksum"
87 : "-----END TEST-----\n";
88 :
89 1 : GBytes *contents = NULL;
90 : GBytes *check;
91 : GBytes *bytes;
92 : guint num;
93 :
94 : /* Check that the (above invalid) OpenPGP checksum is skipped */
95 :
96 1 : bytes = g_bytes_new_static (pem_data, strlen (pem_data));
97 :
98 1 : num = egg_armor_parse (bytes, on_pem_get_contents, &contents);
99 1 : g_assert_cmpint (num, ==, 1);
100 1 : g_assert (contents != NULL);
101 :
102 1 : check = g_bytes_new ("good morning everyone\n", 22);
103 1 : g_assert (g_bytes_equal (check, contents));
104 :
105 1 : g_bytes_unref (check);
106 1 : g_bytes_unref (contents);
107 1 : g_bytes_unref (bytes);
108 1 : }
109 :
110 : static void
111 5 : test_invalid (gconstpointer data)
112 : {
113 : GBytes *bytes;
114 : guint num;
115 :
116 : /* Invalid opening line above */
117 :
118 5 : bytes = g_bytes_new_static (data, strlen (data));
119 :
120 5 : num = egg_armor_parse (bytes, NULL, NULL);
121 5 : g_assert_cmpint (num, ==, 0);
122 :
123 5 : g_bytes_unref (bytes);
124 5 : }
125 :
126 : int
127 1 : main (int argc, char **argv)
128 : {
129 1 : g_test_init (&argc, &argv, NULL);
130 :
131 1 : g_test_add_func ("/armor/parse", test_armor_parse);
132 1 : g_test_add_func ("/armor/skip-checksum", test_armor_skip_checksum);
133 :
134 1 : g_test_add_data_func ("/armor/invalid-start",
135 : "-----BEGIN TEST--",
136 : test_invalid);
137 1 : g_test_add_data_func ("/armor/invalid-end",
138 : "-----BEGIN TEST-----\n"
139 : "Z29vZCBtb3JuaW5nIGV2ZXJ5b25lCg==\n"
140 : "--END TEST-----\n",
141 : test_invalid);
142 1 : g_test_add_data_func ("/armor/invalid-mismatch",
143 : "-----BEGIN TEST-----\n"
144 : "Z29vZCBtb3JuaW5nIGV2ZXJ5b25lCg==\n"
145 : "-----END CERTIFICATE-----\n",
146 : test_invalid);
147 1 : g_test_add_data_func ("/armor/invalid-suffix",
148 : "-----BEGIN TEST-----\n"
149 : "Z29vZCBtb3JuaW5nIGV2ZXJ5b25lCg==\n"
150 : "-----END TEST--xxxxxxxx\n",
151 : test_invalid);
152 1 : g_test_add_data_func ("/armor/invalid-truncated",
153 : "-----BEGIN TEST-----\n"
154 : "Z29vZCBtb3JuaW5nIGV2ZXJ5b25lCg==\n"
155 : "-----END TEST--\n",
156 : test_invalid);
157 :
158 1 : return g_test_run ();
159 : }
|