"Fossies" - the Fresh Open Source Software Archive

Member "opendnssec-2.1.4/enforcer/src/db/test/test_key_data.c" (16 May 2019, 31383 Bytes) of package /linux/misc/dns/opendnssec-2.1.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com>
    3  * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
    4  * Copyright (c) 2014 OpenDNSSEC AB (svb)
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
   24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
   26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  *
   28  */
   29 
   30 #include "CUnit/Basic.h"
   31 
   32 #include "../db_configuration.h"
   33 #include "../db_connection.h"
   34 #include "../key_data.h"
   35 
   36 #include <string.h>
   37 
   38 static db_configuration_list_t* configuration_list = NULL;
   39 static db_configuration_t* configuration = NULL;
   40 static db_connection_t* connection = NULL;
   41 
   42 static key_data_t* object = NULL;
   43 static key_data_list_t* object_list = NULL;
   44 static db_value_t id = DB_VALUE_EMPTY;
   45 static db_clause_list_t* clause_list = NULL;
   46 
   47 static int db_sqlite = 0;
   48 static int db_mysql = 0;
   49 
   50 #if defined(ENFORCER_DATABASE_SQLITE3)
   51 int test_key_data_init_suite_sqlite(void) {
   52     if (configuration_list) {
   53         return 1;
   54     }
   55     if (configuration) {
   56         return 1;
   57     }
   58     if (connection) {
   59         return 1;
   60     }
   61 
   62     /*
   63      * Setup the configuration for the connection
   64      */
   65     if (!(configuration_list = db_configuration_list_new())) {
   66         return 1;
   67     }
   68     if (!(configuration = db_configuration_new())
   69         || db_configuration_set_name(configuration, "backend")
   70         || db_configuration_set_value(configuration, "sqlite")
   71         || db_configuration_list_add(configuration_list, configuration))
   72     {
   73         db_configuration_free(configuration);
   74         configuration = NULL;
   75         db_configuration_list_free(configuration_list);
   76         configuration_list = NULL;
   77         return 1;
   78     }
   79     configuration = NULL;
   80     if (!(configuration = db_configuration_new())
   81         || db_configuration_set_name(configuration, "file")
   82         || db_configuration_set_value(configuration, "test.db")
   83         || db_configuration_list_add(configuration_list, configuration))
   84     {
   85         db_configuration_free(configuration);
   86         configuration = NULL;
   87         db_configuration_list_free(configuration_list);
   88         configuration_list = NULL;
   89         return 1;
   90     }
   91     configuration = NULL;
   92 
   93     /*
   94      * Connect to the database
   95      */
   96     if (!(connection = db_connection_new())
   97         || db_connection_set_configuration_list(connection, configuration_list))
   98     {
   99         db_connection_free(connection);
  100         connection = NULL;
  101         db_configuration_list_free(configuration_list);
  102         configuration_list = NULL;
  103         return 1;
  104     }
  105     configuration_list = NULL;
  106 
  107     if (db_connection_setup(connection)
  108         || db_connection_connect(connection))
  109     {
  110         db_connection_free(connection);
  111         connection = NULL;
  112         return 1;
  113     }
  114 
  115     db_sqlite = 1;
  116     db_mysql = 0;
  117 
  118     return 0;
  119 }
  120 #endif
  121 
  122 #if defined(ENFORCER_DATABASE_MYSQL)
  123 int test_key_data_init_suite_mysql(void) {
  124     if (configuration_list) {
  125         return 1;
  126     }
  127     if (configuration) {
  128         return 1;
  129     }
  130     if (connection) {
  131         return 1;
  132     }
  133 
  134     /*
  135      * Setup the configuration for the connection
  136      */
  137     if (!(configuration_list = db_configuration_list_new())) {
  138         return 1;
  139     }
  140     if (!(configuration = db_configuration_new())
  141         || db_configuration_set_name(configuration, "backend")
  142         || db_configuration_set_value(configuration, "mysql")
  143         || db_configuration_list_add(configuration_list, configuration))
  144     {
  145         db_configuration_free(configuration);
  146         configuration = NULL;
  147         db_configuration_list_free(configuration_list);
  148         configuration_list = NULL;
  149         return 1;
  150     }
  151     configuration = NULL;
  152     if (!(configuration = db_configuration_new())
  153         || db_configuration_set_name(configuration, "host")
  154         || db_configuration_set_value(configuration, ENFORCER_DB_HOST)
  155         || db_configuration_list_add(configuration_list, configuration))
  156     {
  157         db_configuration_free(configuration);
  158         configuration = NULL;
  159         db_configuration_list_free(configuration_list);
  160         configuration_list = NULL;
  161         return 1;
  162     }
  163     configuration = NULL;
  164     if (!(configuration = db_configuration_new())
  165         || db_configuration_set_name(configuration, "port")
  166         || db_configuration_set_value(configuration, ENFORCER_DB_PORT_TEXT)
  167         || db_configuration_list_add(configuration_list, configuration))
  168     {
  169         db_configuration_free(configuration);
  170         configuration = NULL;
  171         db_configuration_list_free(configuration_list);
  172         configuration_list = NULL;
  173         return 1;
  174     }
  175     configuration = NULL;
  176     if (!(configuration = db_configuration_new())
  177         || db_configuration_set_name(configuration, "user")
  178         || db_configuration_set_value(configuration, ENFORCER_DB_USERNAME)
  179         || db_configuration_list_add(configuration_list, configuration))
  180     {
  181         db_configuration_free(configuration);
  182         configuration = NULL;
  183         db_configuration_list_free(configuration_list);
  184         configuration_list = NULL;
  185         return 1;
  186     }
  187     configuration = NULL;
  188     if (!(configuration = db_configuration_new())
  189         || db_configuration_set_name(configuration, "pass")
  190         || db_configuration_set_value(configuration, ENFORCER_DB_PASSWORD)
  191         || db_configuration_list_add(configuration_list, configuration))
  192     {
  193         db_configuration_free(configuration);
  194         configuration = NULL;
  195         db_configuration_list_free(configuration_list);
  196         configuration_list = NULL;
  197         return 1;
  198     }
  199     configuration = NULL;
  200     if (!(configuration = db_configuration_new())
  201         || db_configuration_set_name(configuration, "db")
  202         || db_configuration_set_value(configuration, ENFORCER_DB_DATABASE)
  203         || db_configuration_list_add(configuration_list, configuration))
  204     {
  205         db_configuration_free(configuration);
  206         configuration = NULL;
  207         db_configuration_list_free(configuration_list);
  208         configuration_list = NULL;
  209         return 1;
  210     }
  211     configuration = NULL;
  212 
  213     /*
  214      * Connect to the database
  215      */
  216     if (!(connection = db_connection_new())
  217         || db_connection_set_configuration_list(connection, configuration_list))
  218     {
  219         db_connection_free(connection);
  220         connection = NULL;
  221         db_configuration_list_free(configuration_list);
  222         configuration_list = NULL;
  223         return 1;
  224     }
  225     configuration_list = NULL;
  226 
  227     if (db_connection_setup(connection)
  228         || db_connection_connect(connection))
  229     {
  230         db_connection_free(connection);
  231         connection = NULL;
  232         return 1;
  233     }
  234 
  235     db_sqlite = 0;
  236     db_mysql = 1;
  237 
  238     return 0;
  239 }
  240 #endif
  241 
  242 static int test_key_data_clean_suite(void) {
  243     db_connection_free(connection);
  244     connection = NULL;
  245     db_configuration_free(configuration);
  246     configuration = NULL;
  247     db_configuration_list_free(configuration_list);
  248     configuration_list = NULL;
  249     db_value_reset(&id);
  250     db_clause_list_free(clause_list);
  251     clause_list = NULL;
  252     return 0;
  253 }
  254 
  255 static void test_key_data_new(void) {
  256     CU_ASSERT_PTR_NOT_NULL_FATAL((object = key_data_new(connection)));
  257     CU_ASSERT_PTR_NOT_NULL_FATAL((object_list = key_data_list_new(connection)));
  258 }
  259 
  260 static void test_key_data_set(void) {
  261     db_value_t zone_id = DB_VALUE_EMPTY;
  262     db_value_t hsm_key_id = DB_VALUE_EMPTY;
  263     if (db_sqlite) {
  264         CU_ASSERT(!db_value_from_int32(&zone_id, 1));
  265     }
  266     if (db_mysql) {
  267         CU_ASSERT(!db_value_from_uint64(&zone_id, 1));
  268     }
  269     if (db_sqlite) {
  270         CU_ASSERT(!db_value_from_int32(&hsm_key_id, 1));
  271     }
  272     if (db_mysql) {
  273         CU_ASSERT(!db_value_from_uint64(&hsm_key_id, 1));
  274     }
  275     CU_ASSERT(!key_data_set_zone_id(object, &zone_id));
  276     CU_ASSERT(!key_data_set_hsm_key_id(object, &hsm_key_id));
  277     CU_ASSERT(!key_data_set_algorithm(object, 1));
  278     CU_ASSERT(!key_data_set_inception(object, 1));
  279     CU_ASSERT(!key_data_set_role(object, KEY_DATA_ROLE_KSK));
  280     CU_ASSERT(!key_data_set_role(object, KEY_DATA_ROLE_ZSK));
  281     CU_ASSERT(!key_data_set_role(object, KEY_DATA_ROLE_CSK));
  282     CU_ASSERT(!key_data_set_introducing(object, 1));
  283     CU_ASSERT(!key_data_set_active_zsk(object, 1));
  284     CU_ASSERT(!key_data_set_publish(object, 1));
  285     CU_ASSERT(!key_data_set_active_ksk(object, 1));
  286     CU_ASSERT(!key_data_set_ds_at_parent(object, KEY_DATA_DS_AT_PARENT_UNSUBMITTED));
  287     CU_ASSERT(!key_data_set_ds_at_parent(object, KEY_DATA_DS_AT_PARENT_SUBMIT));
  288     CU_ASSERT(!key_data_set_ds_at_parent(object, KEY_DATA_DS_AT_PARENT_SUBMITTED));
  289     CU_ASSERT(!key_data_set_ds_at_parent(object, KEY_DATA_DS_AT_PARENT_SEEN));
  290     CU_ASSERT(!key_data_set_ds_at_parent(object, KEY_DATA_DS_AT_PARENT_RETRACT));
  291     CU_ASSERT(!key_data_set_ds_at_parent(object, KEY_DATA_DS_AT_PARENT_RETRACTED));
  292     CU_ASSERT(!key_data_set_keytag(object, 1));
  293     CU_ASSERT(!key_data_set_minimize(object, 1));
  294     db_value_reset(&zone_id);
  295     db_value_reset(&hsm_key_id);
  296 }
  297 
  298 static void test_key_data_get(void) {
  299     int ret;
  300     db_value_t zone_id = DB_VALUE_EMPTY;
  301     db_value_t hsm_key_id = DB_VALUE_EMPTY;
  302     if (db_sqlite) {
  303         CU_ASSERT(!db_value_from_int32(&zone_id, 1));
  304     }
  305     if (db_mysql) {
  306         CU_ASSERT(!db_value_from_uint64(&zone_id, 1));
  307     }
  308     if (db_sqlite) {
  309         CU_ASSERT(!db_value_from_int32(&hsm_key_id, 1));
  310     }
  311     if (db_mysql) {
  312         CU_ASSERT(!db_value_from_uint64(&hsm_key_id, 1));
  313     }
  314     CU_ASSERT(!db_value_cmp(key_data_zone_id(object), &zone_id, &ret));
  315     CU_ASSERT(!ret);
  316     CU_ASSERT(!db_value_cmp(key_data_hsm_key_id(object), &hsm_key_id, &ret));
  317     CU_ASSERT(!ret);
  318     CU_ASSERT(key_data_algorithm(object) == 1);
  319     CU_ASSERT(key_data_inception(object) == 1);
  320     CU_ASSERT(key_data_role(object) == KEY_DATA_ROLE_CSK);
  321     CU_ASSERT_PTR_NOT_NULL_FATAL(key_data_role_text(object));
  322     CU_ASSERT(!strcmp(key_data_role_text(object), "CSK"));
  323     CU_ASSERT(key_data_introducing(object) == 1);
  324     CU_ASSERT(key_data_active_zsk(object) == 1);
  325     CU_ASSERT(key_data_publish(object) == 1);
  326     CU_ASSERT(key_data_active_ksk(object) == 1);
  327     CU_ASSERT(key_data_ds_at_parent(object) == KEY_DATA_DS_AT_PARENT_RETRACTED);
  328     CU_ASSERT(key_data_keytag(object) == 1);
  329     CU_ASSERT(key_data_minimize(object) == 1);
  330     db_value_reset(&zone_id);
  331     db_value_reset(&hsm_key_id);
  332 }
  333 
  334 static void test_key_data_create(void) {
  335     CU_ASSERT_FATAL(!key_data_create(object));
  336 }
  337 
  338 static void test_key_data_clauses(void) {
  339     key_data_list_t* new_list;
  340 
  341     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  342     CU_ASSERT_PTR_NOT_NULL(key_data_zone_id_clause(clause_list, key_data_zone_id(object)));
  343     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  344     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  345     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  346     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  347     key_data_list_free(new_list);
  348     db_clause_list_free(clause_list);
  349     clause_list = NULL;
  350 
  351     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  352     CU_ASSERT_PTR_NOT_NULL(key_data_hsm_key_id_clause(clause_list, key_data_hsm_key_id(object)));
  353     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  354     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  355     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  356     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  357     key_data_list_free(new_list);
  358     db_clause_list_free(clause_list);
  359     clause_list = NULL;
  360 
  361     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  362     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  363     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  364     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  365     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  366     key_data_list_free(new_list);
  367     db_clause_list_free(clause_list);
  368     clause_list = NULL;
  369 
  370     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  371     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  372     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  373     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  374     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  375     key_data_list_free(new_list);
  376     db_clause_list_free(clause_list);
  377     clause_list = NULL;
  378 
  379     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  380     CU_ASSERT_PTR_NOT_NULL(key_data_role_clause(clause_list, key_data_role(object)));
  381     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  382     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  383     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  384     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  385     key_data_list_free(new_list);
  386     db_clause_list_free(clause_list);
  387     clause_list = NULL;
  388 
  389     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  390     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  391     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  392     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  393     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  394     key_data_list_free(new_list);
  395     db_clause_list_free(clause_list);
  396     clause_list = NULL;
  397 
  398     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  399     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  400     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  401     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  402     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  403     key_data_list_free(new_list);
  404     db_clause_list_free(clause_list);
  405     clause_list = NULL;
  406 
  407     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  408     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  409     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  410     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  411     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  412     key_data_list_free(new_list);
  413     db_clause_list_free(clause_list);
  414     clause_list = NULL;
  415 
  416     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  417     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  418     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  419     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  420     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  421     key_data_list_free(new_list);
  422     db_clause_list_free(clause_list);
  423     clause_list = NULL;
  424 
  425     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  426     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  427     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  428     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  429     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  430     key_data_list_free(new_list);
  431     db_clause_list_free(clause_list);
  432     clause_list = NULL;
  433 
  434     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  435     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  436     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  437     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  438     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  439     key_data_list_free(new_list);
  440     db_clause_list_free(clause_list);
  441     clause_list = NULL;
  442 
  443     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  444     CU_ASSERT_PTR_NOT_NULL(key_data_ds_at_parent_clause(clause_list, key_data_ds_at_parent(object)));
  445     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  446     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  447     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  448     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  449     key_data_list_free(new_list);
  450     db_clause_list_free(clause_list);
  451     clause_list = NULL;
  452 
  453     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  454     CU_ASSERT_PTR_NOT_NULL(key_data_keytag_clause(clause_list, key_data_keytag(object)));
  455     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  456     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  457     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  458     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  459     key_data_list_free(new_list);
  460     db_clause_list_free(clause_list);
  461     clause_list = NULL;
  462 
  463     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  464     CU_ASSERT(!key_data_list_get_by_clauses(object_list, clause_list));
  465     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(object_list));
  466     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get_by_clauses(connection, clause_list)));
  467     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  468     key_data_list_free(new_list);
  469     db_clause_list_free(clause_list);
  470     clause_list = NULL;
  471 }
  472 
  473 static void test_key_data_count(void) {
  474     size_t count;
  475 
  476     CU_ASSERT(!key_data_count(object, NULL, &count));
  477     CU_ASSERT(count == 1);
  478 
  479     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  480     CU_ASSERT_PTR_NOT_NULL(key_data_zone_id_clause(clause_list, key_data_zone_id(object)));
  481     CU_ASSERT(!key_data_count(object, clause_list, &count));
  482     CU_ASSERT(count == 1);
  483     db_clause_list_free(clause_list);
  484     clause_list = NULL;
  485 
  486     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  487     CU_ASSERT_PTR_NOT_NULL(key_data_hsm_key_id_clause(clause_list, key_data_hsm_key_id(object)));
  488     CU_ASSERT(!key_data_count(object, clause_list, &count));
  489     CU_ASSERT(count == 1);
  490     db_clause_list_free(clause_list);
  491     clause_list = NULL;
  492 
  493     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  494     CU_ASSERT(!key_data_count(object, clause_list, &count));
  495     CU_ASSERT(count == 1);
  496     db_clause_list_free(clause_list);
  497     clause_list = NULL;
  498 
  499     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  500     CU_ASSERT(!key_data_count(object, clause_list, &count));
  501     CU_ASSERT(count == 1);
  502     db_clause_list_free(clause_list);
  503     clause_list = NULL;
  504 
  505     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  506     CU_ASSERT_PTR_NOT_NULL(key_data_role_clause(clause_list, key_data_role(object)));
  507     CU_ASSERT(!key_data_count(object, clause_list, &count));
  508     CU_ASSERT(count == 1);
  509     db_clause_list_free(clause_list);
  510     clause_list = NULL;
  511 
  512     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  513     CU_ASSERT(!key_data_count(object, clause_list, &count));
  514     CU_ASSERT(count == 1);
  515     db_clause_list_free(clause_list);
  516     clause_list = NULL;
  517 
  518     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  519     CU_ASSERT(!key_data_count(object, clause_list, &count));
  520     CU_ASSERT(count == 1);
  521     db_clause_list_free(clause_list);
  522     clause_list = NULL;
  523 
  524     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  525     CU_ASSERT(!key_data_count(object, clause_list, &count));
  526     CU_ASSERT(count == 1);
  527     db_clause_list_free(clause_list);
  528     clause_list = NULL;
  529 
  530     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  531     CU_ASSERT(!key_data_count(object, clause_list, &count));
  532     CU_ASSERT(count == 1);
  533     db_clause_list_free(clause_list);
  534     clause_list = NULL;
  535 
  536     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  537     CU_ASSERT(!key_data_count(object, clause_list, &count));
  538     CU_ASSERT(count == 1);
  539     db_clause_list_free(clause_list);
  540     clause_list = NULL;
  541 
  542     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  543     CU_ASSERT(!key_data_count(object, clause_list, &count));
  544     CU_ASSERT(count == 1);
  545     db_clause_list_free(clause_list);
  546     clause_list = NULL;
  547 
  548     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  549     CU_ASSERT_PTR_NOT_NULL(key_data_ds_at_parent_clause(clause_list, key_data_ds_at_parent(object)));
  550     CU_ASSERT(!key_data_count(object, clause_list, &count));
  551     CU_ASSERT(count == 1);
  552     db_clause_list_free(clause_list);
  553     clause_list = NULL;
  554 
  555     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  556     CU_ASSERT_PTR_NOT_NULL(key_data_keytag_clause(clause_list, key_data_keytag(object)));
  557     CU_ASSERT(!key_data_count(object, clause_list, &count));
  558     CU_ASSERT(count == 1);
  559     db_clause_list_free(clause_list);
  560     clause_list = NULL;
  561 
  562     CU_ASSERT_PTR_NOT_NULL_FATAL((clause_list = db_clause_list_new()));
  563     CU_ASSERT(!key_data_count(object, clause_list, &count));
  564     CU_ASSERT(count == 1);
  565     db_clause_list_free(clause_list);
  566     clause_list = NULL;
  567 }
  568 
  569 static void test_key_data_list(void) {
  570     const key_data_t* item;
  571     key_data_t* item2;
  572     key_data_list_t* new_list;
  573 
  574     CU_ASSERT_FATAL(!key_data_list_get(object_list));
  575     CU_ASSERT_PTR_NOT_NULL_FATAL((item = key_data_list_next(object_list)));
  576     CU_ASSERT_FATAL(!db_value_copy(&id, key_data_id(item)));
  577     CU_ASSERT_PTR_NOT_NULL_FATAL((item = key_data_list_begin(object_list)));
  578 
  579     CU_ASSERT_FATAL(!key_data_list_get(object_list));
  580     CU_ASSERT_PTR_NOT_NULL_FATAL((item2 = key_data_list_get_next(object_list)));
  581     key_data_free(item2);
  582     CU_PASS("key_data_free");
  583     CU_ASSERT_PTR_NOT_NULL_FATAL((item2 = key_data_list_get_begin(object_list)));
  584     key_data_free(item2);
  585     CU_PASS("key_data_free");
  586 
  587     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new_get(connection)));
  588     CU_ASSERT_PTR_NOT_NULL(key_data_list_next(new_list));
  589     key_data_list_free(new_list);
  590 }
  591 
  592 static void test_key_data_list_store(void) {
  593     key_data_t* item;
  594     key_data_list_t* new_list;
  595 
  596     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new(connection)));
  597     CU_ASSERT_FATAL(!key_data_list_object_store(new_list));
  598     CU_ASSERT_FATAL(!key_data_list_get(new_list));
  599 
  600     CU_ASSERT_PTR_NOT_NULL_FATAL(key_data_list_next(new_list));
  601     CU_ASSERT_PTR_NOT_NULL_FATAL(key_data_list_begin(new_list));
  602 
  603     CU_ASSERT_PTR_NOT_NULL_FATAL((item = key_data_list_get_begin(new_list)));
  604     key_data_free(item);
  605     CU_PASS("key_data_free");
  606 
  607     key_data_list_free(new_list);
  608 }
  609 
  610 static void test_key_data_list_associated(void) {
  611     key_data_t* item;
  612     key_data_list_t* new_list;
  613 
  614     CU_ASSERT_PTR_NOT_NULL((new_list = key_data_list_new(connection)));
  615     CU_ASSERT_FATAL(!key_data_list_get(new_list));
  616 
  617     CU_ASSERT_PTR_NOT_NULL_FATAL(key_data_list_next(new_list));
  618     CU_ASSERT_PTR_NOT_NULL_FATAL(key_data_list_begin(new_list));
  619 
  620     CU_ASSERT_PTR_NOT_NULL_FATAL((item = key_data_list_get_begin(new_list)));
  621     key_data_free(item);
  622     CU_PASS("key_data_free");
  623 
  624     key_data_list_free(new_list);
  625 }
  626 
  627 static void test_key_data_read(void) {
  628     CU_ASSERT_FATAL(!key_data_get_by_id(object, &id));
  629 }
  630 
  631 static void test_key_data_verify(void) {
  632     int ret;
  633     db_value_t zone_id = DB_VALUE_EMPTY;
  634     db_value_t hsm_key_id = DB_VALUE_EMPTY;
  635     if (db_sqlite) {
  636         CU_ASSERT(!db_value_from_int32(&zone_id, 1));
  637     }
  638     if (db_mysql) {
  639         CU_ASSERT(!db_value_from_uint64(&zone_id, 1));
  640     }
  641     if (db_sqlite) {
  642         CU_ASSERT(!db_value_from_int32(&hsm_key_id, 1));
  643     }
  644     if (db_mysql) {
  645         CU_ASSERT(!db_value_from_uint64(&hsm_key_id, 1));
  646     }
  647     CU_ASSERT(!db_value_cmp(key_data_zone_id(object), &zone_id, &ret));
  648     CU_ASSERT(!ret);
  649     CU_ASSERT(!db_value_cmp(key_data_hsm_key_id(object), &hsm_key_id, &ret));
  650     CU_ASSERT(!ret);
  651     CU_ASSERT(key_data_algorithm(object) == 1);
  652     CU_ASSERT(key_data_inception(object) == 1);
  653     CU_ASSERT(key_data_role(object) == KEY_DATA_ROLE_CSK);
  654     CU_ASSERT_PTR_NOT_NULL_FATAL(key_data_role_text(object));
  655     CU_ASSERT(!strcmp(key_data_role_text(object), "CSK"));
  656     CU_ASSERT(key_data_introducing(object) == 1);
  657     CU_ASSERT(key_data_active_zsk(object) == 1);
  658     CU_ASSERT(key_data_publish(object) == 1);
  659     CU_ASSERT(key_data_active_ksk(object) == 1);
  660     CU_ASSERT(key_data_ds_at_parent(object) == KEY_DATA_DS_AT_PARENT_RETRACTED);
  661     CU_ASSERT(key_data_keytag(object) == 1);
  662     CU_ASSERT(key_data_minimize(object) == 1);
  663     db_value_reset(&zone_id);
  664     db_value_reset(&hsm_key_id);
  665 }
  666 
  667 static void test_key_data_change(void) {
  668     db_value_t zone_id = DB_VALUE_EMPTY;
  669     db_value_t hsm_key_id = DB_VALUE_EMPTY;
  670     if (db_sqlite) {
  671         CU_ASSERT(!db_value_from_int32(&zone_id, 1));
  672     }
  673     if (db_mysql) {
  674         CU_ASSERT(!db_value_from_uint64(&zone_id, 1));
  675     }
  676     if (db_sqlite) {
  677         CU_ASSERT(!db_value_from_int32(&hsm_key_id, 1));
  678     }
  679     if (db_mysql) {
  680         CU_ASSERT(!db_value_from_uint64(&hsm_key_id, 1));
  681     }
  682     CU_ASSERT(!key_data_set_zone_id(object, &zone_id));
  683     CU_ASSERT(!key_data_set_hsm_key_id(object, &hsm_key_id));
  684     CU_ASSERT(!key_data_set_algorithm(object, 2));
  685     CU_ASSERT(!key_data_set_inception(object, 2));
  686     CU_ASSERT(!key_data_set_role(object, KEY_DATA_ROLE_KSK));
  687     CU_ASSERT(!key_data_set_introducing(object, 2));
  688     CU_ASSERT(!key_data_set_active_zsk(object, 2));
  689     CU_ASSERT(!key_data_set_publish(object, 2));
  690     CU_ASSERT(!key_data_set_active_ksk(object, 2));
  691     CU_ASSERT(!key_data_set_ds_at_parent(object, KEY_DATA_DS_AT_PARENT_UNSUBMITTED));
  692     CU_ASSERT(!key_data_set_keytag(object, 2));
  693     CU_ASSERT(!key_data_set_minimize(object, 2));
  694     db_value_reset(&zone_id);
  695     db_value_reset(&hsm_key_id);
  696 }
  697 
  698 static void test_key_data_update(void) {
  699     CU_ASSERT_FATAL(!key_data_update(object));
  700 }
  701 
  702 static void test_key_data_read2(void) {
  703     CU_ASSERT_FATAL(!key_data_get_by_id(object, &id));
  704 }
  705 
  706 static void test_key_data_verify2(void) {
  707     int ret;
  708     db_value_t zone_id = DB_VALUE_EMPTY;
  709     db_value_t hsm_key_id = DB_VALUE_EMPTY;
  710     if (db_sqlite) {
  711         CU_ASSERT(!db_value_from_int32(&zone_id, 1));
  712     }
  713     if (db_mysql) {
  714         CU_ASSERT(!db_value_from_uint64(&zone_id, 1));
  715     }
  716     if (db_sqlite) {
  717         CU_ASSERT(!db_value_from_int32(&hsm_key_id, 1));
  718     }
  719     if (db_mysql) {
  720         CU_ASSERT(!db_value_from_uint64(&hsm_key_id, 1));
  721     }
  722     CU_ASSERT(!db_value_cmp(key_data_zone_id(object), &zone_id, &ret));
  723     CU_ASSERT(!ret);
  724     CU_ASSERT(!db_value_cmp(key_data_hsm_key_id(object), &hsm_key_id, &ret));
  725     CU_ASSERT(!ret);
  726     CU_ASSERT(key_data_algorithm(object) == 2);
  727     CU_ASSERT(key_data_inception(object) == 2);
  728     CU_ASSERT(key_data_role(object) == KEY_DATA_ROLE_KSK);
  729     CU_ASSERT_PTR_NOT_NULL_FATAL(key_data_role_text(object));
  730     CU_ASSERT(!strcmp(key_data_role_text(object), "KSK"));
  731     CU_ASSERT(key_data_introducing(object) == 2);
  732     CU_ASSERT(key_data_active_zsk(object) == 2);
  733     CU_ASSERT(key_data_publish(object) == 2);
  734     CU_ASSERT(key_data_active_ksk(object) == 2);
  735     CU_ASSERT(key_data_ds_at_parent(object) == KEY_DATA_DS_AT_PARENT_UNSUBMITTED);
  736     CU_ASSERT(key_data_keytag(object) == 2);
  737     CU_ASSERT(key_data_minimize(object) == 2);
  738     db_value_reset(&zone_id);
  739     db_value_reset(&hsm_key_id);
  740 }
  741 
  742 static void test_key_data_cmp(void) {
  743     key_data_t* local_object;
  744 
  745     CU_ASSERT_PTR_NOT_NULL_FATAL((local_object = key_data_new(connection)));
  746     CU_ASSERT(key_data_cmp(object, local_object));
  747 }
  748 
  749 static void test_key_data_delete(void) {
  750     CU_ASSERT_FATAL(!key_data_delete(object));
  751 }
  752 
  753 static void test_key_data_list2(void) {
  754     CU_ASSERT_FATAL(!key_data_list_get(object_list));
  755     CU_ASSERT_PTR_NULL(key_data_list_next(object_list));
  756 }
  757 
  758 static void test_key_data_end(void) {
  759     if (object) {
  760         key_data_free(object);
  761         CU_PASS("key_data_free");
  762     }
  763     if (object_list) {
  764         key_data_list_free(object_list);
  765         CU_PASS("key_data_list_free");
  766     }
  767 }
  768 
  769 static int test_key_data_add_tests(CU_pSuite pSuite) {
  770     if (!CU_add_test(pSuite, "new object", test_key_data_new)
  771         || !CU_add_test(pSuite, "set fields", test_key_data_set)
  772         || !CU_add_test(pSuite, "get fields", test_key_data_get)
  773         || !CU_add_test(pSuite, "create object", test_key_data_create)
  774         || !CU_add_test(pSuite, "object clauses", test_key_data_clauses)
  775         || !CU_add_test(pSuite, "object count", test_key_data_count)
  776         || !CU_add_test(pSuite, "list objects", test_key_data_list)
  777         || !CU_add_test(pSuite, "list objects (store)", test_key_data_list_store)
  778         || !CU_add_test(pSuite, "list objects (associated)", test_key_data_list_associated)
  779         || !CU_add_test(pSuite, "read object by id", test_key_data_read)
  780         || !CU_add_test(pSuite, "verify fields", test_key_data_verify)
  781         || !CU_add_test(pSuite, "change object", test_key_data_change)
  782         || !CU_add_test(pSuite, "update object", test_key_data_update)
  783         || !CU_add_test(pSuite, "reread object by id", test_key_data_read2)
  784         || !CU_add_test(pSuite, "verify fields after update", test_key_data_verify2)
  785         || !CU_add_test(pSuite, "compare objects", test_key_data_cmp)
  786         || !CU_add_test(pSuite, "delete object", test_key_data_delete)
  787         || !CU_add_test(pSuite, "list objects to verify delete", test_key_data_list2)
  788         || !CU_add_test(pSuite, "end test", test_key_data_end))
  789     {
  790         return CU_get_error();
  791     }
  792     return 0;
  793 }
  794 
  795 int test_key_data_add_suite(void) {
  796     CU_pSuite pSuite = NULL;
  797     int ret;
  798 
  799 #if defined(ENFORCER_DATABASE_SQLITE3)
  800     pSuite = CU_add_suite("Test of key data (SQLite)", test_key_data_init_suite_sqlite, test_key_data_clean_suite);
  801     if (!pSuite) {
  802         return CU_get_error();
  803     }
  804     ret = test_key_data_add_tests(pSuite);
  805     if (ret) {
  806         return ret;
  807     }
  808 #endif
  809 #if defined(ENFORCER_DATABASE_MYSQL)
  810     pSuite = CU_add_suite("Test of key data (MySQL)", test_key_data_init_suite_mysql, test_key_data_clean_suite);
  811     if (!pSuite) {
  812         return CU_get_error();
  813     }
  814     ret = test_key_data_add_tests(pSuite);
  815     if (ret) {
  816         return ret;
  817     }
  818 #endif
  819     return 0;
  820 }