|
| 1 | +#include <stdexcept> |
| 2 | +#include <string> |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | + |
| 6 | +#include "kickmsg/Naming.h" |
| 7 | + |
| 8 | +using kickmsg::sanitize_shm_component; |
| 9 | + |
| 10 | +// --- Pass-through for already-valid inputs ------------------------------- |
| 11 | + |
| 12 | +TEST(SanitizeShmComponent, PlainAlnumIsUnchanged) |
| 13 | +{ |
| 14 | + EXPECT_EQ(sanitize_shm_component("topic", "topic"), "topic"); |
| 15 | + EXPECT_EQ(sanitize_shm_component("sensor42", "topic"), "sensor42"); |
| 16 | +} |
| 17 | + |
| 18 | +TEST(SanitizeShmComponent, PortableFilenameCharsSurvive) |
| 19 | +{ |
| 20 | + // POSIX portable filename set [A-Za-z0-9._-] must pass through verbatim |
| 21 | + // — this is what allows `ls /dev/shm` output to stay human-readable. |
| 22 | + EXPECT_EQ(sanitize_shm_component("a.b_c-d.42", "topic"), "a.b_c-d.42"); |
| 23 | +} |
| 24 | + |
| 25 | +// --- Leading slash handling (ROS-style absolute names) ------------------- |
| 26 | + |
| 27 | +TEST(SanitizeShmComponent, StripsSingleLeadingSlash) |
| 28 | +{ |
| 29 | + EXPECT_EQ(sanitize_shm_component("/topic", "topic"), "topic"); |
| 30 | +} |
| 31 | + |
| 32 | +TEST(SanitizeShmComponent, StripsRepeatedLeadingSlashes) |
| 33 | +{ |
| 34 | + // "///a" -> "a" (not ".a" or "..a") — otherwise a user who typed "//x" |
| 35 | + // by accident would silently get a different region than "/x". |
| 36 | + EXPECT_EQ(sanitize_shm_component("///topic", "topic"), "topic"); |
| 37 | +} |
| 38 | + |
| 39 | +// --- Interior slash becomes '.' ------------------------------------------ |
| 40 | + |
| 41 | +TEST(SanitizeShmComponent, InteriorSlashBecomesDot) |
| 42 | +{ |
| 43 | + EXPECT_EQ(sanitize_shm_component("robot/arm", "topic"), |
| 44 | + "robot.arm"); |
| 45 | + EXPECT_EQ(sanitize_shm_component("robot/arm/joint1", "topic"), |
| 46 | + "robot.arm.joint1"); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(SanitizeShmComponent, LeadingAndInteriorSlashesCombine) |
| 50 | +{ |
| 51 | + // The leading-slash strip must not fire for interior slashes after |
| 52 | + // real characters have been seen. |
| 53 | + EXPECT_EQ(sanitize_shm_component("/robot/arm/joint1", "topic"), |
| 54 | + "robot.arm.joint1"); |
| 55 | +} |
| 56 | + |
| 57 | +// --- Fallback mapping for everything else -------------------------------- |
| 58 | + |
| 59 | +TEST(SanitizeShmComponent, InvalidCharsBecomeUnderscore) |
| 60 | +{ |
| 61 | + EXPECT_EQ(sanitize_shm_component("hello world", "topic"), "hello_world"); |
| 62 | + EXPECT_EQ(sanitize_shm_component("a:b;c", "topic"), "a_b_c"); |
| 63 | + EXPECT_EQ(sanitize_shm_component("with\ttab", "topic"), "with_tab"); |
| 64 | + EXPECT_EQ(sanitize_shm_component("weird@name!", "topic"), "weird_name_"); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(SanitizeShmComponent, UnicodeBytesBecomeUnderscores) |
| 68 | +{ |
| 69 | + // Multi-byte UTF-8 sequences: each byte fails isalnum and maps to '_'. |
| 70 | + // We don't care about exact width — only that the output stays within |
| 71 | + // the portable set. "é" is 0xC3 0xA9 -> "__". |
| 72 | + auto out = sanitize_shm_component("caf\xC3\xA9", "topic"); |
| 73 | + EXPECT_EQ(out, "caf__"); |
| 74 | +} |
| 75 | + |
| 76 | +// --- Idempotency --------------------------------------------------------- |
| 77 | + |
| 78 | +TEST(SanitizeShmComponent, Idempotent) |
| 79 | +{ |
| 80 | + // Sanitize twice -> same as sanitize once. Matters because Node |
| 81 | + // sanitizes the prefix in the ctor, and make_topic_name could |
| 82 | + // conceivably be called with the prefix as an input somewhere. |
| 83 | + auto once = sanitize_shm_component("/robot/arm joint1", "topic"); |
| 84 | + auto twice = sanitize_shm_component(once, "topic"); |
| 85 | + EXPECT_EQ(once, "robot.arm_joint1"); |
| 86 | + EXPECT_EQ(twice, once); |
| 87 | +} |
| 88 | + |
| 89 | +// --- Error cases --------------------------------------------------------- |
| 90 | + |
| 91 | +TEST(SanitizeShmComponent, EmptyInputThrows) |
| 92 | +{ |
| 93 | + EXPECT_THROW(sanitize_shm_component("", "topic"), std::invalid_argument); |
| 94 | +} |
| 95 | + |
| 96 | +TEST(SanitizeShmComponent, OnlySlashesThrows) |
| 97 | +{ |
| 98 | + // After stripping leading '/'s there is nothing left — same category |
| 99 | + // of error as an empty input. |
| 100 | + EXPECT_THROW(sanitize_shm_component("/", "topic"), std::invalid_argument); |
| 101 | + EXPECT_THROW(sanitize_shm_component("///", "topic"), std::invalid_argument); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(SanitizeShmComponent, WhatIsIncludedInErrorMessage) |
| 105 | +{ |
| 106 | + // The `what` label is the only way the caller can tell which of the |
| 107 | + // several calls in make_topic_name / make_mailbox_name failed — this |
| 108 | + // test pins that contract so refactors don't accidentally drop it. |
| 109 | + try |
| 110 | + { |
| 111 | + sanitize_shm_component("", "namespace"); |
| 112 | + FAIL() << "expected std::invalid_argument"; |
| 113 | + } |
| 114 | + catch (std::invalid_argument const& e) |
| 115 | + { |
| 116 | + std::string msg = e.what(); |
| 117 | + EXPECT_NE(msg.find("namespace"), std::string::npos) << msg; |
| 118 | + } |
| 119 | +} |
0 commit comments