merge in 5.25
[civicrm-core.git] / tests / phpunit / CRM / Utils / SignerTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Utils_SignerTest
14 * @group headless
15 */
16 class CRM_Utils_SignerTest extends CiviUnitTestCase {
17
18 public function setUp() {
19 parent::setUp();
20 }
21
22 public function testSignValidate() {
23 $cases = [];
24 $cases[] = [
25 'signParams' => [
26 'a' => 'eh',
27 'b' => 'bee',
28 'c' => NULL,
29 ],
30 'validateParams' => [
31 'a' => 'eh',
32 'b' => 'bee',
33 'c' => NULL,
34 ],
35 'isValid' => TRUE,
36 ];
37 $cases[] = [
38 'signParams' => [
39 'a' => 'eh',
40 'b' => 'bee',
41 'c' => NULL,
42 ],
43 'validateParams' => [
44 'a' => 'eh',
45 'b' => 'bee',
46 'c' => NULL,
47 'irrelevant' => 'totally-irrelevant',
48 ],
49 'isValid' => TRUE,
50 ];
51 $cases[] = [
52 'signParams' => [
53 'a' => 'eh',
54 'b' => 'bee',
55 'c' => NULL,
56 ],
57 'validateParams' => [
58 'a' => 'eh',
59 'b' => 'bee',
60 'c' => '',
61 ],
62 'isValid' => TRUE,
63 ];
64 $cases[] = [
65 'signParams' => [
66 'a' => 'eh',
67 'b' => 'bee',
68 'c' => NULL,
69 ],
70 'validateParams' => [
71 'a' => 'eh',
72 'b' => 'bee',
73 'c' => 0,
74 ],
75 'isValid' => FALSE,
76 ];
77 $cases[] = [
78 'signParams' => [
79 'a' => 'eh',
80 'b' => 'bee',
81 'c' => 0,
82 ],
83 'validateParams' => [
84 'a' => 'eh',
85 'b' => 'bee',
86 'c' => NULL,
87 ],
88 'isValid' => FALSE,
89 ];
90 $cases[] = [
91 'signParams' => [
92 'a' => 'eh',
93 'b' => 'bee',
94 'c' => NULL,
95 ],
96 'validateParams' => [
97 'a' => 'eh',
98 'b' => 'bay',
99 'c' => NULL,
100 ],
101 'isValid' => FALSE,
102 ];
103 $cases[] = [
104 'signParams' => [
105 'a' => 'eh',
106 'b' => 'bee',
107 'c' => NULL,
108 ],
109 'validateParams' => [
110 'a' => 'eh',
111 'b' => 'bee',
112 'c' => FALSE,
113 ],
114 'isValid' => FALSE,
115 ];
116 $cases[] = [
117 'signParams' => [
118 // int
119 'a' => 1,
120 'b' => 'bee',
121 ],
122 'validateParams' => [
123 // string
124 'a' => '1',
125 'b' => 'bee',
126 ],
127 'isValid' => TRUE,
128 ];
129
130 foreach ($cases as $caseId => $case) {
131 $signer = new CRM_Utils_Signer('secret', ['a', 'b', 'c']);
132 $signature = $signer->sign($case['signParams']);
133 // arbitrary
134 $this->assertTrue(!empty($signature) && is_string($signature));
135
136 // same as $signer but physically separate
137 $validator = new CRM_Utils_Signer('secret', ['a', 'b', 'c']);
138 $isValid = $validator->validate($signature, $case['validateParams']);
139
140 if ($isValid !== $case['isValid']) {
141 $this->fail("Case ${caseId}: Mismatch: " . var_export($case, TRUE));
142 }
143 $this->assertTrue(TRUE, 'Validation yielded expected result');
144 }
145 }
146
147 }