summaryrefslogtreecommitdiffstats
path: root/tests/lib/encryption/managertest.php
blob: ab297bae0cb015dd08730860541f1ca0e1c38ea8 (plain)
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
<?php

namespace Test\Encryption;

use OC\Encryption\Manager;
use Test\TestCase;

class ManagerTest extends TestCase {

	public function testManagerIsDisabled() {
		$config = $this->getMock('\OCP\IConfig');
		$m = new Manager($config);
		$this->assertFalse($m->isEnabled());
	}

	public function testManagerIsDisabledIfEnabledButNoModules() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getAppValue')->willReturn(true);
		$m = new Manager($config);
		$this->assertFalse($m->isEnabled());
	}

	public function testManagerIsDisabledIfDisabledButModules() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getAppValue')->willReturn(false);
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn(0);
		$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
		$m = new Manager($config);
		$m->registerEncryptionModule($em);
		$this->assertFalse($m->isEnabled());
	}

	public function testManagerIsEnabled() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getSystemValue')->willReturn(true);
		$config->expects($this->any())->method('getAppValue')->willReturn('yes');
		$m = new Manager($config);
		$this->assertTrue($m->isEnabled());
	}

	/**
	 * @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
	 * @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0"
	 */
	public function testModuleRegistration() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getAppValue')->willReturn('yes');
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn(0);
		$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
		$m = new Manager($config);
		$m->registerEncryptionModule($em);
		$this->assertSame(1, count($m->getEncryptionModules()));
		$m->registerEncryptionModule($em);
	}

	public function testModuleUnRegistration() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getAppValue')->willReturn(true);
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn(0);
		$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
		$m = new Manager($config);
		$m->registerEncryptionModule($em);
		$this->assertSame(1,
			count($m->getEncryptionModules())
		);
		$m->unregisterEncryptionModule($em);
		$this->assertEmpty($m->getEncryptionModules());
	}

	/**
	 * @expectedException \OC\Encryption\Exceptions\ModuleDoesNotExistsException
	 * @expectedExceptionMessage Module with id: unknown does not exists.
	 */
	public function testGetEncryptionModuleUnknown() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getAppValue')->willReturn(true);
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn(0);
		$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
		$m = new Manager($config);
		$m->registerEncryptionModule($em);
		$this->assertSame(1, count($m->getEncryptionModules()));
		$m->getEncryptionModule('unknown');
	}

	public function testGetEncryptionModule() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getAppValue')->willReturn(true);
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn(0);
		$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
		$m = new Manager($config);
		$m->registerEncryptionModule($em);
		$this->assertSame(1, count($m->getEncryptionModules()));
		$en0 = $m->getEncryptionModule(0);
		$this->assertEquals(0, $en0->getId());
	}

	public function testGetDefaultEncryptionModule() {
		$config = $this->getMock('\OCP\IConfig');
		$config->expects($this->any())->method('getAppValue')->willReturn(true);
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn(0);
		$em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
		$m = new Manager($config);
		$m->registerEncryptionModule($em);
		$this->assertSame(1, count($m->getEncryptionModules()));
		$en0 = $m->getEncryptionModule(0);
		$this->assertEquals(0, $en0->getId());
	}
}