MCPcopy Create free account
hub / github.com/FastLED/FastLED / FL_TEST_FILE

Function FL_TEST_FILE

tests/fl/asset/asset.cpp:11–123  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9#include "fl/stl/static_assert.h"
10
11FL_TEST_FILE(FL_FILEPATH) {
12
13using namespace fl;
14
15// -----------------------------------------------------------------------------
16// Compile-time: path_has_parent_segment rejects ".." segments but allows
17// innocent-looking paths that merely contain dot characters.
18// -----------------------------------------------------------------------------
19
20FL_STATIC_ASSERT(!fl::asset_detail::path_has_parent_segment("data/track.mp3"),
21 "plain relative path should not be flagged");
22FL_STATIC_ASSERT(!fl::asset_detail::path_has_parent_segment("track.mp3"),
23 "bare file should not be flagged");
24FL_STATIC_ASSERT(!fl::asset_detail::path_has_parent_segment("folder.with.dots/a.mp3"),
25 "dots inside segments must not trigger false positive");
26FL_STATIC_ASSERT(!fl::asset_detail::path_has_parent_segment("."),
27 "single dot is not a parent segment");
28FL_STATIC_ASSERT(!fl::asset_detail::path_has_parent_segment(""),
29 "empty path is not a parent segment");
30
31FL_STATIC_ASSERT(fl::asset_detail::path_has_parent_segment(".."),
32 "bare .. must be rejected");
33FL_STATIC_ASSERT(fl::asset_detail::path_has_parent_segment("../foo"),
34 "leading .. must be rejected");
35FL_STATIC_ASSERT(fl::asset_detail::path_has_parent_segment("foo/.."),
36 "trailing .. must be rejected");
37FL_STATIC_ASSERT(fl::asset_detail::path_has_parent_segment("data/../foo.mp3"),
38 "embedded .. must be rejected");
39FL_STATIC_ASSERT(fl::asset_detail::path_has_parent_segment("a/b/../c"),
40 "deep embedded .. must be rejected");
41// Windows-style separators are treated the same.
42FL_STATIC_ASSERT(fl::asset_detail::path_has_parent_segment("data\\..\\foo.mp3"),
43 "backslash-style .. must also be rejected");
44
45// FL_ASSET() macro returns a valid handle at compile time when the path is OK.
46// The static_assert inside FL_ASSET fires only for ".." paths so this compiles.
47FL_TEST_CASE("fl::asset - FL_ASSET macro produces a valid handle") {
48 asset_ref r = FL_ASSET("data/track.mp3");
49 FL_CHECK(bool(r));
50 FL_CHECK_EQ(r.path(), string_view("data/track.mp3"));
51 FL_CHECK_EQ(r.size(), string_view("data/track.mp3").size());
52}
53
54// The constexpr-callable `fl::asset()` function returns an EMPTY handle when
55// the path contains "..", giving a clean runtime miss instead of a crash.
56FL_TEST_CASE("fl::asset - runtime path with .. produces empty handle") {
57 asset_ref bad = fl::asset("data/../etc/passwd");
58 FL_CHECK_FALSE(bool(bad));
59 FL_CHECK_EQ(bad.size(), 0u);
60}
61
62FL_TEST_CASE("fl::asset - plain path produces non-empty handle") {
63 asset_ref r = fl::asset("data/track.mp3");
64 FL_CHECK(bool(r));
65 FL_CHECK_EQ(r.path(), string_view("data/track.mp3"));
66}
67
68// -----------------------------------------------------------------------------

Callers

nothing calls this directly

Calls 12

path_has_parent_segmentFunction · 0.85
assetFunction · 0.85
register_assetFunction · 0.85
urlClass · 0.85
resolve_assetFunction · 0.85
hostMethod · 0.80
string_viewClass · 0.50
pathMethod · 0.45
sizeMethod · 0.45
isValidMethod · 0.45
findMethod · 0.45
stringMethod · 0.45

Tested by

no test coverage detected