/*
* This file is part of GNUnet
* Copyright (C) 2013 GNUnet e.V.
*
* GNUnet is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* 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
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @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_psyc_util_lib.h"
#include "gnunet_psyc_service.h"
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
/**
* Return value from 'main'.
*/
static int res;
static const struct GNUNET_CONFIGURATION_Handle *cfg;
static struct GNUNET_PeerIdentity this_peer;
/**
* Handle for task for timeout termination.
*/
static struct GNUNET_SCHEDULER_Task * end_badly_task;
static struct GNUNET_PSYC_Master *mst;
static struct GNUNET_PSYC_Slave *slv;
static struct GNUNET_PSYC_Channel *mst_chn, *slv_chn;
static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
struct TransmitClosure
{
struct GNUNET_PSYC_MasterTransmitHandle *mst_tmit;
struct GNUNET_PSYC_SlaveTransmitHandle *slv_tmit;
struct GNUNET_PSYC_Environment *env;
struct GNUNET_PSYC_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;
};
static struct TransmitClosure *tmit;
static 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;
static void
master_transmit ();
static void
master_history_replay_latest ();
static void
master_stopped (void *cls)
{
if (NULL != tmit)
{
GNUNET_PSYC_env_destroy (tmit->env);
GNUNET_free (tmit);
tmit = NULL;
}
GNUNET_SCHEDULER_shutdown ();
}
static 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.
*/
static void
cleanup ()
{
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
*/
static void
end_badly (void *cls)
{
res = 1;
cleanup ();
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test FAILED.\n");
}
/**
* Terminate the test case (success).
*
* @param cls NULL
*/
static void
end_normally (void *cls)
{
res = 0;
cleanup ();
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test PASSED.\n");
}
/**
* Finish the test case (successfully).
*/
static void
end ()
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO, "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);
}
static void
master_message_cb (void *cls, const struct GNUNET_PSYC_MessageHeader *msg)
{
GNUNET_assert (NULL != msg);
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Test #%d: Master got PSYC message fragment of size %u "
"belonging to message ID %" PRIu64 " with flags %x\n",
test, ntohs (msg->header.size),
GNUNET_ntohll (msg->message_id), ntohl (msg->flags));
// FIXME
}
static void