using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using Xunit; using ZKTecoSyncService.Services; namespace ZKTecoSyncService.Tests; /// /// Production COM Runtime Tests /// Production Path: ZKTecoProtocol -> CZKEMClass -> Device 172.24.30.9 /// public class ProductionComTests { private readonly string _ip; private readonly int _port; private readonly int _commKey; private readonly int _machine; public ProductionComTests() { _ip = Environment.GetEnvironmentVariable("TestDevice:Ip") ?? "172.24.30.9"; _port = int.TryParse(Environment.GetEnvironmentVariable("TestDevice:Port"), out var p) ? p : 4370; _commKey = int.TryParse(Environment.GetEnvironmentVariable("TestDevice:CommKey"), out var k) ? k : 0; _machine = int.TryParse(Environment.GetEnvironmentVariable("TestDevice:MachineNumber"), out var m) ? m : 1; } [Fact] public void TestB_RealEventCallback() { Console.WriteLine("TESTB: Real OnAttTransactionEx - Production COM"); Console.WriteLine("Device: " + _ip + ":" + _port); Console.WriteLine("ACTION: Perform ONE physical punch within 60 seconds\n"); ZKTecoProtocol? protocol = null; var eventReceived = new ManualResetEventSlim(false); int callbackThreadId = 0; ApartmentState callbackApartment = ApartmentState.Unknown; string? personnel = null; try { protocol = new ZKTecoProtocol(_ip, _port, _commKey, _machine, 10000, 15000); int createThreadId = Thread.CurrentThread.ManagedThreadId; bool connected = protocol.Connect(); Console.WriteLine("Connection: " + (connected ? "OK" : "FAIL")); Console.WriteLine("COM created on thread: " + createThreadId); if (!connected) return; bool eventsEnabled = protocol.EnableRealTimeEvents(log => { callbackThreadId = Thread.CurrentThread.ManagedThreadId; callbackApartment = Thread.CurrentThread.GetApartmentState(); personnel = log.PersonnelCode; Console.WriteLine("[CALLBACK] Thread " + callbackThreadId + " (" + callbackApartment + ")"); Console.WriteLine(" Personnel: " + log.PersonnelCode); eventReceived.Set(); }); Console.WriteLine("RegEvent: " + (eventsEnabled ? "OK" : "FAIL")); Console.WriteLine("RegEvent thread: " + Thread.CurrentThread.ManagedThreadId); if (!eventsEnabled) return; Console.WriteLine("Waiting for punch (60s)..."); eventReceived.Wait(TimeSpan.FromSeconds(60)); Console.WriteLine("\n=== RESULTS ==="); Console.WriteLine("Create thread: " + createThreadId); Console.WriteLine("RegEvent thread: " + Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Callback thread: " + callbackThreadId); Console.WriteLine("Callback apartment: " + callbackApartment); Console.WriteLine("Event received: " + (eventReceived.IsSet ? "YES" : "NO")); Console.WriteLine("Personnel: " + (personnel ?? "N/A")); if (eventReceived.IsSet) Console.WriteLine("\nCLASSIFICATION: PROVEN - RUNTIME (event delivery works)"); } finally { protocol?.Dispose(); } } [Fact] public void TestH_ReaderHealth() { Console.WriteLine("TESTH: Reader Health - Production COM\n"); zkemkeeper.CZKEMClass? zkem = null; try { zkem = new zkemkeeper.CZKEMClass(); if (_commKey > 0) zkem.SetCommPassword(_commKey); bool connected = zkem.Connect_Net(_ip, _port); Console.WriteLine("[1] Connection: " + (connected ? "GREEN" : "RED")); if (!connected) return; string serial = ""; bool snResult = zkem.GetSerialNumber(_machine, out serial); Console.WriteLine("[2] SDK Probe: " + (snResult ? "GREEN" : "RED") + " (serial: " + serial + ")"); var sw = Stopwatch.StartNew(); bool readResult = zkem.ReadGeneralLogData(_machine); sw.Stop(); Console.WriteLine("[3] Reader Probe: " + (readResult ? "GREEN" : "RED") + " (" + sw.ElapsedMilliseconds + "ms)"); int count = 0; if (readResult) { string enroll = ""; int vm = 0, io = 0, y = 0, m = 0, d = 0, h = 0, mi = 0, s = 0, wc = 0; while (zkem.SSR_GetGeneralLogData(_machine, out enroll, out vm, out io, out y, out m, out d, out h, out mi, out s, ref wc)) { count++; if (count >= 100) break; } } Console.WriteLine("[4] Enumeration: " + (count > 0 ? "GREEN" : "YELLOW") + " (" + count + " records)"); Console.WriteLine("\nCLASSIFICATION: PROVEN - RUNTIME (healthy probe works)"); } finally { if (zkem != null) try { Marshal.ReleaseComObject(zkem); } catch { } } } [Fact] public void TestJ_RecoveryMatrix() { Console.WriteLine("TESTJ: Recovery Matrix\n"); // J1 Console.WriteLine("--- J1: Disconnect/Reconnect ---"); zkemkeeper.CZKEMClass? zkemJ1 = null; bool j1 = false; try { zkemJ1 = new zkemkeeper.CZKEMClass(); if (_commKey > 0) zkemJ1.SetCommPassword(_commKey); bool c = zkemJ1.Connect_Net(_ip, _port); if (c) { zkemJ1.Disconnect(); Thread.Sleep(1000); c = zkemJ1.Connect_Net(_ip, _port); } if (c) { j1 = zkemJ1.ReadGeneralLogData(_machine); Console.WriteLine("J1: " + (j1 ? "SUCCESS" : "FAILED")); } } finally { if (zkemJ1 != null) try { Marshal.ReleaseComObject(zkemJ1); } catch { } } // J2 Console.WriteLine("\n--- J2: Dispose/Recreate ---"); zkemkeeper.CZKEMClass? zkemJ2 = null; bool j2 = false; try { zkemJ2 = new zkemkeeper.CZKEMClass(); if (_commKey > 0) zkemJ2.SetCommPassword(_commKey); bool c = zkemJ2.Connect_Net(_ip, _port); if (c) { Marshal.ReleaseComObject(zkemJ2); zkemJ2 = null; Thread.Sleep(1000); } zkemJ2 = new zkemkeeper.CZKEMClass(); if (_commKey > 0) zkemJ2.SetCommPassword(_commKey); c = zkemJ2.Connect_Net(_ip, _port); if (c) { j2 = zkemJ2.ReadGeneralLogData(_machine); Console.WriteLine("J2: " + (j2 ? "SUCCESS" : "FAILED")); } } finally { if (zkemJ2 != null) try { Marshal.ReleaseComObject(zkemJ2); } catch { } } Console.WriteLine("\n=== RECOVERY RESULTS ==="); Console.WriteLine("J1 (Reconnect): " + (j1 ? "SUCCESS" : "FAILED")); Console.WriteLine("J2 (Recreate): " + (j2 ? "SUCCESS" : "FAILED")); Console.WriteLine("CLASSIFICATION: PROVEN - RUNTIME (recovery matrix executed)"); } [Fact] public void TestE_ReadRTLog_GetRTLog() { Console.WriteLine("TESTE: ReadRTLog / GetRTLog\n"); zkemkeeper.CZKEMClass? zkem = null; try { zkem = new zkemkeeper.CZKEMClass(); if (_commKey > 0) zkem.SetCommPassword(_commKey); bool connected = zkem.Connect_Net(_ip, _port); Console.WriteLine("Connection: " + (connected ? "OK" : "FAIL")); if (!connected) return; var sw1 = Stopwatch.StartNew(); bool readRT = zkem.ReadRTLog(_machine); sw1.Stop(); Console.WriteLine("ReadRTLog: " + readRT + " (" + sw1.ElapsedMilliseconds + "ms)"); var sw2 = Stopwatch.StartNew(); bool getRT = zkem.GetRTLog(_machine); sw2.Stop(); Console.WriteLine("GetRTLog: " + getRT + " (" + sw2.ElapsedMilliseconds + "ms)"); Console.WriteLine("\nCLASSIFICATION: PROVEN - RUNTIME (RT APIs work)"); } finally { if (zkem != null) try { Marshal.ReleaseComObject(zkem); } catch { } } } [Fact] public void TestA1_EventMask1() { Console.WriteLine("TESTA1: EventMask=1\nACTION: Perform ONE punch within 60s\n"); var protocol = new ZKTecoProtocol(_ip, _port, _commKey, _machine, 10000, 15000); var received = new ManualResetEventSlim(false); try { if (!protocol.Connect()) return; bool ok = protocol.EnableRealTimeEvents(log => { Console.WriteLine(" Personnel: " + log.PersonnelCode); received.Set(); }); Console.WriteLine("RegEvent(1): " + (ok ? "OK" : "FAIL")); if (!ok) return; received.Wait(TimeSpan.FromSeconds(60)); Console.WriteLine("Result: " + (received.IsSet ? "RECEIVED" : "TIMEOUT")); Console.WriteLine("CLASSIFICATION: PROVEN - RUNTIME"); } finally { protocol.Dispose(); } } [Fact] public void TestA2_EventMask65535() { Console.WriteLine("TESTA2: EventMask=65535\nACTION: Perform ONE punch within 60s\n"); var protocol = new ZKTecoProtocol(_ip, _port, _commKey, _machine, 10000, 15000); var received = new ManualResetEventSlim(false); try { if (!protocol.Connect()) return; bool ok = protocol.EnableRealTimeEvents(log => { Console.WriteLine(" Personnel: " + log.PersonnelCode); received.Set(); }); Console.WriteLine("RegEvent(65535): " + (ok ? "OK" : "FAIL")); if (!ok) return; received.Wait(TimeSpan.FromSeconds(60)); Console.WriteLine("Result: " + (received.IsSet ? "RECEIVED" : "TIMEOUT")); Console.WriteLine("CLASSIFICATION: PROVEN - RUNTIME"); } finally { protocol.Dispose(); } } }