@@ -150,6 +150,70 @@ public static function anonymizedIpData()
150
150
];
151
151
}
152
152
153
+ /**
154
+ * @dataProvider anonymizedIpDataWithBytes
155
+ */
156
+ public function testAnonymizeWithBytes ($ ip , $ expected , $ bytesForV4 , $ bytesForV6 )
157
+ {
158
+ $ this ->assertSame ($ expected , IpUtils::anonymize ($ ip , $ bytesForV4 , $ bytesForV6 ));
159
+ }
160
+
161
+ public static function anonymizedIpDataWithBytes (): array
162
+ {
163
+ return [
164
+ ['192.168.1.1 ' , '192.168.0.0 ' , 2 , 8 ],
165
+ ['192.168.1.1 ' , '192.0.0.0 ' , 3 , 8 ],
166
+ ['192.168.1.1 ' , '0.0.0.0 ' , 4 , 8 ],
167
+ ['1.2.3.4 ' , '1.2.3.0 ' , 1 , 8 ],
168
+ ['1.2.3.4 ' , '1.2.3.4 ' , 0 , 8 ],
169
+ ['2a01:198:603:0:396e:4789:8e99:890f ' , '2a01:198:603:0:396e:4789:8e99:890f ' , 1 , 0 ],
170
+ ['2a01:198:603:0:396e:4789:8e99:890f ' , '2a01:198:603:0:396e:4789:: ' , 1 , 4 ],
171
+ ['2a01:198:603:10:396e:4789:8e99:890f ' , '2a01:198:603:10:396e:4700:: ' , 1 , 5 ],
172
+ ['2a01:198:603:10:396e:4789:8e99:890f ' , '2a00:: ' , 1 , 15 ],
173
+ ['2a01:198:603:10:396e:4789:8e99:890f ' , ':: ' , 1 , 16 ],
174
+ ['::1 ' , ':: ' , 1 , 1 ],
175
+ ['0:0:0:0:0:0:0:1 ' , ':: ' , 1 , 1 ],
176
+ ['1:0:0:0:0:0:0:1 ' , '1:: ' , 1 , 1 ],
177
+ ['0:0:603:50:396e:4789:8e99:0001 ' , '0:0:603:: ' , 1 , 10 ],
178
+ ['[0:0:603:50:396e:4789:8e99:0001] ' , '[::603:50:396e:4789:8e00:0] ' , 1 , 3 ],
179
+ ['[2a01:198::3] ' , '[2a01:198::] ' , 1 , 2 ],
180
+ ['::ffff:123.234.235.236 ' , '::ffff:123.234.235.0 ' , 1 , 8 ], // IPv4-mapped IPv6 addresses
181
+ ['::123.234.235.236 ' , '::123.234.0.0 ' , 2 , 8 ], // deprecated IPv4-compatible IPv6 address
182
+ ];
183
+ }
184
+
185
+ public function testAnonymizeV4WithNegativeBytes ()
186
+ {
187
+ $ this ->expectException (\InvalidArgumentException::class);
188
+ $ this ->expectExceptionMessage ('Cannot anonymize less than 0 bytes. ' );
189
+
190
+ IpUtils::anonymize ('anything ' , -1 , 8 );
191
+ }
192
+
193
+ public function testAnonymizeV6WithNegativeBytes ()
194
+ {
195
+ $ this ->expectException (\InvalidArgumentException::class);
196
+ $ this ->expectExceptionMessage ('Cannot anonymize less than 0 bytes. ' );
197
+
198
+ IpUtils::anonymize ('anything ' , 1 , -1 );
199
+ }
200
+
201
+ public function testAnonymizeV4WithTooManyBytes ()
202
+ {
203
+ $ this ->expectException (\InvalidArgumentException::class);
204
+ $ this ->expectExceptionMessage ('Cannot anonymize more than 4 bytes for IPv4 and 16 bytes for IPv6. ' );
205
+
206
+ IpUtils::anonymize ('anything ' , 5 , 8 );
207
+ }
208
+
209
+ public function testAnonymizeV6WithTooManyBytes ()
210
+ {
211
+ $ this ->expectException (\InvalidArgumentException::class);
212
+ $ this ->expectExceptionMessage ('Cannot anonymize more than 4 bytes for IPv4 and 16 bytes for IPv6. ' );
213
+
214
+ IpUtils::anonymize ('anything ' , 1 , 17 );
215
+ }
216
+
153
217
/**
154
218
* @dataProvider getIp4SubnetMaskZeroData
155
219
*/
0 commit comments