<p>Here is my work around.  I still have a hard time believing that the test code needs revision, but this is what I did and the tests now pass.</p>
<p>From the above, I changed the call of RunCommandParseJSON() to provide for the explicit call of the command window.  Apparently, this is needed for Windows.  The line became:</p>
<p><code>57  const UniValue result = RunCommandParseJSON(&quot;cmd.exe /c echo '{\&quot;success\&quot;: true}'&quot;);</code></p>
<p>RunCommandParseJSON() is called from /src/util/system.cpp.  It takes a string input representing a child process, which it is to start.  In this case, start the command window and echo the string '{&quot;success&quot;: true}'.</p>
<p>This produced a runtime error:</p>
<p><code>class std::runtime_error: Unable to parse JSON: '{&quot;success&quot;: true}'</code></p>
<p>Note:  the string output from the echo command begins and ends with a single quote.  As noted in the original comments to the code, single quotes are needed to avoid Windows escaping the double quotes from the JSON.</p>
<p>RunCommandParseJSON() calls UniValue::read() from /src/univalue/lib/univalue_read.cpp.  UniValue::read(), does not seem to accommodate the case of a string input beginning and ending with single quotes.</p>
<p>Thus, I changed line 59 from:</p>
<p><code>while (raw &lt; end &amp;&amp; (json_isspace(*raw))) // skip whitespace</code></p>
<p>to:</p>
<p><code>while (raw &lt; end &amp;&amp; (json_isspace(*raw) || (*raw == '\''))) // skip whitespace and skip single quote to begin and end a json string</code></p>
<p>This allowed the for the</p>
<p><code>const UniValue result = RunCommandParseJSON(&quot;cmd.exe /c echo '{\&quot;success\&quot;: true}'&quot;);</code></p>
<p>aspect of the run_command test to succeed.</p>
<p>Still, I was getting errors on run_command when further testing for throwing the proper errors:</p>
<ol>
<li><p>No output to stderr</p>
</li>
<li><p>Error message to stderr</p>
</li>
<li><p>Unable to parse JSON error</p>
</li>
<li><p>The no output to stderr case tests for when you can start the command window process, but the command given is invalid.  This test was revised to allow for the proper calling of the Window’s command window, changing from:</p>
</li>
</ol>
<p><code>BOOST_CHECK_EXCEPTION(RunCommandParseJSON(&quot;false&quot;), std::runtime_error, checkMessageFalse);</code></p>
<p>to:</p>
<p><code>BOOST_CHECK_EXCEPTION(RunCommandParseJSON(&quot;cmd.exe /c false&quot;), std::runtime_error, checkMessageFalse);</code></p>
<p>This produced the desired error, but not the exact error message called for by the test.  I changed the check to:</p>
<p><code>const std::string what(ex.what()); BOOST_CHECK(what.find(&quot;returned 1&quot;) != std::string::npos);</code></p>
<ol start="2">
<li>The Error Message to Stderr tests for being able to start the command window process, receive a valid command, but invalid input.  This was changed from:</li>
</ol>
<p><code>BOOST_CHECK_EXCEPTION(RunCommandParseJSON(&quot;ls nosuchfile&quot;), std::runtime_error, checkMessageStdErr);</code></p>
<p>to:</p>
<p><code>BOOST_CHECK_EXCEPTION(RunCommandParseJSON(&quot;cmd.exe /c dir nosuchfile&quot;), std::runtime_error, checkMessageStdErr);</code></p>
<p>to replicate what I see as the Window’s version of the test.</p>
<ol start="3">
<li>The Unable to ParseJSON error tests for ability to start the command window process, send a valid command with valid input, but the string isn’t able to be parsed.  (This error was actually encountered early on.)  This was changed to:</li>
</ol>
<p><code>BOOST_REQUIRE_THROW(RunCommandParseJSON(&quot;cmd.exe /c echo '{'&quot;), std::runtime_error); // Unable to parse JSON</code></p>
<p>With these changes all unit tests now pass.</p>
<p>I don’t see that these changes changed the nature of the tests.  If someone notices that they do, I would definitely appreciate being told.</p>

CC BY-SA 4.0
2021-06-05T16:58:37.030Z
2021-06-05T16:58:37.030Z
TKChattoraj
<p>Perhaps the following will work.</p>
<pre><code>RunCommandParseJSON(&quot;cmd.exe&quot;, &quot;/c echo '{\&quot;success\&quot;: true}'&quot;);
</code></pre>

CC BY-SA 4.0
2021-04-04T21:29:43.027Z
Thank you for the suggestion.  I ran it and got a similar runtime error :class std::runtime_error: Unable to parse JSON: Microsoft Windows [Version 10.0.19041.867].  Here it looks like the ehco command produced "Microsoft Windows [Version 10.0.19041.867]."  Whereas in the previous runtime error, it produced the desire string, '{"success": true}'.
TKChattoraj
CC BY-SA 4.0
2021-04-04T20:42:51.237Z
2021-04-04T20:42:51.237Z
Penquin
<p>I cloned the Bitcoin Core and did the build. But, I am experiencing a Unit Test Failure in system_tests.cpp, Line 48 BOOST_AUTO_TEST(run_command).</p>
<p>First, my environment:</p>
<ol>
<li>Hardware:  Intel i7-8550U @1.80GHz 2001 Mhz, 4 Cores, 8 Logical Processors, x64.</li>
<li>OS:  Windows 10 Home</li>
<li>Toolchain:  Visual Studios 2019 v142, Configuration: Release, Platform x64.</li>
</ol>
<p>The build completes without errors, but I do get the following message:</p>
<blockquote>
<p>C:\Users\name\source\repos\bitcoin-first\build_msvc\test_bitcoin\test_bitcoin.vcxproj : warning  : Wildcards in project items are not supported in VC projects and can cause unexpected behavior, instability and performance degradation in the Visual Studio IDE. Please refer to <a href="https://aka.ms/cpp/projectwildcards" rel="nofollow noreferrer">https://aka.ms/cpp/projectwildcards</a> for supported options.</p>
</blockquote>
<p>Running test_bitcoin, all tests pass except run_command from source system_tests.cpp line 48.  I get the following error:</p>
<blockquote>
<p>struct boost::process::process_error:  CreateProcess failed: The system cannot find the file specified.</p>
</blockquote>
<p>VS Test Explorer gives additionally:</p>
<blockquote>
<p>unknown location(0): exception in &quot;run_command&quot;: struct boost::process::process_error:  CreateProcess failed: The system cannot find the file specified. system_tests.cpp(52): last checkpoint:</p>
</blockquote>
<p>The relevant code from system_tests.cpp (Please disregard any spacing syntax issues after the line numbers.  I manually added them in and they are not at issue):</p>
<pre><code>48 BOOST_AUTO_TEST_CASE(run_command)
49 {
50    {
51        const UniValue result = RunCommandParseJSON(&quot;&quot;);
52        BOOST_CHECK(result.isNull());
53    }
54    {
55 #ifdef WIN32
56       // Windows requires single quotes to prevent escaping double quotes from the JSON...
57       // const UniValue result = RunCommandParseJSON(&quot;cmd.exe /c echo '{\&quot;success\&quot;: true}'&quot;);
58        const UniValue result = RunCommandParseJSON(&quot;echo '{\&quot;success\&quot;: true}'&quot;);
59   
60 #else
61        // ... but Linux and macOS echo a single quote if it's used
62        const UniValue result = RunCommandParseJSON(&quot;echo \&quot;{\&quot;success\&quot;: true}\&quot;&quot;);
63 #endif
64        BOOST_CHECK(result.isObject());
65        const UniValue&amp; success = find_value(result, &quot;success&quot;);
66        BOOST_CHECK(!success.isNull());
67        BOOST_CHECK_EQUAL(success.getBool(), true);
68    }
69    {
70        // An invalid command is handled by Boost
71        BOOST_CHECK_EXCEPTION(RunCommandParseJSON(&quot;invalid_command&quot;), boost::process::process_error, checkMessage); // Command failed
72    }
73    {
74        // Return non-zero exit code, no output to stderr
75        BOOST_CHECK_EXCEPTION(RunCommandParseJSON(&quot;false&quot;), std::runtime_error, checkMessageFalse);
76    }
77    {
78        // Return non-zero exit code, with error message for stderr
79        BOOST_CHECK_EXCEPTION(RunCommandParseJSON(&quot;ls nosuchfile&quot;), std::runtime_error, checkMessageStdErr);
80    }
81    {
82        BOOST_REQUIRE_THROW(RunCommandParseJSON(&quot;echo \&quot;{\&quot;&quot;), std::runtime_error); // Unable to parse JSON
83    }
84    // Test std::in, except for Windows
85 #ifndef WIN32
86    {
87        const UniValue result = RunCommandParseJSON(&quot;cat&quot;, &quot;{\&quot;success\&quot;: true}&quot;);
88        BOOST_CHECK(result.isObject());
89        const UniValue&amp; success = find_value(result, &quot;success&quot;);
90        BOOST_CHECK(!success.isNull());
91        BOOST_CHECK_EQUAL(success.getBool(), true);
92    }
93 #endif
94 }
95 #endif // HAVE_BOOST_PROCESS
</code></pre>
<p>Line 58 is failing.  Based on this <a href="https://stackoverflow.com/questions/47028660/boost-createprocess-failed">https://stackoverflow.com/questions/47028660/boost-createprocess-failed</a>, I commented out line 58 and added the following Line 57:</p>
<pre><code>57  const UniValue result = RunCommandParseJSON(&quot;cmd.exe /c echo '{\&quot;success\&quot;: true}'&quot;);
</code></pre>
<p>Running again the system_tests.cpp gets me past the CreateProcess failure, but produces the following failure in system_tests.cpp line 48, run_command():</p>
<blockquote>
<p>class std::runtime_error: Unable to parse JSON: '{&quot;success&quot;: true}'.</p>
</blockquote>
<p>VS Test Explorer additionally states:</p>
<blockquote>
<p>unknown location(0): exception in &quot;run_command&quot;: class std::runtime_error: Unable to parse JSON: '{&quot;success&quot;: true}'
system_tests.cpp(52): last checkpoint:</p>
</blockquote>
<p>I'm having a hard time believing the test is incorrect--I've found nothing about this failure.  Instead, I suspect I didn’t correctly set some switch or option or configuration somewhere.</p>
<p>Any comments or help would be greatly appreciated.</p>

<bitcoin-core><bitcoin-core-development><unit-test>
Bitcoin Core: Unit Test Failure: system_tests.cpp: Line 48 BOOST_AUTO_TEST(run_command)
TKChattoraj
# Bitcoin Core: Unit Test Failure: system_tests.cpp: Line 48 BOOST_AUTO_TEST(run_command)

(Asked by: username_0 on 2021-04-04)

I cloned the Bitcoin Core and did the build. But, I am experiencing a Unit Test Failure in system\_tests.cpp, Line 48 BOOST\_AUTO\_TEST(run\_command).

First, my environment:

1. Hardware: Intel i7-8550U @1.80GHz 2001 Mhz, 4 Cores, 8 Logical Processors, x64.
2. OS: Windows 10 Home
3. Toolchain: Visual Studios 2019 v142, Configuration: Release, Platform x64.

The build completes without errors, but I do get the following message:

> C:\Users\name\source\repos\bitcoin-first\build\_msvc\test\_bitcoin\test\_bitcoin.vcxproj : warning : Wildcards in project items are not supported in VC projects and can cause unexpected behavior, instability and performance degradation in the Visual Studio IDE. Please refer to <https://aka.ms/cpp/projectwildcards> for supported options.

Running test\_bitcoin, all tests pass except run\_command from source system\_tests.cpp line 48. I get the following error:

> struct boost::process::process\_error: CreateProcess failed: The system cannot find the file specified.

VS Test Explorer gives additionally:

> unknown location(0): exception in "run\_command": struct boost::process::process\_error: CreateProcess failed: The system cannot find the file specified. system\_tests.cpp(52): last checkpoint:

The relevant code from system\_tests.cpp (Please disregard any spacing syntax issues after the line numbers. I manually added them in and they are not at issue):

```
48 BOOST_AUTO_TEST_CASE(run_command)
49 {
50    {
51        const UniValue result = RunCommandParseJSON("");
52        BOOST_CHECK(result.isNull());
53    }
54    {
55 #ifdef WIN32
56       // Windows requires single quotes to prevent escaping double quotes from the JSON...
57       // const UniValue result = RunCommandParseJSON("cmd.exe /c echo '{\"success\": true}'");
58        const UniValue result = RunCommandParseJSON("echo '{\"success\": true}'");
59   
60 #else
61        // ... but Linux and macOS echo a single quote if it's used
62        const UniValue result = RunCommandParseJSON("echo \"{\"success\": true}\"");
63 #endif
64        BOOST_CHECK(result.isObject());
65        const UniValue& success = find_value(result, "success");
66        BOOST_CHECK(!success.isNull());
67        BOOST_CHECK_EQUAL(success.getBool(), true);
68    }
69    {
70        // An invalid command is handled by Boost
71        BOOST_CHECK_EXCEPTION(RunCommandParseJSON("invalid_command"), boost::process::process_error, checkMessage); // Command failed
72    }
73    {
74        // Return non-zero exit code, no output to stderr
75        BOOST_CHECK_EXCEPTION(RunCommandParseJSON("false"), std::runtime_error, checkMessageFalse);
76    }
77    {
78        // Return non-zero exit code, with error message for stderr
79        BOOST_CHECK_EXCEPTION(RunCommandParseJSON("ls nosuchfile"), std::runtime_error, checkMessageStdErr);
80    }
81    {
82        BOOST_REQUIRE_THROW(RunCommandParseJSON("echo \"{\""), std::runtime_error); // Unable to parse JSON
83    }
84    // Test std::in, except for Windows
85 #ifndef WIN32
86    {
87        const UniValue result = RunCommandParseJSON("cat", "{\"success\": true}");
88        BOOST_CHECK(result.isObject());
89        const UniValue& success = find_value(result, "success");
90        BOOST_CHECK(!success.isNull());
91        BOOST_CHECK_EQUAL(success.getBool(), true);
92    }
93 #endif
94 }
95 #endif // HAVE_BOOST_PROCESS

```

Line 58 is failing. Based on this <https://stackoverflow.com/questions/47028660/boost-createprocess-failed>, I commented out line 58 and added the following Line 57:

```
57  const UniValue result = RunCommandParseJSON("cmd.exe /c echo '{\"success\": true}'");

```

Running again the system\_tests.cpp gets me past the CreateProcess failure, but produces the following failure in system\_tests.cpp line 48, run\_command():

> class std::runtime\_error: Unable to parse JSON: '{"success": true}'.

VS Test Explorer additionally states:

> unknown location(0): exception in "run\_command": class std::runtime\_error: Unable to parse JSON: '{"success": true}'
> system\_tests.cpp(52): last checkpoint:

I'm having a hard time believing the test is incorrect--I've found nothing about this failure. Instead, I suspect I didn’t correctly set some switch or option or configuration somewhere.

Any comments or help would be greatly appreciated.

**Tags:** bitcoin-core, bitcoin-core-development, unit-test


---

## 2 answers

### Answer by username_0 on 2021-06-05. Score: 1

Here is my work around. I still have a hard time believing that the test code needs revision, but this is what I did and the tests now pass.

From the above, I changed the call of RunCommandParseJSON() to provide for the explicit call of the command window. Apparently, this is needed for Windows. The line became:

`57 const UniValue result = RunCommandParseJSON("cmd.exe /c echo '{\"success\": true}'");`

RunCommandParseJSON() is called from /src/util/system.cpp. It takes a string input representing a child process, which it is to start. In this case, start the command window and echo the string '{"success": true}'.

This produced a runtime error:

`class std::runtime_error: Unable to parse JSON: '{"success": true}'`

Note: the string output from the echo command begins and ends with a single quote. As noted in the original comments to the code, single quotes are needed to avoid Windows escaping the double quotes from the JSON.

RunCommandParseJSON() calls UniValue::read() from /src/univalue/lib/univalue\_read.cpp. UniValue::read(), does not seem to accommodate the case of a string input beginning and ending with single quotes.

Thus, I changed line 59 from:

`while (raw < end && (json_isspace(*raw))) // skip whitespace`

to:

`while (raw < end && (json_isspace(*raw) || (*raw == '\''))) // skip whitespace and skip single quote to begin and end a json string`

This allowed the for the

`const UniValue result = RunCommandParseJSON("cmd.exe /c echo '{\"success\": true}'");`

aspect of the run\_command test to succeed.

Still, I was getting errors on run\_command when further testing for throwing the proper errors:

1. No output to stderr
2. Error message to stderr
3. Unable to parse JSON error
4. The no output to stderr case tests for when you can start the command window process, but the command given is invalid. This test was revised to allow for the proper calling of the Window’s command window, changing from:

`BOOST_CHECK_EXCEPTION(RunCommandParseJSON("false"), std::runtime_error, checkMessageFalse);`

to:

`BOOST_CHECK_EXCEPTION(RunCommandParseJSON("cmd.exe /c false"), std::runtime_error, checkMessageFalse);`

This produced the desired error, but not the exact error message called for by the test. I changed the check to:

`const std::string what(ex.what()); BOOST_CHECK(what.find("returned 1") != std::string::npos);`

2. The Error Message to Stderr tests for being able to start the command window process, receive a valid command, but invalid input. This was changed from:

`BOOST_CHECK_EXCEPTION(RunCommandParseJSON("ls nosuchfile"), std::runtime_error, checkMessageStdErr);`

to:

`BOOST_CHECK_EXCEPTION(RunCommandParseJSON("cmd.exe /c dir nosuchfile"), std::runtime_error, checkMessageStdErr);`

to replicate what I see as the Window’s version of the test.

3. The Unable to ParseJSON error tests for ability to start the command window process, send a valid command with valid input, but the string isn’t able to be parsed. (This error was actually encountered early on.) This was changed to:

`BOOST_REQUIRE_THROW(RunCommandParseJSON("cmd.exe /c echo '{'"), std::runtime_error); // Unable to parse JSON`

With these changes all unit tests now pass.

I don’t see that these changes changed the nature of the tests. If someone notices that they do, I would definitely appreciate being told.


---

### Answer by username_1 on 2021-04-04. Score: 0

Perhaps the following will work.

```
RunCommandParseJSON("cmd.exe", "/c echo '{\"success\": true}'");

```

> **Comments:**
> **username_0**: Thank you for the suggestion.  I ran it and got a similar runtime error :class std::runtime_error: Unable to parse JSON: Microsoft Windows [Version 10.0.19041.867].  Here it looks like the ehco command produced "Microsoft Windows [Version 10.0.19041.867]."  Whereas in the previous runtime error, it produced the desire string, '{"success": true}'.  

---

