key = new Key($this->public, $this->private); $this->keyManager = $this->createMock(Manager::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->userManager = $this->createMock(IUserManager::class); $this->signer = new Signer( $this->keyManager, $this->timeFactory, $this->userManager ); } public function testSign(): void { $user = $this->createMock(IUser::class); $user->method('getCloudId') ->willReturn('foo@example.com'); $this->timeFactory->method('getTime') ->willReturn(42); $this->keyManager->method('getKey') ->with($this->equalTo($user)) ->willReturn($this->key); $data = [ 'foo' => 'bar', 'abc' => 'def', 'xyz' => 123, ]; $expects = [ 'message' => [ 'data' => $data, 'type' => 'myType', 'signer' => 'foo@example.com', 'timestamp' => 42, ], 'signature' => 'E1fNdoWMX1QmSyKv+S3FtOgLe9niYGQFWOKGaMLxc2h7s3V++EIqJvw/NCLBfrUowzWkTzhkjfbHaf88Hz34WAn4sAwXYAO8cnboQs6SClKRzQ/nvbtLgd2wm9RQ8UTOM7wR6C7HpIn4qqJ4BTQ1bAwYAiJ2GoK+W8wC0o0Gpub2906j3JJ4cbc9lufd5ohWKCev8Ubem/EEKaRIZA7qHh+Q1MKXTaJQJlCjTMe5PyGy0fsmtVxsPls3/Fkd9sVeHEHSYHzOiF6ttlxWou4TdRbq3WSEVpt1DOOvkKI9w2+zBJ7IPH8CnVpXcdIzWDctUygZKzNMUQnweDOOziEdUw==' ]; $result = $this->signer->sign('myType', $data, $user); $this->assertEquals($expects, $result); } public function testVerifyValid(): void { $data = [ 'message' => [ 'data' => [ 'foo' => 'bar', 'abc' => 'def', 'xyz' => 123, ], 'type' => 'myType', 'signer' => 'foo@example.com', 'timestamp' => 42, ], 'signature' => 'E1fNdoWMX1QmSyKv+S3FtOgLe9niYGQFWOKGaMLxc2h7s3V++EIqJvw/NCLBfrUowzWkTzhkjfbHaf88Hz34WAn4sAwXYAO8cnboQs6SClKRzQ/nvbtLgd2wm9RQ8UTOM7wR6C7HpIn4qqJ4BTQ1bAwYAiJ2GoK+W8wC0o0Gpub2906j3JJ4cbc9lufd5ohWKCev8Ubem/EEKaRIZA7qHh+Q1MKXTaJQJlCjTMe5PyGy0fsmtVxsPls3/Fkd9sVeHEHSYHzOiF6ttlxWou4TdRbq3WSEVpt1DOOvkKI9w2+zBJ7IPH8CnVpXcdIzWDctUygZKzNMUQnweDOOziEdUw==' ]; $user = $this->createMock(IUser::class); $this->keyManager->method('getKey') ->with($this->equalTo($user)) ->willReturn($this->key); $this->userManager->method('get') ->with('foo') ->willReturn($user); $this->assertTrue($this->signer->verify($data)); } public function testVerifyInvalid(): void { $data = [ 'message' => [ 'data' => [ 'foo' => 'bar', 'abc' => 'def', 'xyz' => 123, ], 'type' => 'myType', 'signer' => 'foo@example.com', 'timestamp' => 42, ], 'signature' => 'Invalid sig' ]; $user = $this->createMock(IUser::class); $this->keyManager->method('getKey') ->with($this->equalTo($user)) ->willReturn($this->key); $this->userManager->method('get') ->with('foo') ->willReturn($user); $this->assertFalse($this->signer->verify($data)); } public function testVerifyInvalidData(): void { $data = [ ]; $this->assertFalse($this->signer->verify($data)); } }