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
|
//
// Copyright 2017 Garrett D'Amore <garrett@damore.org>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include "convey.h"
#include "nng.h"
#include "core/nng_impl.h"
#include <string.h>
// Transport common tests. By making a common test framework for transports,
// we can avoid rewriting the same tests for each new transport. Include this
// file once in your test code. The test framework uses the REQ/REP protocol
// for comms.
typedef struct {
char addr[NNG_MAXADDRLEN+1];
nng_socket reqsock;
nng_socket repsock;
nni_tran *tran;
} trantest;
void
trantest_init(trantest *tt, const char *addr)
{
snprintf(tt->addr, sizeof (tt->addr), "%s", addr);
tt->tran = nni_tran_find(addr);
So(tt->tran != NULL);
So(nng_open(&tt->reqsock, NNG_PROTO_REQ) == 0);
So(nng_open(&tt->repsock, NNG_PROTO_REP) == 0);
}
void
trantest_fini(trantest *tt)
{
nng_close(tt->reqsock);
nng_close(tt->repsock);
}
void
trantest_scheme(trantest *tt)
{
Convey("Scheme is correct", {
size_t l = strlen(tt->tran->tran_scheme);
So(strncmp(tt->addr, tt->tran->tran_scheme, l) == 0);
So(strncmp(tt->addr + l, "://", 3) == 0);
})
}
void
trantest_conn_refused(trantest *tt)
{
Convey("Connection refused works", {
nng_endpoint ep = 0;
So(nng_dial(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == NNG_ECONNREFUSED);
So(ep == 0);
So(nng_dial(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == NNG_ECONNREFUSED);
So(ep == 0);
})
}
void
trantest_duplicate_listen(trantest *tt)
{
Convey("Duplicate listen rejected", {
nng_endpoint ep;
So(nng_listen(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
So(ep != 0);
ep = 0;
So(nng_listen(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == NNG_EADDRINUSE);
So(ep == 0);
})
}
void
trantest_listen_accept(trantest *tt)
{
Convey("Listen and accept" ,{
nng_endpoint ep;
ep = 0;
So(nng_listen(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
So(ep != 0);
ep = 0;
So(nng_dial(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
So(ep != 0);
})
}
void
trantest_send_recv(trantest *tt)
{
Convey("Send and recv", {
nng_endpoint ep = 0;
nng_msg *send;
nng_msg *recv;
size_t len;
ep = 0;
So(nng_listen(tt->repsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
So(ep != 0);
ep = 0;
So(nng_dial(tt->reqsock, tt->addr, &ep, NNG_FLAG_SYNCH) == 0);
So(ep != 0);
send = NULL;
So(nng_msg_alloc(&send, 0) == 0);
So(send != NULL);
So(nng_msg_append(send, "ping", 5) == 0);
So(nng_sendmsg(tt->reqsock, send, 0) == 0);
recv = NULL;
So(nng_recvmsg(tt->repsock, &recv, 0) == 0);
So(recv != NULL);
So(nng_msg_len(recv) == 5);
So(strcmp(nng_msg_body(recv), "ping") == 0);
nng_msg_free(recv);
len = strlen("acknowledge");
So(nng_msg_alloc(&send, 0) == 0);
So(nng_msg_append(send, "acknowledge", len) == 0);
So(nng_sendmsg(tt->repsock, send, 0) == 0);
So(nng_recvmsg(tt->reqsock, &recv, 0) == 0);
So(recv != NULL);
So(nng_msg_len(recv) == strlen("acknowledge"));
So(strcmp(nng_msg_body(recv), "acknowledge") == 0);
nng_msg_free(recv);
})
}
void
trantest_test_all(const char *addr)
{
trantest tt;
Convey("Given transport", {
trantest_init(&tt, addr);
Reset({
trantest_fini(&tt);
})
trantest_scheme(&tt);
trantest_conn_refused(&tt);
trantest_duplicate_listen(&tt);
trantest_listen_accept(&tt);
trantest_send_recv(&tt);
})
}
|