Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
294f14a7
Commit
294f14a7
authored
9 years ago
by
Clark Tomlinson
Browse files
Options
Downloads
Plain Diff
Merge pull request #15538 from owncloud/encryption-crypt-tests
add unit test for crypt.php
parents
e512a69a
d1fa3c6b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
apps/encryption/lib/crypto/crypt.php
+20
-29
20 additions, 29 deletions
apps/encryption/lib/crypto/crypt.php
apps/encryption/tests/lib/crypto/cryptTest.php
+263
-0
263 additions, 0 deletions
apps/encryption/tests/lib/crypto/cryptTest.php
with
283 additions
and
29 deletions
apps/encryption/lib/crypto/crypt.php
+
20
−
29
View file @
294f14a7
...
...
@@ -67,6 +67,8 @@ class Crypt {
}
/**
* create new private/public key-pair for user
*
* @return array|bool
*/
public
function
createKeyPair
()
{
...
...
@@ -105,6 +107,8 @@ class Crypt {
}
/**
* Generates a new private key
*
* @return resource
*/
public
function
getOpenSSLPKey
()
{
...
...
@@ -113,13 +117,16 @@ class Crypt {
}
/**
* get openSSL Config
*
* @return array
*/
private
function
getOpenSSLConfig
()
{
$config
=
[
'private_key_bits'
=>
4096
];
$config
=
array_merge
(
\OC
::
$server
->
getConfig
()
->
getSystemValue
(
'openssl'
,
[]),
$config
);
$config
=
array_merge
(
$config
,
$this
->
config
->
getSystemValue
(
'openssl'
,
[])
);
return
$config
;
}
...
...
@@ -186,7 +193,10 @@ class Crypt {
}
/**
* @return mixed|string
* return Cipher either from config.php or the default cipher defined in
* this class
*
* @return string
*/
public
function
getCipher
()
{
$cipher
=
$this
->
config
->
getSystemValue
(
'cipher'
,
self
::
DEFAULT_CIPHER
);
...
...
@@ -276,6 +286,8 @@ class Crypt {
}
/**
* remove padding
*
* @param $padded
* @return bool|string
*/
...
...
@@ -287,6 +299,8 @@ class Crypt {
}
/**
* split iv from encrypted content
*
* @param $catFile
* @return array
*/
...
...
@@ -356,6 +370,8 @@ class Crypt {
}
/**
* generate initialization vector
*
* @return string
* @throws GenericEncryptionException
*/
...
...
@@ -396,31 +412,6 @@ class Crypt {
return
$key
;
}
/**
* Check if a file's contents contains an IV and is symmetrically encrypted
*
* @param $content
* @return bool
*/
public
function
isCatFileContent
(
$content
)
{
if
(
!
$content
)
{
return
false
;
}
$noPadding
=
$this
->
removePadding
(
$content
);
// Fetch encryption metadata from end of file
$meta
=
substr
(
$noPadding
,
-
22
);
// Fetch identifier from start of metadata
$identifier
=
substr
(
$meta
,
0
,
6
);
if
(
$identifier
===
'00iv00'
)
{
return
true
;
}
return
false
;
}
/**
* @param $encKeyFile
* @param $shareKey
...
...
This diff is collapsed.
Click to expand it.
apps/encryption/tests/lib/crypto/cryptTest.php
0 → 100644
+
263
−
0
View file @
294f14a7
<?php
/**
* @author Björn Schießle <schiessle@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace
OCA\Encryption\Tests\Crypt
;
use
OCA\Encryption\Crypto\Crypt
;
use
Test\TestCase
;
class
cryptTest
extends
TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject */
private
$logger
;
/** @var \PHPUnit_Framework_MockObject_MockObject */
private
$userSession
;
/** @var \PHPUnit_Framework_MockObject_MockObject */
private
$config
;
/** @var Crypt */
private
$crypt
;
public
function
setUp
()
{
parent
::
setUp
();
$this
->
logger
=
$this
->
getMockBuilder
(
'OCP\ILogger'
)
->
disableOriginalConstructor
()
->
getMock
();
$this
->
logger
->
expects
(
$this
->
any
())
->
method
(
'warning'
)
->
willReturn
(
true
);
$this
->
userSession
=
$this
->
getMockBuilder
(
'OCP\IUserSession'
)
->
disableOriginalConstructor
()
->
getMock
();
$this
->
config
=
$this
->
getMockBuilder
(
'OCP\IConfig'
)
->
disableOriginalConstructor
()
->
getMock
();
$this
->
crypt
=
new
Crypt
(
$this
->
logger
,
$this
->
userSession
,
$this
->
config
);
}
/**
* test getOpenSSLConfig without any additional parameters
*/
public
function
testGetOpenSSLConfigBasic
()
{
$this
->
config
->
expects
(
$this
->
once
())
->
method
(
'getSystemValue'
)
->
with
(
$this
->
equalTo
(
'openssl'
),
$this
->
equalTo
([]))
->
willReturn
(
array
());
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'getOpenSSLConfig'
);
$this
->
assertSame
(
1
,
count
(
$result
));
$this
->
assertArrayHasKey
(
'private_key_bits'
,
$result
);
$this
->
assertSame
(
4096
,
$result
[
'private_key_bits'
]);
}
/**
* test getOpenSSLConfig with additional parameters defined in config.php
*/
public
function
testGetOpenSSLConfig
()
{
$this
->
config
->
expects
(
$this
->
once
())
->
method
(
'getSystemValue'
)
->
with
(
$this
->
equalTo
(
'openssl'
),
$this
->
equalTo
([]))
->
willReturn
(
array
(
'foo'
=>
'bar'
,
'private_key_bits'
=>
1028
));
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'getOpenSSLConfig'
);
$this
->
assertSame
(
2
,
count
(
$result
));
$this
->
assertArrayHasKey
(
'private_key_bits'
,
$result
);
$this
->
assertArrayHasKey
(
'foo'
,
$result
);
$this
->
assertSame
(
1028
,
$result
[
'private_key_bits'
]);
$this
->
assertSame
(
'bar'
,
$result
[
'foo'
]);
}
/**
* test generateHeader
*/
public
function
testGenerateHeader
()
{
$this
->
config
->
expects
(
$this
->
once
())
->
method
(
'getSystemValue'
)
->
with
(
$this
->
equalTo
(
'cipher'
),
$this
->
equalTo
(
'AES-256-CFB'
))
->
willReturn
(
'AES-128-CFB'
);
$this
->
assertSame
(
'HBEGIN:cipher:AES-128-CFB:HEND'
,
$this
->
crypt
->
generateHeader
()
);
}
/**
* @dataProvider dataProviderGetCipher
* @param string $configValue
* @param string $expected
*/
public
function
testGetCipher
(
$configValue
,
$expected
)
{
$this
->
config
->
expects
(
$this
->
once
())
->
method
(
'getSystemValue'
)
->
with
(
$this
->
equalTo
(
'cipher'
),
$this
->
equalTo
(
'AES-256-CFB'
))
->
willReturn
(
$configValue
);
$this
->
assertSame
(
$expected
,
$this
->
crypt
->
getCipher
()
);
}
/**
* data provider for testGetCipher
*
* @return array
*/
public
function
dataProviderGetCipher
()
{
return
array
(
array
(
'AES-128-CFB'
,
'AES-128-CFB'
),
array
(
'AES-256-CFB'
,
'AES-256-CFB'
),
array
(
'unknown'
,
'AES-256-CFB'
)
);
}
/**
* test concatIV()
*/
public
function
testConcatIV
()
{
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'concatIV'
,
array
(
'content'
,
'my_iv'
));
$this
->
assertSame
(
'content00iv00my_iv'
,
$result
);
}
/**
* test splitIV()
*/
public
function
testSplitIV
()
{
$data
=
'encryptedContent00iv001234567890123456'
;
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'splitIV'
,
array
(
$data
));
$this
->
assertTrue
(
is_array
(
$result
));
$this
->
assertSame
(
2
,
count
(
$result
));
$this
->
assertArrayHasKey
(
'encrypted'
,
$result
);
$this
->
assertArrayHasKey
(
'iv'
,
$result
);
$this
->
assertSame
(
'encryptedContent'
,
$result
[
'encrypted'
]);
$this
->
assertSame
(
'1234567890123456'
,
$result
[
'iv'
]);
}
/**
* test addPadding()
*/
public
function
testAddPadding
()
{
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'addPadding'
,
array
(
'data'
));
$this
->
assertSame
(
'dataxx'
,
$result
);
}
/**
* test removePadding()
*
* @dataProvider dataProviderRemovePadding
* @param $data
* @param $expected
*/
public
function
testRemovePadding
(
$data
,
$expected
)
{
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'removePadding'
,
array
(
$data
));
$this
->
assertSame
(
$expected
,
$result
);
}
/**
* data provider for testRemovePadding
*
* @return array
*/
public
function
dataProviderRemovePadding
()
{
return
array
(
array
(
'dataxx'
,
'data'
),
array
(
'data'
,
false
)
);
}
/**
* test parseHeader()
*/
public
function
testParseHeader
()
{
$header
=
'HBEGIN:foo:bar:cipher:AES-256-CFB:HEND'
;
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'parseHeader'
,
array
(
$header
));
$this
->
assertTrue
(
is_array
(
$result
));
$this
->
assertSame
(
2
,
count
(
$result
));
$this
->
assertArrayHasKey
(
'foo'
,
$result
);
$this
->
assertArrayHasKey
(
'cipher'
,
$result
);
$this
->
assertSame
(
'bar'
,
$result
[
'foo'
]);
$this
->
assertSame
(
'AES-256-CFB'
,
$result
[
'cipher'
]);
}
/**
* test encrypt()
*
* @return string
*/
public
function
testEncrypt
()
{
$decrypted
=
'content'
;
$password
=
'password'
;
$iv
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'generateIv'
);
$this
->
assertTrue
(
is_string
(
$iv
));
$this
->
assertSame
(
16
,
strlen
(
$iv
));
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'encrypt'
,
array
(
$decrypted
,
$iv
,
$password
));
$this
->
assertTrue
(
is_string
(
$result
));
return
array
(
'password'
=>
$password
,
'iv'
=>
$iv
,
'encrypted'
=>
$result
,
'decrypted'
=>
$decrypted
);
}
/**
* test decrypt()
*
* @depends testEncrypt
*/
public
function
testDecrypt
(
$data
)
{
$result
=
\Test_Helper
::
invokePrivate
(
$this
->
crypt
,
'decrypt'
,
array
(
$data
[
'encrypted'
],
$data
[
'iv'
],
$data
[
'password'
]));
$this
->
assertSame
(
$data
[
'decrypted'
],
$result
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment