/*
* This file is part of GNUnet
* Copyright (C) 2013 Christian Grothoff (and other contributing authors)
*
* GNUnet 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 3, or (at your
* option) any later version.
*
* GNUnet 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.
*
* You should have received a copy of the GNU General Public License
* along with GNUnet; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* @file psyc/test_psyc.c
* @brief Tests for the PSYC API.
* @author Gabor X Toth
* @author Christian Grothoff
*/
#include <inttypes.h>
#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_common.h"
#include "gnunet_util_lib.h"
#include "gnunet_testing_lib.h"
#include "gnunet_env_lib.h"
#include "gnunet_psyc_util_lib.h"
#include "gnunet_psyc_service.h"
#include "gnunet_core_service.h"
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
/**
* Return value from 'main'.
*/
int res;
const struct GNUNET_CONFIGURATION_Handle *cfg;
struct GNUNET_CORE_Handle *core;
struct GNUNET_PeerIdentity this_peer;
/**
* Handle for task for timeout termination.
*/
struct GNUNET_SCHEDULER_Task * end_badly_task;
struct GNUNET_PSYC_Master *mst;
struct GNUNET_PSYC_Slave *slv;
struct GNUNET_PSYC_Channel *mst_chn, *slv_chn;
struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
struct TransmitClosure
{
struct GNUNET_PSYC_MasterTransmitHandle *mst_tmit;
struct GNUNET_PSYC_SlaveTransmitHandle *slv_tmit;
struct GNUNET_ENV_Environment *env;
struct GNUNET_ENV_Modifier *mod;
char *data[16];
const char *mod_value;
size_t mod_value_size;
uint8_t data_delay[16];
uint8_t data_count;
uint8_t paused;
uint8_t n;
};
struct TransmitClosure *tmit;
uint8_t join_req_count, end_count;
enum
{
TEST_NONE = 0,
TEST_MASTER_START = 1,
TEST_SLAVE_JOIN_REJECT = 2,
TEST_SLAVE_JOIN_ACCEPT = 3,
TEST_SLAVE_ADD = 4,
TEST_SLAVE_REMOVE = 5,
TEST_SLAVE_TRANSMIT = 6,
TEST_MASTER_TRANSMIT = 7,
TEST_MASTER_HISTORY_REPLAY_LATEST = 8,
TEST_SLAVE_HISTORY_REPLAY_LATEST = 9,
TEST_MASTER_HISTORY_REPLAY = 10,
TEST_SLAVE_HISTORY_REPLAY = 11,
TEST_MASTER_STATE_GET = 12,
TEST_SLAVE_STATE_GET = 13,
TEST_MASTER_STATE_GET_PREFIX = 14,
TEST_SLAVE_STATE_GET_PREFIX = 15,
} test;
void
master_transmit ();
void
master_history_replay_latest ();
void master_stopped (void *cls)
{
if (NULL != tmit)
{
GNUNET_ENV_environment_destroy (tmit->env);
GNUNET_free (tmit);
tmit = NULL;
}
GNUNET_SCHEDULER_shutdown ();
}
void slave_parted (void *cls)
{
if (NULL != mst)
{
GNUNET_PSYC_master_stop (mst, GNUNET_NO, &master_stopped, NULL);
mst = NULL;
}
else
master_stopped (NULL);
}
/**
* Clean up all resources used.
*/
void
cleanup ()
{
if (NULL != core)
{
GNUNET_CORE_disconnect (core);
core = NULL;
}
if (NULL != slv)
{
GNUNET_PSYC_slave_part (slv, GNUNET_NO, &slave_parted, NULL);
slv = NULL;
}
else
slave_parted (NULL);
}
/**
* Terminate the test case (failure).
*
* @param cls NULL
* @param tc scheduler context
*/
void
end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
res = 1;
cleanup ();
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
}
/**
* Terminate the test case (success).
*
* @param cls NULL
* @param tc scheduler context
*/
void
end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
res = 0;
cleanup ();
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
}
/**
* Finish the test case (successfully).
*/
void
end ()
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending tests.\n");
if (end_badly_task != NULL)
{
GNUNET_SCHEDULER_cancel (end_badly_task);
end_badly_task = NULL;
}
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
&end_normally, NULL);
}
void
master_message_cb (void *cls, uint64_t message_id, uint32_t flags,
const struct GNUNET_PSYC_MessageHeader *msg)
{
GNUNET_assert (NULL != msg);
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Test #%d: Master got PSYC message fragment of size %u "
"belonging to message ID %" PRIu64 " with flags %x\n",
test,