예제 #1
0
파일: MsgTest.php 프로젝트: thefox/phpchat
 public function testSaveLoad()
 {
     $runName = uniqid('', true);
     $fileName = 'testfile_msg_' . date('Ymd_His') . '_' . $runName . '.yml';
     $msg = new Msg('test_data/' . $fileName);
     $msg->setDatadirBasePath('test_data');
     $msg->setDataChanged(true);
     $msg->setVersion(21);
     $this->assertEquals(21, $msg->getVersion());
     $msg->setId('cafed00d-2131-4159-8e11-0b4dbadb1738');
     $this->assertEquals('cafed00d-2131-4159-8e11-0b4dbadb1738', $msg->getId());
     $msg->setRelayNodeId('cafed00d-2231-4159-8e11-0b4dbadb1738');
     $this->assertEquals('cafed00d-2231-4159-8e11-0b4dbadb1738', $msg->getRelayNodeId());
     $msg->setSrcNodeId('cafed00d-2331-4159-8e11-0b4dbadb1738');
     $this->assertEquals('cafed00d-2331-4159-8e11-0b4dbadb1738', $msg->getSrcNodeId());
     $msg->setSrcSslKeyPub(static::SRC1_SSL_KEY_PUB);
     $this->assertEquals(static::SRC1_SSL_KEY_PUB, $msg->getSrcSslKeyPub());
     $msg->setSrcUserNickname('thefox');
     $this->assertEquals('thefox', $msg->getSrcUserNickname());
     $msg->setDstNodeId('cafed00d-2431-4159-8e11-0b4dbadb1738');
     $this->assertEquals('cafed00d-2431-4159-8e11-0b4dbadb1738', $msg->getDstNodeId());
     $msg->setDstSslPubKey(static::DST1_SSL_KEY_PUB);
     $this->assertEquals(static::DST1_SSL_KEY_PUB, $msg->getDstSslPubKey());
     $msg->setSubject('my first subject');
     $this->assertEquals('my first subject', $msg->getSubject());
     $msg->setText('hello world! this is a test');
     $this->assertEquals('hello world! this is a test', $msg->getText());
     $msg->setPassword('my_password01');
     $this->assertEquals('my_password01', $msg->getPassword());
     $msg->setChecksum('checksuuuum_sum');
     $this->assertEquals('checksuuuum_sum', $msg->getChecksum());
     $msg->setSentNodes(array(21, 2, 1987));
     $msg->addSentNode(42);
     $this->assertEquals(array(21, 2, 1987, 42), $msg->getSentNodes());
     $msg->setRelayCount(22);
     $this->assertEquals(22, $msg->getRelayCount());
     $msg->setForwardCycles(23);
     $msg->incForwardCycles();
     $this->assertEquals(24, $msg->getForwardCycles());
     $msg->setEncryptionMode('D');
     $this->assertEquals('D', $msg->getEncryptionMode());
     $msg->setStatus('O');
     $this->assertEquals('O', $msg->getStatus());
     $msg->setTimeCreated(679874400);
     $this->assertEquals(679874400, $msg->getTimeCreated());
     $msg->setSslKeyPrv(static::SRC1_SSL_KEY_PRV, static::SSL_KEY_PRV_PASS);
     $this->assertTrue($msg->encrypt());
     $this->assertTrue((bool) $msg->save());
     $finder = new Finder();
     $files = $finder->in('test_data')->depth(0)->name($fileName)->files();
     $this->assertEquals(1, count($files));
     $msg = new Msg('test_data/' . $fileName);
     $msg->setDatadirBasePath('test_data');
     $this->assertTrue($msg->load());
     $this->assertEquals(21, $msg->getVersion());
     $this->assertEquals('cafed00d-2131-4159-8e11-0b4dbadb1738', $msg->getId());
     $this->assertEquals('cafed00d-2231-4159-8e11-0b4dbadb1738', $msg->getRelayNodeId());
     $this->assertEquals('cafed00d-2331-4159-8e11-0b4dbadb1738', $msg->getSrcNodeId());
     $this->assertEquals(static::SRC1_SSL_KEY_PUB, $msg->getSrcSslKeyPub());
     $this->assertEquals('cafed00d-2431-4159-8e11-0b4dbadb1738', $msg->getDstNodeId());
     $this->assertEquals(array(21, 2, 1987, 42), $msg->getSentNodes());
     $this->assertEquals(22, $msg->getRelayCount());
     $this->assertEquals(24, $msg->getForwardCycles());
     $this->assertEquals('D', $msg->getEncryptionMode());
     $this->assertEquals('O', $msg->getStatus());
     $this->assertEquals(679874400, $msg->getTimeCreated());
     $msg = new Msg('test_data/' . $fileName);
     $msg->setDatadirBasePath('test_data');
     $this->assertTrue($msg->load());
     $msg->setDstSslPubKey(static::DST1_SSL_KEY_PUB);
     $msg->setSslKeyPrv(static::DST1_SSL_KEY_PRV, static::SSL_KEY_PRV_PASS);
     $subject = 'N/A';
     $text = 'N/A';
     try {
         $text = $msg->decrypt();
         $subject = $msg->getSubject();
     } catch (Exception $e) {
         $text = $e->getMessage();
     }
     $this->assertEquals('my first subject', $subject);
     $this->assertEquals('hello world! this is a test', $text);
     $this->assertEquals('thefox', $msg->getSrcUserNickname());
     $msg = new Msg('test_data/' . $fileName);
     $msg->setDatadirBasePath('test_data');
     $this->assertTrue($msg->load());
     $msg->setDstSslPubKey(static::DST2_SSL_KEY_PUB);
     $msg->setSslKeyPrv(static::DST2_SSL_KEY_PRV, static::SSL_KEY_PRV_PASS);
     $subject = 'N/A';
     $text = 'N/A';
     try {
         $text = $msg->decrypt();
         $subject = $msg->getSubject();
     } catch (Exception $e) {
         $text = 'FAILED OK';
     }
     $this->assertEquals('N/A', $subject);
     $this->assertEquals('FAILED OK', $text);
     $this->assertEquals('', $msg->getSrcUserNickname());
     $msg = new Msg('test_data/not_existing.yml');
     $this->assertFalse($msg->load());
 }
예제 #2
0
파일: MsgDb.php 프로젝트: thefox/phpchat
 public function msgUpdate(Msg $msgNew)
 {
     $rv = false;
     if (isset($this->msgs[$msgNew->getId()])) {
         $msgOld = $this->msgs[$msgNew->getId()];
         if ($msgOld->getVersion() != $msgNew->getVersion()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: version'."\n";
             $msgOld->setVersion($msgNew->getVersion());
             $this->setDataChanged(true);
             $rv = true;
         }
         /*if($msgOld->getId() != $msgNew->getId()){
         			#print __CLASS__.'->'.__FUNCTION__.': changed: id'."\n";
         			$msgOld->setId($msgNew->getId());
         			$this->setDataChanged(true);
         			$rv = true;
         		}*/
         if ($msgOld->getSrcNodeId() != $msgNew->getSrcNodeId()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed srcNodeId: '.$msgOld->getSrcNodeId().' '.$msgNew->getSrcNodeId()."\n";
             $msgOld->setSrcNodeId($msgNew->getSrcNodeId());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getSrcSslKeyPub() != $msgNew->getSrcSslKeyPub()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: srcSslKeyPub'."\n";
             $msgOld->setSrcSslKeyPub($msgNew->getSrcSslKeyPub());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getDstNodeId() != $msgNew->getDstNodeId()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: dstNodeId'."\n";
             $msgOld->setDstNodeId($msgNew->getDstNodeId());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getDstSslPubKey() != $msgNew->getDstSslPubKey()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: dstSslPubKey'."\n";
             $msgOld->setDstSslPubKey($msgNew->getDstSslPubKey());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getSubject() != $msgNew->getSubject()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: subject'."\n";
             $msgOld->setSubject($msgNew->getSubject());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getBody() != $msgNew->getBody()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: text'."\n";
             $msgOld->setBody($msgNew->getBody());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getText() != $msgNew->getText()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: text'."\n";
             $msgOld->setText($msgNew->getText());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getPassword() != $msgNew->getPassword()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: password'."\n";
             $msgOld->setPassword($msgNew->getPassword());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getChecksum() != $msgNew->getChecksum()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: checksum'."\n";
             $msgOld->setChecksum($msgNew->getChecksum());
             $this->setDataChanged(true);
             $rv = true;
         }
         $msgOldSentNodes = $msgOld->getSentNodes();
         $msgNewSentNodes = $msgNew->getSentNodes();
         #if(count($msgOldSentNodes) < count($msgNewSentNodes)){
         if ($msgOldSentNodes != $msgNewSentNodes) {
             $msgOld->setSentNodes(array_unique(array_merge($msgOldSentNodes, $msgNewSentNodes)));
         }
         if ($msgOld->getRelayCount() != $msgNew->getRelayCount()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: relayCount'."\n";
             $msgOld->setRelayCount($msgNew->getRelayCount());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getForwardCycles() != $msgNew->getForwardCycles()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: forwardCycles'."\n";
             $msgOld->setForwardCycles($msgNew->getForwardCycles());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getEncryptionMode() != $msgNew->getEncryptionMode()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: encryptionMode, '.$msgOld->getEncryptionMode();
             #print ', '.$msgNew->getEncryptionMode()."\n";
             $msgOld->setEncryptionMode($msgNew->getEncryptionMode());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getStatus() != $msgNew->getStatus()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: status'."\n";
             $msgOld->setStatus($msgNew->getStatus());
             $this->setDataChanged(true);
             $rv = true;
         }
         if ($msgOld->getTimeCreated() != $msgNew->getTimeCreated()) {
             #print __CLASS__.'->'.__FUNCTION__.': changed: timeCreated'."\n";
             $msgOld->setTimeCreated($msgNew->getTimeCreated());
             $this->setDataChanged(true);
             $rv = true;
         }
         /*if($msgOld->getDataChanged() != $msgNew->getDataChanged()){
         			#print __CLASS__.'->'.__FUNCTION__.': changed: dataChanged'."\n";
         			$msgOld->setDataChanged($msgNew->getDataChanged());
         		}*/
     }
     return $rv;
 }