|
4 | 4 |
|
5 | 5 | use InvalidArgumentException; |
6 | 6 | use LogicException; |
| 7 | +use Opis\Closure\Security\SecurityException; |
7 | 8 | use RuntimeException; |
8 | 9 | use Vectorial1024\LaravelProcessAsync\AsyncTask; |
9 | 10 | use Vectorial1024\LaravelProcessAsync\AsyncTaskStatus; |
|
13 | 14 | use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutErrorTask; |
14 | 15 | use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutNoOpTask; |
15 | 16 | use Vectorial1024\LaravelProcessAsync\Tests\Tasks\TestTimeoutNormalTask; |
| 17 | +use function Opis\Closure\init; |
| 18 | +use function Opis\Closure\set_security_provider; |
16 | 19 |
|
17 | 20 | class AsyncTaskTest extends BaseTestCase |
18 | 21 | { |
19 | | - // directly running the runner |
| 22 | + public function setUp(): void |
| 23 | + { |
| 24 | + // we need to reset the security key in case some of our tests change the secret key |
| 25 | + AsyncTask::loadSecretKey(); |
| 26 | + } |
| 27 | + |
| 28 | + // directly running the runner |
20 | 29 |
|
21 | 30 | public function testCanRunClosure() |
22 | 31 | { |
@@ -74,6 +83,42 @@ public function testConfigNoTimeLimit() |
74 | 83 | $this->assertNull($task->getTimeLimit()); |
75 | 84 | } |
76 | 85 |
|
| 86 | + public function testCanVerifySender() |
| 87 | + { |
| 88 | + // test that, when given a serialized closure signed by us, we can correctly reproduce the closure |
| 89 | + $testSecretKey = "LaravelProcAsync"; |
| 90 | + $testClosure = fn () => "Hello there!"; |
| 91 | + // reset the security provider |
| 92 | + set_security_provider(null); |
| 93 | + init($testSecretKey); |
| 94 | + // serialize once |
| 95 | + $testTask = new AsyncTask($testClosure); |
| 96 | + $checkingString = $testTask->toBase64Serial(); |
| 97 | + // then unserialize it |
| 98 | + $reproducedTask = AsyncTask::fromBase64Serial($checkingString); |
| 99 | + $this->assertTrue($reproducedTask instanceof AsyncTask); |
| 100 | + } |
| 101 | + |
| 102 | + public function testCannotVerifySender() |
| 103 | + { |
| 104 | + // test that, when given a serialized closure signed by Mallory, we can correctly reject the closure |
| 105 | + $testSecretKey = "LaravelProcAsync"; |
| 106 | + $testMalloryKey = "HereComesDatMallory"; |
| 107 | + $testClosure = fn () => "Hello there!"; |
| 108 | + // reset the security provider |
| 109 | + // mallory appears! |
| 110 | + set_security_provider($testMalloryKey); |
| 111 | + // serialize once |
| 112 | + $testTask = new AsyncTask($testClosure); |
| 113 | + $checkingString = $testTask->toBase64Serial(); |
| 114 | + // then unserialize it |
| 115 | + set_security_provider($testSecretKey); |
| 116 | + $this->expectException(SecurityException::class); |
| 117 | + // should throw |
| 118 | + $reproducedTask = AsyncTask::fromBase64Serial($checkingString); |
| 119 | + unset($reproducedTask); |
| 120 | + } |
| 121 | + |
77 | 122 | // --------- |
78 | 123 |
|
79 | 124 | // integration test with the cli artisan via a mocked artisan file, which tests various features of this library |
|
0 commit comments