You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.3 KiB
85 lines
2.3 KiB
const WebSocket = require('ws');
|
|
const readline = require('readline');
|
|
|
|
// 创建readline接口用于用户输入
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
// 测试WebSocket认证验证
|
|
async function testUserAuthValidation() {
|
|
console.log('===== 开始测试用户认证验证 =====');
|
|
console.log('此测试将验证不存在的用户ID是否无法通过认证');
|
|
|
|
// 不存在的用户ID(与日志中的相同)
|
|
const nonExistentUserId = 'user_1765760444819';
|
|
|
|
// 服务器WebSocket地址
|
|
const wsUrl = 'ws://localhost:3003';
|
|
|
|
return new Promise((resolve) => {
|
|
// 创建WebSocket连接
|
|
const ws = new WebSocket(wsUrl);
|
|
|
|
ws.on('open', () => {
|
|
console.log('WebSocket连接已建立');
|
|
|
|
// 准备认证消息
|
|
const authMessage = {
|
|
type: 'auth',
|
|
userId: nonExistentUserId,
|
|
userType: 'customer',
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
console.log(`发送认证请求: ${JSON.stringify(authMessage)}`);
|
|
ws.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('收到服务器响应:', JSON.stringify(message));
|
|
|
|
// 检查是否收到认证错误消息
|
|
if (message.type === 'auth_error' && message.message === '用户不存在') {
|
|
console.log('✅ 测试成功: 不存在的用户ID正确被拒绝认证');
|
|
resolve(true);
|
|
} else if (message.type === 'auth_success') {
|
|
console.log('❌ 测试失败: 不存在的用户ID错误地通过了认证');
|
|
resolve(false);
|
|
}
|
|
|
|
// 关闭连接
|
|
ws.close();
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('WebSocket错误:', error);
|
|
resolve(false);
|
|
});
|
|
|
|
ws.on('close', () => {
|
|
console.log('WebSocket连接已关闭');
|
|
});
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
console.error('❌ 测试超时: 未收到服务器响应');
|
|
ws.close();
|
|
resolve(false);
|
|
}, 10000);
|
|
});
|
|
}
|
|
|
|
// 运行测试
|
|
testUserAuthValidation()
|
|
.then((result) => {
|
|
console.log('===== 测试完成 =====');
|
|
console.log('最终结果:', result ? '通过' : '失败');
|
|
rl.close();
|
|
})
|
|
.catch((error) => {
|
|
console.error('测试执行错误:', error);
|
|
rl.close();
|
|
});
|
|
|