Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
fix(agent): some mqtt topics not cleaned up
Browse files Browse the repository at this point in the history
fix #285

Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry authored and DIOHz0r committed Feb 8, 2018
1 parent 5a989aa commit 73dbc94
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 26 deletions.
4 changes: 2 additions & 2 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1389,15 +1389,15 @@ public static function getTopicsToCleanup() {
foreach ($rows as $row) {
$topics[] = 'Policy/' . $row['symbol'];
}
return $topics + [
return array_merge($topics, [
'Command/Subscribe',
'Command/Ping',
'Command/Geolocate',
'Command/Inventory',
'Command/Lock',
'Command/Wipe',
'Command/Unenroll',
];
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion scripts/mqtt.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
* ------------------------------------------------------------------------------
*/


// Ensure current directory when run from crontab
chdir(dirname($_SERVER["SCRIPT_FILENAME"]));

if (isset($argv[1])) {
if ($argv[1] == '--tests') {
echo "running in testing environment" . PHP_EOL;
define('GLPI_ROOT', dirname(dirname(dirname(__DIR__))));
define("GLPI_CONFIG_DIR", GLPI_ROOT . "/tests");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/suite-install/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testInstallPlugin() {
// Configure MQTT broker
\Config::setConfigurationValues($pluginName, [
'mqtt_broker_address' => '127.0.0.1',
'mqtt_internal_broker_address' => '127.0.0.1',
'mqtt_broker_internal_address' => '127.0.0.1',
]);

// Force the MQTT backend's credentials
Expand Down
69 changes: 47 additions & 22 deletions tests/suite-integration/PluginFlyvemdmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,29 +346,39 @@ public function testUnenrollAgent() {
$this->boolean($agent->isNewItem())
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));

$tester = $this;
$mockedAgent = $this->newMockInstance($this->testedClass());
$mockedAgent->getFromDB($agent->getID());
sleep(2);

$mockedAgent->getMockController()->notify = function (
$topic,
$mqttMessage,
$qos = 0,
$retain = 0
) use ($tester, &$mockedAgent) {
$tester->string($topic)->isEqualTo($mockedAgent->getTopic() . "/Command/Unenroll");
$tester->string($mqttMessage)
->isEqualTo(json_encode(['unenroll' => 'now'], JSON_UNESCAPED_SLASHES));
$tester->integer($qos)->isEqualTo(0);
$tester->integer($retain)->isEqualTo(1);
};
$log = new \PluginFlyvemdmMqttlog();

$mockedAgent->update([
'id' => $mockedAgent->getID(),
$rows = $log->find('', '`id` DESC', '1');
$row = array_pop($rows);
if ($row === null) {
$row = ['id' => 0];
}
$startLogId = $row['id'] + 1;

$agent->update([
'id' => $agent->getID(),
'_unenroll' => '',
]);

$this->mock($mockedAgent)->call('notify')->once();
// Get the latest MQTT message
sleep(2);

$rows = $log->find('', '`id` DESC', '1');
$row = array_pop($rows);
if ($row === null) {
$row = ['id' => 0];
}
$endLogId = $row['id'];
$this->integer($startLogId)->isLessThanOrEqualTo($endLogId, 'No MQTT message sent or logged');

$baseTopic = $agent->getTopic();
$expected = [
"$baseTopic/Command/Unenroll" => json_encode(['unenroll' => 'now']),
];
$rows = $log->find("`id` >= '$startLogId' AND `id` <= '$endLogId' AND `direction` = 'O'");
$this->compareListOfMQTTMessages($expected, $rows);
}

/**
Expand Down Expand Up @@ -565,7 +575,7 @@ public function testPingRequest() {
sleep(2);

$log = new \PluginFlyvemdmMqttlog();
$rows = $log->find('', '`date` DESC', '1');
$rows = $log->find("`direction` = 'O'", '`date` DESC', '1');
$row = array_pop($rows);

// check the topic of the message
Expand Down Expand Up @@ -600,7 +610,7 @@ public function testGeolocateRequest() {
sleep(2);

$log = new \PluginFlyvemdmMqttlog();
$rows = $log->find('', '`date` DESC', '1');
$rows = $log->find("`direction` = 'O'", '`date` DESC', '1');
$row = array_pop($rows);

// check the topic of the message
Expand Down Expand Up @@ -639,7 +649,7 @@ public function testInventoryRequest() {
sleep(2);

$log = new \PluginFlyvemdmMqttlog();
$rows = $log->find('', '`date` DESC', '1');
$rows = $log->find('"`direction` = 'O'"', '`date` DESC', '1');
$row = array_pop($rows);

// check the topic of the message
Expand Down Expand Up @@ -802,7 +812,7 @@ private function wipeDevice(\PluginFlyvemdmAgent $agent, $wipe = true, $expected
sleep(2);

$log = new \PluginFlyvemdmMqttlog();
$rows = $log->find('', '`date` DESC', '1');
$rows = $log->find("`direction` = 'O'", '`date` DESC', '1');
$row = array_pop($rows);

// check the topic of the message
Expand Down Expand Up @@ -992,4 +1002,19 @@ private function agentFromInvitation(
return $this->enrollFromInvitation($user, $input);
}

/**
* Compare two lists of MQTT messages
* @param array $expected array of topics => messages
* @param array $received array of messages obtained by find() method
*/
private function compareListOfMQTTMessages(array $expected, array $received) {
// Check the count of messages matches
$this->array($received)->hasSize(count(array_values($expected)));

foreach ($received as $aMessage) {
$this->string($aMessage['direction'])->isEqualTo('I');
$this->array($expected)->hasKey($aMessage['topic']);
$this->array($expected)->contains($aMessage['message']);
}
}
}
61 changes: 61 additions & 0 deletions tests/suite-unit/PluginFlyvemdmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,65 @@ public function testCanViewItem() {
$testedInstance = $this->newTestedInstance;
$this->boolean($testedInstance->canViewItem())->isFalse();
}

public function testGetTopicsToCleanup() {
$topics = \PluginFlyvemdmAgent::getTopicsToCleanup();

$expected = [
// Commands
'Command/Subscribe',
'Command/Ping',
'Command/Geolocate',
'Command/Inventory',
'Command/Lock',
'Command/Wipe',
'Command/Unenroll',

// Policies
'Policy/passwordEnabled',
'Policy/passwordMinLength',
'Policy/passwordQuality',
'Policy/passwordMinLetters',
'Policy/passwordMinLowerCase',
'Policy/passwordMinNonLetter',
'Policy/passwordMinNumeric',
'Policy/passwordMinSymbols',
'Policy/passwordMinUpperCase',
'Policy/MaximumFailedPasswordsForWipe',
'Policy/MaximumTimeToLock',
'Policy/storageEncryption',
'Policy/disableCamera',
'Policy/deployApp',
'Policy/removeApp',
'Policy/deployFile',
'Policy/removeFile',
'Policy/disableWifi',
'Policy/disableBluetooth',
'Policy/useTLS',
'Policy/disableRoaming',
'Policy/disableGPS',
'Policy/disableUsbMtp',
'Policy/disableUsbPtp',
'Policy/disableUsbAdb',
'Policy/disableFmRadio',
'Policy/disableMobileLine',
'Policy/disableVoiceMail',
'Policy/disableCallAutoAnswer',
'Policy/disableVoiceDictation',
'Policy/disableNfc',
'Policy/disableHostpotTethering',
'Policy/disableUsbOnTheGo',
'Policy/disableSmsMms',
'Policy/disableAirplaneMode',
'Policy/disableStatusBar',
'Policy/disableScreenCapture',
'Policy/resetPassword',
'Policy/disableSpeakerphone',
'Policy/disableCreateVpnProfiles',
'Policy/inventoryFrequency',
];

$this->array($topics)->size->isEqualTo(count($expected));
$this->array($topics)->containsValues($expected);
}
}

0 comments on commit 73dbc94

Please sign in to comment.