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