Esempio n. 1
0
 public function testMsgDbBridge()
 {
     $uuid1 = '11000000-1000-4001-8001-1000000000';
     $uuid2 = '21000000-2000-4002-8002-20000000';
     $runName = uniqid('', true);
     $prvFileName = 'testfile_cronjob_id_rsa_' . date('Ymd_His') . '_' . $runName . '.prv';
     $pubFileName = 'testfile_cronjob_id_rsa_' . date('Ymd_His') . '_' . $runName . '.pub';
     $settignsFileName = 'testfile_cronjob_settings_' . date('Ymd_His') . '_' . $runName . '.pub';
     file_put_contents('test_data/' . $prvFileName, static::NODE_LOCAL_SSL_KEY_PRV);
     file_put_contents('test_data/' . $pubFileName, static::NODE_LOCAL_SSL_KEY_PUB);
     $settings = new Settings('test_data/' . $settignsFileName);
     $settings->data['datadir'] = 'test_data';
     $settings->data['node']['id'] = Node::genIdHexStr(static::NODE_LOCAL_SSL_KEY_PUB);
     $settings->data['node']['sslKeyPrvPass'] = '******';
     $settings->data['node']['sslKeyPrvPath'] = 'test_data/' . $prvFileName;
     $settings->data['node']['sslKeyPubPath'] = 'test_data/' . $pubFileName;
     $settings->data['node']['bridge']['client']['enabled'] = true;
     $localNode = new Node();
     $localNode->setIdHexStr($settings->data['node']['id']);
     $localNode->setUri($settings->data['node']['uriLocal']);
     $localNode->setSslKeyPub(file_get_contents($settings->data['node']['sslKeyPubPath']));
     $nodes = array();
     $nodes[1] = new Node();
     $nodes[1]->setIdHexStr($uuid1 . '01');
     $nodes[1]->setUri('tcp://127.0.0.1:25000');
     $nodes[1]->setSslKeyPub(static::NODE1_SSL_KEY_PUB);
     $nodes[2] = new Node();
     $nodes[2]->setIdHexStr($uuid1 . '02');
     $nodes[2]->setUri('tcp://127.0.0.2:25000');
     $nodes[2]->setSslKeyPub(static::NODE2_SSL_KEY_PUB);
     // Bridge Server
     $nodes[3] = new Node();
     $nodes[3]->setIdHexStr($uuid1 . '03');
     $nodes[3]->setUri('tcp://127.0.0.3:25000');
     $nodes[3]->setSslKeyPub(static::NODE3_SSL_KEY_PUB);
     $nodes[3]->setBridgeServer(true);
     // Bridge Server
     $nodes[4] = new Node();
     $nodes[4]->setIdHexStr($uuid1 . '04');
     $nodes[4]->setUri('tcp://127.0.0.4:25000');
     $nodes[4]->setSslKeyPub(static::NODE4_SSL_KEY_PUB);
     $nodes[4]->setBridgeServer(true);
     $nodes[5] = new Node();
     $nodes[5]->setIdHexStr($uuid1 . '05');
     $nodes[5]->setUri('tcp://127.0.0.5:25000');
     $nodes[5]->setSslKeyPub(static::NODE5_SSL_KEY_PUB);
     $table = new Table();
     $table->setDatadirBasePath($settings->data['datadir']);
     $table->setLocalNode($localNode);
     $table->nodeEnclose($nodes[1]);
     $table->nodeEnclose($nodes[2]);
     $table->nodeEnclose($nodes[3]);
     $table->nodeEnclose($nodes[4]);
     $table->nodeEnclose($nodes[5]);
     $msgs = array();
     for ($nodeNo = 2001; $nodeNo <= 2004; $nodeNo++) {
         $msg = new Msg();
         $msg->setId($uuid2 . $nodeNo);
         $msg->setSrcNodeId($settings->data['node']['id']);
         $msg->setSrcSslKeyPub($table->getLocalNode()->getSslKeyPub());
         #$msg->setSrcUserNickname($settings->data['user']['nickname']);
         $msg->setText('this is  a test. ' . date('Y-m-d H:i:s'));
         $msg->setSslKeyPrvPath($settings->data['node']['sslKeyPrvPath'], $settings->data['node']['sslKeyPrvPass']);
         $msg->setStatus('O');
         #$msg->setDstNodeId($nodes[0]->getIdHexStr());
         #$msg->setDstSslPubKey($nodes[0]->getSslKeyPub());
         $msg->setEncryptionMode('D');
         $msgs[$nodeNo] = $msg;
         #fwrite(STDOUT, __METHOD__.' msg setup: '.$nodeNo.' /'.$msg->getId().'/'.PHP_EOL);
     }
     $msgs[2001]->setDstNodeId($nodes[1]->getIdHexStr());
     $msgs[2001]->setDstSslPubKey($nodes[1]->getSslKeyPub());
     $msgs[2002]->setDstNodeId($nodes[2]->getIdHexStr());
     $msgs[2002]->setDstSslPubKey($nodes[2]->getSslKeyPub());
     $msgs[2003]->setDstNodeId($nodes[3]->getIdHexStr());
     $msgs[2003]->setDstSslPubKey($nodes[3]->getSslKeyPub());
     // Foreign msg.
     $msgs[2004]->setSrcNodeId($nodes[4]->getIdHexStr());
     $msgs[2004]->setDstNodeId($nodes[5]->getIdHexStr());
     $msgs[2004]->setDstSslPubKey($nodes[5]->getSslKeyPub());
     $msgs[2004]->setStatus('U');
     $msgDb = new MsgDb();
     $msgDb->setDatadirBasePath($settings->data['datadir']);
     $cronjobLog = new Logger('cronjob');
     #$cronjobLog->pushHandler(new LoggerStreamHandler('php://stdout', Logger::DEBUG));
     $cronjob = new Cronjob();
     $cronjob->setLog($cronjobLog);
     $cronjob->setMsgDb($msgDb);
     $cronjob->setSettings($settings);
     $cronjob->setTable($table);
     // Encrypt
     foreach ($msgs as $msgId => $msg) {
         try {
             $msg->encrypt();
         } catch (Exception $e) {
             fwrite(STDOUT, 'ERROR: ' . $e->getMessage() . PHP_EOL);
         }
         $cronjob->getMsgDb()->msgAdd($msg);
     }
     // Init Nodes
     #fwrite(STDOUT, __METHOD__.' init nodes'.PHP_EOL);
     $cronjob->msgDbInitNodes();
     $cronjobMsgs = $cronjob->getMsgDb()->getMsgs();
     #foreach($cronjobMsgs as $msgId => $msg){
     #	$outMsg = '/'.$msg->getId().'/ /'.$msg->getStatus().'/ /'.$msg->getEncryptionMode().'/';
     #	fwrite(STDOUT, __METHOD__.' cronjob msg: '.$outMsg.PHP_EOL);
     #}
     $updateMsgs = $cronjob->msgDbSendAll();
     /*foreach($updateMsgs as $msgId => $msg){
     			$outMsg = '/'.$msg['obj']->getId().'/';
     			$outMsg .= ' /'.$msg['obj']->getStatus().'/';
     			$outMsg .= ' /'.$msg['obj']->getEncryptionMode().'/ '.count($msg['nodes']);
     			fwrite(STDOUT, __METHOD__.' msg: '.$outMsg.PHP_EOL);
     			
     			foreach($msg['nodes'] as $nodeId => $node){
     				$outMsg = $nodeId.' /'.(int)$node->getBridgeServer().'/';
     				$outMsg = '/'.(int)is_object($node).'/ /'.$node.'/';
     				fwrite(STDOUT, __METHOD__.'     node: '.$outMsg.PHP_EOL);
     			}
     		}*/
     $this->assertEquals('O', $msgs[2001]->getStatus());
     $this->assertEquals('O', $msgs[2002]->getStatus());
     $this->assertEquals('O', $msgs[2003]->getStatus());
     $this->assertEquals('U', $msgs[2004]->getStatus());
     $this->assertEquals('D', $msgs[2001]->getEncryptionMode());
     $this->assertEquals('D', $msgs[2002]->getEncryptionMode());
     $this->assertEquals('D', $msgs[2003]->getEncryptionMode());
     $this->assertEquals('D', $msgs[2004]->getEncryptionMode());
     $this->assertEquals(2, count($updateMsgs[$uuid2 . '2001']['nodes']));
     $this->assertEquals(2, count($updateMsgs[$uuid2 . '2002']['nodes']));
     $this->assertEquals(2, count($updateMsgs[$uuid2 . '2003']['nodes']));
     $this->assertEquals(2, count($updateMsgs[$uuid2 . '2004']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '03', $updateMsgs[$uuid2 . '2001']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '04', $updateMsgs[$uuid2 . '2001']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '03', $updateMsgs[$uuid2 . '2002']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '04', $updateMsgs[$uuid2 . '2002']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '03', $updateMsgs[$uuid2 . '2003']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '04', $updateMsgs[$uuid2 . '2003']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '03', $updateMsgs[$uuid2 . '2004']['nodes']));
     $this->assertTrue(array_key_exists($uuid1 . '04', $updateMsgs[$uuid2 . '2004']['nodes']));
 }
Esempio n. 2
0
 public function testGetMsgById()
 {
     $db = new MsgDb();
     $msg = new Msg();
     $msg->setId('76cabb4d-e729-4a50-a792-e223704c2943');
     $db->msgAdd($msg);
     $msg = new Msg();
     $msg->setId('76cabb4d-e729-4a50-a792-e223704c2944');
     $db->msgAdd($msg);
     $msg = new Msg();
     $msg->setId('76cabb4d-e729-4a50-a792-e223704c2945');
     $db->msgAdd($msg);
     $msg = $db->getMsgById('76cabb4d-e729-4a50-a792-e223704c2944');
     $this->assertTrue(is_object($msg));
     $msg = $db->getMsgById('76cabb4d-e729-4a50-a792-e223704c2946');
     $this->assertEquals(null, $msg);
 }