From 0be02d40c257e81fa010aa2af4afe0cdf5001231 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 15 Oct 2018 19:20:07 +0200 Subject: [PATCH] fix package test + graph test started --- Module/PackageManager.php | 9 ++-- Stdlib/Base/ExactFloat.php | 38 ---------------- Stdlib/Graph/Graph.php | 68 +++++++++++++++++++--------- tests/Module/PackageManagerTest.php | 31 +++++++++++++ tests/Stdlib/Base/ExactFloatTest.php | 0 tests/Stdlib/Graph/GraphTest.php | 37 ++++++++++++++- 6 files changed, 118 insertions(+), 65 deletions(-) delete mode 100644 Stdlib/Base/ExactFloat.php delete mode 100644 tests/Stdlib/Base/ExactFloatTest.php diff --git a/Module/PackageManager.php b/Module/PackageManager.php index c29755587..054d4e00e 100644 --- a/Module/PackageManager.php +++ b/Module/PackageManager.php @@ -280,9 +280,10 @@ final class PackageManager */ private function authenticate(string $signedHash, string $rawHash) : bool { - return \sodium_crypto_sign_verify_detached($signedHash, $rawHash, $this->publicKey); - //$unsignedHash = \sodium_crypto_sign_open($signedHash, $this->publicKey); - - return $unsignedHash === $rawHash; + try { + return \sodium_crypto_sign_verify_detached($signedHash, $rawHash, $this->publicKey); + } catch(\Throwable $t) { + return false; + } } } diff --git a/Stdlib/Base/ExactFloat.php b/Stdlib/Base/ExactFloat.php deleted file mode 100644 index 058fae748..000000000 --- a/Stdlib/Base/ExactFloat.php +++ /dev/null @@ -1,38 +0,0 @@ -nodes[$key]; + return $this->nodes[$key] ?? null; } /** @@ -154,13 +154,25 @@ class Graph * * @param mixed $key Edge key * - * @return Edge + * @return Edge|null * * @since 1.0.0 */ - public function getEdge($key) : Edge + public function getEdge($key) : ?Edge { - return $this->edges[$key]; + return $this->edges[$key] ?? null; + } + + /** + * Get graph edges + * + * @return Node[] + * + * @since 1.0.0 + */ + public function getEdges() : array + { + return $this->edges; } /** @@ -193,7 +205,7 @@ class Graph /** * Get all node neighbors. * - * @param Node $node Graph node + * @param mixed $node Graph node * * @return Node[] * @@ -201,6 +213,10 @@ class Graph */ public function getNeighbors($node) : array { + if (!($node instanceof Node)) { + $node = $this->getNode($node); + } + $edges = $this->getEdgesOfNode($node); $neighbors = []; @@ -278,7 +294,7 @@ class Graph */ public function getCircle() : array { - // todo: implement + return []; } /** @@ -290,7 +306,7 @@ class Graph */ public function getFloydWarshallShortestPath() : array { - // todo: implement + return []; } /** @@ -302,7 +318,7 @@ class Graph */ public function getDijkstraShortestPath() : array { - // todo: implement + return []; } /** @@ -314,7 +330,7 @@ class Graph */ public function depthFirstTraversal() : array { - // todo: implement + return []; } /** @@ -326,7 +342,7 @@ class Graph */ public function breadthFirstTraversal() : array { - // todo: implement + return []; } /** @@ -338,22 +354,30 @@ class Graph */ public function longestPath() : array { - // todo: implement + return []; } /** * Get longest path between two nodes. * - * @param Node $node1 Graph node - * @param Node $node2 Graph node + * @param mixed $node1 Graph node + * @param mixed $node2 Graph node * * @return Node[] * * @since 1.0.0 */ - public function longestPathBetweenNodes(Node $node1, Node $node2) : array + public function longestPathBetweenNodes($node1, $node2) : array { - // todo: implement + if (!($node1 instanceof Node)) { + $node1 = $this->getNode($node1); + } + + if (!($node2 instanceof Node)) { + $node2 = $this->getNode($node2); + } + + return []; } /** @@ -414,22 +438,22 @@ class Graph public function getGirth() : int { - // todo: implement + return 0; } public function getCircuitRank() : int { - // todo: implement + return 0; } public function getNodeConnectivity() : int { - // todo: implement + return 0; } public function getEdgeConnectivity() : int { - // todo: implement + return 0; } public function isConnected() : bool @@ -442,6 +466,8 @@ class Graph { // todo: implement // get all unconnected sub graphs + + return []; } public function isBipartite() : bool diff --git a/tests/Module/PackageManagerTest.php b/tests/Module/PackageManagerTest.php index 1315f13de..f8dbba38a 100644 --- a/tests/Module/PackageManagerTest.php +++ b/tests/Module/PackageManagerTest.php @@ -31,6 +31,8 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase } if (file_exists(__DIR__ . '/testPackageExtracted')) { + \array_map('unlink', \glob(__DIR__ . '/testPackageExtracted/testSubPackage/*')); + \rmdir(__DIR__ . '/testPackageExtracted/testSubPackage'); \array_map('unlink', \glob(__DIR__ . '/testPackageExtracted/*')); } @@ -89,6 +91,33 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase self::assertTrue($package->isValid()); } + public function testPackageInvalidKey() + { + $package = new PackageManager( + __DIR__ . '/testPackage.zip', + '/invalid', + \file_get_contents(__DIR__ . '/public.key') . ' ' + ); + + $package->extract(__DIR__ . '/testPackageExtracted'); + + self::assertFalse($package->isValid()); + } + + public function testPackageInvalidContent() + { + $package = new PackageManager( + __DIR__ . '/testPackage.zip', + '/invalid', + \file_get_contents(__DIR__ . '/public.key') + ); + + $package->extract(__DIR__ . '/testPackageExtracted'); + \file_put_contents(__DIR__ . '/testPackageExtracted/info.json', ' ', FILE_APPEND); + + self::assertFalse($package->isValid()); + } + public function testCleanup() { $package = new PackageManager( @@ -111,6 +140,8 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase } if (file_exists(__DIR__ . '/testPackageExtracted')) { + \array_map('unlink', \glob(__DIR__ . '/testPackageExtracted/testSubPackage/*')); + \rmdir(__DIR__ . '/testPackageExtracted/testSubPackage'); \array_map('unlink', \glob(__DIR__ . '/testPackageExtracted/*')); \rmdir(__DIR__ . '/testPackageExtracted'); } diff --git a/tests/Stdlib/Base/ExactFloatTest.php b/tests/Stdlib/Base/ExactFloatTest.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/Stdlib/Graph/GraphTest.php b/tests/Stdlib/Graph/GraphTest.php index 5420aa8f2..932066cd9 100644 --- a/tests/Stdlib/Graph/GraphTest.php +++ b/tests/Stdlib/Graph/GraphTest.php @@ -17,8 +17,41 @@ use phpOMS\Stdlib\Graph\Graph; class GraphTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + public function testDefault() { - self::markTestIncomplete(); + $graph = new Graph(); + + self::assertEquals(null, $graph->getNode('invalid')); + self::assertEquals([], $graph->getNodes()); + + self::assertEquals(null, $graph->getEdge('invalid')); + self::assertEquals([], $graph->getEdges()); + + self::assertEquals([], $graph->getEdgesOfNode('invalid')); + self::assertEquals([], $graph->getNeighbors('invalid')); + + self::assertEquals(0, $graph->getDimension()); + self::assertEquals(0, $graph->getDiameter()); + self::assertEquals(0, $graph->getOrder()); + self::assertEquals(0, $graph->getSize()); + self::assertEquals(0, $graph->getGirth()); + self::assertEquals(0, $graph->getCircuitRank()); + self::assertEquals(0, $graph->getNodeConnectivity()); + self::assertEquals(0, $graph->getEdgeConnectivity()); + + self::assertTrue($graph->isConnected()); + self::assertTrue($graph->isBipartite()); + self::assertTrue($graph->isTriangleFree()); + self::assertTrue($graph->isCircleFree()); + + self::assertEquals([], $graph->getBridges()); + self::assertEquals([], $graph->getCircle()); + self::assertEquals([], $graph->getFloydWarshallShortestPath()); + self::assertEquals([], $graph->getDijkstraShortestPath()); + self::assertEquals([], $graph->depthFirstTraversal()); + self::assertEquals([], $graph->breadthFirstTraversal()); + self::assertEquals([], $graph->longestPath()); + self::assertEquals([], $graph->longestPathBetweenNodes('invalid1', 'invalid2')); + self::assertEquals([], $graph->getUnconnected()); } }