# frozen_string_literal: true # Redmine - project management software # Copyright (C) 2006- Jean-Philippe Lang # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module Redmine # Helper module to get information about the Redmine database module Database class << self # Returns true if the database is SQLite def sqlite? ActiveRecord::Base.connection.adapter_name =~ /sqlite/i end # Returns true if the database is PostgreSQL def postgresql? /postgresql/i.match?(ActiveRecord::Base.connection.adapter_name) end # Returns the PostgreSQL version or nil if another DBMS is used def postgresql_version postgresql? ? ActiveRecord::Base.connection.send(:postgresql_version) : nil end # Returns true if the database is a PostgreSQL >=9.0 database with the unaccent extension installed def postgresql_unaccent? if postgresql? return @postgresql_unaccent unless @postgresql_unaccent.nil? begin sql = "SELECT name FROM pg_available_extensions " \ "WHERE installed_version IS NOT NULL and name = 'unaccent'" @postgresql_unaccent = postgresql_version >= 90000 && ActiveRecord::Base.connection.select_value(sql).present? rescue false end else false end end # Returns true if the database is MySQL def mysql? /mysql/i.match?(ActiveRecord::Base.connection.adapter_name) end def mysql_version mysql? ? ActiveRecord::Base.connection.select_value("SELECT VERSION()") : nil end # Returns a SQL statement for case/accent (if possible) insensitive match def like(left, right, options={}) neg = (options[:match] == false ? 'NOT ' : '') if postgresql? if postgresql_unaccent? "unaccent(#{left}) #{neg}ILIKE unaccent(#{right})" else "#{left} #{neg}ILIKE #{right}" end elsif mysql? "#{left} #{neg}LIKE #{right}" else "#{left} #{neg}LIKE #{right} ESCAPE '\\'" end end # Returns a SQL statement to cast a timestamp column to a date given a time zone # Returns nil if not implemented for the current database def timestamp_to_date(column, time_zone) if postgresql? if time_zone identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier "(#{column}::timestamptz AT TIME ZONE '#{identifier}')::date" else "#{column}::date" end elsif mysql? if time_zone user_identifier = ActiveSupport::TimeZone.find_tzinfo(time_zone.name).identifier local_identifier = ActiveSupport::TimeZone.find_tzinfo(Time.zone.name).identifier "DATE(CONVERT_TZ(#{column},'#{local_identifier}', '#{user_identifier}'))" else "DATE(#{column})" end end end # Resets database information def reset @postgresql_unaccent = nil end end end end > Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Encryption/UtilTest.php
blob: 8d800cf6f3479f175e37421c507a9d393dd0745a (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

namespace Test\Encryption;

use OC\Encryption\Util;
use OC\Files\View;
use OCP\Encryption\IEncryptionModule;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserManager;
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
	 */
	protected int $headerSize = 8192;

	/** @var \PHPUnit\Framework\MockObject\MockObject */
	protected $view;

	/** @var \PHPUnit\Framework\MockObject\MockObject|IUserManager */
	protected $userManager;

	/** @var \PHPUnit\Framework\MockObject\MockObject|IGroupManager */
	protected $groupManager;

	/** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
	private $config;
	private Util $util;

	protected function setUp(): void {
		parent::setUp();
		$this->view = $this->getMockBuilder(View::class)
			->disableOriginalConstructor()
			->getMock();

		$this->userManager = $this->createMock(IUserManager::class);
		$this->groupManager = $this->createMock(IGroupManager::class);
		$this->config = $this->createMock(IConfig::class);

		$this->util = new Util(
			$this->view,
			$this->userManager,
			$this->groupManager,
			$this->config
		);
	}

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

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

	/**
	 * @dataProvider providesHeaders
	 */
	public function testCreateHeader($expected, $header, $moduleId) {
		$em = $this->createMock(IEncryptionModule::class);
		$em->expects($this->any())->method('getId')->willReturn($moduleId);

		$result = $this->util->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'],
		];
	}


	public function testCreateHeaderFailed() {
		$this->expectException(\OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException::class);


		$header = ['header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo'];

		$em = $this->createMock(IEncryptionModule::class);
		$em->expects($this->any())->method('getId')->willReturn('moduleId');

		$this->util->createHeader($header, $em);
	}

	/**
	 * @dataProvider providePathsForTestIsExcluded
	 */
	public function testIsExcluded($path, $keyStorageRoot, $expected) {
		$this->config->expects($this->once())
			->method('getAppValue')
			->with('core', 'encryption_key_storage_root', '')
			->willReturn($keyStorageRoot);
		$this->userManager
			->expects($this->any())
			->method('userExists')
			->willReturnCallback([$this, 'isExcludedCallback']);

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

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

		];
	}

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

		return false;
	}

	/**
	 * @dataProvider dataTestIsFile
	 */
	public function testIsFile($path, $expected) {
		$this->assertSame($expected,
			$this->util->isFile($path)
		);
	}

	public function dataTestIsFile() {
		return [
			['/user/files/test.txt', true],
			['/user/files', true],
			['/user/files_versions/test.txt', false],
			['/user/foo/files/test.txt', false],
			['/files/foo/files/test.txt', false],
			['/user', false],
			['/user/test.txt', false],
		];
	}

	/**
	 * @dataProvider dataTestStripPartialFileExtension
	 *
	 * @param string $path
	 * @param string $expected
	 */
	public function testStripPartialFileExtension($path, $expected) {
		$this->assertSame($expected,
			$this->util->stripPartialFileExtension($path));
	}

	public function dataTestStripPartialFileExtension() {
		return [
			['/foo/test.txt', '/foo/test.txt'],
			['/foo/test.txt.part', '/foo/test.txt'],
			['/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'],
			['/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'],
		];
	}
}