1 // -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2011 New Dream Network
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License version 2, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "include/types.h"
16 #include "msg/msg_types.h"
17 #include "gtest/gtest.h"
21 // input, parsed+printed addr output, leftover
22 // if the parse fails, output + leftover should both be blank.
23 const char *addr_checks[][3] = {
24 { "127.0.0.1", "127.0.0.1:0/0", "" },
25 { "127.0.0.1 foo", "127.0.0.1:0/0", " foo" },
26 { "127.0.0.1:1234 foo", "127.0.0.1:1234/0", " foo" },
27 { "127.0.0.1:1234/5678 foo", "127.0.0.1:1234/5678", " foo" },
28 { "1.2.3:4 a", "", "" },
29 { "2607:f298:4:2243::5522", "[2607:f298:4:2243::5522]:0/0", "" },
30 { "[2607:f298:4:2243::5522]", "[2607:f298:4:2243::5522]:0/0", "" },
31 { "2607:f298:4:2243::5522a", "", "" },
32 { "[2607:f298:4:2243::5522]a", "[2607:f298:4:2243::5522]:0/0", "a" },
33 { "[2607:f298:4:2243::5522]:1234a", "[2607:f298:4:2243::5522]:1234/0", "a" },
34 { "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "[2001:db8:85a3::8a2e:370:7334]:0/0", "" },
35 { "2001:2db8:85a3:4334:4324:8a2e:1370:7334", "[2001:2db8:85a3:4334:4324:8a2e:1370:7334]:0/0", "" },
36 { "::", "[::]:0/0", "" },
37 { "::zz", "[::]:0/0", "zz" },
38 { ":: 12:34", "[::]:0/0", " 12:34" },
40 { "-asdf", "-", "asdf" },
41 { "legacy:1.2.3.4", "1.2.3.4:0/0", "" },
42 { "legacy:1.2.3.4:12", "1.2.3.4:12/0", "" },
43 { "legacy:1.2.3.4:12/34", "1.2.3.4:12/34", "" },
44 { "msgr2:1.2.3.4", "msgr2:1.2.3.4:0/0", "" },
45 { "msgr2:1.2.3.4:12", "msgr2:1.2.3.4:12/0", "" },
46 { "msgr2:1.2.3.4:12/34", "msgr2:1.2.3.4:12/34", "" },
50 TEST(Msgr, TestAddrParsing)
52 for (unsigned i = 0; addr_checks[i][0]; ++i) {
55 bool ok = a.parse(addr_checks[i][0], &end);
64 cout << "'" << addr_checks[i][0] << "' -> '" << out << "' + '" << left << "'" << std::endl;
66 ASSERT_EQ(out, addr_checks[i][1]);
67 ASSERT_EQ(left, addr_checks[i][2]);
71 const char *addr_checks2[][3] = {
72 { "127.0.0.1", "127.0.0.1:0/0", "" },
73 { "127.0.0.1 foo", "127.0.0.1:0/0", " foo" },
74 { "127.0.0.1:1234 foo", "127.0.0.1:1234/0", " foo" },
75 { "127.0.0.1:1234/5678 foo", "127.0.0.1:1234/5678", " foo" },
76 { "2607:f298:4:2243::5522", "[2607:f298:4:2243::5522]:0/0", "" },
77 { "[2607:f298:4:2243::5522]", "[2607:f298:4:2243::5522]:0/0", "" },
78 { "[2607:f298:4:2243::5522]a", "[2607:f298:4:2243::5522]:0/0", "a" },
79 { "[2607:f298:4:2243::5522]:1234a", "[2607:f298:4:2243::5522]:1234/0", "a" },
80 { "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "[2001:db8:85a3::8a2e:370:7334]:0/0", "" },
81 { "2001:2db8:85a3:4334:4324:8a2e:1370:7334", "[2001:2db8:85a3:4334:4324:8a2e:1370:7334]:0/0", "" },
82 { "legacy:1.2.3.4", "1.2.3.4:0/0", "" },
83 { "legacy:1.2.3.4:12", "1.2.3.4:12/0", "" },
84 { "legacy:1.2.3.4:12/34", "1.2.3.4:12/34", "" },
88 TEST(Msgr, TestAddrEncodeAddrvecDecode)
90 for (unsigned i = 0; addr_checks2[i][0]; ++i) {
92 entity_addrvec_t addrvec;
93 bufferlist::iterator bli;
95 bool ok = addr.parse(addr_checks2[i][0], &end);
101 ASSERT_EQ(addr, addrvec.v[0]);
105 TEST(Msgr, TestAddrvec0EncodeAddrDecode)
107 for (unsigned i = 0; addr_checks2[i][0]; ++i) {
109 entity_addrvec_t addrvec;
111 bufferlist::iterator bli;
112 const char *end = "";
113 bool ok = addr.parse(addr_checks2[i][0], &end);
115 addrvec.v.push_back(addr);
116 addrvec.encode(bl, 0);
124 TEST(Msgr, TestEmptyAddrvecEncodeAddrDecode)
126 entity_addrvec_t addrvec;
129 bufferlist::iterator bli;
130 addrvec.encode(bl, 0);
133 ASSERT_EQ(addr, entity_addr_t(1, 0));
136 const char *addrvec_checks[][4] = {
137 { "legacy:1.2.3.4", "msgr2:1.2.3.4", "legacy:1.2.3.4", "msgr2:1.2.3.4" },
138 { "msgr2:1.2.3.5", "legacy:1.2.3.5", "legacy:1.2.3.5", "msgr2:1.2.3.5" },
139 { "msgr2:1.2.3.6", "msgr2:1.2.3.6", "legacy:1.2.3.6", "msgr2:1.2.3.6" },
140 { "msgr2:1.2.3.7", "legacy:1.2.3.7", "legacy:1.2.3.7", "msgr2:1.2.3.7" },
141 { NULL, NULL, NULL, NULL },
145 * multiple addrs where one is legacy and others are not
146 * legacy addr is in position 0
148 TEST(Msgr, TestAddrvecEncodeAddrDecode0)
151 entity_addrvec_t addrvec;
153 bufferlist::iterator bli;
155 for (unsigned i = 0; addrvec_checks[i][0]; ++i) {
156 const char *end = "";
157 bool ok = addr.parse(addrvec_checks[i][0], &end);
159 addrvec.v.push_back(addr);
162 addrvec.encode(bl, 0);
167 ASSERT_EQ(addr, addrvec.v[0]);
171 * multiple addrs where one is legacy and others are not
172 * legacy addr is not in position 0
174 TEST(Msgr, TestAddrvecEncodeAddrDecode1)
176 entity_addr_t addr, a;
177 entity_addrvec_t addrvec;
179 bufferlist::iterator bli;
182 for (unsigned i = 0; addrvec_checks[i][1]; ++i) {
183 const char *end = "";
184 bool ok = addr.parse(addrvec_checks[i][1], &end);
186 if (addr.type == entity_addr_t::TYPE_LEGACY && flag) {
190 addrvec.v.push_back(addr);
193 addrvec.encode(bl, 0);
201 /* multiple legacy addrs */
202 TEST(Msgr, TestAddrvecEncodeAddrDecode2)
205 entity_addrvec_t addrvec;
207 bufferlist::iterator bli;
209 for (unsigned i = 0; addrvec_checks[i][2]; ++i) {
210 const char *end = "";
211 bool ok = addr.parse(addrvec_checks[i][2], &end);
213 addrvec.v.push_back(addr);
216 addrvec.encode(bl, 0);
221 ASSERT_EQ(addr, addrvec.v[0]);
224 /* all non-legacy addrs */
225 TEST(Msgr, TestAddrvecEncodeAddrDecode3)
228 entity_addrvec_t addrvec;
230 bufferlist::iterator bli;
232 for (unsigned i = 0; addrvec_checks[i][3]; ++i) {
233 const char *end = "";
234 bool ok = addr.parse(addrvec_checks[i][3], &end);
236 addrvec.v.push_back(addr);
239 addrvec.encode(bl, 0);
244 ASSERT_NE(addr, addrvec.v[0]); // it's not the first addr(which is non-legacy)
245 ASSERT_NE(addr, entity_addr_t(1, 0)); // it's not a blank addr either