Merge pull request #14846 from eileenmcnaughton/ex_pos
[civicrm-core.git] / tests / phpunit / CRM / Utils / SignerTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
28 /**
29 * Class CRM_Utils_SignerTest
30 * @group headless
31 */
32 class CRM_Utils_SignerTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 }
37
38 public function testSignValidate() {
39 $cases = [];
40 $cases[] = [
41 'signParams' => [
42 'a' => 'eh',
43 'b' => 'bee',
44 'c' => NULL,
45 ],
46 'validateParams' => [
47 'a' => 'eh',
48 'b' => 'bee',
49 'c' => NULL,
50 ],
51 'isValid' => TRUE,
52 ];
53 $cases[] = [
54 'signParams' => [
55 'a' => 'eh',
56 'b' => 'bee',
57 'c' => NULL,
58 ],
59 'validateParams' => [
60 'a' => 'eh',
61 'b' => 'bee',
62 'c' => NULL,
63 'irrelevant' => 'totally-irrelevant',
64 ],
65 'isValid' => TRUE,
66 ];
67 $cases[] = [
68 'signParams' => [
69 'a' => 'eh',
70 'b' => 'bee',
71 'c' => NULL,
72 ],
73 'validateParams' => [
74 'a' => 'eh',
75 'b' => 'bee',
76 'c' => '',
77 ],
78 'isValid' => TRUE,
79 ];
80 $cases[] = [
81 'signParams' => [
82 'a' => 'eh',
83 'b' => 'bee',
84 'c' => NULL,
85 ],
86 'validateParams' => [
87 'a' => 'eh',
88 'b' => 'bee',
89 'c' => 0,
90 ],
91 'isValid' => FALSE,
92 ];
93 $cases[] = [
94 'signParams' => [
95 'a' => 'eh',
96 'b' => 'bee',
97 'c' => 0,
98 ],
99 'validateParams' => [
100 'a' => 'eh',
101 'b' => 'bee',
102 'c' => NULL,
103 ],
104 'isValid' => FALSE,
105 ];
106 $cases[] = [
107 'signParams' => [
108 'a' => 'eh',
109 'b' => 'bee',
110 'c' => NULL,
111 ],
112 'validateParams' => [
113 'a' => 'eh',
114 'b' => 'bay',
115 'c' => NULL,
116 ],
117 'isValid' => FALSE,
118 ];
119 $cases[] = [
120 'signParams' => [
121 'a' => 'eh',
122 'b' => 'bee',
123 'c' => NULL,
124 ],
125 'validateParams' => [
126 'a' => 'eh',
127 'b' => 'bee',
128 'c' => FALSE,
129 ],
130 'isValid' => FALSE,
131 ];
132 $cases[] = [
133 'signParams' => [
134 // int
135 'a' => 1,
136 'b' => 'bee',
137 ],
138 'validateParams' => [
139 // string
140 'a' => '1',
141 'b' => 'bee',
142 ],
143 'isValid' => TRUE,
144 ];
145
146 foreach ($cases as $caseId => $case) {
147 $signer = new CRM_Utils_Signer('secret', ['a', 'b', 'c']);
148 $signature = $signer->sign($case['signParams']);
149 // arbitrary
150 $this->assertTrue(!empty($signature) && is_string($signature));
151
152 // same as $signer but physically separate
153 $validator = new CRM_Utils_Signer('secret', ['a', 'b', 'c']);
154 $isValid = $validator->validate($signature, $case['validateParams']);
155
156 if ($isValid !== $case['isValid']) {
157 $this->fail("Case ${caseId}: Mismatch: " . var_export($case, TRUE));
158 }
159 $this->assertTrue(TRUE, 'Validation yielded expected result');
160 }
161 }
162
163 }