Completed
Push — stable8.2 ( 3b3780...cb9d0b )
by Lukas
29s
created

UtilTest::testWrapStorage()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.8571
cc 2
eloc 17
nc 2
nop 2
1
<?php
2
3
namespace Test\Encryption;
4
5
use OC\Encryption\Util;
6
use Test\TestCase;
7
8
class UtilTest extends TestCase {
9
10
	/**
11
	 * block size will always be 8192 for a PHP stream
12
	 * @see https://bugs.php.net/bug.php?id=21641
13
	 * @var integer
14
	 */
15
	protected $headerSize = 8192;
16
17
	/** @var \PHPUnit_Framework_MockObject_MockObject */
18
	protected $view;
19
20
	/** @var \PHPUnit_Framework_MockObject_MockObject */
21
	protected $userManager;
22
23
	/** @var \PHPUnit_Framework_MockObject_MockObject */
24
	protected $groupManager;
25
26
	/** @var \PHPUnit_Framework_MockObject_MockObject */
27
	private $config;
28
29
	/** @var  \OC\Encryption\Util */
30
	private $util;
31
32 View Code Duplication
	public function setUp() {
33
		parent::setUp();
34
		$this->view = $this->getMockBuilder('OC\Files\View')
35
			->disableOriginalConstructor()
36
			->getMock();
37
38
		$this->userManager = $this->getMockBuilder('OC\User\Manager')
39
			->disableOriginalConstructor()
40
			->getMock();
41
42
		$this->groupManager = $this->getMockBuilder('OC\Group\Manager')
43
			->disableOriginalConstructor()
44
			->getMock();
45
46
		$this->config = $this->getMockBuilder('OCP\IConfig')
47
			->disableOriginalConstructor()
48
			->getMock();
49
50
		$this->util = new Util(
51
			$this->view,
52
			$this->userManager,
53
			$this->groupManager,
54
			$this->config
55
		);
56
57
	}
58
59
	/**
60
	 * @dataProvider providesHeadersForEncryptionModule
61
	 */
62
	public function testGetEncryptionModuleId($expected, $header) {
63
		$id = $this->util->getEncryptionModuleId($header);
64
		$this->assertEquals($expected, $id);
65
	}
66
67
	public function providesHeadersForEncryptionModule() {
68
		return [
69
			['', []],
70
			['', ['1']],
71
			[2, ['oc_encryption_module' => 2]],
72
		];
73
	}
74
75
	/**
76
	 * @dataProvider providesHeaders
77
	 */
78
	public function testCreateHeader($expected, $header, $moduleId) {
79
80
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
81
		$em->expects($this->any())->method('getId')->willReturn($moduleId);
82
83
		$result = $this->util->createHeader($header, $em);
84
		$this->assertEquals($expected, $result);
85
	}
86
87
	public function providesHeaders() {
88
		return [
89
			[str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
90
				, [], '0'],
91
			[str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
92
				, ['custom_header' => 'foo'], '0'],
93
		];
94
	}
95
96
	/**
97
	 * @expectedException \OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException
98
	 */
99
	public function testCreateHeaderFailed() {
100
101
		$header = array('header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo');
102
103
		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
104
		$em->expects($this->any())->method('getId')->willReturn('moduleId');
105
106
		$this->util->createHeader($header, $em);
107
	}
108
109
	/**
110
	 * @dataProvider providePathsForTestIsExcluded
111
	 */
112
	public function testIsExcluded($path, $keyStorageRoot, $expected) {
113
		$this->config->expects($this->once())
114
			->method('getAppValue')
115
			->with('core', 'encryption_key_storage_root', '')
116
			->willReturn($keyStorageRoot);
117
		$this->userManager
118
			->expects($this->any())
119
			->method('userExists')
120
			->will($this->returnCallback(array($this, 'isExcludedCallback')));
121
122
		$this->assertSame($expected,
123
			$this->util->isExcluded($path)
124
		);
125
	}
126
127
	public function providePathsForTestIsExcluded() {
128
		return array(
129
			array('/files_encryption', '', true),
130
			array('files_encryption/foo.txt', '', true),
131
			array('test/foo.txt', '', false),
132
			array('/user1/files_encryption/foo.txt', '', true),
133
			array('/user1/files/foo.txt', '', false),
134
			array('/keyStorage/user1/files/foo.txt', 'keyStorage', true),
135
			array('/keyStorage/files_encryption', '/keyStorage', true),
136
			array('keyStorage/user1/files_encryption', '/keyStorage/', true),
137
138
		);
139
	}
140
141
	public function isExcludedCallback() {
142
		$args = func_get_args();
143
		if ($args[0] === 'user1') {
144
			return true;
145
		}
146
147
		return false;
148
	}
149
150
	/**
151
	 * @dataProvider dataTestIsFile
152
	 */
153
	public function testIsFile($path, $expected) {
154
		$this->assertSame($expected,
155
			$this->util->isFile($path)
156
		);
157
	}
158
159 View Code Duplication
	public function dataTestIsFile() {
160
		return array(
161
			array('/user/files/test.txt', true),
162
			array('/user/files', true),
163
			array('/user/files_versions/test.txt', false),
164
			array('/user/foo/files/test.txt', false),
165
			array('/files/foo/files/test.txt', false),
166
			array('/user', false),
167
			array('/user/test.txt', false),
168
		);
169
	}
170
171
	/**
172
	 * @dataProvider dataTestStripPartialFileExtension
173
	 *
174
	 * @param string $path
175
	 * @param string $expected
176
	 */
177
	public function testStripPartialFileExtension($path, $expected) {
178
		$this->assertSame($expected,
179
			$this->util->stripPartialFileExtension($path));
180
	}
181
182 View Code Duplication
	public function dataTestStripPartialFileExtension() {
183
		return array(
184
			array('/foo/test.txt', '/foo/test.txt'),
185
			array('/foo/test.txt.part', '/foo/test.txt'),
186
			array('/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'),
187
			array('/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'),
188
		);
189
	}
190
191
}
192