selftest.cpp 19 KB

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