Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(system.networkInterface): add networkInterface faker #1133

Merged
merged 8 commits into from
Aug 4, 2022
69 changes: 69 additions & 0 deletions src/modules/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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}`;
}
}
6 changes: 6 additions & 0 deletions test/__snapshots__/system.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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"`;
Expand All @@ -116,6 +118,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"`;
Expand All @@ -136,4 +140,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"`;
84 changes: 84 additions & 0 deletions test/system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const functionNames = [
'filePath',
'fileType',
'mimeType',
'networkInterface',
'semver',
];

Expand Down Expand Up @@ -76,6 +77,7 @@ describe('system', () => {
'jpg',
'm1v',
'm2a',
'm1v',
'm2v',
'm3a',
'mp2',
Expand Down Expand Up @@ -283,6 +285,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}$/);
});
});
}
});

Expand Down