GCC Code Coverage Report


Directory: src/gate/
File: src/gate/net/cxx_winrmclients.cpp
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 0 61 0.0%
Functions: 0 11 0.0%
Branches: 0 54 0.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, 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 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
99 try
100 {
101 if(continueProcessing)
102 {
103 switch(output_type)
104 {
105 case GATE_WINRMCLIENT_OUTPUT_TYPE_STDOUT:
106 {
107 if(output_buffer)
108 {
109 ptrSink->onWinRmStdOut(Blob(*output_buffer).clone());
110 }
111 else
112 {
113 ptrSink->onWinRmStdOutEnd();
114 }
115 break;
116 }
117
118 case GATE_WINRMCLIENT_OUTPUT_TYPE_STDERR:
119 {
120 if(output_buffer)
121 {
122 ptrSink->onWinRmStdErr(Blob(*output_buffer).clone());
123 }
124 else
125 {
126 ptrSink->onWinRmStdErrEnd();
127 }
128 break;
129 }
130
131 case GATE_WINRMCLIENT_OUTPUT_TYPE_EXIT:
132 {
133 String text = String::createStatic(
134 static_cast<char const*>(gate_blob_data(output_buffer)), gate_blob_length(output_buffer));
135 int64_t exitCode = text.parseInt();
136 ptrSink->onWinRmExit(static_cast<intptr_t>(exitCode));
137 break;
138 }
139 }
140 }
141 }
142 catch(...)
143 {
144 continueProcessing = false;
145 }
146 return continueProcessing;
147 }
148
149
150 void WinrmClient::receiveOutput(String const& shellId, String const& commandId, uint32_t timeoutMs,
151 WinrmClientOutputSink& callbackSink)
152 {
153 this->receiveOutput(shellId, commandId, timeoutMs, &winrmclient_output_dispatcher, &callbackSink);
154 }
155
156
157 } // end of namespace net
158 } // end of namespace gate
159