selftest.cpp 19 KB

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