SecurityFunctionsTest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "SecurityFunctionsTest.h"
  11. /*
  12. Description:
  13. Tests:
  14. virtual void RakPeerInterface::AddToSecurityExceptionList ( const char * ip )
  15. virtual void RakPeerInterface::AddToBanList ( const char * IP, TimeMS milliseconds = 0 )
  16. virtual void RakPeerInterface::GetIncomingPassword ( char * passwordData, int * passwordDataLength )
  17. virtual void RakPeerInterface::InitializeSecurity ( const char * pubKeyE, const char * pubKeyN, const char * privKeyP, const char * privKeyQ )
  18. virtual bool RakPeerInterface::IsBanned ( const char * IP )
  19. virtual bool RakPeerInterface::IsInSecurityExceptionList ( const char * ip )
  20. virtual void RakPeerInterface::RemoveFromSecurityExceptionList ( const char * ip )
  21. virtual void RakPeerInterface::RemoveFromBanList ( const char * IP )
  22. virtual void RakPeerInterface::SetIncomingPassword ( const char * passwordData, int passwordDataLength )
  23. virtual void ClearBanList (void)=0
  24. Success conditions:
  25. All functions pass tests.
  26. Failure conditions:
  27. Any function fails test.
  28. Client connects with no password
  29. Client connects with wrong password
  30. Client failed to connect with correct password
  31. Client was banned but connected anyways
  32. GetIncomingPassword returned wrong password
  33. IsBanned does not show localhost as banned
  34. Localhost was not unbanned
  35. Client failed to connect after banlist removal
  36. Client failed to connect after banlist removal with clear function
  37. Client did not connect encrypted
  38. Client connected encrypted but shouldn't have
  39. IsInSecurityExceptionList does not register localhost addition
  40. RakPeerInterface Functions used, tested indirectly by its use:
  41. Startup
  42. SetMaximumIncomingConnections
  43. Receive
  44. DeallocatePacket
  45. Send
  46. IsConnected
  47. GetStatistics
  48. RakPeerInterface Functions Explicitly Tested:
  49. SetIncomingPassword
  50. GetIncomingPassword
  51. AddToBanList
  52. IsBanned
  53. RemoveFromBanList
  54. ClearBanList
  55. InitializeSecurity //Disabled because of RakNetStatistics changes
  56. AddToSecurityExceptionList //Disabled because of RakNetStatistics changes
  57. IsInSecurityExceptionList //Disabled because of RakNetStatistics changes
  58. RemoveFromSecurityExceptionList //Disabled because of RakNetStatistics changes
  59. */
  60. int SecurityFunctionsTest::RunTest(DataStructures::List<RakString> params,bool isVerbose,bool noPauses)
  61. {
  62. char thePassword[]="password";
  63. server=RakPeerInterface::GetInstance();
  64. client=RakPeerInterface::GetInstance();
  65. client->Startup(1,&SocketDescriptor(),1);
  66. server->Startup(1,&SocketDescriptor(60000,0),1);
  67. server->SetMaximumIncomingConnections(1);
  68. server->SetIncomingPassword(thePassword,(int)strlen(thePassword));
  69. char returnedPass[22];
  70. int returnedLen=22;
  71. server->GetIncomingPassword(returnedPass,&returnedLen);
  72. returnedPass[returnedLen]=0;//Password is a data block convert to null terminated string to make the test easier
  73. if (strcmp(returnedPass,thePassword)!=0)
  74. {
  75. if (isVerbose)
  76. {
  77. printf("%s was returned but %s is the password\n",returnedPass,thePassword);
  78. DebugTools::ShowError("GetIncomingPassword returned wrong password\n",!noPauses && isVerbose,__LINE__,__FILE__);
  79. }
  80. return 5;
  81. }
  82. SystemAddress serverAddress;
  83. serverAddress.SetBinaryAddress("127.0.0.1");
  84. serverAddress.port=60000;
  85. TimeMS entryTime=GetTimeMS();
  86. if (isVerbose)
  87. printf("Testing if no password is rejected\n");
  88. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  89. {
  90. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  91. {
  92. client->Connect("127.0.0.1",serverAddress.port,0,0);
  93. }
  94. RakSleep(100);
  95. }
  96. if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  97. {
  98. if (isVerbose)
  99. DebugTools::ShowError("Client connected with no password\n",!noPauses && isVerbose,__LINE__,__FILE__);
  100. return 1;
  101. }
  102. if (isVerbose)
  103. printf("Testing if incorrect password is rejected\n");
  104. char badPass[]="badpass";
  105. entryTime=GetTimeMS();
  106. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  107. {
  108. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  109. {
  110. client->Connect("127.0.0.1",serverAddress.port,badPass,(int)strlen(badPass));
  111. }
  112. RakSleep(100);
  113. }
  114. if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  115. {
  116. if (isVerbose)
  117. DebugTools::ShowError("Client connected with wrong password\n",!noPauses && isVerbose,__LINE__,__FILE__);
  118. return 2;
  119. }
  120. if (isVerbose)
  121. printf("Testing if correct password is accepted\n");
  122. entryTime=GetTimeMS();
  123. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  124. {
  125. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  126. {
  127. client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword));
  128. }
  129. RakSleep(100);
  130. }
  131. if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  132. {
  133. if (isVerbose)
  134. DebugTools::ShowError("Client failed to connect with correct password\n",!noPauses && isVerbose,__LINE__,__FILE__);
  135. return 3;
  136. }
  137. while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client
  138. {
  139. client->CloseConnection (serverAddress,true,0,LOW_PRIORITY);
  140. }
  141. if (isVerbose)
  142. printf("Testing if connection is rejected after adding to ban list\n");
  143. server->AddToBanList("127.0.0.1",0);
  144. entryTime=GetTimeMS();
  145. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  146. {
  147. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  148. {
  149. client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword));
  150. }
  151. RakSleep(100);
  152. }
  153. if(!server->IsBanned("127.0.0.1"))
  154. {
  155. if (isVerbose)
  156. DebugTools::ShowError("IsBanned does not show localhost as banned\n",!noPauses && isVerbose,__LINE__,__FILE__);
  157. return 6;
  158. }
  159. if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  160. {
  161. if (isVerbose)
  162. DebugTools::ShowError("Client was banned but connected anyways\n",!noPauses && isVerbose,__LINE__,__FILE__);
  163. return 4;
  164. }
  165. if (isVerbose)
  166. printf("Testing if connection is accepted after ban removal by RemoveFromBanList\n");
  167. server->RemoveFromBanList("127.0.0.1");
  168. if(server->IsBanned("127.0.0.1"))
  169. {
  170. if (isVerbose)
  171. DebugTools::ShowError("Localhost was not unbanned\n",!noPauses && isVerbose,__LINE__,__FILE__);
  172. return 7;
  173. }
  174. entryTime=GetTimeMS();
  175. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  176. {
  177. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  178. {
  179. client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword));
  180. }
  181. RakSleep(100);
  182. }
  183. if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  184. {
  185. if (isVerbose)
  186. DebugTools::ShowError("Client failed to connect after banlist removal\n",!noPauses && isVerbose,__LINE__,__FILE__);
  187. return 8;
  188. }
  189. while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client
  190. {
  191. client->CloseConnection (serverAddress,true,0,LOW_PRIORITY);
  192. }
  193. if (isVerbose)
  194. printf("Testing if connection is rejected after adding to ban list\n");
  195. server->AddToBanList("127.0.0.1",0);
  196. entryTime=GetTimeMS();
  197. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  198. {
  199. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  200. {
  201. client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword));
  202. }
  203. RakSleep(100);
  204. }
  205. if(!server->IsBanned("127.0.0.1"))
  206. {
  207. if (isVerbose)
  208. DebugTools::ShowError("IsBanned does not show localhost as banned\n",!noPauses && isVerbose,__LINE__,__FILE__);
  209. return 6;
  210. }
  211. if (CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  212. {
  213. if (isVerbose)
  214. DebugTools::ShowError("Client was banned but connected anyways\n",!noPauses && isVerbose,__LINE__,__FILE__);
  215. return 4;
  216. }
  217. if (isVerbose)
  218. printf("Testing if connection is accepted after ban removal by ClearBanList\n");
  219. server->ClearBanList();
  220. if(server->IsBanned("127.0.0.1"))
  221. {
  222. if (isVerbose)
  223. DebugTools::ShowError("Localhost was not unbanned\n",!noPauses && isVerbose,__LINE__,__FILE__);
  224. return 7;
  225. }
  226. entryTime=GetTimeMS();
  227. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  228. {
  229. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  230. {
  231. client->Connect("127.0.0.1",serverAddress.port,thePassword,(int)strlen(thePassword));
  232. }
  233. RakSleep(100);
  234. }
  235. if (!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true))
  236. {
  237. if (isVerbose)
  238. DebugTools::ShowError("Client failed to connect after banlist removal with clear function\n",!noPauses && isVerbose,__LINE__,__FILE__);
  239. return 9;
  240. }
  241. while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client
  242. {
  243. client->CloseConnection (serverAddress,true,0,LOW_PRIORITY);
  244. }
  245. /*//Disabled because of statistics changes
  246. if (isVerbose)
  247. printf("Testing InitializeSecurity on server\n");
  248. //-----------------------------
  249. // RSACrypt is a using namespace RakNet;
  250. class that handles RSA encryption/decryption internally
  251. RSACrypt rsacrypt;
  252. uint32_t e;
  253. uint32_t modulus[RAKNET_RSA_FACTOR_LIMBS];
  254. uint32_t p[RAKNET_RSA_FACTOR_LIMBS/2],q[RAKNET_RSA_FACTOR_LIMBS/2];
  255. printf("Generating %i bit key. This will take a while...\n", RAKNET_RSA_FACTOR_LIMBS*32);
  256. rsacrypt.generatePrivateKey(RAKNET_RSA_FACTOR_LIMBS);
  257. e=rsacrypt.getPublicExponent();
  258. rsacrypt.getPublicModulus(modulus);
  259. rsacrypt.getPrivateP(p);
  260. rsacrypt.getPrivateQ(q);
  261. RakPeerInterface::DestroyInstance(server);
  262. server=RakPeerInterface::GetInstance();
  263. server->InitializeSecurity(0,0,(char*)p, (char*)q);
  264. server->Startup(1,30,&SocketDescriptor(60000,0),1);
  265. server->SetMaximumIncomingConnections(1);
  266. server->SetIncomingPassword(thePassword,strlen(thePassword));
  267. if (isVerbose)
  268. printf("Testing if client connects encrypted\n");
  269. entryTime=GetTimeMS();
  270. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  271. {
  272. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  273. {
  274. client->Connect("127.0.0.1",serverAddress.port,thePassword,strlen(thePassword));
  275. }
  276. RakSleep(100);
  277. }
  278. char str2[]="AAAAAAAAAA";
  279. str2[0]=(char)(ID_USER_PACKET_ENUM+1);
  280. client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true);
  281. client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true);
  282. Packet *packet;
  283. entryTime=GetTimeMS();
  284. while(GetTimeMS()-entryTime<1000)
  285. {
  286. for (packet=server->Receive(); packet;server->DeallocatePacket(packet), packet=server->Receive())
  287. {
  288. }
  289. }
  290. RakNetStatistics *rss;
  291. rss=client->GetStatistics(serverAddress);
  292. if (rss->encryptionBitsSent<=0)//If we did connect encrypted we should see encryptionBitsSent
  293. {
  294. if (isVerbose)
  295. DebugTools::ShowError("Client did not connect encrypted\n",!noPauses && isVerbose,__LINE__,__FILE__);
  296. return 10;
  297. }
  298. while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client
  299. {
  300. client->CloseConnection (serverAddress,true,0,LOW_PRIORITY);
  301. }
  302. //Destroy to clear statistics
  303. RakPeerInterface::DestroyInstance(client);
  304. client=RakPeerInterface::GetInstance();
  305. client->Startup(1,30,&SocketDescriptor(),1);
  306. if (isVerbose)
  307. printf("Testing AddToSecurityExceptionList client should connect without encryption\n");
  308. server->AddToSecurityExceptionList("127.0.0.1");
  309. if (!server->IsInSecurityExceptionList("127.0.0.1"))
  310. {
  311. if (isVerbose)
  312. DebugTools::ShowError("IsInSecurityExceptionList does not register localhost addition\n",!noPauses && isVerbose,__LINE__,__FILE__);
  313. return 12;
  314. }
  315. entryTime=GetTimeMS();
  316. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  317. {
  318. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  319. {
  320. client->Connect("127.0.0.1",serverAddress.port,thePassword,strlen(thePassword));
  321. }
  322. RakSleep(100);
  323. }
  324. str2[0]=(char)(ID_USER_PACKET_ENUM+1);
  325. client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true);
  326. client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true);
  327. // Packet *packet;
  328. entryTime=GetTimeMS();
  329. while(GetTimeMS()-entryTime<1000)
  330. {
  331. for (packet=server->Receive(); packet;server->DeallocatePacket(packet), packet=server->Receive())
  332. {
  333. }
  334. }
  335. rss=client->GetStatistics(serverAddress);
  336. if (rss->encryptionBitsSent>0)//If we did connect encrypted we should see encryptionBitsSent
  337. {
  338. if (isVerbose)
  339. DebugTools::ShowError("Client connected encrypted but shouldn't have\n",!noPauses && isVerbose,__LINE__,__FILE__);
  340. return 11;
  341. }
  342. if (isVerbose)
  343. printf("Testing RemoveFromSecurityExceptionList\n");
  344. while(CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))//disconnect client
  345. {
  346. client->CloseConnection (serverAddress,true,0,LOW_PRIORITY);
  347. }
  348. server->RemoveFromSecurityExceptionList("127.0.0.1");
  349. if (isVerbose)
  350. printf("Testing if client connects encrypted\n");
  351. entryTime=GetTimeMS();
  352. while(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true)&&GetTimeMS()-entryTime<5000)
  353. {
  354. if(!CommonFunctions::ConnectionStateMatchesOptions (client,serverAddress,true,true,true,true))
  355. {
  356. client->Connect("127.0.0.1",serverAddress.port,thePassword,strlen(thePassword));
  357. }
  358. RakSleep(100);
  359. }
  360. str2[0]=(char)(ID_USER_PACKET_ENUM+1);
  361. client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true);
  362. client->Send(str2,(int) strlen(str2)+1, HIGH_PRIORITY, RELIABLE_ORDERED ,0, UNASSIGNED_SYSTEM_ADDRESS, true);
  363. entryTime=GetTimeMS();
  364. while(GetTimeMS()-entryTime<1000)
  365. {
  366. for (packet=server->Receive(); packet;server->DeallocatePacket(packet), packet=server->Receive())
  367. {
  368. }
  369. }
  370. rss=client->GetStatistics(serverAddress);
  371. if (rss->encryptionBitsSent<=0)//If we did connect encrypted we should see encryptionBitsSent
  372. {
  373. if (isVerbose)
  374. DebugTools::ShowError("Client did not connect encrypted\n",!noPauses && isVerbose,__LINE__,__FILE__);
  375. return 10;
  376. }
  377. */
  378. return 0;
  379. }
  380. RakString SecurityFunctionsTest::GetTestName()
  381. {
  382. return "SecurityFunctionsTest";
  383. }
  384. RakString SecurityFunctionsTest::ErrorCodeToString(int errorCode)
  385. {
  386. switch (errorCode)
  387. {
  388. case 0:
  389. return "No error";
  390. break;
  391. case 1:
  392. return "Client connected with no password";
  393. break;
  394. case 2:
  395. return "Client connected with wrong password";
  396. break;
  397. case 3:
  398. return "Client failed to connect with correct password";
  399. break;
  400. case 4:
  401. return "Client was banned but connected anyways";
  402. break;
  403. case 5:
  404. return "GetIncomingPassword returned wrong password";
  405. break;
  406. case 6:
  407. return "IsBanned does not show localhost as banned";
  408. break;
  409. case 7:
  410. return "Localhost was not unbanned";
  411. break;
  412. case 8:
  413. return "Client failed to connect after banlist removal";
  414. break;
  415. case 9:
  416. return "Client failed to connect after banlist removal with clear function";
  417. break;
  418. case 10:
  419. return "Client did not connect encrypted";
  420. break;
  421. case 11:
  422. return "Client connected encrypted but shouldn't have";
  423. break;
  424. case 12:
  425. return "IsInSecurityExceptionList does not register localhost addition";
  426. break;
  427. default:
  428. return "Undefined Error";
  429. }
  430. }
  431. SecurityFunctionsTest::SecurityFunctionsTest(void)
  432. {
  433. }
  434. SecurityFunctionsTest::~SecurityFunctionsTest(void)
  435. {
  436. }
  437. void SecurityFunctionsTest::DestroyPeers()
  438. {
  439. RakPeerInterface::DestroyInstance(client);
  440. RakPeerInterface::DestroyInstance(server);
  441. }
粤ICP备19079148号