-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJavaRuntimeServiceTest.php
More file actions
235 lines (200 loc) · 7.97 KB
/
JavaRuntimeServiceTest.php
File metadata and controls
235 lines (200 loc) · 7.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Jeidison\JSignPDF\Tests;
use donatj\MockWebServer\MockWebServer;
use donatj\MockWebServer\Response;
use org\bovigo\vfs\vfsStream;
use InvalidArgumentException;
use Jeidison\JSignPDF\Runtime\JavaRuntimeService;
use Jeidison\JSignPDF\Sign\JSignParam;
use PharData;
use PHPUnit\Framework\TestCase;
class JavaRuntimeServiceTest extends TestCase
{
public string $testTmpDir = '';
protected function setUp(): void
{
$this->testTmpDir = sys_get_temp_dir() . '/jsignpdf_temp_dir_' . uniqid();
mkdir(directory: $this->testTmpDir, recursive: true);
}
public function testGetPathWhenJavaIsInstalled(): void
{
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$jsignParam->setIsUseJavaInstalled(true);
$path = $service->getPath($jsignParam);
$this->assertEquals('java', $path);
}
public function testGetPathWithCustomAndValidJavaPath(): void
{
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
vfsStream::setup('download');
mkdir('vfs://download/bin');
touch('vfs://download/bin/java');
chmod('vfs://download/bin/java', 0755);
$jsignParam->setJavaPath('vfs://download/bin/java');
$jsignParam->setJavaDownloadUrl('');
$path = $service->getPath($jsignParam);
$this->assertEquals('vfs://download/bin/java', $path);
}
public function testGetPathWithCustomJavaPath(): void
{
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$expectedPath = __FILE__;
$jsignParam->setJavaPath($expectedPath);
$jsignParam->setJavaDownloadUrl('');
$actual = $service->getPath($jsignParam);
$this->assertEquals($expectedPath, $actual);
}
public function testGetPathWithDownloadUrlAndNotRealDirectory(): void
{
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$root = vfsStream::setup('download');
$root->chmod(0770)
->chgrp(vfsStream::GROUP_USER_1)
->chown(vfsStream::OWNER_USER_1);
chgrp('vfs://download', 44);
$jsignParam->setJavaPath('vfs://download/not_real_directory');
$jsignParam->setJavaDownloadUrl('https://fake.url');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/not a real directory/');
$service->getPath($jsignParam);
}
public function testGetPathWithDownloadUrlWithInvalidUrl(): void
{
vfsStream::setup('download');
mkdir('vfs://download/bin');
touch('vfs://download/bin/java');
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$jsignParam->setJavaPath('vfs://download/bin/java');
$jsignParam->setJavaDownloadUrl('invalid_url');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/url.*invalid/');
$service->getPath($jsignParam);
}
public function testGetPathWithDownloadUrlWith4xxError(): void
{
vfsStream::setup('download');
mkdir('vfs://download/bin');
touch('vfs://download/bin/java');
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$jsignParam->setJavaPath('vfs://download/bin/java');
$jsignParam->setJavaDownloadUrl('https://404.domain');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/Failure to download/');
$service->getPath($jsignParam);
}
public function testGetPathWithDownloadUrlWithInvalidGzipedFile(): void
{
vfsStream::setup('download');
mkdir('vfs://download/bin');
touch('vfs://download/bin/java');
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$jsignParam->setJavaPath('vfs://download/bin/java');
$server = new MockWebServer();
$server->start();
$server->setResponseOfPath(
'/',
new Response(
'invalid body response',
)
);
$url = $server->getServerRoot();
$jsignParam->setJavaDownloadUrl($url);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/cannot be extracted/');
$service->getPath($jsignParam);
}
public function testGetPathWithDownloadUrlWithInvalidJavaPackage(): void
{
mkdir($this->testTmpDir . '/bin', 0755, true);
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$jsignParam->setJavaPath($this->testTmpDir . '/bin/java');
$tar = new PharData($this->testTmpDir . '/temp.tar.gz');
$tar->addFromString('file.txt', 'invalid file');
$server = new MockWebServer();
$server->start();
$server->setResponseOfPath(
'/',
new Response(
gzencode(file_get_contents($this->testTmpDir . '/temp.tar.gz')),
)
);
unlink($this->testTmpDir . '/temp.tar.gz');
$url = $server->getServerRoot();
$jsignParam->setJavaDownloadUrl($url);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/Invalid tar content/');
$service->getPath($jsignParam);
}
public function testGetPathWithDownloadUrlWithInvalidVersion(): void
{
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
// When the version is invalid, will try to download the package
$server = new MockWebServer();
$server->start();
$server->setResponseOfPath(
'/',
new Response('invalid response'),
);
$baseUrl = $server->getServerRoot();
$url = $baseUrl . '/OpenJDK21U-jre_x64_linux_hotspot_21.0.8_9.tar.gz';
$jsignParam->setJavaDownloadUrl($url);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/cannot be extracted/');
$service->getPath($jsignParam);
}
public function testGetPathWithDownloadUrlWithValidVersion(): void
{
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$tarGzFilename = 'OpenJDK21U-jre_x64_linux_hotspot_21.0.8_9.tar.gz';
$url = 'https://fake.url/' . $tarGzFilename;
$jsignParam->setJavaDownloadUrl($url);
// When have a file with an expected name, will consider that the
// downloaded java version is right
touch($this->testTmpDir . '/.java_version_' . $tarGzFilename);
$jsignParam->setJavaPath($this->testTmpDir . '/bin/java');
$javaPath = $service->getPath($jsignParam);
$this->assertEquals($jsignParam->getJavaPath(), $javaPath);
}
public function testGetPathWithoutJavaFallback(): void
{
mkdir($this->testTmpDir . '/bin', 0755, true);
$jsignParam = new JSignParam();
$service = new JavaRuntimeService();
$jsignParam->setJavaPath('');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessageMatches('/Java not found/');
$service->getPath($jsignParam);
}
protected function tearDown(): void
{
$dirs = glob(sys_get_temp_dir() . '/jsignpdf_temp_*', GLOB_ONLYDIR);
foreach ($dirs as $dir) {
if (is_dir($dir)) {
$this->removeDirectoryContents($dir);
rmdir($dir);
}
}
}
private function removeDirectoryContents($dir): void
{
$it = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
}
}