00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <time.h>
00044
00045 #include "CUnit/Basic.h"
00046
00047 #include "ksm/ksm.h"
00048 #include "ksm/db_fields.h"
00049 #include "ksm/datetime.h"
00050 #include "ksm/string_util.h"
00051 #include "test_routines.h"
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 static void TestKeyClear(KSM_KEYDATA* data)
00065 {
00066 memset(data, 0, sizeof(KSM_KEYDATA));
00067
00068 return;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 static void TestKeyDefaults(KSM_KEYDATA* data)
00083 {
00084 TestKeyClear(data);
00085
00086 data->algorithm = KSM_ALGORITHM_RSASHA1;
00087 data->keytype = KSM_TYPE_ZSK;
00088 data->siglifetime = 7 * 24 * 3600;
00089 data->state = KSM_STATE_GENERATE;
00090
00091 data->flags |= (KEYDATA_M_ALGORITHM | KEYDATA_M_KEYTYPE |
00092 KEYDATA_M_SIGLIFETIME | KEYDATA_M_STATE);
00093
00094 return;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 static void TestKsmKeyPairCreate(void)
00105 {
00106
00107 DB_ID key_id;
00108 int status = 0;
00109 int rowcount;
00110 char* sql;
00111 int where = 0;
00112
00113
00114 int policy_id = 2;
00115 char* HSMKeyID = "0x1";
00116 int smID = 1;
00117 int size = 1024;
00118 int alg = KSM_ALGORITHM_DSASHA1;
00119 char* generate = DtParseDateTimeString("now");
00120
00121 status = KsmKeyPairCreate(policy_id, HSMKeyID, smID, size, alg, generate, &key_id);
00122
00123 CU_ASSERT_EQUAL(status, 0);
00124
00125
00126
00127 sql = DqsCountInit("keypairs");
00128 DqsConditionInt(&sql, "ID", DQS_COMPARE_EQ, key_id, where++);
00129 DqsEnd(&sql);
00130 status = DbIntQuery(DbHandle(), &rowcount, sql);
00131 DqsFree(sql);
00132
00133 CU_ASSERT_EQUAL(status, 0);
00134
00135 CU_ASSERT_EQUAL(rowcount, 1);
00136
00137 StrFree(generate);
00138
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 static void TestKsmDnssecKeyCreate(void)
00149 {
00150
00151 DB_ID keypair_id;
00152 DB_ID dnsseckey_id;
00153 int status = 0;
00154 int rowcount;
00155 char* sql;
00156 int where = 0;
00157 int zone_id = 1;
00158
00159
00160 int policy_id = 2;
00161 char* HSMKeyID = "0x1";
00162 int smID = 1;
00163 int size = 1024;
00164 int alg = KSM_ALGORITHM_DSASHA1;
00165 char* generate = "2009-01-01";
00166
00167 status = KsmKeyPairCreate(policy_id, HSMKeyID, smID, size, alg, generate, &keypair_id);
00168
00169 CU_ASSERT_EQUAL(status, 0);
00170
00171
00172
00173 status = KsmDnssecKeyCreate(zone_id, keypair_id, KSM_TYPE_ZSK, KSM_STATE_GENERATE, generate, &dnsseckey_id);
00174
00175 CU_ASSERT_EQUAL(status, 0);
00176
00177
00178
00179 sql = DqsCountInit("dnsseckeys");
00180 DqsConditionInt(&sql, "ID", DQS_COMPARE_EQ, dnsseckey_id, where++);
00181 DqsEnd(&sql);
00182 status = DbIntQuery(DbHandle(), &rowcount, sql);
00183 DqsFree(sql);
00184
00185 CU_ASSERT_EQUAL(status, 0);
00186
00187 CU_ASSERT_EQUAL(rowcount, 1);
00188
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198 static void TestKsmKeyPredict(void)
00199 {
00200 int policy_id = 2;
00201 int keytype = KSM_TYPE_KSK;
00202 int keys_shared = KSM_KEYS_SHARED;
00203 int interval = 86400*4;
00204 int count;
00205 int status;
00206
00207 status = KsmKeyPredict(policy_id, keytype, keys_shared, interval, &count, KSM_ROLL_DEFAULT, 1);
00208
00209 CU_ASSERT_EQUAL(status, 0);
00210 CU_ASSERT_EQUAL(count, 7);
00211
00212 keytype = KSM_TYPE_ZSK;
00213 status = KsmKeyPredict(policy_id, keytype, keys_shared, interval, &count, KSM_ROLL_DEFAULT, 1);
00214
00215 CU_ASSERT_EQUAL(status, 0);
00216 CU_ASSERT_EQUAL(count, 7);
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226 static void TestKsmKeyCountQueue(void)
00227 {
00228 int zone_id = 1;
00229 int keytype = KSM_TYPE_KSK;
00230 int count;
00231 int status;
00232
00233 status = KsmKeyCountQueue(keytype, &count, zone_id);
00234
00235 CU_ASSERT_EQUAL(status, 0);
00236 CU_ASSERT_EQUAL(count, 1);
00237
00238 keytype = KSM_TYPE_ZSK;
00239 status = KsmKeyCountQueue(keytype, &count, zone_id);
00240
00241 CU_ASSERT_EQUAL(status, 0);
00242 CU_ASSERT_EQUAL(count, 1);
00243 }
00244
00245
00246
00247
00248
00249
00250
00251
00252 static void TestKsmKeyCountUnallocated(void)
00253 {
00254 int policy_id = 2;
00255 int sm = -1;
00256 int bits = -1;
00257 int algorithm = -1;
00258 int count;
00259 int status;
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 CU_ASSERT_EQUAL(status, 0);
00270 CU_ASSERT_EQUAL(count, 13);
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280 static void TestKsmKeyGetUnallocated(void)
00281 {
00282 int policy_id = 2;
00283 int sm = 1;
00284 int bits = 1024;
00285 int algorithm = KSM_ALGORITHM_RSASHA1;
00286 int keypair_id;
00287 DB_ID dnsseckey_id;
00288 int zone_id = 1;
00289 int status;
00290
00291 status = KsmKeyGetUnallocated(policy_id, sm, bits, algorithm, zone_id, 1, &keypair_id);
00292
00293 CU_ASSERT_EQUAL(status, 0);
00294 CU_ASSERT_EQUAL(keypair_id, 3);
00295
00296 status = KsmDnssecKeyCreate(zone_id, keypair_id, KSM_TYPE_ZSK, KSM_STATE_GENERATE, "now", &dnsseckey_id);
00297 CU_ASSERT_EQUAL(status, 0);
00298
00299 status = KsmKeyGetUnallocated(policy_id, sm, bits, algorithm, zone_id, 1, &keypair_id);
00300
00301 CU_ASSERT_EQUAL(status, 0);
00302 CU_ASSERT_EQUAL(keypair_id, 4);
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312 static void TestKsmDnssecKeyCreateOnPolicy(void)
00313 {
00314
00315 DB_ID key_pair_id;
00316 int status = 0;
00317 int rowcount;
00318 char* sql;
00319 int where = 0;
00320
00321
00322 int policy_id = 2;
00323 char* HSMKeyID = "0x1";
00324 int smID = 1;
00325 int size = 1024;
00326 int alg = KSM_ALGORITHM_DSASHA1;
00327 char* generate = "2009-01-01";
00328
00329
00330 status = KsmParameterSet("zones_share_keys", "keys", 1, policy_id);
00331 CU_ASSERT_EQUAL(status, 0);
00332
00333 status = KsmKeyPairCreate(policy_id, HSMKeyID, smID, size, alg, generate, &key_pair_id);
00334 CU_ASSERT_EQUAL(status, 0);
00335
00336
00337
00338 CU_ASSERT_EQUAL(status, 0);
00339
00340
00341
00342 sql = DqsCountInit("dnsseckeys");
00343 DqsConditionInt(&sql, "keypair_id", DQS_COMPARE_EQ, key_pair_id, where++);
00344 DqsEnd(&sql);
00345 status = DbIntQuery(DbHandle(), &rowcount, sql);
00346 DqsFree(sql);
00347
00348 CU_ASSERT_EQUAL(status, 0);
00349
00350
00351 CU_ASSERT_EQUAL(rowcount, 2);
00352
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 int TestKsmKey(void);
00371 int TestKsmKey(void)
00372 {
00373 struct test_testdef tests[] = {
00374 {"KsmKeyPairCreate", TestKsmKeyPairCreate},
00375 {"KsmDnssecKeyCreate", TestKsmDnssecKeyCreate},
00376 {"KsmKeyPredict", TestKsmKeyPredict},
00377 {"KsmKeyCountQueue", TestKsmKeyCountQueue},
00378
00379 {"KsmKeyGetUnallocated", TestKsmKeyGetUnallocated},
00380
00381 {NULL, NULL}
00382 };
00383
00384
00385
00386
00387
00388
00389 return TcuCreateSuite("KsmKey", TdbSetup, TdbTeardown, tests);
00390 }