1/*	$NetBSD: dh_test.c,v 1.2 2024/02/21 22:52:49 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16#include <inttypes.h>
17#include <sched.h> /* IWYU pragma: keep */
18#include <setjmp.h>
19#include <stdarg.h>
20#include <stddef.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24
25/*
26 * As a workaround, include an OpenSSL header file before including cmocka.h,
27 * because OpenSSL 3.1.0 uses __attribute__(malloc), conflicting with a
28 * redefined malloc in cmocka.h.
29 */
30#include <openssl/err.h>
31
32#define UNIT_TESTING
33#include <cmocka.h>
34
35#include <isc/result.h>
36#include <isc/string.h>
37#include <isc/util.h>
38
39#include <dns/name.h>
40
41#include "dst_internal.h"
42
43#include <tests/dns.h>
44
45static int
46setup_test(void **state) {
47	isc_result_t result;
48
49	UNUSED(state);
50
51	result = dst_lib_init(mctx, NULL);
52
53	if (result != ISC_R_SUCCESS) {
54		return (1);
55	}
56
57	return (0);
58}
59
60static int
61teardown_test(void **state) {
62	UNUSED(state);
63
64	dst_lib_destroy();
65
66	return (0);
67}
68
69/* OpenSSL DH_compute_key() failure */
70ISC_RUN_TEST_IMPL(dh_computesecret) {
71	dst_key_t *key = NULL;
72	isc_buffer_t buf;
73	unsigned char array[1024];
74	isc_result_t result;
75	dns_fixedname_t fname;
76	dns_name_t *name;
77
78	UNUSED(state);
79
80	name = dns_fixedname_initname(&fname);
81	isc_buffer_constinit(&buf, "dh.", 3);
82	isc_buffer_add(&buf, 3);
83	result = dns_name_fromtext(name, &buf, NULL, 0, NULL);
84	assert_int_equal(result, ISC_R_SUCCESS);
85
86	result = dst_key_fromfile(name, 18602, DST_ALG_DH,
87				  DST_TYPE_PUBLIC | DST_TYPE_KEY, TESTS_DIR,
88				  mctx, &key);
89	assert_int_equal(result, ISC_R_SUCCESS);
90
91	isc_buffer_init(&buf, array, sizeof(array));
92	result = dst_key_computesecret(key, key, &buf);
93	assert_int_equal(result, DST_R_NOTPRIVATEKEY);
94	result = key->func->computesecret(key, key, &buf);
95	assert_int_equal(result, DST_R_COMPUTESECRETFAILURE);
96
97	dst_key_free(&key);
98}
99
100ISC_TEST_LIST_START
101ISC_TEST_ENTRY_CUSTOM(dh_computesecret, setup_test, teardown_test)
102ISC_TEST_LIST_END
103
104ISC_TEST_MAIN
105