1<?php
2/**
3 * Test the secret class.
4 *
5 * PHP version 5
6 *
7 * @category   Horde
8 * @package    Secret
9 * @subpackage UnitTests
10 * @author     Michael Slusarz <slusarz@horde.org>
11 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
12 * @link       http://pear.horde.org/index.php?package=Secret
13 */
14
15/**
16 * Test the secret class.
17 *
18 * Copyright 2009-2016 Horde LLC (http://www.horde.org/)
19 *
20 * See the enclosed file COPYING for license information (LGPL). If you
21 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
22 *
23 * @category   Horde
24 * @package    Secret
25 * @subpackage UnitTests
26 * @author     Michael Slusarz <slusarz@horde.org>
27 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
28 * @link       http://pear.horde.org/index.php?package=Secret
29 */
30
31class Horde_Secret_Unit_SecretTest extends PHPUnit_Framework_TestCase
32{
33    public function test8BitKey()
34    {
35        $secret = new Horde_Secret();
36
37        $key = "\x88";
38        $plaintext = "\x01\x01\x01\x01\x01\x01\x01\x01";
39
40        $this->assertEquals($plaintext, $secret->read($key, $secret->write($key, $plaintext)));
41    }
42
43    public function test64BitKey()
44    {
45        $secret = new Horde_Secret();
46
47        $key = "\x00\x00\x00\x00\x00\x00\x00\x00";
48        $plaintext = "\x01\x01\x01\x01\x01\x01\x01\x01";
49
50        $this->assertEquals($plaintext, $secret->read($key, $secret->write($key, $plaintext)));
51    }
52
53    public function test128BitKey()
54    {
55        $secret = new Horde_Secret();
56
57        $key = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
58        $plaintext = "\x01\x01\x01\x01\x01\x01\x01\x01";
59
60        $this->assertEquals($plaintext, $secret->read($key, $secret->write($key, $plaintext)));
61    }
62
63    /**
64     * Bug #9121: Remove null padding on stored data
65     */
66    public function testNullPadding()
67    {
68        $secret = new Horde_Secret();
69
70        $key = "\x88";
71        $plaintext = "\x01\x01\x01\x01\x01\x01\x01\x01";
72
73        $this->assertEquals($plaintext, $secret->read($key, $secret->write($key, $plaintext)));
74    }
75
76    /**
77     * @expectedException Horde_Secret_Exception
78     */
79    public function testKeyException()
80    {
81        $secret = new Horde_Secret();
82        $secret->read(new Horde_Secret_Stub_Message(), "\x01");
83    }
84
85    public function testLongKeyException()
86    {
87        $secret = new Horde_Secret();
88        $this->assertEquals(
89            $secret->read('012345678901234567890123456789012345678901234567890123456', "\x01"),
90            $secret->read('012345678901234567890123456789012345678901234567890123456789', "\x01")
91        );
92    }
93
94    public function testShortKeyRead()
95    {
96        $secret = new Horde_Secret();
97        $this->assertEquals('', $secret->read('', "\x01"));
98    }
99
100    public function testShortKeyWrite()
101    {
102        $secret = new Horde_Secret();
103        $this->assertEquals('', $secret->write('', "\x01"));
104    }
105}
106