OpenDNSSEC-enforcer  1.4.1
test_ksm_request.c
Go to the documentation of this file.
1 /*
2  * $Id: test_ksm_request.c 5818 2011-10-27 08:12:18Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * Filename: test_ksm_parameter.c - Test Key Parameter Module
31  *
32  * Description:
33  * This is a short test module to check the functions in the Ksm Parameter
34  * module.
35  *
36  * The test program makes use of the CUnit framework, as described in
37  * http://cunit.sourceforge.net
38 -*/
39 
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
44 
45 #include "CUnit/Basic.h"
46 
47 #include "ksm/ksm.h"
48 #include "ksm/datetime.h"
49 #include "test_routines.h"
50 
51 int l_keytype = -1;
52 int no_keys = 0;
53 
54 /*
55  * TestCallback Function
56  */
57 static int TestCallbackFn(void* context, KSM_KEYDATA* data)
58 {
59  fprintf(stderr, "\n");
60  fprintf(stderr, "\t\t\t<Key>\n");
61  fprintf(stderr, "\t\t\t\t<Flags>%d</Flags>\n", data->keytype);
62  fprintf(stderr, "\t\t\t\t<Algorithm>%d</Algorithm>\n", data->algorithm);
63  fprintf(stderr, "\t\t\t\t<Locator>%s</Locator>\n", data->location);
64  if (data->keytype == KSM_TYPE_KSK)
65  {
66  fprintf(stderr, "\t\t\t\t<KSK />\n");
67  }
68  else
69  {
70  fprintf(stderr, "\t\t\t\t<ZSK />\n");
71  }
72  fprintf(stderr, "\t\t\t\t<%s />\n", KsmKeywordStateValueToName(data->state));
73  fprintf(stderr, "\t\t\t</Key>\n");
74  fprintf(stderr, "\n");
75 
76  /*printf("%s %lu %d %d %s\n", KsmKeywordStateValueToName(data->state),
77  data->keypair_id, data->keytype, data->algorithm, data->location); */
78 
79  no_keys++;
80 
81  return 0;
82 }
83 
84 
85 /*+
86  * TestKsmRequestKeys - Test Request code
87  *
88  * Description:
89  * Tests that a parameter can be set
90 -*/
91 
92 static void TestKsmRequestKeys(void)
93 {
94  int keytype = 0; /*KSM_TYPE_ZSK;*/ /* Type of key */
95  int rollover = 0; /* Set 1 to roll over the current key */
96  int status = 0;
97  int zone_id = 1; /* opendnssec.org */
98  int newDS = 0;
99  int policy_id = 2;
100  int sm = 1; /* count over all security modules */
101  int bits = 1024; /* count over all sizes */
102  int algorithm = KSM_ALGORITHM_RSASHA1; /* count over all algorithms */
103  int keypair_id;
104  DB_ID dnsseckey_id;
105 
106  char* datetime = DtParseDateTimeString("now");
107 
108  /* Allocate a key to the zone (routines previously tested) */
109  status = KsmKeyGetUnallocated(policy_id, sm, bits, algorithm, zone_id, 1, &keypair_id);
110  CU_ASSERT_EQUAL(status, 0);
111 
112  status = KsmDnssecKeyCreate(zone_id, keypair_id, KSM_TYPE_ZSK, KSM_STATE_GENERATE, datetime, NULL, &dnsseckey_id);
113  CU_ASSERT_EQUAL(status, 0);
114 
115  /* push the key into some state that update can operate on */
116  status = KsmRequestChangeStateN( KSM_TYPE_ZSK, datetime, 1,
118 
119  CU_ASSERT_EQUAL(status, 0);
120 
121  /* Check that keys of a particular type can be requested */
122  KsmRequestKeys(keytype, rollover, datetime, TestCallbackFn, NULL, policy_id, zone_id, 0, &newDS);
123 
124  /*CU_ASSERT_EQUAL(status, 1);*/ /* just make sure that something flags this as needing more work */
125  CU_ASSERT_EQUAL(no_keys, 2);
126 
127  /* TODO work out some test scenarios here and use Callback to check */
128 }
129 
130 /*
131  * TestKsmRequest - Create Test Suite
132  *
133  * Description:
134  * Adds the test suite to the CUnit test registry and adds all the tests
135  * to it.
136  *
137  * Arguments:
138  * None.
139  *
140  * Returns:
141  * int
142  * Return status. 0 => Success.
143  */
144 
145 int TestKsmRequest(void); /* Declaration */
146 int TestKsmRequest(void)
147 {
148  struct test_testdef tests[] = {
149  {"KsmRequest", TestKsmRequestKeys},
150  {NULL, NULL}
151  };
152 
153  /* TODO
154  * have been a bit lazy here and reuse TdbSetup etc...
155  * this has the consequence of all the setups running for each suite
156  * if this gets too slow then we will need to separate them out
157  * */
158  return TcuCreateSuite("KsmRequest", TdbSetup, TdbTeardown, tests);
159 }
#define KSM_TYPE_ZSK
Definition: ksm.h:359
int KsmRequestChangeStateN(int keytype, const char *datetime, int count, int src_state, int dst_state, int zone_id)
Definition: ksm_request.c:1041
char location[KSM_NAME_LENGTH]
Definition: ksm.h:111
int l_keytype
int state
Definition: ksm.h:101
int TcuCreateSuite(const char *title, int(*init)(), int(*teardown)(), struct test_testdef *tests)
int TestKsmRequest(void)
const char * KsmKeywordStateValueToName(int value)
Definition: ksm_keyword.c:244
char * DtParseDateTimeString(const char *string)
Definition: datetime.c:617
unsigned long DB_ID
Definition: database.h:80
#define KSM_ALGORITHM_RSASHA1
Definition: ksm.h:333
int KsmDnssecKeyCreate(int zone_id, int keypair_id, int keytype, int state, const char *time, const char *retTime, DB_ID *id)
Definition: ksm_key.c:143
int keytype
Definition: ksm.h:102
int algorithm
Definition: ksm.h:103
int TdbTeardown(void)
#define KSM_STATE_PUBLISH
Definition: ksm.h:364
int no_keys
int KsmKeyGetUnallocated(int policy_id, int sm, int bits, int algorithm, int zone_id, int share_keys, int *keypair_id)
Definition: ksm_key.c:871
int TdbSetup(void)
#define KSM_TYPE_KSK
Definition: ksm.h:357
int KsmRequestKeys(int keytype, int rollover, const char *datetime, KSM_REQUEST_CALLBACK callback, void *context, int policy_id, int zone_id, int run_interval, int *NewDS)
Definition: ksm_request.c:97
#define KSM_STATE_GENERATE
Definition: ksm.h:362