/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/kdb/t_ulog.c - Unit tests for KDB update log */2/*3* Copyright (C) 2014 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* This program performs unit tests for the update log functions in kdb_log.c.34* Right now it contains only a test for issue #7839, checking that35* ulog_add_update behaves appropriately when the last serial number is36* reached.37*38* The test program accepts one argument, which it unlinks and then maps with39* ulog_map(). This lets us test all of the update log functions except for40* ulog_replay(), which needs to open and modify a Kerberos database.41* ulog_replay is adequately exercised by the functional tests in t_iprop.py.42*/4344#include "k5-int.h"45#include "kdb_log.h"4647/* Use a zeroed context structure to avoid reading the profile. This works48* fine for the ulog functions. */49static struct _krb5_context context_st;50static krb5_context context = &context_st;5152int53main(int argc, char **argv)54{55kdb_log_context *lctx;56kdb_hlog_t *ulog;57kdb_incr_update_t upd;58const char *filename;5960if (argc != 2) {61fprintf(stderr, "Usage: %s filename\n", argv[0]);62exit(1);63}64filename = argv[1];65unlink(filename);6667if (ulog_map(context, filename, 10) != 0)68abort();69lctx = context->kdblog_context;70ulog = lctx->ulog;7172/* Modify the ulog to look like it has reached the last serial number.73* Leave the timestamps at 0 and don't bother setting up the entries. */74ulog->kdb_num = lctx->ulogentries;75ulog->kdb_last_sno = (kdb_sno_t)-1;76ulog->kdb_first_sno = ulog->kdb_last_sno - ulog->kdb_num + 1;7778/* Add an empty update. This should reinitialize the ulog, then add the79* update with serial number 2. */80memset(&upd, 0, sizeof(kdb_incr_update_t));81if (ulog_add_update(context, &upd) != 0)82abort();83assert(ulog->kdb_num == 2);84assert(ulog->kdb_first_sno == 1);85assert(ulog->kdb_last_sno == 2);86return 0;87}888990