Skip to content

Commit b7e2800

Browse files
committed
PHPLIB-1194: Test log message for non-genuine hosts
1 parent 840f450 commit b7e2800

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed

tests/LogNonGenuineHostTest.php

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace MongoDB\Tests;
4+
5+
use MongoDB\Client;
6+
use MongoDB\Driver\Exception\InvalidArgumentException;
7+
use Psr\Log\AbstractLogger;
8+
use Psr\Log\LoggerInterface;
9+
10+
use function func_get_args;
11+
use function MongoDB\addLogger;
12+
use function MongoDB\removeLogger;
13+
14+
/** @see https://jira.mongodb.org/browse/DRIVERS-2583 */
15+
class LogNonGenuineHostTest extends TestCase
16+
{
17+
private LoggerInterface $logger;
18+
19+
public function setUp(): void
20+
{
21+
$this->logger = $this->createTestPsrLogger();
22+
23+
addLogger($this->logger);
24+
}
25+
26+
public function tearDown(): void
27+
{
28+
removeLogger($this->logger);
29+
}
30+
31+
/** @dataProvider provideCosmosUris */
32+
public function testCosmosUriLogsInfoMessage(string $uri): void
33+
{
34+
$this->createClientAndIgnoreSrvLookupError($uri);
35+
36+
$expectedLog = [
37+
'info',
38+
'You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb',
39+
['domain' => 'mongoc'],
40+
];
41+
42+
$this->assertContains($expectedLog, $this->logger->logs);
43+
}
44+
45+
public function provideCosmosUris(): array
46+
{
47+
return [
48+
['mongodb://a.mongo.cosmos.azure.com:19555/'],
49+
['mongodb://a.MONGO.COSMOS.AZURE.COM:19555/'],
50+
['mongodb+srv://a.mongo.cosmos.azure.com/'],
51+
['mongodb+srv://A.MONGO.COSMOS.AZURE.COM/'],
52+
// Mixing genuine and nongenuine hosts (unlikely in practice)
53+
['mongodb://a.example.com:27017,b.mongo.cosmos.azure.com:19555/'],
54+
];
55+
}
56+
57+
/** @dataProvider provideDocumentDbUris */
58+
public function testDocumentDbUriLogsInfoMessage(string $uri): void
59+
{
60+
$this->createClientAndIgnoreSrvLookupError($uri);
61+
62+
$expectedLog = [
63+
'info',
64+
'You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb',
65+
['domain' => 'mongoc'],
66+
];
67+
68+
$this->assertContains($expectedLog, $this->logger->logs);
69+
}
70+
71+
public function provideDocumentDbUris(): array
72+
{
73+
return [
74+
['mongodb://a.docdb.amazonaws.com:27017/'],
75+
['mongodb://a.docdb-elastic.amazonaws.com:27017/'],
76+
['mongodb://a.DOCDB.AMAZONAWS.COM:27017/'],
77+
['mongodb://a.DOCDB-ELASTIC.AMAZONAWS.COM:27017/'],
78+
['mongodb+srv://a.DOCDB.AMAZONAWS.COM/'],
79+
['mongodb+srv://a.DOCDB-ELASTIC.AMAZONAWS.COM/'],
80+
// Mixing genuine and nongenuine hosts (unlikely in practice)
81+
['mongodb://a.example.com:27017,b.docdb.amazonaws.com:27017/'],
82+
['mongodb://a.example.com:27017,b.docdb-elastic.amazonaws.com:27017/'],
83+
];
84+
}
85+
86+
/** @dataProvider provideGenuineUris */
87+
public function testGenuineUriDoesNotLog(string $uri): void
88+
{
89+
$this->createClientAndIgnoreSrvLookupError($uri);
90+
$this->assertEmpty($this->logger->logs);
91+
}
92+
93+
public function provideGenuineUris(): array
94+
{
95+
return [
96+
['mongodb://a.example.com:27017,b.example.com:27017/'],
97+
['mongodb://a.mongodb.net:27017'],
98+
['mongodb+srv://a.example.com/'],
99+
['mongodb+srv://a.mongodb.net/'],
100+
// Host names do not end with expected suffix
101+
['mongodb://a.mongo.cosmos.azure.com.tld:19555/'],
102+
['mongodb://a.docdb.amazonaws.com.tld:27017/'],
103+
['mongodb://a.docdb-elastic.amazonaws.com.tld:27017/'],
104+
// SRV host names do not end with expected suffix
105+
['mongodb+srv://a.mongo.cosmos.azure.com.tld/'],
106+
['mongodb+srv://a.docdb.amazonaws.com.tld/'],
107+
['mongodb+srv://a.docdb-elastic.amazonaws.com.tld/'],
108+
];
109+
}
110+
111+
private function createClientAndIgnoreSrvLookupError(string $uri): void
112+
{
113+
try {
114+
$client = new Client($uri);
115+
} catch (InvalidArgumentException $e) {
116+
$this->assertStringContainsString('Failed to look up SRV record', $e->getMessage());
117+
}
118+
}
119+
120+
private function createTestPsrLogger(): LoggerInterface
121+
{
122+
return new class extends AbstractLogger {
123+
public $logs = [];
124+
125+
/* Note: parameter type hints are omitted for compatibility with
126+
* psr/log 1.1.4 and PHP 7.4. */
127+
public function log($level, $message, array $context = []): void
128+
{
129+
/* Ignore debug-level log messages from PHPC (e.g. connection
130+
* string, Manager creation, handshake data). */
131+
if ($level === 'debug') {
132+
return;
133+
}
134+
135+
$this->logs[] = func_get_args();
136+
}
137+
};
138+
}
139+
}

0 commit comments

Comments
 (0)