// syscall.js
const util = require('util');
const exec = util.promisify(require('child_process').exec);
export const ping = (event, target) => {
const ipv4Pattern = /((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/; // IPv4
const domainNamePattern = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/; // 域名
if ((!ipv4Pattern.test(target)) && (!domainNamePattern.test(target))) {
return Promise.reject(new Error(
target.toString() + ' is neither a valid IPv4 address nor a valid domain name.'
));
}
return exec('ping ' + target);
};
// syscall.test.js
import { ping } from "../../utils/syscall";
describe('Syscall', () => {
it.concurrent('ping valid ip success', async () => {
await ping(undefined, '127.0.0.1')
.then((stdout, stderr) => {
expect(stderr).toBeUndefined();
expect(stdout).toBeDefined();
})
.catch(() => {
expect(false).toBe(true);
});
});
});
网友评论