Skip to content

Commit 5979f82

Browse files
feat(system.networkInterface): add networkInterface faker (#1133)
Co-authored-by: Shinigami <chrissi92@hotmail.de>
1 parent 407466f commit 5979f82

3 files changed

Lines changed: 303 additions & 0 deletions

File tree

src/modules/system/index.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ const commonMimeTypes = [
1414
'text/html',
1515
];
1616

17+
const commonInterfaceTypes = ['en', 'wl', 'ww'] as const;
18+
const commonInterfaceSchemas = {
19+
index: 'o',
20+
slot: 's',
21+
mac: 'x',
22+
pci: 'p',
23+
} as const;
24+
1725
/**
1826
* Generates fake data for many computer systems properties.
1927
*/
@@ -195,4 +203,65 @@ export class System {
195203
this.faker.datatype.number(9),
196204
].join('.');
197205
}
206+
207+
/**
208+
* 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).
209+
*
210+
* @param options The options to use. Defaults to `{}`.
211+
* @param options.interfaceType The interface type. Can be one of `en`, `wl`, `ww`.
212+
* @param options.interfaceSchema The interface schema. Can be one of `index`, `slot`, `mac`, `pci`.
213+
*
214+
* @example
215+
* faker.system.networkInterface() // 'enp0s3'
216+
* faker.system.networkInterface({ interfaceType: 'wl' }) // 'wlo1'
217+
* faker.system.networkInterface({ interfaceSchema: 'mac' }) // 'enx000c29c00000'
218+
* faker.system.networkInterface({ interfaceType: 'en', interfaceSchema: 'pci' }) // 'enp5s0f1d0'
219+
*/
220+
networkInterface(
221+
options: {
222+
interfaceType?: typeof commonInterfaceTypes[number];
223+
interfaceSchema?: keyof typeof commonInterfaceSchemas;
224+
} = {}
225+
): string {
226+
const {
227+
interfaceType = this.faker.helpers.arrayElement(commonInterfaceTypes),
228+
interfaceSchema = this.faker.helpers.objectKey(commonInterfaceSchemas),
229+
} = options;
230+
231+
let suffix: string;
232+
let prefix = '';
233+
switch (interfaceSchema) {
234+
case 'index':
235+
suffix = this.faker.datatype.number(9).toString();
236+
break;
237+
case 'slot':
238+
suffix = `${this.faker.datatype.number(9)}${
239+
this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ??
240+
''
241+
}${
242+
this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ??
243+
''
244+
}`;
245+
break;
246+
case 'mac':
247+
suffix = this.faker.internet.mac('');
248+
break;
249+
case 'pci':
250+
prefix =
251+
this.faker.helpers.maybe(() => `P${this.faker.datatype.number(9)}`) ??
252+
'';
253+
suffix = `${this.faker.datatype.number(9)}s${this.faker.datatype.number(
254+
9
255+
)}${
256+
this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ??
257+
''
258+
}${
259+
this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ??
260+
''
261+
}`;
262+
break;
263+
}
264+
265+
return `${prefix}${interfaceType}${commonInterfaceSchemas[interfaceSchema]}${suffix}`;
266+
}
198267
}

test/__snapshots__/system.spec.ts.snap

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,48 @@ exports[`system > 42 > fileType 1`] = `"image"`;
2424

2525
exports[`system > 42 > mimeType 1`] = `"application/vnd.ibm.rights-management"`;
2626

27+
exports[`system > 42 > networkInterface > noArgs 1`] = `"wlp1s7"`;
28+
29+
exports[`system > 42 > networkInterface > with {"interfaceSchema":"index"} 1`] = `"wlo7"`;
30+
31+
exports[`system > 42 > networkInterface > with {"interfaceSchema":"mac"} 1`] = `"wlxcf2bc9927210"`;
32+
33+
exports[`system > 42 > networkInterface > with {"interfaceSchema":"pci"} 1`] = `"wlp9s1"`;
34+
35+
exports[`system > 42 > networkInterface > with {"interfaceSchema":"slot"} 1`] = `"wls7d7"`;
36+
37+
exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"index"} 1`] = `"eno3"`;
38+
39+
exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"mac"} 1`] = `"enx5cf2bc992721"`;
40+
41+
exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"pci"} 1`] = `"P7enp9s1"`;
42+
43+
exports[`system > 42 > networkInterface > with {"interfaceType":"en","interfaceSchema":"slot"} 1`] = `"ens3"`;
44+
45+
exports[`system > 42 > networkInterface > with {"interfaceType":"en"} 1`] = `"ens7d7"`;
46+
47+
exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"index"} 1`] = `"wlo3"`;
48+
49+
exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"mac"} 1`] = `"wlx5cf2bc992721"`;
50+
51+
exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"pci"} 1`] = `"P7wlp9s1"`;
52+
53+
exports[`system > 42 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"slot"} 1`] = `"wls3"`;
54+
55+
exports[`system > 42 > networkInterface > with {"interfaceType":"wl"} 1`] = `"wls7d7"`;
56+
57+
exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"index"} 1`] = `"wwo3"`;
58+
59+
exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"mac"} 1`] = `"wwx5cf2bc992721"`;
60+
61+
exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"pci"} 1`] = `"P7wwp9s1"`;
62+
63+
exports[`system > 42 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"slot"} 1`] = `"wws3"`;
64+
65+
exports[`system > 42 > networkInterface > with {"interfaceType":"ww"} 1`] = `"wws7d7"`;
66+
67+
exports[`system > 42 > networkInterface > with {} 1`] = `"wlp1s7"`;
68+
2769
exports[`system > 42 > semver 1`] = `"3.7.9"`;
2870

2971
exports[`system > 1211 > commonFileExt 1`] = `"htm"`;
@@ -50,6 +92,48 @@ exports[`system > 1211 > fileType 1`] = `"x-shader"`;
5092

5193
exports[`system > 1211 > mimeType 1`] = `"text/vnd.dmclientscript"`;
5294

95+
exports[`system > 1211 > networkInterface > noArgs 1`] = `"wws8d1"`;
96+
97+
exports[`system > 1211 > networkInterface > with {"interfaceSchema":"index"} 1`] = `"wwo4"`;
98+
99+
exports[`system > 1211 > networkInterface > with {"interfaceSchema":"mac"} 1`] = `"wwx7ec32f0a2a3c"`;
100+
101+
exports[`system > 1211 > networkInterface > with {"interfaceSchema":"pci"} 1`] = `"P8wwp7s2f9d6"`;
102+
103+
exports[`system > 1211 > networkInterface > with {"interfaceSchema":"slot"} 1`] = `"wws4"`;
104+
105+
exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"index"} 1`] = `"eno9"`;
106+
107+
exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"mac"} 1`] = `"enxe7ec32f0a2a3"`;
108+
109+
exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"pci"} 1`] = `"enp4s8d1"`;
110+
111+
exports[`system > 1211 > networkInterface > with {"interfaceType":"en","interfaceSchema":"slot"} 1`] = `"ens9f8"`;
112+
113+
exports[`system > 1211 > networkInterface > with {"interfaceType":"en"} 1`] = `"P8enp7s2f9d6"`;
114+
115+
exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"index"} 1`] = `"wlo9"`;
116+
117+
exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"mac"} 1`] = `"wlxe7ec32f0a2a3"`;
118+
119+
exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"pci"} 1`] = `"wlp4s8d1"`;
120+
121+
exports[`system > 1211 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"slot"} 1`] = `"wls9f8"`;
122+
123+
exports[`system > 1211 > networkInterface > with {"interfaceType":"wl"} 1`] = `"P8wlp7s2f9d6"`;
124+
125+
exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"index"} 1`] = `"wwo9"`;
126+
127+
exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"mac"} 1`] = `"wwxe7ec32f0a2a3"`;
128+
129+
exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"pci"} 1`] = `"wwp4s8d1"`;
130+
131+
exports[`system > 1211 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"slot"} 1`] = `"wws9f8"`;
132+
133+
exports[`system > 1211 > networkInterface > with {"interfaceType":"ww"} 1`] = `"P8wwp7s2f9d6"`;
134+
135+
exports[`system > 1211 > networkInterface > with {} 1`] = `"wws8d1"`;
136+
53137
exports[`system > 1211 > semver 1`] = `"9.4.8"`;
54138

55139
exports[`system > 1337 > commonFileExt 1`] = `"wav"`;
@@ -76,6 +160,48 @@ exports[`system > 1337 > fileType 1`] = `"font"`;
76160

77161
exports[`system > 1337 > mimeType 1`] = `"application/vnd.chipnuts.karaoke-mmd"`;
78162

163+
exports[`system > 1337 > networkInterface > noArgs 1`] = `"enx234870538945"`;
164+
165+
exports[`system > 1337 > networkInterface > with {"interfaceSchema":"index"} 1`] = `"eno5"`;
166+
167+
exports[`system > 1337 > networkInterface > with {"interfaceSchema":"mac"} 1`] = `"enx823487053894"`;
168+
169+
exports[`system > 1337 > networkInterface > with {"interfaceSchema":"pci"} 1`] = `"enp1s2f5d0"`;
170+
171+
exports[`system > 1337 > networkInterface > with {"interfaceSchema":"slot"} 1`] = `"ens5f2d5"`;
172+
173+
exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"index"} 1`] = `"eno2"`;
174+
175+
exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"mac"} 1`] = `"enx482348705389"`;
176+
177+
exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"pci"} 1`] = `"P5enp1s2f5d0"`;
178+
179+
exports[`system > 1337 > networkInterface > with {"interfaceType":"en","interfaceSchema":"slot"} 1`] = `"ens2d2"`;
180+
181+
exports[`system > 1337 > networkInterface > with {"interfaceType":"en"} 1`] = `"ens5f2d5"`;
182+
183+
exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"index"} 1`] = `"wlo2"`;
184+
185+
exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"mac"} 1`] = `"wlx482348705389"`;
186+
187+
exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"pci"} 1`] = `"P5wlp1s2f5d0"`;
188+
189+
exports[`system > 1337 > networkInterface > with {"interfaceType":"wl","interfaceSchema":"slot"} 1`] = `"wls2d2"`;
190+
191+
exports[`system > 1337 > networkInterface > with {"interfaceType":"wl"} 1`] = `"wls5f2d5"`;
192+
193+
exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"index"} 1`] = `"wwo2"`;
194+
195+
exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"mac"} 1`] = `"wwx482348705389"`;
196+
197+
exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"pci"} 1`] = `"P5wwp1s2f5d0"`;
198+
199+
exports[`system > 1337 > networkInterface > with {"interfaceType":"ww","interfaceSchema":"slot"} 1`] = `"wws2d2"`;
200+
201+
exports[`system > 1337 > networkInterface > with {"interfaceType":"ww"} 1`] = `"wws5f2d5"`;
202+
203+
exports[`system > 1337 > networkInterface > with {} 1`] = `"enx234870538945"`;
204+
79205
exports[`system > 1337 > semver 1`] = `"2.5.1"`;
80206

81207
exports[`system > seed: 42 > commonFileExt() 1`] = `"png"`;
@@ -96,6 +222,8 @@ exports[`system > seed: 42 > fileType() 1`] = `"image"`;
96222

97223
exports[`system > seed: 42 > mimeType() 1`] = `"application/vnd.ibm.rights-management"`;
98224

225+
exports[`system > seed: 42 > networkInterface() 1`] = `"wlp1s7"`;
226+
99227
exports[`system > seed: 42 > semver() 1`] = `"3.7.9"`;
100228

101229
exports[`system > seed: 1211 > commonFileExt() 1`] = `"htm"`;
@@ -116,6 +244,8 @@ exports[`system > seed: 1211 > fileType() 1`] = `"x-shader"`;
116244

117245
exports[`system > seed: 1211 > mimeType() 1`] = `"text/vnd.dmclientscript"`;
118246

247+
exports[`system > seed: 1211 > networkInterface() 1`] = `"wws8d1"`;
248+
119249
exports[`system > seed: 1211 > semver() 1`] = `"9.4.8"`;
120250

121251
exports[`system > seed: 1337 > commonFileExt() 1`] = `"wav"`;
@@ -136,4 +266,6 @@ exports[`system > seed: 1337 > fileType() 1`] = `"font"`;
136266

137267
exports[`system > seed: 1337 > mimeType() 1`] = `"application/vnd.chipnuts.karaoke-mmd"`;
138268

269+
exports[`system > seed: 1337 > networkInterface() 1`] = `"enx234870538945"`;
270+
139271
exports[`system > seed: 1337 > semver() 1`] = `"2.5.1"`;

test/system.spec.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const functionNames = [
1616
'filePath',
1717
'fileType',
1818
'mimeType',
19+
'networkInterface',
1920
'semver',
2021
];
2122

@@ -46,6 +47,24 @@ describe('system', () => {
4647
t.describe('fileExt', (t) => {
4748
t.it('noArgs').it('with mimeType', 'application/json');
4849
});
50+
51+
t.describe('networkInterface', (t) => {
52+
t.it('noArgs');
53+
for (const interfaceSchema of [
54+
undefined,
55+
'index',
56+
'slot',
57+
'mac',
58+
'pci',
59+
] as const) {
60+
for (const interfaceType of [undefined, 'en', 'wl', 'ww'] as const) {
61+
t.it(`with ${JSON.stringify({ interfaceType, interfaceSchema })}`, {
62+
interfaceType,
63+
interfaceSchema,
64+
});
65+
}
66+
}
67+
});
4968
});
5069

5170
for (const seed of seededRuns) {
@@ -76,6 +95,7 @@ describe('system', () => {
7695
'jpg',
7796
'm1v',
7897
'm2a',
98+
'm1v',
7999
'm2v',
80100
'm3a',
81101
'mp2',
@@ -283,6 +303,88 @@ describe('system', () => {
283303
).toSatisfy(validator.isSemVer);
284304
});
285305
});
306+
307+
describe('networkInterface()', () => {
308+
it('should return network interface', () => {
309+
const networkInterface = faker.system.networkInterface();
310+
311+
expect(
312+
networkInterface,
313+
`generated network interface should be valid network interface.`
314+
).toMatch(
315+
/^(?: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)?)$/
316+
);
317+
});
318+
319+
it('should return a network interface with a given type', () => {
320+
const networkInterface = faker.system.networkInterface({
321+
interfaceType: 'wl',
322+
});
323+
324+
expect(
325+
networkInterface,
326+
`generated network interface should be valid network interface.`
327+
).toMatch(
328+
/^(?:P\d)?wl(?:o\d|s\d(?:f\d)?(?:d\d)?|x[a-f\d]{12}|p\ds\d(?:f\d)?(?:d\d)?)$/
329+
);
330+
});
331+
332+
it('should return a network interface with an index schema', () => {
333+
const networkInterface = faker.system.networkInterface({
334+
interfaceSchema: 'index',
335+
});
336+
337+
expect(
338+
networkInterface,
339+
`generated network interface should be valid network interface.`
340+
).toMatch(/^(?:en|wl|ww)o\d$/);
341+
});
342+
343+
it('should return a network interface with a slot schema', () => {
344+
const networkInterface = faker.system.networkInterface({
345+
interfaceSchema: 'slot',
346+
});
347+
348+
expect(
349+
networkInterface,
350+
`generated network interface should be valid network interface.`
351+
).toMatch(/^(?:en|wl|ww)s\d(?:f\d)?(?:d\d)?$/);
352+
});
353+
354+
it('should return a network interface with a mac schema', () => {
355+
const networkInterface = faker.system.networkInterface({
356+
interfaceSchema: 'mac',
357+
});
358+
359+
expect(
360+
networkInterface,
361+
`generated network interface should be valid network interface.`
362+
).toMatch(/^(?:en|wl|ww)x[a-f\d]{12}$/);
363+
});
364+
365+
it('should return a network interface with a pci schema', () => {
366+
const networkInterface = faker.system.networkInterface({
367+
interfaceSchema: 'pci',
368+
});
369+
370+
expect(
371+
networkInterface,
372+
`generated network interface should be valid network interface.`
373+
).toMatch(/^(?:P\d)?(?:en|wl|ww)p\ds\d(?:f\d)?(?:d\d)?$/);
374+
});
375+
376+
it('should return a network interface with a given type and schema', () => {
377+
const networkInterface = faker.system.networkInterface({
378+
interfaceType: 'en',
379+
interfaceSchema: 'mac',
380+
});
381+
382+
expect(
383+
networkInterface,
384+
`generated network interface should be valid network interface.`
385+
).toMatch(/^enx[a-f\d]{12}$/);
386+
});
387+
});
286388
}
287389
});
288390

0 commit comments

Comments
 (0)