Debian Codegraph @

tests/unittests/torture_pki_dsa.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8
9 #include "pki.c"
10 #include "torture.h"
11 #include "torture_key.h"
12 #include "torture_pki.h"
13
14 #define LIBSSH_DSA_TESTKEY "libssh_testkey.id_dsa"
15 #define LIBSSH_DSA_TESTKEY_PASSPHRASE "libssh_testkey_passphrase.id_dsa"
16
17 const char template[] = "temp_dir_XXXXXX";
18 const unsigned char INPUT[] = "12345678901234567890";
19
20 struct pki_st {
21 char *cwd;
22 char *temp_dir;
23 };
24
25 static int setup_dsa_key(void **state)
26 {
27 struct pki_st *test_state = NULL;
28 char *cwd = NULL;
29 char *tmp_dir = NULL;
30 int rc = 0;
31
32 test_state = (struct pki_st *)malloc(sizeof(struct pki_st));
33 assert_non_null(test_state);
34
35 cwd = torture_get_current_working_dir();
36 assert_non_null(cwd);
37
38 tmp_dir = torture_make_temp_dir(template);
39 assert_non_null(tmp_dir);
40
41 test_state->cwd = cwd;
42 test_state->temp_dir = tmp_dir;
43
44 *state = test_state;
45
46 rc = torture_change_dir(tmp_dir);
47 assert_int_equal(rc, 0);
48
49 printf("Changed directory to: %s\n", tmp_dir);
50
51 torture_write_file(LIBSSH_DSA_TESTKEY,
52 torture_get_testkey(SSH_KEYTYPE_DSS, 0));
53 torture_write_file(LIBSSH_DSA_TESTKEY_PASSPHRASE,
54 torture_get_testkey(SSH_KEYTYPE_DSS, 1));
55 torture_write_file(LIBSSH_DSA_TESTKEY ".pub",
56 torture_get_testkey_pub(SSH_KEYTYPE_DSS));
57 torture_write_file(LIBSSH_DSA_TESTKEY "-cert.pub",
58 torture_get_testkey_pub(SSH_KEYTYPE_DSS_CERT01));
59
60 return 0;
61 }
62
63 static int setup_openssh_dsa_key(void **state)
64 {
65 struct pki_st *test_state = NULL;
66 char *cwd = NULL;
67 char *tmp_dir = NULL;
68 int rc = 0;
69
70 test_state = (struct pki_st *)malloc(sizeof(struct pki_st));
71 assert_non_null(test_state);
72
73 cwd = torture_get_current_working_dir();
74 assert_non_null(cwd);
75
76 tmp_dir = torture_make_temp_dir(template);
77 assert_non_null(tmp_dir);
78
79 test_state->cwd = cwd;
80 test_state->temp_dir = tmp_dir;
81
82 *state = test_state;
83
84 rc = torture_change_dir(tmp_dir);
85 assert_int_equal(rc, 0);
86
87 torture_write_file(LIBSSH_DSA_TESTKEY,
88 torture_get_openssh_testkey(SSH_KEYTYPE_DSS, 0));
89 torture_write_file(LIBSSH_DSA_TESTKEY_PASSPHRASE,
90 torture_get_openssh_testkey(SSH_KEYTYPE_DSS, 1));
91 torture_write_file(LIBSSH_DSA_TESTKEY ".pub",
92 torture_get_testkey_pub(SSH_KEYTYPE_DSS));
93 torture_write_file(LIBSSH_DSA_TESTKEY "-cert.pub",
94 torture_get_testkey_pub(SSH_KEYTYPE_DSS_CERT01));
95
96 return 0;
97 }
98
99 static int teardown(void **state) {
100
101 struct pki_st *test_state = NULL;
102 int rc = 0;
103
104 test_state = *((struct pki_st **)state);
105
106 assert_non_null(test_state);
107 assert_non_null(test_state->cwd);
108 assert_non_null(test_state->temp_dir);
109
110 rc = torture_change_dir(test_state->cwd);
111 assert_int_equal(rc, 0);
112
113 rc = torture_rmdirs(test_state->temp_dir);
114 assert_int_equal(rc, 0);
115
116 SAFE_FREE(test_state->temp_dir);
117 SAFE_FREE(test_state->cwd);
118 SAFE_FREE(test_state);
119
120 return 0;
121 }
122
123 static void torture_pki_dsa_import_pubkey_file(void **state)
124 {
125 ssh_key pubkey = NULL;
126 int rc;
127
128 (void)state;
129
130 /* The key doesn't have the hostname as comment after the key */
131 rc = ssh_pki_import_pubkey_file(LIBSSH_DSA_TESTKEY ".pub", &pubkey);
132 assert_int_equal(rc, SSH_ERROR);
133 assert_null(pubkey);
134 }
135
136 static void torture_pki_dsa_import_pubkey_from_openssh_privkey(void **state)
137 {
138 ssh_key pubkey = NULL;
139 int rc;
140
141 (void)state;
142
143 /* The key doesn't have the hostname as comment after the key */
144 rc = ssh_pki_import_pubkey_file(LIBSSH_DSA_TESTKEY_PASSPHRASE, &pubkey);
145 assert_int_equal(rc, SSH_ERROR);
146 assert_null(pubkey);
147 }
148
149 static void torture_pki_dsa_import_privkey_base64(void **state)
150 {
151 int rc;
152 ssh_key key = NULL;
153 const char *passphrase = torture_get_testkey_passphrase();
154
155 (void) state; /* unused */
156
157 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_DSS, 0),
158 passphrase,
159 NULL,
160 NULL,
161 &key);
162 assert_int_equal(rc, SSH_ERROR);
163 assert_null(key);
164 }
165
166 static void torture_pki_generate_dsa(void **state)
167 {
168 int rc;
169 ssh_key key = NULL;
170
171 (void) state;
172
173 /* Setup */
174 rc = ssh_pki_generate(SSH_KEYTYPE_DSS, 2048, &key);
175 assert_int_equal(rc, SSH_ERROR);
176 assert_null(key);
177 }
178
179 static void torture_pki_dsa_import_cert_file(void **state)
180 {
181 int rc;
182 ssh_key cert = NULL;
183
184 (void) state; /* unused */
185
186 rc = ssh_pki_import_cert_file(LIBSSH_DSA_TESTKEY "-cert.pub", &cert);
187 assert_int_equal(rc, SSH_ERROR);
188 assert_null(cert);
189 }
190
191 int torture_run_tests(void)
192 {
193 int rc;
194 struct CMUnitTest tests[] = {
195 cmocka_unit_test_setup_teardown(torture_pki_dsa_import_pubkey_file,
196 setup_dsa_key,
197 teardown),
198 cmocka_unit_test_setup_teardown(torture_pki_dsa_import_pubkey_from_openssh_privkey,
199 setup_openssh_dsa_key,
200 teardown),
201 cmocka_unit_test_setup_teardown(torture_pki_dsa_import_privkey_base64,
202 setup_dsa_key,
203 teardown),
204 cmocka_unit_test_setup_teardown(torture_pki_dsa_import_privkey_base64,
205 setup_openssh_dsa_key,
206 teardown),
207 cmocka_unit_test_setup_teardown(torture_pki_dsa_import_cert_file,
208 setup_dsa_key,
209 teardown),
210 cmocka_unit_test_setup_teardown(torture_pki_generate_dsa,
211 setup_dsa_key,
212 teardown),
213 };
214
215 ssh_init();
216 torture_filter_tests(tests);
217 rc = cmocka_run_group_tests(tests, NULL, NULL);
218 ssh_finalize();
219 return rc;
220 }