aboutsummaryrefslogtreecommitdiffstats
path: root/Source/testing/proposal_test.c
blob: 1b16390d3bb74707b7aa3a85d60e3a7a8a1d5bc4 (plain)
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
/**
 * @file proposal_test.c
 *
 * @brief Tests for the proposal_t class.
 *
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "proposal_test.h"

#include <daemon.h>
#include <config/proposal.h>
#include <utils/logger.h>


/**
 * Described in header.
 */
void test_proposal(protected_tester_t *tester)
{
	proposal_t *proposal1, *proposal2, *proposal3;
	iterator_t *iterator;
	algorithm_t *algo;
	bool result;

	proposal1 = proposal_create(1);
	proposal1->add_algorithm(proposal1, PROTO_ESP, ENCRYPTION_ALGORITHM, ENCR_3DES, 0);
	proposal1->add_algorithm(proposal1, PROTO_ESP, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 32);
	proposal1->add_algorithm(proposal1, PROTO_ESP, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 16);
	proposal1->add_algorithm(proposal1, PROTO_ESP, ENCRYPTION_ALGORITHM, ENCR_BLOWFISH, 0);
	proposal1->add_algorithm(proposal1, PROTO_ESP, INTEGRITY_ALGORITHM, AUTH_HMAC_SHA1_96, 20);
	proposal1->add_algorithm(proposal1, PROTO_ESP, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 20);
	proposal1->add_algorithm(proposal1, PROTO_AH, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
	proposal1->add_algorithm(proposal1, PROTO_AH, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
	
	proposal2 = proposal_create(2);
	proposal2->add_algorithm(proposal2, PROTO_ESP, ENCRYPTION_ALGORITHM, ENCR_3IDEA, 0);
	proposal2->add_algorithm(proposal2, PROTO_ESP, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 16);
	proposal2->add_algorithm(proposal2, PROTO_ESP, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 20);
	proposal1->add_algorithm(proposal2, PROTO_AH, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
	
	/* ah and esp prop */
	proposal3 = proposal1->select(proposal1, proposal2);
	tester->assert_false(tester, proposal3 == NULL, "proposal select");
	if (proposal3)
	{
		result = proposal3->get_algorithm(proposal3, PROTO_ESP, ENCRYPTION_ALGORITHM, &algo);
		tester->assert_true(tester, result, "encryption algo select");
		tester->assert_true(tester, algo->algorithm == ENCR_AES_CBC, "encryption algo");
		tester->assert_true(tester, algo->key_size == 16, "encryption keylen");
		
		
		result = proposal3->get_algorithm(proposal3, PROTO_ESP, INTEGRITY_ALGORITHM, &algo);
		tester->assert_true(tester, result, "integrity algo select");
		tester->assert_true(tester, algo->algorithm == AUTH_HMAC_MD5_96, "integrity algo");
		tester->assert_true(tester, algo->key_size == 20, "integrity keylen");
		
		iterator = proposal3->create_algorithm_iterator(proposal3, PROTO_ESP, INTEGRITY_ALGORITHM);
		tester->assert_false(tester, iterator == NULL, "integrity algo select");
		while(iterator->has_next(iterator))
		{
			iterator->current(iterator, (void**)&algo);
			tester->assert_true(tester, algo->algorithm == AUTH_HMAC_MD5_96, "integrity algo");
			tester->assert_true(tester, algo->key_size == 20, "integrity keylen");
		}
		iterator->destroy(iterator);
		
		iterator = proposal3->create_algorithm_iterator(proposal3, PROTO_AH, DIFFIE_HELLMAN_GROUP );
		tester->assert_false(tester, iterator == NULL, "dh group algo select");
		while(iterator->has_next(iterator))
		{
			iterator->current(iterator, (void**)&algo);
			tester->assert_true(tester, algo->algorithm == MODP_1024_BIT, "dh group algo");
			tester->assert_true(tester, algo->key_size == 0, "dh gorup keylen");
		}
		iterator->destroy(iterator);
		
		proposal3->destroy(proposal3);
	}
	
	proposal1->destroy(proposal1);
	proposal2->destroy(proposal2);
	return;
}