GCC Code Coverage Report


Directory: src/gate/
File: src/gate/net/cxx_winrmclients.cpp
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 0 46 0.0%
Functions: 0 11 0.0%
Branches: 0 62 0.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28 #include "gate/net/winrmclients.hpp"
29
30 #include "gate/exceptions.hpp"
31
32 namespace gate
33 {
34 namespace net
35 {
36 WinrmClientOutputSink::~WinrmClientOutputSink() noexcept
37 {
38 }
39
40 WinrmClient::WinrmClient(String const& server, uint16_t port, String const& user, String const& password, enumint_t flags)
41 {
42 result_t result = gate_winrmclient_create(&this->impl, server.c_impl(), port, user.c_impl(), password.c_impl(), flags);
43 GATEXX_CHECK_EXCEPTION(result);
44 }
45
46 WinrmClient::~WinrmClient() noexcept
47 {
48 gate_winrmclient_release(&this->impl);
49 }
50
51 String WinrmClient::newShell(String const& workDir, Array<String> const& envVars)
52 {
53 gate_string_t shellId = GATE_STRING_INIT_EMPTY;
54 result_t result = gate_winrmclient_shell_new(&this->impl, workDir.c_impl(), envVars.c_impl(), &shellId);
55 GATEXX_CHECK_EXCEPTION(result);
56 return String::createFrom(shellId);
57 }
58
59 void WinrmClient::deleteShell(String const& shellId)
60 {
61 result_t result = gate_winrmclient_shell_delete(&this->impl, shellId.c_impl());
62 GATEXX_CHECK_EXCEPTION(result);
63 }
64
65 String WinrmClient::execute(String const& shellId, String const& command, Array<String> const& args)
66 {
67 gate_string_t commandId = GATE_STRING_INIT_EMPTY;
68 result_t result = gate_winrmclient_shell_command_start(&this->impl, shellId.c_impl(), command.c_impl(), args.c_impl(), &commandId);
69 GATEXX_CHECK_EXCEPTION(result);
70 return String::createFrom(commandId);
71 }
72
73 void WinrmClient::abortExecution(String const& shellId, String const& commandId)
74 {
75 result_t result = gate_winrmclient_shell_command_abort(&this->impl, shellId.c_impl(), commandId.c_impl());
76 GATEXX_CHECK_EXCEPTION(result);
77 }
78
79 size_t WinrmClient::sendInput(String const& shellId, String const& commandId, Blob const& data)
80 {
81 result_t result = gate_winrmclient_shell_command_send(&this->impl, shellId.c_impl(), commandId.c_impl(), data.c_impl());
82 GATEXX_CHECK_EXCEPTION(result);
83 return data.length();
84 }
85
86 void WinrmClient::receiveOutput(String const& shellId, String const& commandId, uint32_t timeoutMs,
87 gate_winrmclient_output_callback callback, void* userparam)
88 {
89 result_t result = gate_winrmclient_shell_command_receive(
90 &this->impl, shellId.c_impl(), commandId.c_impl(), timeoutMs, callback, userparam);
91 GATEXX_CHECK_EXCEPTION(result);
92 }
93
94 static gate_bool_t GATE_CALL winrmclient_output_dispatcher(gate_enumint_t output_type, gate_blob_t const* output_buffer, void* user_param)
95 {
96 WinrmClientOutputSink* ptrSink = static_cast<WinrmClientOutputSink*>(user_param);
97 gate_bool_t continueProcessing = ptrSink != NULL;
98 if(continueProcessing)
99 {
100 ExceptionInfo xcptInfo;
101 GATEXX_TRY_CATCHINFO(xcptInfo, {
102 switch(output_type)
103 {
104 case GATE_WINRMCLIENT_OUTPUT_TYPE_STDOUT:
105 {
106 if(output_buffer)
107 {
108 ptrSink->onWinRmStdOut(Blob(*output_buffer).clone());
109 }
110 else
111 {
112 ptrSink->onWinRmStdOutEnd();
113 }
114 break;
115 }
116 case GATE_WINRMCLIENT_OUTPUT_TYPE_STDERR:
117 {
118 if(output_buffer)
119 {
120 ptrSink->onWinRmStdErr(Blob(*output_buffer).clone());
121 }
122 else
123 {
124 ptrSink->onWinRmStdErrEnd();
125 }
126 break;
127 }
128
129 case GATE_WINRMCLIENT_OUTPUT_TYPE_EXIT:
130 {
131 String text = String::createStatic(
132 static_cast<char const*>(gate_blob_data(output_buffer)), gate_blob_length(output_buffer));
133 int64_t exitCode = text.parseInt();
134 ptrSink->onWinRmExit(static_cast<intptr_t>(exitCode));
135 break;
136 }
137 }
138 });
139 continueProcessing = xcptInfo.succeeded();
140 }
141 return continueProcessing;
142 }
143
144
145 void WinrmClient::receiveOutput(String const& shellId, String const& commandId, uint32_t timeoutMs,
146 WinrmClientOutputSink& callbackSink)
147 {
148 this->receiveOutput(shellId, commandId, timeoutMs, &winrmclient_output_dispatcher, &callbackSink);
149 }
150
151
152 } // end of namespace net
153 } // end of namespace gate
154