forked from automation-stack/node-machine-id
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
135 lines (112 loc) · 4.47 KB
/
test.js
File metadata and controls
135 lines (112 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const { execFile } = require('child_process');
const { promisify } = require('util');
const execFileAsync = promisify(execFile);
// Test function for PowerShell approach
async function testPowerShellMachineGuid() {
console.log('Testing PowerShell machine GUID retrieval...');
console.log('Platform:', process.platform);
if (process.platform !== 'win32') {
console.log('❌ This test is only for Windows platforms');
return;
}
try {
// Test the PowerShell approach
console.log('\n🔍 Testing PowerShell approach...');
const { stdout, stderr } = await execFileAsync('powershell.exe', [
'-NoProfile',
'-Command',
'(Get-ItemProperty -Path \'HKLM:\\SOFTWARE\\Microsoft\\Cryptography\' -Name MachineGuid).MachineGuid'
], {
windowsHide: true,
timeout: 10000 // 10 second timeout
});
if (stderr) {
console.log('⚠️ PowerShell stderr:', stderr);
}
const machineGuid = stdout.trim();
console.log('✅ PowerShell result:', machineGuid);
// Validate GUID format
const guidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (guidPattern.test(machineGuid)) {
console.log('✅ GUID format is valid');
} else {
console.log('❌ GUID format is invalid');
}
// Compare with current REG.exe approach
console.log('\n🔍 Testing current REG.exe approach for comparison...');
const regCommand = process.arch === 'ia32' && process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432')
? '%windir%\\sysnative\\cmd.exe /c %windir%\\System32\\REG.exe'
: '%windir%\\System32\\REG.exe';
const { stdout: regStdout } = await execFileAsync('cmd.exe', [
'/c',
`${regCommand} QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid`
], {
windowsHide: true,
timeout: 10000
});
// Extract GUID from REG output
const regGuid = regStdout
.toString()
.split('REG_SZ')[1]
.replace(/\r+|\n+|\s+/ig, '')
.toLowerCase();
console.log('✅ REG.exe result:', regGuid);
// Compare results
if (machineGuid.toLowerCase() === regGuid.toLowerCase()) {
console.log('✅ Both methods return the same GUID - PowerShell approach works correctly!');
} else {
console.log('❌ Methods return different GUIDs:');
console.log(' PowerShell:', machineGuid.toLowerCase());
console.log(' REG.exe: ', regGuid.toLowerCase());
}
} catch (error) {
console.error('❌ Error during test:', error.message);
if (error.code) {
console.error(' Error code:', error.code);
}
if (error.signal) {
console.error(' Signal:', error.signal);
}
}
}
// Test function using callback style (like in your example)
function testPowerShellCallback() {
console.log('\n🔍 Testing PowerShell with callback style...');
return new Promise((resolve, reject) => {
execFile('powershell.exe', [
'-NoProfile',
'-Command',
'(Get-ItemProperty -Path \'HKLM:\\SOFTWARE\\Microsoft\\Cryptography\' -Name MachineGuid).MachineGuid'
], { windowsHide: true }, (err, stdout) => {
if (err) {
console.error('❌ Callback style error:', err.message);
reject(err);
} else {
const result = stdout.trim();
console.log('✅ Callback style result:', result);
resolve(result);
}
});
});
}
// Run the tests
async function runTests() {
console.log('='.repeat(60));
console.log('Windows Machine GUID PowerShell Test');
console.log('='.repeat(60));
try {
await testPowerShellMachineGuid();
await testPowerShellCallback();
console.log('\n' + '='.repeat(60));
console.log('Test completed successfully!');
console.log('='.repeat(60));
} catch (error) {
console.error('\n❌ Test failed:', error.message);
process.exit(1);
}
}
// Run if this file is executed directly
if (require.main === module) {
runTests();
}
module.exports = { testPowerShellMachineGuid, testPowerShellCallback };