blob: ac594bfaec0b7fe6d20aea9b4dab6dac647f9653 [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Testing framework."><meta name="keywords" content="rust, rustlang, rust-lang, test"><title>ring::test - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../../ring/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../ring/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Module test</a></h2><div class="sidebar-elems"><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Module <a href="../index.html">ring</a>::<wbr><a class="mod" href="#">test</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/ring/test.rs.html#15-640">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Testing framework.</p>
<p>Unlike the rest of <em>ring</em>, this testing framework uses panics pretty
liberally. It was originally designed for internal use–it drives most of
<em>ring</em>’s internal tests, and so it is optimized for getting <em>ring</em>’s tests
written quickly at the expense of some usability. The documentation is
lacking. The best way to learn it is to look at some examples. The digest
tests are the most complicated because they use named sections. Other tests
avoid named sections and so are easier to understand.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2><h3 id="writing-tests"><a href="#writing-tests">Writing Tests</a></h3>
<p>Input files look like this:</p>
<div class="example-wrap"><pre class="language-text"><code>
HMAC = SHA1
Input = &quot;My test data&quot;
Key = &quot;&quot;
Output = 61afdecb95429ef494d61fdee15990cabf0826fc
HMAC = SHA256
Input = &quot;Sample message for keylen&lt;blocklen&quot;
Key = 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
Output = A28CF43130EE696A98F14A37678B56BCFCBDD9E5CF69717FECF5480F0EBDF790</code></pre></div>
<p>Test cases are separated with blank lines. Note how the bytes of the <code>Key</code>
attribute are specified as a quoted string in the first test case and as
hex in the second test case; you can use whichever form is more convenient
and you can mix and match within the same file. The empty sequence of bytes
can only be represented with the quoted string form (<code>&quot;&quot;</code>).</p>
<p>Here’s how you would consume the test data:</p>
<div class="example-wrap ignore"><div class='tooltip'></div><pre class="rust rust-example-rendered"><code><span class="kw">use </span>ring::test;
test::run(<span class="macro">test::test_file!</span>(<span class="string">&quot;hmac_tests.txt&quot;</span>), |section, test_case| {
<span class="macro">assert_eq!</span>(section, <span class="string">&quot;&quot;</span>); <span class="comment">// This test doesn&#39;t use named sections.
</span><span class="kw">let </span>digest_alg = test_case.consume_digest_alg(<span class="string">&quot;HMAC&quot;</span>);
<span class="kw">let </span>input = test_case.consume_bytes(<span class="string">&quot;Input&quot;</span>);
<span class="kw">let </span>key = test_case.consume_bytes(<span class="string">&quot;Key&quot;</span>);
<span class="kw">let </span>output = test_case.consume_bytes(<span class="string">&quot;Output&quot;</span>);
<span class="comment">// Do the actual testing here
</span>});</code></pre></div>
<p>Note that <code>consume_digest_alg</code> automatically maps the string “SHA1” to a
reference to <code>digest::SHA1_FOR_LEGACY_USE_ONLY</code>, “SHA256” to
<code>digest::SHA256</code>, etc.</p>
<h3 id="output-when-a-test-fails"><a href="#output-when-a-test-fails">Output When a Test Fails</a></h3>
<p>When a test case fails, the framework automatically prints out the test
case. If the test case failed with a panic, then the backtrace of the panic
will be printed too. For example, let’s say the failing test case looks
like this:</p>
<div class="example-wrap"><pre class="language-text"><code>Curve = P-256
a = 2b11cb945c8cf152ffa4c9c2b1c965b019b35d0b7626919ef0ae6cb9d232f8af
b = 18905f76a53755c679fb732b7762251075ba95fc5fedb60179e730d418a9143c
r = 18905f76a53755c679fb732b7762251075ba95fc5fedb60179e730d418a9143c</code></pre></div>
<p>If the test fails, this will be printed (if <code>$RUST_BACKTRACE</code> is <code>1</code>):</p>
<div class="example-wrap"><pre class="language-text"><code>src/example_tests.txt: Test panicked.
Curve = P-256
a = 2b11cb945c8cf152ffa4c9c2b1c965b019b35d0b7626919ef0ae6cb9d232f8af
b = 18905f76a53755c679fb732b7762251075ba95fc5fedb60179e730d418a9143c
r = 18905f76a53755c679fb732b7762251075ba95fc5fedb60179e730d418a9143c
thread &#39;example_test&#39; panicked at &#39;Test failed.&#39;, src\test.rs:206
stack backtrace:
0: 0x7ff654a05c7c - std::rt::lang_start::h61f4934e780b4dfc
1: 0x7ff654a04f32 - std::rt::lang_start::h61f4934e780b4dfc
2: 0x7ff6549f505d - std::panicking::rust_panic_with_hook::hfe203e3083c2b544
3: 0x7ff654a0825b - rust_begin_unwind
4: 0x7ff6549f63af - std::panicking::begin_panic_fmt::h484cd47786497f03
5: 0x7ff654a07e9b - rust_begin_unwind
6: 0x7ff654a0ae95 - core::panicking::panic_fmt::h257ceb0aa351d801
7: 0x7ff654a0b190 - core::panicking::panic::h4bb1497076d04ab9
8: 0x7ff65496dc41 - from_file&lt;closure&gt;
at C:\Users\Example\example\&lt;core macros&gt;:4
9: 0x7ff65496d49c - example_test
at C:\Users\Example\example\src\example.rs:652
10: 0x7ff6549d192a - test::stats::Summary::new::ha139494ed2e4e01f
11: 0x7ff6549d51a2 - test::stats::Summary::new::ha139494ed2e4e01f
12: 0x7ff654a0a911 - _rust_maybe_catch_panic
13: 0x7ff6549d56dd - test::stats::Summary::new::ha139494ed2e4e01f
14: 0x7ff654a03783 - std::sys::thread::Thread::new::h2b08da6cd2517f79
15: 0x7ff968518101 - BaseThreadInitThunk</code></pre></div>
<p>Notice that the output shows the name of the data file
(<code>src/example_tests.txt</code>), the test inputs that led to the failure, and the
stack trace to the line in the test code that panicked: entry 9 in the
stack trace pointing to line 652 of the file <code>example.rs</code>.</p>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.File.html" title="ring::test::File struct">File</a></div><div class="item-right docblock-short">A test input file.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.TestCase.html" title="ring::test::TestCase struct">TestCase</a></div><div class="item-right docblock-short">A test case. A test case consists of a set of named attributes. Every
attribute in the test case must be consumed exactly once; this helps catch
typos and omissions.</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.compile_time_assert_clone.html" title="ring::test::compile_time_assert_clone fn">compile_time_assert_clone</a></div><div class="item-right docblock-short"><code>compile_time_assert_clone::&lt;T&gt;();</code> fails to compile if <code>T</code> doesn’t
implement <code>Clone</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.compile_time_assert_copy.html" title="ring::test::compile_time_assert_copy fn">compile_time_assert_copy</a></div><div class="item-right docblock-short"><code>compile_time_assert_copy::&lt;T&gt;();</code> fails to compile if <code>T</code> doesn’t
implement <code>Copy</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.compile_time_assert_send.html" title="ring::test::compile_time_assert_send fn">compile_time_assert_send</a></div><div class="item-right docblock-short"><code>compile_time_assert_send::&lt;T&gt;();</code> fails to compile if <code>T</code> doesn’t
implement <code>Send</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.compile_time_assert_sync.html" title="ring::test::compile_time_assert_sync fn">compile_time_assert_sync</a></div><div class="item-right docblock-short"><code>compile_time_assert_sync::&lt;T&gt;();</code> fails to compile if <code>T</code> doesn’t
implement <code>Sync</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.from_hex.html" title="ring::test::from_hex fn">from_hex</a></div><div class="item-right docblock-short">Decode an string of hex digits into a sequence of bytes. The input must
have an even number of digits.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.run.html" title="ring::test::run fn">run</a></div><div class="item-right docblock-short">Parses test cases out of the given file, calling <code>f</code> on each vector until
<code>f</code> fails or until all the test vectors have been read. <code>f</code> can indicate
failure either by returning <code>Err()</code> or by panicking.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="ring" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>