selftest.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <stdexcept>
  32. #include <iostream>
  33. #include <string>
  34. #include <vector>
  35. #include "node/Constants.hpp"
  36. #include "node/RuntimeEnvironment.hpp"
  37. #include "node/InetAddress.hpp"
  38. #include "node/Utils.hpp"
  39. #include "node/Identity.hpp"
  40. #include "node/Packet.hpp"
  41. #include "node/Salsa20.hpp"
  42. #include "node/MAC.hpp"
  43. #include "node/Peer.hpp"
  44. #include "node/Condition.hpp"
  45. #include "node/NodeConfig.hpp"
  46. #include "node/Dictionary.hpp"
  47. #include "node/EthernetTap.hpp"
  48. #include "node/SHA512.hpp"
  49. #include "node/C25519.hpp"
  50. #include "node/Poly1305.hpp"
  51. #include "node/CertificateOfMembership.hpp"
  52. #include "node/HttpClient.hpp"
  53. #include "node/Defaults.hpp"
  54. #include "node/Node.hpp"
  55. #ifdef __WINDOWS__
  56. #include <tchar.h>
  57. #endif
  58. using namespace ZeroTier;
  59. #include "selftest-crypto-vectors.hpp"
  60. static unsigned char fuzzbuf[1048576];
  61. static Condition webDoneCondition;
  62. static std::string webSha512ShouldBe;
  63. static void testHttpHandler(void *arg,int code,const std::string &url,bool onDisk,const std::string &body)
  64. {
  65. unsigned char sha[64];
  66. if (code == 200) {
  67. SHA512::hash(sha,body.data(),(unsigned int)body.length());
  68. if (webSha512ShouldBe == Utils::hex(sha,64))
  69. std::cout << "got " << body.length() << " bytes, response code " << code << ", SHA-512 OK" << std::endl;
  70. else std::cout << "got " << body.length() << " bytes, response code " << code << ", SHA-512 FAILED!" << std::endl;
  71. } else std::cout << "ERROR " << code << ": " << body << std::endl;
  72. webDoneCondition.signal();
  73. }
  74. static int testHttp()
  75. {
  76. webSha512ShouldBe = "221b348c8278ad2063c158fb15927c35dc6bb42880daf130d0574025f88ec350811c34fae38a014b576d3ef5c98af32bb540e68204810db87a51fa9b239ea567";
  77. std::cout << "[http] fetching http://download.zerotier.com/dev/1k ... "; std::cout.flush();
  78. HttpClient::GET("http://download.zerotier.com/dev/1k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0);
  79. webDoneCondition.wait();
  80. webSha512ShouldBe = "342e1a058332aad2d7a5412c1d9cd4ad02b4038178ca0c3ed9d34e3cf0905c118b684e5d2a935a158195d453d7d69e9c6e201e252620fb53f29611794a5d4b0c";
  81. std::cout << "[http] fetching http://download.zerotier.com/dev/2k ... "; std::cout.flush();
  82. HttpClient::GET("http://download.zerotier.com/dev/2k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0);
  83. webDoneCondition.wait();
  84. webSha512ShouldBe = "439562e1471dd6bdb558cb680f38dd7742e521497e280cb1456a31f74b9216b7d98145b3896c2f68008e6ac0c1662a4cb70562caeac294c5d01f378b22a21292";
  85. std::cout << "[http] fetching http://download.zerotier.com/dev/4k ... "; std::cout.flush();
  86. HttpClient::GET("http://download.zerotier.com/dev/4k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0);
  87. webDoneCondition.wait();
  88. webSha512ShouldBe = "fbd3901a9956158b9d290efa1af4fff459d8c03187c98b0e630d10a19fab61940e668652257763973f6cde34f2aa81574f9a50b1979b675b45ddd18d69a4ceb8";
  89. std::cout << "[http] fetching http://download.zerotier.com/dev/8k ... "; std::cout.flush();
  90. HttpClient::GET("http://download.zerotier.com/dev/8k",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0);
  91. webDoneCondition.wait();
  92. webSha512ShouldBe = "098ae593f8c3a962f385f9f008ec2116ad22eea8bc569fc88a06a0193480fdfb27470345c427116d19179fb2a74df21d95fe5f1df575a9f2d10d99595708b765";
  93. std::cout << "[http] fetching http://download.zerotier.com/dev/4m ... "; std::cout.flush();
  94. HttpClient::GET("http://download.zerotier.com/dev/4m",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0);
  95. webDoneCondition.wait();
  96. webSha512ShouldBe = "";
  97. std::cout << "[http] fetching http://download.zerotier.com/dev/NOEXIST ... "; std::cout.flush();
  98. HttpClient::GET("http://download.zerotier.com/dev/NOEXIST",HttpClient::NO_HEADERS,30,&testHttpHandler,(void *)0);
  99. webDoneCondition.wait();
  100. webSha512ShouldBe = "";
  101. std::cout << "[http] fetching http://1.1.1.1/SHOULD_TIME_OUT ... "; std::cout.flush();
  102. HttpClient::GET("http://1.1.1.1/SHOULD_TIME_OUT",HttpClient::NO_HEADERS,4,&testHttpHandler,(void *)0);
  103. webDoneCondition.wait();
  104. return 0;
  105. }
  106. static int testCrypto()
  107. {
  108. unsigned char buf1[16384];
  109. unsigned char buf2[sizeof(buf1)],buf3[sizeof(buf1)];
  110. for(int i=0;i<3;++i) {
  111. Utils::getSecureRandom(buf1,64);
  112. std::cout << "[crypto] getSecureRandom: " << Utils::hex(buf1,64) << std::endl;
  113. }
  114. std::cout << "[crypto] Testing SHA-512... "; std::cout.flush();
  115. SHA512::hash(buf1,sha512TV0Input,(unsigned int)strlen(sha512TV0Input));
  116. if (memcmp(buf1,sha512TV0Digest,64)) {
  117. std::cout << "FAIL" << std::endl;
  118. return -1;
  119. }
  120. std::cout << "PASS" << std::endl;
  121. std::cout << "[crypto] Testing Poly1305... "; std::cout.flush();
  122. Poly1305::compute(buf1,poly1305TV0Input,sizeof(poly1305TV0Input),poly1305TV0Key);
  123. if (memcmp(buf1,poly1305TV0Tag,16)) {
  124. std::cout << "FAIL (1)" << std::endl;
  125. return -1;
  126. }
  127. Poly1305::compute(buf1,poly1305TV1Input,sizeof(poly1305TV1Input),poly1305TV1Key);
  128. if (memcmp(buf1,poly1305TV1Tag,16)) {
  129. std::cout << "FAIL (2)" << std::endl;
  130. return -1;
  131. }
  132. std::cout << "PASS" << std::endl;
  133. std::cout << "[crypto] Testing C25519 and Ed25519 against test vectors... "; std::cout.flush();
  134. for(int k=0;k<ZT_NUM_C25519_TEST_VECTORS;++k) {
  135. C25519::Pair p1,p2;
  136. memcpy(p1.pub.data,C25519_TEST_VECTORS[k].pub1,p1.pub.size());
  137. memcpy(p1.priv.data,C25519_TEST_VECTORS[k].priv1,p1.priv.size());
  138. memcpy(p2.pub.data,C25519_TEST_VECTORS[k].pub2,p2.pub.size());
  139. memcpy(p2.priv.data,C25519_TEST_VECTORS[k].priv2,p2.priv.size());
  140. C25519::agree(p1,p2.pub,buf1,64);
  141. C25519::agree(p2,p1.pub,buf2,64);
  142. if (memcmp(buf1,buf2,64)) {
  143. std::cout << "FAIL (1)" << std::endl;
  144. return -1;
  145. }
  146. if (memcmp(buf1,C25519_TEST_VECTORS[k].agreement,64)) {
  147. std::cout << "FAIL (2)" << std::endl;
  148. return -1;
  149. }
  150. C25519::Signature sig1 = C25519::sign(p1,buf1,64);
  151. if (memcmp(sig1.data,C25519_TEST_VECTORS[k].agreementSignedBy1,64)) {
  152. std::cout << "FAIL (3)" << std::endl;
  153. return -1;
  154. }
  155. C25519::Signature sig2 = C25519::sign(p2,buf1,64);
  156. if (memcmp(sig2.data,C25519_TEST_VECTORS[k].agreementSignedBy2,64)) {
  157. std::cout << "FAIL (4)" << std::endl;
  158. return -1;
  159. }
  160. }
  161. std::cout << "PASS" << std::endl;
  162. std::cout << "[crypto] Testing C25519 ECC key agreement... "; std::cout.flush();
  163. for(unsigned int i=0;i<100;++i) {
  164. memset(buf1,64,sizeof(buf1));
  165. memset(buf2,64,sizeof(buf2));
  166. memset(buf3,64,sizeof(buf3));
  167. C25519::Pair p1 = C25519::generate();
  168. C25519::Pair p2 = C25519::generate();
  169. C25519::Pair p3 = C25519::generate();
  170. C25519::agree(p1,p2.pub,buf1,64);
  171. C25519::agree(p2,p1.pub,buf2,64);
  172. C25519::agree(p3,p1.pub,buf3,64);
  173. // p1<>p2 should equal p1<>p2
  174. if (memcmp(buf1,buf2,64)) {
  175. std::cout << "FAIL (1)" << std::endl;
  176. return -1;
  177. }
  178. // p2<>p1 should not equal p3<>p1
  179. if (!memcmp(buf2,buf3,64)) {
  180. std::cout << "FAIL (2)" << std::endl;
  181. return -1;
  182. }
  183. }
  184. std::cout << "PASS" << std::endl;
  185. std::cout << "[crypto] Testing Ed25519 ECC signatures... "; std::cout.flush();
  186. C25519::Pair didntSign = C25519::generate();
  187. for(unsigned int i=0;i<10;++i) {
  188. C25519::Pair p1 = C25519::generate();
  189. for(unsigned int k=0;k<sizeof(buf1);++k)
  190. buf1[k] = (unsigned char)rand();
  191. C25519::Signature sig = C25519::sign(p1,buf1,sizeof(buf1));
  192. if (!C25519::verify(p1.pub,buf1,sizeof(buf1),sig)) {
  193. std::cout << "FAIL (1)" << std::endl;
  194. return -1;
  195. }
  196. ++buf1[17];
  197. if (C25519::verify(p1.pub,buf1,sizeof(buf1),sig)) {
  198. std::cout << "FAIL (2)" << std::endl;
  199. return -1;
  200. }
  201. --buf1[17];
  202. if (!C25519::verify(p1.pub,buf1,sizeof(buf1),sig)) {
  203. std::cout << "FAIL (3)" << std::endl;
  204. return -1;
  205. }
  206. if (C25519::verify(didntSign.pub,buf1,sizeof(buf1),sig)) {
  207. std::cout << "FAIL (2)" << std::endl;
  208. return -1;
  209. }
  210. for(unsigned int k=0;k<64;++k) {
  211. C25519::Signature sig2(sig);
  212. sig2.data[rand() % sig2.size()] ^= (unsigned char)(1 << (rand() & 7));
  213. if (C25519::verify(p1.pub,buf1,sizeof(buf1),sig2)) {
  214. std::cout << "FAIL (5)" << std::endl;
  215. return -1;
  216. }
  217. }
  218. }
  219. std::cout << "PASS" << std::endl;
  220. std::cout << "[crypto] Testing Salsa20... "; std::cout.flush();
  221. for(unsigned int i=0;i<4;++i) {
  222. for(unsigned int k=0;k<sizeof(buf1);++k)
  223. buf1[k] = (unsigned char)rand();
  224. memset(buf2,0,sizeof(buf2));
  225. memset(buf3,0,sizeof(buf3));
  226. Salsa20 s20;
  227. s20.init("12345678123456781234567812345678",256,"12345678",20);
  228. s20.encrypt(buf1,buf2,sizeof(buf1));
  229. s20.init("12345678123456781234567812345678",256,"12345678",20);
  230. s20.decrypt(buf2,buf3,sizeof(buf2));
  231. if (memcmp(buf1,buf3,sizeof(buf1))) {
  232. std::cout << "FAIL (encrypt/decrypt test)" << std::endl;
  233. return -1;
  234. }
  235. }
  236. Salsa20 s20(s20TV0Key,256,s20TV0Iv,20);
  237. memset(buf1,0,sizeof(buf1));
  238. memset(buf2,0,sizeof(buf2));
  239. s20.encrypt(buf1,buf2,64);
  240. if (memcmp(buf2,s20TV0Ks,64)) {
  241. std::cout << "FAIL (test vector 0)" << std::endl;
  242. return -1;
  243. }
  244. s20.init(s2012TV0Key,256,s2012TV0Iv,12);
  245. memset(buf1,0,sizeof(buf1));
  246. memset(buf2,0,sizeof(buf2));
  247. s20.encrypt(buf1,buf2,64);
  248. if (memcmp(buf2,s2012TV0Ks,64)) {
  249. std::cout << "FAIL (test vector 1)" << std::endl;
  250. return -1;
  251. }
  252. std::cout << "PASS" << std::endl;
  253. return 0;
  254. }
  255. static int testIdentity()
  256. {
  257. Identity id;
  258. Buffer<512> buf;
  259. std::cout << "[identity] Validate known-good identity... "; std::cout.flush();
  260. if (!id.fromString(KNOWN_GOOD_IDENTITY)) {
  261. std::cout << "FAIL (1)" << std::endl;
  262. return -1;
  263. }
  264. if (!id.locallyValidate()) {
  265. std::cout << "FAIL (2)" << std::endl;
  266. return -1;
  267. }
  268. std::cout << "PASS" << std::endl;
  269. std::cout << "[identity] Validate known-bad identity... "; std::cout.flush();
  270. if (!id.fromString(KNOWN_BAD_IDENTITY)) {
  271. std::cout << "FAIL (1)" << std::endl;
  272. return -1;
  273. }
  274. if (id.locallyValidate()) {
  275. std::cout << "FAIL (2)" << std::endl;
  276. return -1;
  277. }
  278. std::cout << "PASS (i.e. it failed)" << std::endl;
  279. for(unsigned int k=0;k<4;++k) {
  280. std::cout << "[identity] Generate identity... "; std::cout.flush();
  281. uint64_t genstart = Utils::now();
  282. id.generate();
  283. uint64_t genend = Utils::now();
  284. std::cout << "(took " << (genend - genstart) << "ms): " << id.toString(true) << std::endl;
  285. std::cout << "[identity] Locally validate identity: ";
  286. if (id.locallyValidate()) {
  287. std::cout << "PASS" << std::endl;
  288. } else {
  289. std::cout << "FAIL" << std::endl;
  290. return -1;
  291. }
  292. }
  293. {
  294. Identity id2;
  295. buf.clear();
  296. id.serialize(buf,true);
  297. id2.deserialize(buf);
  298. std::cout << "[identity] Serialize and deserialize (w/private): ";
  299. if ((id == id2)&&(id2.locallyValidate())) {
  300. std::cout << "PASS" << std::endl;
  301. } else {
  302. std::cout << "FAIL" << std::endl;
  303. return -1;
  304. }
  305. }
  306. {
  307. Identity id2;
  308. buf.clear();
  309. id.serialize(buf,false);
  310. id2.deserialize(buf);
  311. std::cout << "[identity] Serialize and deserialize (no private): ";
  312. if ((id == id2)&&(id2.locallyValidate())) {
  313. std::cout << "PASS" << std::endl;
  314. } else {
  315. std::cout << "FAIL" << std::endl;
  316. return -1;
  317. }
  318. }
  319. {
  320. Identity id2;
  321. id2.fromString(id.toString(true).c_str());
  322. std::cout << "[identity] Serialize and deserialize (ASCII w/private): ";
  323. if ((id == id2)&&(id2.locallyValidate())) {
  324. std::cout << "PASS" << std::endl;
  325. } else {
  326. std::cout << "FAIL" << std::endl;
  327. return -1;
  328. }
  329. }
  330. {
  331. Identity id2;
  332. id2.fromString(id.toString(false).c_str());
  333. std::cout << "[identity] Serialize and deserialize (ASCII no private): ";
  334. if ((id == id2)&&(id2.locallyValidate())) {
  335. std::cout << "PASS" << std::endl;
  336. } else {
  337. std::cout << "FAIL" << std::endl;
  338. return -1;
  339. }
  340. }
  341. return 0;
  342. }
  343. static int testCertificate()
  344. {
  345. Identity authority;
  346. std::cout << "[certificate] Generating identity to act as authority... "; std::cout.flush();
  347. authority.generate();
  348. std::cout << authority.address().toString() << std::endl;
  349. Identity idA,idB;
  350. std::cout << "[certificate] Generating identities A and B... "; std::cout.flush();
  351. idA.generate();
  352. idB.generate();
  353. std::cout << idA.address().toString() << ", " << idB.address().toString() << std::endl;
  354. std::cout << "[certificate] Generating certificates A and B...";
  355. CertificateOfMembership cA(10000,100,1,idA.address());
  356. CertificateOfMembership cB(10099,100,1,idB.address());
  357. std::cout << std::endl;
  358. std::cout << "[certificate] Signing certificates A and B with authority...";
  359. cA.sign(authority);
  360. cB.sign(authority);
  361. std::cout << std::endl;
  362. //std::cout << "[certificate] A: " << cA.toString() << std::endl;
  363. //std::cout << "[certificate] B: " << cB.toString() << std::endl;
  364. std::cout << "[certificate] A agrees with B and B with A... ";
  365. if (cA.agreesWith(cB))
  366. std::cout << "yes, ";
  367. else {
  368. std::cout << "FAIL" << std::endl;
  369. return -1;
  370. }
  371. if (cB.agreesWith(cA))
  372. std::cout << "yes." << std::endl;
  373. else {
  374. std::cout << "FAIL" << std::endl;
  375. return -1;
  376. }
  377. std::cout << "[certificate] Testing string serialization... ";
  378. CertificateOfMembership copyA(cA.toString());
  379. CertificateOfMembership copyB(cB.toString());
  380. if (copyA != cA) {
  381. std::cout << "FAIL" << std::endl;
  382. return -1;
  383. }
  384. if (copyB != cB) {
  385. std::cout << "FAIL" << std::endl;
  386. return -1;
  387. }
  388. std::cout << "PASS" << std::endl;
  389. std::cout << "[certificate] Generating two certificates that should not agree...";
  390. cA = CertificateOfMembership(10000,100,1,idA.address());
  391. cB = CertificateOfMembership(10101,100,1,idB.address());
  392. std::cout << std::endl;
  393. std::cout << "[certificate] A agrees with B and B with A... ";
  394. if (!cA.agreesWith(cB))
  395. std::cout << "no, ";
  396. else {
  397. std::cout << "FAIL" << std::endl;
  398. return -1;
  399. }
  400. if (!cB.agreesWith(cA))
  401. std::cout << "no." << std::endl;
  402. else {
  403. std::cout << "FAIL" << std::endl;
  404. return -1;
  405. }
  406. return 0;
  407. }
  408. static int testPacket()
  409. {
  410. unsigned char salsaKey[32],hmacKey[32];
  411. Packet a,b;
  412. a.zeroAll();
  413. b.zeroAll();
  414. for(unsigned int i=0;i<32;++i) {
  415. salsaKey[i] = (unsigned char)rand();
  416. hmacKey[i] = (unsigned char)rand();
  417. }
  418. std::cout << "[packet] Testing Packet encoder/decoder... ";
  419. a.reset(Address(),Address(),Packet::VERB_HELLO);
  420. for(int i=0;i<32;++i)
  421. a.append("supercalifragilisticexpealidocious",(unsigned int)strlen("supercalifragilisticexpealidocious"));
  422. b = a;
  423. if (a != b) {
  424. std::cout << "FAIL (assign)" << std::endl;
  425. return -1;
  426. }
  427. a.compress();
  428. unsigned int complen = a.size();
  429. a.uncompress();
  430. std::cout << "(compressed: " << complen << ", decompressed: " << a.size() << ") ";
  431. if (a != b) {
  432. std::cout << "FAIL (compresssion)" << std::endl;
  433. return -1;
  434. }
  435. a.armor(salsaKey,true);
  436. if (!a.dearmor(salsaKey)) {
  437. std::cout << "FAIL (encrypt-decrypt/verify)" << std::endl;
  438. return -1;
  439. }
  440. std::cout << "PASS" << std::endl;
  441. return 0;
  442. }
  443. static int testOther()
  444. {
  445. std::cout << "[other] Testing hex encode/decode... "; std::cout.flush();
  446. for(unsigned int k=0;k<1000;++k) {
  447. unsigned int flen = (rand() % 8194) + 1;
  448. for(unsigned int i=0;i<flen;++i)
  449. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  450. std::string dec = Utils::unhex(Utils::hex(fuzzbuf,flen).c_str());
  451. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  452. std::cout << "FAILED!" << std::endl;
  453. std::cout << Utils::hex(fuzzbuf,flen) << std::endl;
  454. std::cout << Utils::hex(dec.data(),(unsigned int)dec.length()) << std::endl;
  455. return -1;
  456. }
  457. }
  458. std::cout << "PASS" << std::endl;
  459. std::cout << "[other] Testing command bus encode/decode... "; std::cout.flush();
  460. try {
  461. static char key[32] = { 0 };
  462. for(unsigned int k=0;k<100;++k) {
  463. std::vector<std::string> original;
  464. for(unsigned int i=0,j=rand() % 256,l=(rand() % 1024)+1;i<j;++i)
  465. original.push_back(std::string(l,'x'));
  466. std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets(NodeConfig::encodeControlMessage(key,1,original));
  467. //std::cout << packets.size() << ' '; std::cout.flush();
  468. std::vector<std::string> after;
  469. for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator i(packets.begin());i!=packets.end();++i) {
  470. unsigned long convId = 9999;
  471. if (!NodeConfig::decodeControlMessagePacket(key,i->data(),i->size(),convId,after)) {
  472. std::cout << "FAIL (decode)" << std::endl;
  473. return -1;
  474. }
  475. if (convId != 1) {
  476. std::cout << "FAIL (conversation ID)" << std::endl;
  477. return -1;
  478. }
  479. }
  480. if (after != original) {
  481. std::cout << "FAIL (compare)" << std::endl;
  482. return -1;
  483. }
  484. }
  485. } catch (std::exception &exc) {
  486. std::cout << "FAIL (" << exc.what() << ")" << std::endl;
  487. return -1;
  488. }
  489. std::cout << "PASS" << std::endl;
  490. std::cout << "[other] Testing Dictionary... "; std::cout.flush();
  491. for(int k=0;k<1000;++k) {
  492. Dictionary a,b;
  493. int nk = rand() % 32;
  494. for(int q=0;q<nk;++q) {
  495. std::string k,v;
  496. int kl = (rand() % 512);
  497. int vl = (rand() % 512);
  498. for(int i=0;i<kl;++i)
  499. k.push_back((char)rand());
  500. for(int i=0;i<vl;++i)
  501. v.push_back((char)rand());
  502. a[k] = v;
  503. }
  504. std::string aser = a.toString();
  505. b.fromString(aser);
  506. if (a != b) {
  507. std::cout << "FAIL!" << std::endl;
  508. return -1;
  509. }
  510. }
  511. std::cout << "PASS" << std::endl;
  512. return 0;
  513. }
  514. #ifdef __WINDOWS__
  515. int _tmain(int argc, _TCHAR* argv[])
  516. #else
  517. int main(int argc,char **argv)
  518. #endif
  519. {
  520. int r = 0;
  521. // Code to generate the C25519 test vectors -- did this once and then
  522. // put these up top so that we can ensure that every platform produces
  523. // the same result.
  524. /*
  525. for(int k=0;k<32;++k) {
  526. C25519::Pair p1 = C25519::generate();
  527. C25519::Pair p2 = C25519::generate();
  528. unsigned char agg[64];
  529. C25519::agree(p1,p2.pub,agg,64);
  530. C25519::Signature sig1 = C25519::sign(p1,agg,64);
  531. C25519::Signature sig2 = C25519::sign(p2,agg,64);
  532. printf("{{");
  533. for(int i=0;i<64;++i)
  534. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p1.pub.data[i]);
  535. printf("},{");
  536. for(int i=0;i<64;++i)
  537. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p1.priv.data[i]);
  538. printf("},{");
  539. for(int i=0;i<64;++i)
  540. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p2.pub.data[i]);
  541. printf("},{");
  542. for(int i=0;i<64;++i)
  543. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p2.priv.data[i]);
  544. printf("},{");
  545. for(int i=0;i<64;++i)
  546. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)agg[i]);
  547. printf("},{");
  548. for(int i=0;i<96;++i)
  549. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)sig1.data[i]);
  550. printf("},{");
  551. for(int i=0;i<96;++i)
  552. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)sig2.data[i]);
  553. printf("}}\n");
  554. }
  555. exit(0);
  556. */
  557. std::cout << "[info] sizeof(void *) == " << sizeof(void *) << std::endl;
  558. std::cout << "[info] default home: " << ZT_DEFAULTS.defaultHomePath << std::endl;
  559. std::cout << "[info] system authtoken.secret: " << Node::LocalClient::authTokenDefaultSystemPath() << std::endl;
  560. std::cout << "[info] user authtoken.secret: " << Node::LocalClient::authTokenDefaultUserPath() << std::endl;
  561. srand((unsigned int)time(0));
  562. r |= testHttp();
  563. r |= testCrypto();
  564. r |= testPacket();
  565. r |= testOther();
  566. r |= testIdentity();
  567. r |= testCertificate();
  568. if (r)
  569. std::cout << std::endl << "SOMETHING FAILED!" << std::endl;
  570. return r;
  571. }