utiltest.php 3.35 KB
Newer Older
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
<?php

namespace Test\Encryption;

use OC\Encryption\Util;
use Test\TestCase;

class UtilTest extends TestCase {

	/**
	 * block size will always be 8192 for a PHP stream
	 * @see https://bugs.php.net/bug.php?id=21641
	 * @var integer
	 */
	protected $headerSize = 8192;

	/** @var \PHPUnit_Framework_MockObject_MockObject */
	protected $view;

	/** @var \PHPUnit_Framework_MockObject_MockObject */
	protected $userManager;

	public function setUp() {
		parent::setUp();
		$this->view = $this->getMockBuilder('OC\Files\View')
			->disableOriginalConstructor()
			->getMock();

		$this->userManager = $this->getMockBuilder('OC\User\Manager')
			->disableOriginalConstructor()
			->getMock();
	}

	/**
	 * @dataProvider providesHeadersForEncryptionModule
	 */
	public function testGetEncryptionModuleId($expected, $header) {
		$u = new Util($this->view, $this->userManager);
		$id = $u->getEncryptionModuleId($header);
		$this->assertEquals($expected, $id);
	}

	public function providesHeadersForEncryptionModule() {
		return [
			['', []],
			['', ['1']],
			[2, ['oc_encryption_module' => 2]],
		];
	}

	/**
	 * @dataProvider providesHeaders
	 */
	public function testReadHeader($header, $expected, $moduleId) {
		$expected['oc_encryption_module'] = $moduleId;
		$u = new Util($this->view, $this->userManager);
		$result = $u->readHeader($header);
		$this->assertSameSize($expected, $result);
		foreach ($expected as $key => $value) {
			$this->assertArrayHasKey($key, $result);
			$this->assertSame($value, $result[$key]);
		}
	}

	/**
	 * @dataProvider providesHeaders
	 */
	public function testCreateHeader($expected, $header, $moduleId) {

		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn($moduleId);

		$u = new Util($this->view, $this->userManager);
		$result = $u->createHeader($header, $em);
		$this->assertEquals($expected, $result);
	}

	public function providesHeaders() {
		return [
			[str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
				, [], '0'],
			[str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
				, ['custom_header' => 'foo'], '0'],
		];
	}

	/**
	 * @expectedException \OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException
	 */
	public function testCreateHeaderFailed() {

		$header = array('header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo');

		$em = $this->getMock('\OCP\Encryption\IEncryptionModule');
		$em->expects($this->any())->method('getId')->willReturn('moduleId');

		$u = new Util($this->view, $this->userManager);
		$u->createHeader($header, $em);
	}

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
	/**
	 * @dataProvider providePathsForTestIsExcluded
	 */
	public function testIsEcluded($path, $expected) {
		$this->userManager
			->expects($this->any())
			->method('userExists')
			->will($this->returnCallback(array($this, 'isExcludedCallback')));

		$u = new Util($this->view, $this->userManager);

		$this->assertSame($expected,
			$u->isExcluded($path)
		);
	}

	public function providePathsForTestIsExcluded() {
		return array(
			array('files_encryption/foo.txt', true),
			array('test/foo.txt', false),
			array('/user1/files_encryption/foo.txt', true),
			array('/user1/files/foo.txt', false),

		);
	}

	public function isExcludedCallback() {
		$args = func_get_args();
		if ($args[0] === 'user1') {
			return true;
		}

		return false;
	}

136
}