Skip to content

Commit

Permalink
test: 补充 arpTable 单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Dec 8, 2023
1 parent 02fac9b commit 8728289
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
27 changes: 23 additions & 4 deletions src/__test__/arpTable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
* @Author: renxia
* @Date: 2023-12-08 10:11:08
* @LastEditors: renxia
* @LastEditTime: 2023-12-08 11:40:29
* @LastEditTime: 2023-12-08 11:59:50
* @Description:
*/
import { getArpTable } from '../arpTable';
// import { getAllMac, getAllPhysicsMac, getMac } from '../getMac';
// import { isValidMac } from '../utils';
import { getArpIpByMac, getArpMacByIp, getArpTable } from '../arpTable';
import { ifacesMock, arpANStdout } from './testData.mock';

let platform: keyof typeof arpANStdout = 'win32';
Expand Down Expand Up @@ -55,4 +53,25 @@ describe('getArpTable.ts', () => {
info = await getArpTable(arpANStdout.mac);
expect(info.table.some(d => d.mac === '<incomplete>')).toBeFalsy();
});

it('getArpMacByIp', async () => {
platform = 'win32';
let mac = await getArpMacByIp('10.10.1.1');
expect(mac).toBe('dc:ef:80:37:aa:ff');

mac = await getArpMacByIp('10.10.1.0');
expect(mac).toBe('');
});

it('getArpIpByMac', async () => {
platform = 'win32';
let ip = await getArpIpByMac('dc-ef-80-37-aa-ff');
expect(ip[0]).toBe('10.10.1.1');

ip = await getArpIpByMac('dc-ef-80-37');
expect(ip.length > 1).toBeTruthy();

ip = await getArpIpByMac('dc:ef:80:37');
expect(ip[0]).toBe('10.10.1.1');
});
});
7 changes: 3 additions & 4 deletions src/arpTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: renxia
* @Date: 2023-12-08 09:31:39
* @LastEditors: renxia
* @LastEditTime: 2023-12-08 11:50:52
* @LastEditTime: 2023-12-08 11:58:38
* @Description:
*/
import { execSync } from 'child_process';
Expand Down Expand Up @@ -80,10 +80,9 @@ export async function getArpMacByIp(ip: string) {

export async function getArpIpByMac(mac: string) {
mac = formatMac(mac);
if (!mac) return '';
if (!mac) return [];
const { table } = await getArpTable();
const item = table.find(d => d.mac.includes(mac));
return item ? item.ip : '';
return table.filter(d => d.mac.includes(mac)).map(d => d.ip);
}

// getArpTable().then(d => console.log(d.table));

0 comments on commit 8728289

Please sign in to comment.