From 5979f82e17d4f9adf80fa795afb668d57b33411f Mon Sep 17 00:00:00 2001 From: Andrew Titmuss Date: Fri, 5 Aug 2022 00:52:40 +1000 Subject: [PATCH] feat(system.networkInterface): add networkInterface faker (#1133) Co-authored-by: Shinigami --- src/modules/system/index.ts | 69 +++++++++++++ test/__snapshots__/system.spec.ts.snap | 132 +++++++++++++++++++++++++ test/system.spec.ts | 102 +++++++++++++++++++ 3 files changed, 303 insertions(+) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 77be5a8267c..46a12b19156 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -14,6 +14,14 @@ const commonMimeTypes = [ 'text/html', ]; +const commonInterfaceTypes = ['en', 'wl', 'ww'] as const; +const commonInterfaceSchemas = { + index: 'o', + slot: 's', + mac: 'x', + pci: 'p', +} as const; + /** * Generates fake data for many computer systems properties. */ @@ -195,4 +203,65 @@ export class System { this.faker.datatype.number(9), ].join('.'); } + + /** + * Returns a random [network interface](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-understanding_the_predictable_network_interface_device_names). + * + * @param options The options to use. Defaults to `{}`. + * @param options.interfaceType The interface type. Can be one of `en`, `wl`, `ww`. + * @param options.interfaceSchema The interface schema. Can be one of `index`, `slot`, `mac`, `pci`. + * + * @example + * faker.system.networkInterface() // 'enp0s3' + * faker.system.networkInterface({ interfaceType: 'wl' }) // 'wlo1' + * faker.system.networkInterface({ interfaceSchema: 'mac' }) // 'enx000c29c00000' + * faker.system.networkInterface({ interfaceType: 'en', interfaceSchema: 'pci' }) // 'enp5s0f1d0' + */ + networkInterface( + options: { + interfaceType?: typeof commonInterfaceTypes[number]; + interfaceSchema?: keyof typeof commonInterfaceSchemas; + } = {} + ): string { + const { + interfaceType = this.faker.helpers.arrayElement(commonInterfaceTypes), + interfaceSchema = this.faker.helpers.objectKey(commonInterfaceSchemas), + } = options; + + let suffix: string; + let prefix = ''; + switch (interfaceSchema) { + case 'index': + suffix = this.faker.datatype.number(9).toString(); + break; + case 'slot': + suffix = `${this.faker.datatype.number(9)}${ + this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ?? + '' + }${ + this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ?? + '' + }`; + break; + case 'mac': + suffix = this.faker.internet.mac(''); + break; + case 'pci': + prefix = + this.faker.helpers.maybe(() => `P${this.faker.datatype.number(9)}`) ?? + ''; + suffix = `${this.faker.datatype.number(9)}s${this.faker.datatype.number( + 9 + )}${ + this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ?? + '' + }${ + this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ?? + '' + }`; + break; + } + + return `${prefix}${interfaceType}${commonInterfaceSchemas[interfaceSchema]}${suffix}`; + } } diff --git a/test/__snapshots__/system.spec.ts.snap b/test/__snapshots__/system.spec.ts.snap index 9d9a69c1bf4..f3eda41ad97 100644 --- a/test/__snapshots__/system.spec.ts.snap +++ b/test/__snapshots__/system.spec.ts.snap @@ -24,6 +24,48 @@ exports[`system > 42 > fileType 1`] = `"image"`; exports[`system > 42 > mimeType 1`] = `"application/vnd.ibm.rights-management"`; +exports[`system > 42 > networkInterface > noArgs 1`] = `"wlp1s7"`; + +exports[`system > 42 > networkInterface > with {"interfaceSchema":"index"} 1`] = `"wlo7"`; + +exports[`system > 42 > networkInterface > with {"interfaceSchema":"mac"} 1`] = `"wlxcf2bc9927210"`; + +exports[`system > 42 > networkInterface > with {"interfaceSchema":"pci"} 1`] = `"wlp9s1"`; + +exports[`system > 42 > networkInterface > with {"interfaceSchema":"slot"} 1`] = `"wls7d7"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"index"} 1`] = `"eno3"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"mac"} 1`] = `"enx5cf2bc992721"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"pci"} 1`] = `"P7enp9s1"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"slot"} 1`] = `"ens3"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"en"} 1`] = `"ens7d7"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"index"} 1`] = `"wlo3"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"mac"} 1`] = `"wlx5cf2bc992721"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"pci"} 1`] = `"P7wlp9s1"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"slot"} 1`] = `"wls3"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"wl"} 1`] = `"wls7d7"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"index"} 1`] = `"wwo3"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"mac"} 1`] = `"wwx5cf2bc992721"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"pci"} 1`] = `"P7wwp9s1"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"slot"} 1`] = `"wws3"`; + +exports[`system > 42 > networkInterface > with {"interfaceType":"ww"} 1`] = `"wws7d7"`; + +exports[`system > 42 > networkInterface > with {} 1`] = `"wlp1s7"`; + exports[`system > 42 > semver 1`] = `"3.7.9"`; exports[`system > 1211 > commonFileExt 1`] = `"htm"`; @@ -50,6 +92,48 @@ exports[`system > 1211 > fileType 1`] = `"x-shader"`; exports[`system > 1211 > mimeType 1`] = `"text/vnd.dmclientscript"`; +exports[`system > 1211 > networkInterface > noArgs 1`] = `"wws8d1"`; + +exports[`system > 1211 > networkInterface > with {"interfaceSchema":"index"} 1`] = `"wwo4"`; + +exports[`system > 1211 > networkInterface > with {"interfaceSchema":"mac"} 1`] = `"wwx7ec32f0a2a3c"`; + +exports[`system > 1211 > networkInterface > with {"interfaceSchema":"pci"} 1`] = `"P8wwp7s2f9d6"`; + +exports[`system > 1211 > networkInterface > with {"interfaceSchema":"slot"} 1`] = `"wws4"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"index"} 1`] = `"eno9"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"mac"} 1`] = `"enxe7ec32f0a2a3"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"pci"} 1`] = `"enp4s8d1"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"slot"} 1`] = `"ens9f8"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"en"} 1`] = `"P8enp7s2f9d6"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"index"} 1`] = `"wlo9"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"mac"} 1`] = `"wlxe7ec32f0a2a3"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"pci"} 1`] = `"wlp4s8d1"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"slot"} 1`] = `"wls9f8"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"wl"} 1`] = `"P8wlp7s2f9d6"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"index"} 1`] = `"wwo9"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"mac"} 1`] = `"wwxe7ec32f0a2a3"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"pci"} 1`] = `"wwp4s8d1"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"slot"} 1`] = `"wws9f8"`; + +exports[`system > 1211 > networkInterface > with {"interfaceType":"ww"} 1`] = `"P8wwp7s2f9d6"`; + +exports[`system > 1211 > networkInterface > with {} 1`] = `"wws8d1"`; + exports[`system > 1211 > semver 1`] = `"9.4.8"`; exports[`system > 1337 > commonFileExt 1`] = `"wav"`; @@ -76,6 +160,48 @@ exports[`system > 1337 > fileType 1`] = `"font"`; exports[`system > 1337 > mimeType 1`] = `"application/vnd.chipnuts.karaoke-mmd"`; +exports[`system > 1337 > networkInterface > noArgs 1`] = `"enx234870538945"`; + +exports[`system > 1337 > networkInterface > with {"interfaceSchema":"index"} 1`] = `"eno5"`; + +exports[`system > 1337 > networkInterface > with {"interfaceSchema":"mac"} 1`] = `"enx823487053894"`; + +exports[`system > 1337 > networkInterface > with {"interfaceSchema":"pci"} 1`] = `"enp1s2f5d0"`; + +exports[`system > 1337 > networkInterface > with {"interfaceSchema":"slot"} 1`] = `"ens5f2d5"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"index"} 1`] = `"eno2"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"mac"} 1`] = `"enx482348705389"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"pci"} 1`] = `"P5enp1s2f5d0"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"slot"} 1`] = `"ens2d2"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"en"} 1`] = `"ens5f2d5"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"index"} 1`] = `"wlo2"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"mac"} 1`] = `"wlx482348705389"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"pci"} 1`] = `"P5wlp1s2f5d0"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"slot"} 1`] = `"wls2d2"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"wl"} 1`] = `"wls5f2d5"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"index"} 1`] = `"wwo2"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"mac"} 1`] = `"wwx482348705389"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"pci"} 1`] = `"P5wwp1s2f5d0"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"slot"} 1`] = `"wws2d2"`; + +exports[`system > 1337 > networkInterface > with {"interfaceType":"ww"} 1`] = `"wws5f2d5"`; + +exports[`system > 1337 > networkInterface > with {} 1`] = `"enx234870538945"`; + exports[`system > 1337 > semver 1`] = `"2.5.1"`; exports[`system > seed: 42 > commonFileExt() 1`] = `"png"`; @@ -96,6 +222,8 @@ exports[`system > seed: 42 > fileType() 1`] = `"image"`; exports[`system > seed: 42 > mimeType() 1`] = `"application/vnd.ibm.rights-management"`; +exports[`system > seed: 42 > networkInterface() 1`] = `"wlp1s7"`; + exports[`system > seed: 42 > semver() 1`] = `"3.7.9"`; exports[`system > seed: 1211 > commonFileExt() 1`] = `"htm"`; @@ -116,6 +244,8 @@ exports[`system > seed: 1211 > fileType() 1`] = `"x-shader"`; exports[`system > seed: 1211 > mimeType() 1`] = `"text/vnd.dmclientscript"`; +exports[`system > seed: 1211 > networkInterface() 1`] = `"wws8d1"`; + exports[`system > seed: 1211 > semver() 1`] = `"9.4.8"`; exports[`system > seed: 1337 > commonFileExt() 1`] = `"wav"`; @@ -136,4 +266,6 @@ exports[`system > seed: 1337 > fileType() 1`] = `"font"`; exports[`system > seed: 1337 > mimeType() 1`] = `"application/vnd.chipnuts.karaoke-mmd"`; +exports[`system > seed: 1337 > networkInterface() 1`] = `"enx234870538945"`; + exports[`system > seed: 1337 > semver() 1`] = `"2.5.1"`; diff --git a/test/system.spec.ts b/test/system.spec.ts index 8a44201d902..05e2c53daba 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -16,6 +16,7 @@ const functionNames = [ 'filePath', 'fileType', 'mimeType', + 'networkInterface', 'semver', ]; @@ -46,6 +47,24 @@ describe('system', () => { t.describe('fileExt', (t) => { t.it('noArgs').it('with mimeType', 'application/json'); }); + + t.describe('networkInterface', (t) => { + t.it('noArgs'); + for (const interfaceSchema of [ + undefined, + 'index', + 'slot', + 'mac', + 'pci', + ] as const) { + for (const interfaceType of [undefined, 'en', 'wl', 'ww'] as const) { + t.it(`with ${JSON.stringify({ interfaceType, interfaceSchema })}`, { + interfaceType, + interfaceSchema, + }); + } + } + }); }); for (const seed of seededRuns) { @@ -76,6 +95,7 @@ describe('system', () => { 'jpg', 'm1v', 'm2a', + 'm1v', 'm2v', 'm3a', 'mp2', @@ -283,6 +303,88 @@ describe('system', () => { ).toSatisfy(validator.isSemVer); }); }); + + describe('networkInterface()', () => { + it('should return network interface', () => { + const networkInterface = faker.system.networkInterface(); + + expect( + networkInterface, + `generated network interface should be valid network interface.` + ).toMatch( + /^(?:P\d)?(?:en|wl|ww)(?:o\d|s\d(?:f\d)?(?:d\d)?|x[a-f\d]{12}|p\ds\d(?:f\d)?(?:d\d)?)$/ + ); + }); + + it('should return a network interface with a given type', () => { + const networkInterface = faker.system.networkInterface({ + interfaceType: 'wl', + }); + + expect( + networkInterface, + `generated network interface should be valid network interface.` + ).toMatch( + /^(?:P\d)?wl(?:o\d|s\d(?:f\d)?(?:d\d)?|x[a-f\d]{12}|p\ds\d(?:f\d)?(?:d\d)?)$/ + ); + }); + + it('should return a network interface with an index schema', () => { + const networkInterface = faker.system.networkInterface({ + interfaceSchema: 'index', + }); + + expect( + networkInterface, + `generated network interface should be valid network interface.` + ).toMatch(/^(?:en|wl|ww)o\d$/); + }); + + it('should return a network interface with a slot schema', () => { + const networkInterface = faker.system.networkInterface({ + interfaceSchema: 'slot', + }); + + expect( + networkInterface, + `generated network interface should be valid network interface.` + ).toMatch(/^(?:en|wl|ww)s\d(?:f\d)?(?:d\d)?$/); + }); + + it('should return a network interface with a mac schema', () => { + const networkInterface = faker.system.networkInterface({ + interfaceSchema: 'mac', + }); + + expect( + networkInterface, + `generated network interface should be valid network interface.` + ).toMatch(/^(?:en|wl|ww)x[a-f\d]{12}$/); + }); + + it('should return a network interface with a pci schema', () => { + const networkInterface = faker.system.networkInterface({ + interfaceSchema: 'pci', + }); + + expect( + networkInterface, + `generated network interface should be valid network interface.` + ).toMatch(/^(?:P\d)?(?:en|wl|ww)p\ds\d(?:f\d)?(?:d\d)?$/); + }); + + it('should return a network interface with a given type and schema', () => { + const networkInterface = faker.system.networkInterface({ + interfaceType: 'en', + interfaceSchema: 'mac', + }); + + expect( + networkInterface, + `generated network interface should be valid network interface.` + ).toMatch(/^enx[a-f\d]{12}$/); + }); + }); } });