version fixes
[civicrm-core.git] / tests / phpunit / CRM / Utils / SignerTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27 require_once 'CiviTest/CiviUnitTestCase.php';
28
29 /**
30 * Class CRM_Utils_SignerTest
31 */
32 class CRM_Utils_SignerTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 public function testSignValidate() {
39 $cases = array();
40 $cases[] = array(
41 'signParams' => array(
42 'a' => 'eh',
43 'b' => 'bee',
44 'c' => NULL,
45 ),
46 'validateParams' => array(
47 'a' => 'eh',
48 'b' => 'bee',
49 'c' => NULL,
50 ),
51 'isValid' => TRUE,
52 );
53 $cases[] = array(
54 'signParams' => array(
55 'a' => 'eh',
56 'b' => 'bee',
57 'c' => NULL,
58 ),
59 'validateParams' => array(
60 'a' => 'eh',
61 'b' => 'bee',
62 'c' => NULL,
63 'irrelevant' => 'totally-irrelevant',
64 ),
65 'isValid' => TRUE,
66 );
67 $cases[] = array(
68 'signParams' => array(
69 'a' => 'eh',
70 'b' => 'bee',
71 'c' => NULL,
72 ),
73 'validateParams' => array(
74 'a' => 'eh',
75 'b' => 'bee',
76 'c' => '',
77 ),
78 'isValid' => TRUE,
79 );
80 $cases[] = array(
81 'signParams' => array(
82 'a' => 'eh',
83 'b' => 'bee',
84 'c' => NULL,
85 ),
86 'validateParams' => array(
87 'a' => 'eh',
88 'b' => 'bee',
89 'c' => 0,
90 ),
91 'isValid' => FALSE,
92 );
93 $cases[] = array(
94 'signParams' => array(
95 'a' => 'eh',
96 'b' => 'bee',
97 'c' => 0,
98 ),
99 'validateParams' => array(
100 'a' => 'eh',
101 'b' => 'bee',
102 'c' => NULL,
103 ),
104 'isValid' => FALSE,
105 );
106 $cases[] = array(
107 'signParams' => array(
108 'a' => 'eh',
109 'b' => 'bee',
110 'c' => NULL,
111 ),
112 'validateParams' => array(
113 'a' => 'eh',
114 'b' => 'bay',
115 'c' => NULL,
116 ),
117 'isValid' => FALSE,
118 );
119 $cases[] = array(
120 'signParams' => array(
121 'a' => 'eh',
122 'b' => 'bee',
123 'c' => NULL,
124 ),
125 'validateParams' => array(
126 'a' => 'eh',
127 'b' => 'bee',
128 'c' => FALSE,
129 ),
130 'isValid' => FALSE,
131 );
132 $cases[] = array(
133 'signParams' => array(
134 'a' => 1, // int
135 'b' => 'bee',
136 ),
137 'validateParams' => array(
138 'a' => '1', // string
139 'b' => 'bee',
140 ),
141 'isValid' => TRUE,
142 );
143
144 foreach ($cases as $caseId => $case) {
145 require_once 'CRM/Utils/Signer.php';
146 $signer = new CRM_Utils_Signer('secret', array('a', 'b', 'c'));
147 $signature = $signer->sign($case['signParams']);
148 $this->assertTrue(!empty($signature) && is_string($signature)); // arbitrary
149
150 $validator = new CRM_Utils_Signer('secret', array('a', 'b', 'c')); // same as $signer but physically separate
151 $isValid = $validator->validate($signature, $case['validateParams']);
152
153 if ($isValid !== $case['isValid']) {
154 $this->fail("Case ${caseId}: Mismatch: " . var_export($case, TRUE));
155 }
156 $this->assertTrue(TRUE, 'Validation yielded expected result');
157 }
158 }
159
160 }