File: test_xmppaddr.c

package info (click to toggle)
libstrophe 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,356 kB
  • sloc: ansic: 16,695; makefile: 301; sh: 61
file content (121 lines) | stat: -rw-r--r-- 3,171 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* SPDX-License-Identifier: MIT OR GPL-3.0-only */
/* test_xmppaddr.c
** libstrophe XMPP client library -- test routines for the xmppaddr
** certificate API's
**
** Copyright (C) 2021 Steffen Jaeckel
**
**  This software is provided AS-IS with no warranty, either express
**  or implied.
**
**  This program is dual licensed under the MIT or GPLv3 licenses.
*/

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>

#include "strophe.h"

#include "test.h"

static int
password_callback(char *pw, size_t pw_max, xmpp_conn_t *conn, void *userdata)
{
    (void)pw_max;
    (void)userdata;
    (void)conn;
    memcpy(pw, "abc123", 7);
    return 6;
}

int main()
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    struct {
        int needs_callback;
        char *pem, *key;
    } client_cert[] = {
        {0, "tests/cert.pem", "tests/key.pem"},
        {1, "tests/cert.pem", "tests/key_encrypted.pem"},
        {0, NULL, "tests/cert.emptypass.pfx"},
        {0, NULL, "tests/cert.nopass.pfx"},
        {1, NULL, "tests/cert.pfx"},
        {0, "tests/cert.emptypass.pfx", NULL},
        {0, "tests/cert.nopass.pfx", NULL},
        {1, "tests/cert.pfx", NULL},
    };

    const char *srcdir;
    char *certbuf, *keybuf;
    char xmppaddr_num[] = "0";
    unsigned int m, n;

    xmpp_initialize();
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    ctx = xmpp_ctx_new(NULL, log);

    srcdir = getenv("srcdir");

    certbuf = malloc(MAXPATHLEN);
    keybuf = malloc(MAXPATHLEN);

    for (m = 0; m < sizeof(client_cert) / sizeof(client_cert[0]); ++m) {
        char *certfile = certbuf, *keyfile = keybuf;
        conn = xmpp_conn_new(ctx);

        if (client_cert[m].pem)
            snprintf(certfile, MAXPATHLEN, "%s/%s", srcdir, client_cert[m].pem);
        else
            certfile = NULL;

        if (client_cert[m].key)
            snprintf(keyfile, MAXPATHLEN, "%s/%s", srcdir, client_cert[m].key);
        else
            keyfile = NULL;

        if (client_cert[m].needs_callback)
            xmpp_conn_set_password_callback(conn, password_callback, NULL);

        xmpp_conn_set_client_cert(conn, certfile, keyfile);

        xmppaddr_num[0] = '0' + xmpp_conn_cert_xmppaddr_num(conn);

        COMPARE("2", xmppaddr_num);

        for (n = 0; n < 3; ++n) {
            char *r = xmpp_conn_cert_xmppaddr(conn, n);
            switch (n) {
            case 0:
                COMPARE(
                    "very.long.username@so.the.asn1.length.is.a.valid.ascii."
                    "character",
                    r);
                break;
            case 1:
                COMPARE("second@xmpp.jid", r);
                break;
            default:
                if (r != NULL) {
                    printf("\nThere shall only be two id-on-xmppAddr SANs!\n"
                           "Found another one: %s\n",
                           r);
                    exit(1);
                }
                break;
            }
            free(r);
        }
        xmpp_conn_release(conn);
    }
    free(certbuf);
    free(keybuf);

    xmpp_ctx_free(ctx);
    xmpp_shutdown();

    return 0;
}