@@ -150,6 +150,70 @@ public static function anonymizedIpData()
150150 ];
151151 }
152152
153+ /**
154+ * @dataProvider anonymizedIpDataWithBytes
155+ */
156+ public function testAnonymizeWithBytes ($ ip , $ expected , $ bytesToAnonymize )
157+ {
158+ $ this ->assertSame ($ expected , IpUtils::anonymize ($ ip , $ bytesToAnonymize ));
159+ }
160+
161+ public static function anonymizedIpDataWithBytes ()
162+ {
163+ return [
164+ ['192.168.1.1 ' , '192.168.0.0 ' , ['v4 ' => 2 ]],
165+ ['192.168.1.1 ' , '192.0.0.0 ' , ['v4 ' => 3 ]],
166+ ['192.168.1.1 ' , '0.0.0.0 ' , ['v4 ' => 4 ]],
167+ ['1.2.3.4 ' , '1.2.3.0 ' , ['v4 ' => 1 ]],
168+ ['1.2.3.4 ' , '1.2.3.4 ' , ['v4 ' => 0 ]],
169+ ['2a01:198:603:0:396e:4789:8e99:890f ' , '2a01:198:603:0:396e:4789:8e99:890f ' , ['v6 ' => 0 ]],
170+ ['2a01:198:603:0:396e:4789:8e99:890f ' , '2a01:198:603:0:396e:4789:: ' , ['v6 ' => 4 ]],
171+ ['2a01:198:603:10:396e:4789:8e99:890f ' , '2a01:198:603:10:396e:4700:: ' , ['v6 ' => 5 ]],
172+ ['2a01:198:603:10:396e:4789:8e99:890f ' , '2a00:: ' , ['v6 ' => 15 ]],
173+ ['2a01:198:603:10:396e:4789:8e99:890f ' , ':: ' , ['v6 ' => 16 ]],
174+ ['::1 ' , ':: ' , ['v6 ' => 1 ]],
175+ ['0:0:0:0:0:0:0:1 ' , ':: ' , ['v6 ' => 1 ]],
176+ ['1:0:0:0:0:0:0:1 ' , '1:: ' , ['v6 ' => 1 ]],
177+ ['0:0:603:50:396e:4789:8e99:0001 ' , '0:0:603:: ' , ['v6 ' => 10 ]],
178+ ['[0:0:603:50:396e:4789:8e99:0001] ' , '[::603:50:396e:4789:8e00:0] ' , ['v6 ' => 3 ]],
179+ ['[2a01:198::3] ' , '[2a01:198::] ' , ['v6 ' => 2 ]],
180+ ['::ffff:123.234.235.236 ' , '::ffff:123.234.235.0 ' , ['v4 ' => 1 ]], // IPv4-mapped IPv6 addresses
181+ ['::123.234.235.236 ' , '::123.234.0.0 ' , ['v4 ' => 2 ]], // 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 ' , ['v4 ' => -1 ]);
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 ' , ['v6 ' => -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 ' , ['v4 ' => 5 ]);
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 ' , ['v6 ' => 17 ]);
215+ }
216+
153217 /**
154218 * @dataProvider getIp4SubnetMaskZeroData
155219 */
0 commit comments