Debian Codegraph @

rsvg/tests/README.md

raw symbols SCIP
1 # Librsvg test suite
2
3 ==================
4
5 Librsvg's test suite is split like this:
6
7 * Unit tests in the Rust code, run normally with "cargo test".
8
9 * Rust integration tests in this tests/ directory.
10
11 * C API tests in this tests/ directory.
12
13 The C API and Rust tests run the library with its public APIs in
14 both languages. In addition, the Rust tests also exercise the
15 rsvg-convert program.
16
17 **For the impatient:** you can use `cargo test` to run most of the
18 test suite; this is fine for regular development. This will *not* run
19 the C API tests or some of the long-running tests that exercise the
20 hard-coded limits of the library.
21
22 To run the full test suite, see ["Running the test
23 suite"](#running-the-test-suite) below.
24
25 ## Unit tests
26
27 ----------
28
29 The library's source code has small unit tests for particular sections
30 of the code.
31
32 **It is better to catch errors early**, in the unit tests, if
33 possible. The test suite in this tests/ directory is for black box
34 tests, which run the library as a normal program would use it.
35
36 * **What should be in a unit test** - a small test of an algorithm; a check for
37 computed values given some starting values; checks for edge cases.
38
39 * **What should be in these black-box tests** - rendering tests that exercise a
40 particular part of the code; CSS cascading tests; images that expose bugs and
41 that we want to avoid regressing on later.
42
43 For example, there are unit tests of the path data parser (the `<path
44 d="M10 10 L20 20 ...">` element and its `d` attribute, to ensure that
45 the parser handles all the path commands and catches errors
46 appropriately. Correspondingly, there are a bunch of black-box tests
47 that exercise particular features of path rendering ("does this
48 actually draw a line, or an arc?").
49
50 ## Running the test suite
51
52 ----------
53
54 For regular development, use `cargo test`. This will run most of the
55 test suite, except for the C API tests and the long-running tests
56 which exercise the hard-coded limits of the library.
57
58 To run the full test suite, you need to go through meson. Run the
59 following commands in the toplevel source directory:
60
61 ```sh
62 export TESTS_OUTPUT_DIR=/some/directory/for/test/failures
63 mkdir -p _build
64 meson setup _build -Ddocs=enabled -Dintrospection=enabled -Dvala=enabled
65 meson compile -C _build
66 meson test -C _build
67 ```
68
69 ## Running a single test case
70
71 ----------
72
73 For example, use this:
74
75 ```sh
76 cargo test -p librsvg --test render_crash -- bug1088_fuzz_cairo_out_of_bounds
77 ```
78
79 * ``-p librsvg`` selects that package in the Cargo workspace.
80
81 * ``--test render_crash`` selects that test binary.
82
83 * ``-- bug1088_fuzz_cairo_out_of_bounds`` selects the test case.
84
85 ## Artifacts produced from the tests
86
87 **Failures for reference tests:** Tests that render a document and
88 compare the result to a reference image will write details on failures
89 to files under the `TESTS_OUTPUT_DIR` environment variable. If this
90 variable is missing, the files will appear under `$TMPDIR/rsvg-test-output`.
91
92 **Test logs:** the C API tests produce a `$builddir/tests/*.log`
93 series of files with a list of test results.
94
95 ## C API tests - `api.c`
96
97 These test the full C API of librsvg: all the public functions; the
98 RsvgHandle class, its methods, and its GObject properties; all the
99 deprecated functions. Any new public APIs should get tested here.
100
101 The tests are intended to preserve the historical peculiarities of the
102 C API, to ensure ABI compatibility across versions of the library.
103
104 These tests are not meant to exhaustively test librsvg's features.
105 For those, you should look at the [Rust integration
106 tests](#rust-integration-tests).
107
108 This C API test suite is built upon [Glib's GTest utility
109 functions][gtest], which let you define tests in the C language.
110
111 ## Rust integration tests
112
113 These are built as a Rust binary in this tests/ directory, and are
114 runnable with `cargo test`.
115
116 ### Rust API tests - `api.rs`
117
118 Tests the public Rust API of librsvg.
119
120 ### Crash tests - `loading_crash.rs`
121
122 These load and parse an SVG, and ensure that there are no crashes in
123 the process. Note that this does *not* render the images.
124
125 The SVG images live in the `fixtures/crash` directory. The files are
126 just tested to not cause crashes during the loading process; it does
127 not matter if the files are well-formed XML, for example.
128
129 ## Rendering crash tests - `render_crash.rs`
130
131 We use these tests to ensure there are no regressions after fixing a
132 bug where a particular SVG loads fine, but it crashes the renderer.
133
134 The test files are in the `fixtures/render-crash` directory. The
135 module loads the files and renders them, without comparing the results
136 to anything in particular.
137
138 ## General bug regression tests - `bugs.rs`
139
140 These test fixes for specific bugs in the library, so that the bugs don't recur.
141
142 ## Error tests - `errors.rs`
143
144 These test conditions which should produce errors during loading or rendering.
145
146 During loading, librsvg will report malformed XML as errors. It will
147 also report an error if an SVG file has more elements than what is
148 configured in librsvg's internal limits; this is intended to prevent
149 uncontrolled memory consumption.
150
151 During rendering, librsvg will report errors if the SVG document hits
152 librsvg's internal limits for the number of instanced objects; this is
153 intended to prevent uncontrolled CPU consumption from malicious SVG
154 files.
155
156 The test files are in the `fixtures/errors` directory.
157
158 ## Tests for SVG filter effects - `filters.rs`
159
160 These test the semantics of the `filter` property, and specific filter
161 functions.
162
163 ## Reference tests - `reference.rs`
164
165 These are the bulk of the rendering tests, where the results of
166 rendering SVG files are compared against reference PNG images.
167
168 The reference tests allow for minor differences in the pixel values of
169 the results. Each pixel's RGBA components gets compared to the
170 corresponding one in the reference image:
171
172 * If the absolute value of the difference between corresponding RGBA components
173 is more than 2, the test suite considers the result to be *distinguishable*
174 from the reference, but otherwise acceptable.
175
176 * If the absolute value of the difference is more than the number in the
177 `RSVG_TEST_TOLERANCE` environment variable, the result is *inacceptable* and
178 the test suite fails; the default is 2 if that variable is not set. You can
179 tweak this value if your machine's floating-point unit produces wildly
180 different results.
181
182 The test files are in the `fixtures/reftests/` directory. Each
183 image-based reference test uses two files: `foo.svg` and
184 `foo-ref.png`. The test harness will render `foo.svg` and compare the
185 results to `foo-ref.png`.
186
187 Failing tests will appear as part of the `cargo test` output. It will
188 print the filenames for the output and difference images for failed
189 tests, as follows.
190
191 Each `foo.svg` test file produces a `foo-out.png` result, and if that
192 result is *distinguishable* from the reference PNG (per the
193 terminology above), the test will also produce a `foo-diff.png` which
194 you can examine by hand. See "[Examining failed reference
195 tests](#examining-failed-reference-tests)" below.
196
197 **Ignoring tests:** SVG test files in `fixtures/reftests` whose names
198 begin with "`ignore`" will be skipped from the tests. That is,
199 anything that matches "ignore*.svg`" will not be included in the
200 tests. You can use this to skip a few problematic files temporarily.
201
202 As of 2020/Oct/22 we have an informal organization of these files:
203
204 * `fixtures/reftests/svg1.1` - Tests from the W3C's SVG1.1 test suite. These are
205 supposed to test all of SVG's features; we will add them one by one as librsvg
206 starts implementing the features.
207
208 * `fixtures/reftests/svg2` - Tests for SVG2 or CSS3 features.
209
210 * `fixtures/reftests/bugs/*.svg` - Tests for particular bug numbers. Please use
211 the bug number from Gitlab, like `1234-blah.svg`, and the corresponding
212 `1234-blah-ref.png` for the known-good reference image.
213
214 **Note:** Librsvg migrated from git.gnome.org and bugzilla.gnome.org to
215 gitlab.gnome.org. Bug numbers in Bugzilla were around 6 digits in length; in
216 Gitlab, they are small numbers.
217
218 * `fixtures/reftests/*.svg` - Tests for special situations that arose during
219 development.
220
221 * `fixtures/reftests/adwaita/*.svg` - A snapshot of the Adwaita icon theme
222 (GNOME's default icon theme), to ensure that librsvg renders it correctly.
223
224 ### Examining failed reference tests
225
226 Let's say you run `cargo test` and see that one of the tests fails. The test log
227 may have lines like these:
228
229 ```text
230 ---- reference::svg_1_1_tests_fixtures_reftests_svg1_1_painting_stroke_01_t_svg stdout ----
231 output: output/painting-stroke-01-t-out.png
232 painting-stroke-01-t: 12414 pixels changed with maximum difference of 255
233 diff: output/painting-stroke-01-t-diff.png
234 thread 'reference::svg_1_1_tests_fixtures_reftests_svg1_1_painting_stroke_01_t_svg' panicked at 'surfaces are too different', tests/src/reference.rs:319:25
235
236 ```
237
238 This means that the test file
239 `fixtures/reftests/svg1.1/painting-stroke-01-t.svg` got rendered, and
240 produced incorrect output when compared to
241 `fixtures/reftests/svg1.1/painting-stroke-01-t-ref.png`.
242
243 When a test fails, rsvg-test creates two images in `tests/output`:
244
245 ```text
246 tests/output/foo-out.png
247 tests/output/foo-diff.png
248 ```
249
250 In this case, `foo-out.png` is the actual rendered output, which is presumed to
251 be incorrect, since it differs from the `foo-ref.png` reference image.
252 The `foo-diff.png` is a "visual diff" that you can see in an image
253 viewer; pixels that differ are highlighted.
254
255 It is up to you to decide what to do next:
256
257 * If the `foo-out.png` image looks correct, and the only difference with respect
258 to the `foo-ref.png` reference image is that antialiased edges look different,
259 or font rendering is slightly different due to the font-rendering machinery in
260 your system, you can just regenerate the test image. See "
261 [Regenerating reference images](#regenerating-reference-images)" below.
262
263 * If the `foo-out.png` image is obviously wrong when compared to the
264 `foo-ref.png` reference, you can [file a bug][bug]. You can wait until someone
265 fixes it, or try to [fix the bug yourself][pull-requests]!
266
267 * Any other situation of course deserves attention. Feel free to
268 [ask the maintainers][maintainer] about it; even if you figure out the problem
269 yourself, a failed test almost always indicates a problem that is not just on
270 your end.
271
272 ### Regenerating reference images
273
274 Let's say the test `tests/fixtures/reftests/.../foo.svg` failed. Then you
275 fix the bug, or determine that the output image is in fact correct,
276 and it just differs from the reference image due to antialiasing
277 artifacts. In this case, your next step is to regenerate the
278 reference image so the test passes again.
279
280 **You should not just use rsvg-convert to render test files!** The
281 test machinery sets up conditions for [reproducible font
282 rendering](#reproducible-font-rendering), which are not available to
283 rsvg-convert.
284
285 Run `cargo test`, and copy the resulting `foo-out.png` to the
286 `tests/fixtures/.../foo-ref.png` that corresponds to `foo.svg`.
287
288 You can then run `cargo test` again and ensure that the tests pass.
289
290 ### Issues with the official SVG test suite
291
292 Our SVG files in tests/fixtures/reftests/svg1.1 come from the "SVG 1.1
293 Second Edition test suite" archive linked here:
294
295 <https://www.w3.org/Graphics/SVG/WG/wiki/Test_Suite_Overview>
296
297 We don't know how the reference PNG files in that archive are
298 generated. However, they are done in such a way that objects tend not
299 to be pixel-aligned. For example, many tests have a rectangular frame
300 around the whole viewport, defined like this:
301
302 ```text
303 <rect id="test-frame" x="1" y="1" width="478" height="358" fill="none" stroke="#000000"/>
304 ```
305
306 This specifies no stroke width, so it uses 1 by default. The desired
307 effect is "stroke this rectangle with a 1-pixel wide line".
308
309 However, notice that the (x, y) coordinates of the rect are (1, 1).
310 This means that the actual bounds of the stroked outline are from
311 (0.5, 0.5) to (479.5, 359.5). The result is a fuzzy outline: it
312 occupies two pixels of width, with each pixel having half-black
313 coverage.
314
315 Some elements in the reference PNG images from the official SVG test
316 suite are pixel-aligned, and some are not, like the example test-frame
317 above. It looks like their renderer uses a (0.5, 0.5) offset just for
318 strokes, but not for fills, which sounds hackish.
319
320 Our test suite **does not** use special offsets, so that SVG images
321 not from the official test suite are rendered "normally". **This means
322 that the reference images from the official test suite will always
323 fail initially**, since stroke outlines will be fuzzy in librsvg, but
324 not in the test suite (and conversely, SVGs *not* from the test suite
325 would be crisp in librsvg but probably not in the test suite's
326 renderer renderer).
327
328 Also, the original reference PNGs from the SVG 1.1 test suite either
329 use fonts that are different from those usually on free software
330 systems, or they use SVG fonts which librsvg currently doesn't support
331 (i.e. with glyph shapes referenced from a secondary SVG).
332
333 In any case, look at the results by hand, and compare them by eye to
334 the official reference image. If the thing being tested looks
335 correct, and just the outlines are fuzzy — and also it is just the
336 actual font shapes that are different — then the test is probably
337 correct. Follow the procedure as in
338 "[Regenerating reference images](#regenerating-reference-images)"
339 listed above in order to have a reference image suitable for librsvg.
340
341 ### Reproducible font rendering
342
343 The test runners set up special conditions so that font rendering is
344 reproducible across systems. Normally, font rendering can vary quite
345 a bit depending on various factors:
346
347 * Versions of fontconfig, freetype, cairo, and pango.
348 * Installed fonts.
349 * The system's font mappings.
350 * The user's settings for font antialiasing, hinting, etc.
351
352 The test suite includes part of the **Roboto** fonts in
353 `librsvg/tests/resources`, and creates a configuration font map with
354 just those fonts. In addition, the Pango context used for rendering
355 is set up with a hardcoded mode for antialiasing, hinting, and hint
356 metrics.
357
358 [gtest]: https://docs.gtk.org/glib/testing.html
359 [bug]: ../../devel-docs/bugs.rst
360 [pull-requests]: ../../devel-docs/contributing.rst
361 [maintainer]: README.md#maintainers