faker icon indicating copy to clipboard operation
faker copied to clipboard

feat(internet): Add RFC 5737 testing IPv4 support

Open thomas-phillips-nz opened this issue 1 year ago • 5 comments

Adds support for generating random testing-safe IPv4 addresses according to RFC 5737

Sometimes you need to generate an IP address but don't want the system being tested to try and resolve or send traffic to the IP

I guess this could be added as an option to the existing ipv4() function?

thomas-phillips-nz avatar Oct 10 '23 04:10 thomas-phillips-nz

Codecov Report

Merging #2460 (cc9c202) into next (2d591de) will increase coverage by 0.00%. The diff coverage is 100.00%.

Additional details and impacted files
@@           Coverage Diff           @@
##             next    #2460   +/-   ##
=======================================
  Coverage   99.58%   99.58%           
=======================================
  Files        2823     2823           
  Lines      255497   255518   +21     
  Branches     1102     1103    +1     
=======================================
+ Hits       254434   254458   +24     
+ Misses       1035     1032    -3     
  Partials       28       28           
Files Coverage Δ
src/modules/internet/index.ts 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

codecov[bot] avatar Oct 10 '23 07:10 codecov[bot]

[...] Sometimes you need to generate an IP address but don't want the system being tested to try and resolve or send traffic to the IP [...]

Please don't do that with ANY data generated by Faker (emails, phone numbers, etc). This should include IP addresses as well. See our README:

https://github.com/faker-js/faker/blob/350dfbf2c57f023a78567da60bf4792ebd1a41f7/README.md?plain=1#L46-L48

xDivisionByZerox avatar Oct 10 '23 07:10 xDivisionByZerox

Totally fine for this to be closed if you think it's unnecessary with that guidance in the readme

thomas-phillips-nz avatar Oct 10 '23 07:10 thomas-phillips-nz

Team Proposal

How about the following api?

ipv4(options: {ipRange: LiteralUnion<'TEST-NET-1' | 'TEST-NET-2' | 'TEST-NET-3'>}) {
  let { ipRange } = options;
  if (ipRange != null) {
    if (ipRange === 'TEST-NET-1')
      ipRange  = '192.0.2.0/24'
    ...

    const [ipBase, mask] = ipRange.split('/');
    const maskInt = +mask;
    const maskBit = 2 ** mask -1;
    const ipBaseInt = ipToInt32(ipBase) & ^maskBit;
    const ipSuffix = faker.number.int(maskBit);
    return toIpv4(ipBaseInt + ipSuffix)
  } else {
    // current impl
  }
}

ST-DDT avatar Oct 17 '23 17:10 ST-DDT

@thomas-phillips-nz What do you think of our proposal?

ST-DDT avatar Oct 30 '23 09:10 ST-DDT

Superseded by https://github.com/faker-js/faker/pull/2992

  • https://github.com/faker-js/faker/pull/2992

Thanks for suggesting this feature.

ST-DDT avatar Jul 05 '24 14:07 ST-DDT