private function sendGenTestDataDefault() { $dirName1 = 'testdir_client1_' . date('Ymd_His') . '_' . uniqid('', true); $dirName2 = 'testdir_client2_' . date('Ymd_His') . '_' . uniqid('', true); $filesystem = new Filesystem(); $filesystem->mkdir('test_data/' . $dirName1, $mode = 0777); $filesystem->mkdir('test_data/' . $dirName2, $mode = 0777); file_put_contents('test_data/' . $dirName1 . '/id_rsa.prv', static::NODE_LOCAL_SSL_KEY_PRV1); file_put_contents('test_data/' . $dirName1 . '/id_rsa.pub', static::NODE_LOCAL_SSL_KEY_PUB1); file_put_contents('test_data/' . $dirName2 . '/id_rsa.prv', static::NODE_LOCAL_SSL_KEY_PRV2); file_put_contents('test_data/' . $dirName2 . '/id_rsa.pub', static::NODE_LOCAL_SSL_KEY_PUB2); $localNode1 = new Node(); $localNode1->setUri('tcp://127.0.0.1:25000'); $localNode1->setIdHexStr('cafed00d-2131-4159-8e11-0b4dbadb1738'); $table1 = new Table('test_data/' . $dirName1 . '/table.yml'); $table1->setDatadirBasePath('test_data/' . $dirName1); $table1->setLocalNode($localNode1); for ($n = 0; $n < 5; $n++) { $node = new Node(); $node->setUri('tcp://192.168.241.' . $n); $node->setIdHexStr('10000000-1000-4000-8000-1' . sprintf('%011d', $n)); $table1->nodeEnclose($node); #fwrite(STDOUT, 'node: /'.$node->getIdHexStr().'/ '.$node->getIpPort()."\n"); } $table1->setDataChanged(true); $table1->save(); $localNode2 = new Node(); $localNode2->setUri('tcp://127.0.0.2:25000'); $localNode2->setIdHexStr('cafed00d-2131-4159-8e11-0b4dbadb1739'); $table2 = new Table('test_data/' . $dirName2 . '/table.yml'); $table2->setDatadirBasePath('test_data/' . $dirName2); $table2->setLocalNode($localNode2); for ($n = 5; $n < 10; $n++) { $node = new Node(); $node->setUri('tcp://192.168.241.' . $n); $node->setIdHexStr('10000000-1000-4000-8000-1' . sprintf('%011d', $n)); $table2->nodeEnclose($node); #fwrite(STDOUT, 'node: /'.$node->getIdHexStr().'/ '.$node->getIpPort()."\n"); } $table2->setDataChanged(true); $table2->save(); $settings1 = new Settings(); $settings1->data['datadir'] = 'test_data/' . $dirName1; $settings1->data['firstRun'] = false; $settings1->data['timeCreated'] = time(); $settings1->data['node']['uriLocal'] = 'tcp://127.0.0.1'; $settings1->data['node']['id'] = Node::genIdHexStr(static::NODE_LOCAL_SSL_KEY_PUB1); $settings1->data['node']['sslKeyPrvPass'] = '******'; $settings1->data['node']['sslKeyPrvPath'] = 'test_data/' . $dirName1 . '/id_rsa.prv'; $settings1->data['node']['sslKeyPubPath'] = 'test_data/' . $dirName1 . '/id_rsa.pub'; $settings1->data['user']['nickname'] = 'user1'; $settings2 = new Settings(); $settings2->data['datadir'] = 'test_data/' . $dirName2; $settings2->data['firstRun'] = false; $settings2->data['timeCreated'] = time(); $settings2->data['node']['uriLocal'] = 'tcp://127.0.0.2'; $settings2->data['node']['id'] = Node::genIdHexStr(static::NODE_LOCAL_SSL_KEY_PUB2); $settings2->data['node']['sslKeyPrvPass'] = '******'; $settings2->data['node']['sslKeyPrvPath'] = 'test_data/' . $dirName2 . '/id_rsa.prv'; $settings2->data['node']['sslKeyPubPath'] = 'test_data/' . $dirName2 . '/id_rsa.pub'; $settings2->data['user']['nickname'] = 'user2'; $log1 = new Logger('client_1'); #$log1->pushHandler(new LoggerStreamHandler('php://stdout', Logger::DEBUG)); $log2 = new Logger('client_2'); #$log2->pushHandler(new LoggerStreamHandler('php://stdout', Logger::DEBUG)); $kernel1 = new Kernel(); $kernel1->setLog($log1); $kernel1->setSettings($settings1); $kernel1->init(); $kernel2 = new Kernel(); $kernel2->setLog($log2); $kernel2->setSettings($settings2); $kernel2->init(); $server1 = $kernel1->getServer(); $server1->setLog($log1); $server2 = $kernel2->getServer(); $server2->setLog($log2); $client1 = new Client(); $client1->debug = true; $client1->setSslPrv($settings1->data['node']['sslKeyPrvPath'], $settings1->data['node']['sslKeyPrvPass']); $client1->setId(1); $client1->setUri($settings1->data['node']['uriLocal']); $client1->setServer($server1); $this->assertEquals($settings1->data['node']['uriLocal'], 'tcp://' . $client1->getUri()->getHost()); #fwrite(STDOUT, 'ip1: /'.$client1->getUri()->getHost().'/'."\n"); $client2 = new Client(); $client2->debug = true; $client2->setSslPrv($settings2->data['node']['sslKeyPrvPath'], $settings2->data['node']['sslKeyPrvPass']); $client2->setId(2); $client2->setUri($settings2->data['node']['uriLocal']); $client2->setServer($server2); $this->assertEquals($settings2->data['node']['uriLocal'], 'tcp://' . $client2->getUri()->getHost()); #fwrite(STDOUT, 'ip2: /'.$client2->getUri()->getHost().'/'."\n"); return array($client1, $client2); }
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'])); }
public function testNodeEnclose5() { $runName = uniqid('', true); $fileName = 'testfile_table_nodeenclose5_' . date('Ymd_His') . '_' . $runName . '.yml'; $localNode = new Node(); $localNode->setIdHexStr('11000001-2002-4004-8008-100000000006'); $table = new Table('test_data/' . $fileName); $table->setDatadirBasePath('test_data'); $table->setLocalNode($localNode); $node1 = new Node(); $node1->setIdHexStr('11000001-2002-4004-8008-100000000001'); $node1->setUri('tcp://192.168.241.1:25000'); $node2 = new Node(); $node2->setIdHexStr('11000001-2002-4004-8008-100000000002'); $node2->setUri('tcp://192.168.241.2:25000'); $node3 = new Node(); $node3->setUri('tcp://192.168.241.1:25000'); $node4 = new Node(); $node4->setIdHexStr('11000001-2002-4004-8008-100000000003'); $node4->setUri('tcp://192.168.241.2:25000'); $table->nodeEnclose($node1); $table->nodeEnclose($node2); $node = $table->nodeEnclose($node3); $this->assertEquals('00000000-0000-4000-8000-000000000000', $node->getIdHexStr()); $this->assertEquals('tcp://192.168.241.1:25000', (string) $node->getUri()); $node = $table->nodeEnclose($node4); $this->assertEquals('11000001-2002-4004-8008-100000000003', $node->getIdHexStr()); $this->assertEquals('tcp://192.168.241.2:25000', (string) $node->getUri()); $this->assertEquals('', (string) $node2->getUri()); $table->save(); }