summaryrefslogtreecommitdiff
path: root/gcr/tests/test-secret-exchange.c
blob: 810ec69ac73769c5b23ddc57ca84a24f30c684e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
   Copyright (C) 2011 Collabora Ltd

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stefw@collabora.co.uk>
*/

#include "config.h"

#include "gcr/gcr-base.h"

#include "egg/egg-testing.h"

#include <glib.h>

#include <errno.h>

typedef struct {
	GcrSecretExchange *caller;
	GcrSecretExchange *callee;
} Test;

static void
setup (Test *test, gconstpointer unused)
{
	test->caller = gcr_secret_exchange_new (NULL);
	g_assert (GCR_IS_SECRET_EXCHANGE (test->caller));
	test->callee = gcr_secret_exchange_new (NULL);
	g_assert (GCR_IS_SECRET_EXCHANGE (test->callee));
}

static void
teardown (Test *test,
          gconstpointer unused)
{
	g_object_unref (test->caller);
	egg_assert_not_object (test->caller);

	g_object_unref (test->callee);
	egg_assert_not_object (test->callee);
}

static void
test_perform_exchange (Test *test,
                       gconstpointer unused)
{
	gchar *exchange;

	exchange = gcr_secret_exchange_begin (test->caller);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->callee, exchange))
		g_assert_not_reached ();

	g_free (exchange);

	exchange = gcr_secret_exchange_send (test->callee, "the secret", -1);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->caller, exchange))
		g_assert_not_reached ();

	g_assert_cmpstr (gcr_secret_exchange_get_secret (test->caller, NULL), ==, "the secret");

	g_free (exchange);
}

static void
test_perform_reverse (Test *test,
                       gconstpointer unused)
{
	gchar *exchange;

	exchange = gcr_secret_exchange_begin (test->caller);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->callee, exchange))
		g_assert_not_reached ();

	g_free (exchange);

	exchange = gcr_secret_exchange_send (test->callee, NULL, -1);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->caller, exchange))
		g_assert_not_reached ();

	g_free (exchange);

	g_assert (gcr_secret_exchange_get_secret (test->caller, NULL) == NULL);

	exchange = gcr_secret_exchange_send (test->caller, "reverse secret", -1);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->callee, exchange))
		g_assert_not_reached ();

	g_free (exchange);

	g_assert_cmpstr (gcr_secret_exchange_get_secret (test->callee, NULL), ==, "reverse secret");
}

static void
test_perform_multiple (Test *test,
                       gconstpointer unused)
{
	gchar *exchange;

	exchange = gcr_secret_exchange_begin (test->caller);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->callee, exchange))
		g_assert_not_reached ();

	g_free (exchange);

	exchange = gcr_secret_exchange_send (test->callee, "first secret", -1);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->caller, exchange))
		g_assert_not_reached ();

	g_free (exchange);

	g_assert_cmpstr (gcr_secret_exchange_get_secret (test->caller, NULL), ==, "first secret");

	exchange = gcr_secret_exchange_send (test->callee, "second secret", -1);
	g_assert (exchange);

	if (!gcr_secret_exchange_receive (test->caller, exchange))
		g_assert_not_reached ();

	g_free (exchange);

	g_assert_cmpstr (gcr_secret_exchange_get_secret (test->caller, NULL), ==, "second secret");
}

int
main (int argc, char **argv)
{
#if !GLIB_CHECK_VERSION(2,35,0)
	g_type_init ();
#endif
	g_test_init (&argc, &argv, NULL);
	g_set_prgname ("test-secret-exchange");

	g_test_add ("/gcr/secret-exchange/perform-exchange", Test, NULL, setup, test_perform_exchange, teardown);
	g_test_add ("/gcr/secret-exchange/perform-reverse", Test, NULL, setup, test_perform_reverse, teardown);
	g_test_add ("/gcr/secret-exchange/perform-multiple", Test, NULL, setup, test_perform_multiple, teardown);

	return g_test_run ();
}