selftest.cpp 19 KB

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