summaryrefslogtreecommitdiff
path: root/regress/unittests/sshkey/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'regress/unittests/sshkey/common.c')
-rw-r--r--regress/unittests/sshkey/common.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/regress/unittests/sshkey/common.c b/regress/unittests/sshkey/common.c
new file mode 100644
index 000000000..b73a788dd
--- /dev/null
+++ b/regress/unittests/sshkey/common.c
@@ -0,0 +1,80 @@
1/* $OpenBSD: common.c,v 1.1 2014/06/24 01:14:18 djm Exp $ */
2/*
3 * Helpers for key API tests
4 *
5 * Placed in the public domain
6 */
7
8#include "includes.h"
9
10#include <sys/types.h>
11#include <sys/param.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <stdio.h>
15#include <stdint.h>
16#include <stdlib.h>
17#include <string.h>
18#include <unistd.h>
19
20#include <openssl/bn.h>
21#include <openssl/ec.h>
22#include <openssl/rsa.h>
23#include <openssl/dsa.h>
24#include <openssl/objects.h>
25
26#include "../test_helper/test_helper.h"
27
28#include "ssherr.h"
29#include "authfile.h"
30#include "sshkey.h"
31#include "sshbuf.h"
32
33#include "common.h"
34
35struct sshbuf *
36load_file(const char *name)
37{
38 int fd;
39 struct sshbuf *ret;
40
41 ASSERT_PTR_NE(ret = sshbuf_new(), NULL);
42 ASSERT_INT_NE(fd = open(test_data_file(name), O_RDONLY), -1);
43 ASSERT_INT_EQ(sshkey_load_file(fd, name, ret), 0);
44 close(fd);
45 return ret;
46}
47
48struct sshbuf *
49load_text_file(const char *name)
50{
51 struct sshbuf *ret = load_file(name);
52 const u_char *p;
53
54 /* Trim whitespace at EOL */
55 for (p = sshbuf_ptr(ret); sshbuf_len(ret) > 0;) {
56 if (p[sshbuf_len(ret) - 1] == '\r' ||
57 p[sshbuf_len(ret) - 1] == '\t' ||
58 p[sshbuf_len(ret) - 1] == ' ' ||
59 p[sshbuf_len(ret) - 1] == '\n')
60 ASSERT_INT_EQ(sshbuf_consume_end(ret, 1), 0);
61 else
62 break;
63 }
64 /* \0 terminate */
65 ASSERT_INT_EQ(sshbuf_put_u8(ret, 0), 0);
66 return ret;
67}
68
69BIGNUM *
70load_bignum(const char *name)
71{
72 BIGNUM *ret = NULL;
73 struct sshbuf *buf;
74
75 buf = load_text_file(name);
76 ASSERT_INT_NE(BN_hex2bn(&ret, (const char *)sshbuf_ptr(buf)), 0);
77 sshbuf_free(buf);
78 return ret;
79}
80