Imported Upstream version 1.37.1
[platform/upstream/grpc.git] / src / php / tests / unit_tests / ChannelTest.php
index 583c618..b7df853 100644 (file)
  *
  */
 
-class ChannelTest extends PHPUnit_Framework_TestCase
+class ChannelTest extends \PHPUnit\Framework\TestCase
 {
-    public function setUp()
+    public function setUp(): void
     {
     }
 
-    public function tearDown()
+    public function tearDown(): void
     {
         if (!empty($this->channel)) {
             $this->channel->close();
@@ -37,6 +37,50 @@ class ChannelTest extends PHPUnit_Framework_TestCase
         $this->assertSame('Grpc\Channel', get_class($this->channel));
     }
 
+    public function testConstructorCreateSsl()
+    {
+        $channel = new Grpc\Channel('localhost:50033', 
+            ['credentials' => \Grpc\ChannelCredentials::createSsl()]);
+        $this->assertNotNull($channel);
+    }
+
+    public function testCreateXdsWithSsl()
+    {
+        $xdsCreds = \Grpc\ChannelCredentials::createXds(
+            \Grpc\ChannelCredentials::createSsl()
+        );
+        $this->assertNotNull($xdsCreds);
+    }
+
+    public function disabled_testCreateXdsWithInsecure() {
+        $xdsCreds = \Grpc\ChannelCredentials::createXds(
+            \Grpc\ChannelCredentials::createInsecure()
+        );
+        $this->assertNotNull($xdsCreds);
+    }
+
+    public function testCreateXdsWithNull() {
+        $this->expectException(\InvalidArgumentException::class);
+        $xdsCreds = \Grpc\ChannelCredentials::createXds(null);
+    }
+
+    public function testCreateXdsWithInvalidType()
+    {
+        $expected = $this->logicalOr(
+            // PHP8
+            new \PHPUnit\Framework\Constraint\Exception(\InvalidArgumentException::class),
+            // PHP7
+            new \PHPUnit\Framework\Constraint\Exception(\TypeError::class)
+        );
+        try {
+            $xdsCreds = \Grpc\ChannelCredentials::createXds("invalid-type");
+        } catch (\Throwable $exception) {
+            $this->assertThat($exception, $expected);
+            return;
+        }
+        $this->assertThat(null, $expected);
+    }
+
     public function testGetConnectivityState()
     {
         $this->channel = new Grpc\Channel('localhost:50001',
@@ -99,67 +143,53 @@ class ChannelTest extends PHPUnit_Framework_TestCase
         $this->channel->close();
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
     public function testInvalidConstructorWithNull()
     {
+        $this->expectException(\InvalidArgumentException::class);
         $this->channel = new Grpc\Channel();
         $this->assertNull($this->channel);
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
     public function testInvalidConstructorWith()
     {
+        $this->expectException(\InvalidArgumentException::class);
         $this->channel = new Grpc\Channel('localhost:50008', 'invalid');
         $this->assertNull($this->channel);
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
     public function testInvalidCredentials()
     {
+        $this->expectException(\InvalidArgumentException::class);
         $this->channel = new Grpc\Channel('localhost:50009',
             ['credentials' => new Grpc\Timeval(100)]);
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
     public function testInvalidOptionsArray()
     {
+        $this->expectException(\InvalidArgumentException::class);
         $this->channel = new Grpc\Channel('localhost:50010',
             ['abc' => []]);
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
     public function testInvalidGetConnectivityStateWithArray()
     {
+        $this->expectException(\InvalidArgumentException::class);
         $this->channel = new Grpc\Channel('localhost:50011',
             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
         $this->channel->getConnectivityState([]);
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
     public function testInvalidWatchConnectivityState()
     {
+        $this->expectException(\InvalidArgumentException::class);
         $this->channel = new Grpc\Channel('localhost:50012',
             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
         $this->channel->watchConnectivityState([]);
     }
 
-    /**
-     * @expectedException InvalidArgumentException
-     */
     public function testInvalidWatchConnectivityState2()
     {
+        $this->expectException(\InvalidArgumentException::class);
         $this->channel = new Grpc\Channel('localhost:50013',
             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
         $this->channel->watchConnectivityState(1, 'hi');
@@ -282,70 +312,92 @@ class ChannelTest extends PHPUnit_Framework_TestCase
         $this->channel2->close();
     }
 
-    public function testPersistentChannelSameChannelCredentials()
-    {
-        $creds1 = Grpc\ChannelCredentials::createSsl();
-        $creds2 = Grpc\ChannelCredentials::createSsl();
-
-        $this->channel1 = new Grpc\Channel('localhost:50019',
-                                           ["credentials" => $creds1,
-                                             "grpc_target_persist_bound" => 3,
-                                             ]);
-        $this->channel2 = new Grpc\Channel('localhost:50019',
-                                           ["credentials" => $creds2]);
-
-        // try to connect on channel1
-        $state = $this->channel1->getConnectivityState(true);
-        $this->waitUntilNotIdle($this->channel1);
-
-        $state = $this->channel1->getConnectivityState();
-        $this->assertConnecting($state);
-        $state = $this->channel2->getConnectivityState();
-        $this->assertConnecting($state);
-
-        $this->channel1->close();
-        $this->channel2->close();
-    }
-
-    public function testPersistentChannelDifferentChannelCredentials()
-    {
-        $creds1 = Grpc\ChannelCredentials::createSsl();
-        $creds2 = Grpc\ChannelCredentials::createSsl(
-            file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
-
-        $this->channel1 = new Grpc\Channel('localhost:50020',
-                                           ["credentials" => $creds1,
-                                             "grpc_target_persist_bound" => 3,
-                                             ]);
-        $this->channel2 = new Grpc\Channel('localhost:50020',
-                                           ["credentials" => $creds2]);
-
-        // try to connect on channel1
-        $state = $this->channel1->getConnectivityState(true);
-        $this->waitUntilNotIdle($this->channel1);
-
-        $state = $this->channel1->getConnectivityState();
-        $this->assertConnecting($state);
-        $state = $this->channel2->getConnectivityState();
-        $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
-
-        $this->channel1->close();
-        $this->channel2->close();
+    public function persistentChannelSameChannelCredentialsProvider(): array
+    {
+        return [
+            [
+                Grpc\ChannelCredentials::createSsl(),
+                Grpc\ChannelCredentials::createSsl(),
+                50301,
+            ],
+            [
+                Grpc\ChannelCredentials::createSsl(
+                    file_get_contents(dirname(__FILE__) . '/../data/ca.pem')
+                ),
+                Grpc\ChannelCredentials::createSsl(
+                    file_get_contents(dirname(__FILE__) . '/../data/ca.pem')
+                ),
+                50302,
+            ],
+            [
+                Grpc\ChannelCredentials::createInSecure(),
+                Grpc\ChannelCredentials::createInSecure(),
+                50303,
+            ],
+            [
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl()
+                ),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl()
+                ),
+                50304,
+            ],
+            [
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl()
+                ),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl()
+                ),
+                50305,
+            ],
+            [
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl(
+                        file_get_contents(dirname(__FILE__) . '/../data/ca.pem')
+                    )
+                ),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl(
+                        file_get_contents(dirname(__FILE__) . '/../data/ca.pem')
+                    )
+                ),
+                50306,
+            ],
+            /*
+            [
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createInSecure()
+                ),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createInSecure()
+                ),
+                50307,
+            ],
+            */
+        ];
     }
 
-    public function testPersistentChannelSameChannelCredentialsRootCerts()
-    {
-        $creds1 = Grpc\ChannelCredentials::createSsl(
-            file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
-        $creds2 = Grpc\ChannelCredentials::createSsl(
-            file_get_contents(dirname(__FILE__).'/../data/ca.pem'));
-
-        $this->channel1 = new Grpc\Channel('localhost:50021',
-                                           ["credentials" => $creds1,
-                                             "grpc_target_persist_bound" => 3,
-                                             ]);
-        $this->channel2 = new Grpc\Channel('localhost:50021',
-                                           ["credentials" => $creds2]);
+    /**
+     * @dataProvider persistentChannelSameChannelCredentialsProvider
+     */
+    public function testPersistentChannelSameChannelCredentials(
+        $creds1,
+        $creds2,
+        $port
+    ) {
+        $this->channel1 = new Grpc\Channel(
+            'localhost:' . $port,
+            [
+                "credentials" => $creds1,
+                "grpc_target_persist_bound" => 3,
+            ]
+        );
+        $this->channel2 = new Grpc\Channel(
+            'localhost:' . $port,
+            ["credentials" => $creds2]
+        );
 
         // try to connect on channel1
         $state = $this->channel1->getConnectivityState(true);
@@ -360,17 +412,80 @@ class ChannelTest extends PHPUnit_Framework_TestCase
         $this->channel2->close();
     }
 
-    public function testPersistentChannelDifferentSecureChannelCredentials()
-    {
-        $creds1 = Grpc\ChannelCredentials::createSsl();
-        $creds2 = Grpc\ChannelCredentials::createInsecure();
+    public function persistentChannelDifferentChannelCredentialsProvider(): array
+    {
+        return [
+            [
+                Grpc\ChannelCredentials::createSsl(),
+                Grpc\ChannelCredentials::createSsl(
+                    file_get_contents(dirname(__FILE__) . '/../data/ca.pem')
+                ),
+                50351,
+            ],
+            [
+                Grpc\ChannelCredentials::createSsl(),
+                Grpc\ChannelCredentials::createInsecure(),
+                50352,
+            ],
+            [
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl()
+                ),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl(
+                        file_get_contents(dirname(__FILE__) . '/../data/ca.pem')
+                    )
+                ),
+                50353,
+            ],
+            /*
+            [
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl()
+                ),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createInsecure()
+                ),
+                50354,
+            ],
+            [
+                \Grpc\ChannelCredentials::createInsecure(),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createInsecure()
+                ),
+                50355,
+            ],
+            */
+            [
+                \Grpc\ChannelCredentials::createSsl(),
+                \Grpc\ChannelCredentials::createXds(
+                    \Grpc\ChannelCredentials::createSsl()
+                ),
+                50356,
+            ],
+        ];
+    }
 
-        $this->channel1 = new Grpc\Channel('localhost:50022',
-                                           ["credentials" => $creds1,
-                                             "grpc_target_persist_bound" => 3,
-                                             ]);
-        $this->channel2 = new Grpc\Channel('localhost:50022',
-                                           ["credentials" => $creds2]);
+    /**
+     * @dataProvider persistentChannelDifferentChannelCredentialsProvider
+     */
+    public function testPersistentChannelDifferentChannelCredentials(
+        $creds1,
+        $creds2,
+        $port
+    ) {
+
+        $this->channel1 = new Grpc\Channel(
+            'localhost:' . $port,
+            [
+                "credentials" => $creds1,
+                "grpc_target_persist_bound" => 3,
+            ]
+        );
+        $this->channel2 = new Grpc\Channel(
+            'localhost:' . $port,
+            ["credentials" => $creds2]
+        );
 
         // try to connect on channel1
         $state = $this->channel1->getConnectivityState(true);
@@ -403,11 +518,9 @@ class ChannelTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
     }
 
-    /**
-     * @expectedException RuntimeException
-     */
     public function testPersistentChannelSharedChannelClose2()
     {
+        $this->expectException(\RuntimeException::class);
         // same underlying channel
         $this->channel1 = new Grpc\Channel('localhost:50223', [
             "grpc_target_persist_bound" => 3,
@@ -639,12 +752,9 @@ class ChannelTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(GRPC\CHANNEL_IDLE, $state);
     }
 
-    /**
-     * @expectedException RuntimeException
-     */
     public function testPersistentChannelForceNewOldChannelClose2()
     {
-
+        $this->expectException(\RuntimeException::class);
         $this->channel1 = new Grpc\Channel('localhost:50230', [
             "grpc_target_persist_bound" => 2,
         ]);