blob: 030b26bde529968d1fb8e84e60b1c57970832f67 [file]
// Copyright 2026 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "src/utils/json.h"
#include <string>
#include <string_view>
#include "src/utils/gtest.h"
namespace dawn {
namespace {
using JsonTest = ::testing::Test;
TEST_F(JsonTest, EmptyAndPlainStrings) {
EXPECT_EQ(EscapeJsonString(""), "");
EXPECT_EQ(EscapeJsonString("hello world 123!@#"), "hello world 123!@#");
// Forward slash does not require escaping in JSON.
EXPECT_EQ(EscapeJsonString("a/b/c"), "a/b/c");
}
TEST_F(JsonTest, ShortEscapes) {
EXPECT_EQ(EscapeJsonString("\""), "\\\"");
EXPECT_EQ(EscapeJsonString("\\"), "\\\\");
EXPECT_EQ(EscapeJsonString("\b"), "\\b");
EXPECT_EQ(EscapeJsonString("\f"), "\\f");
EXPECT_EQ(EscapeJsonString("\n"), "\\n");
EXPECT_EQ(EscapeJsonString("\r"), "\\r");
EXPECT_EQ(EscapeJsonString("\t"), "\\t");
EXPECT_EQ(EscapeJsonString("line1\n\"quoted\"\t\\path"), "line1\\n\\\"quoted\\\"\\t\\\\path");
}
TEST_F(JsonTest, ControlCharacters) {
// Embedded NUL byte via string_view with explicit length.
EXPECT_EQ(EscapeJsonString(std::string_view("a\0b", 3)), "a\\u0000b");
EXPECT_EQ(EscapeJsonString("\x01\x07\x0b\x1b\x1f"), "\\u0001\\u0007\\u000b\\u001b\\u001f");
// First printable ASCII (0x20 space) and DEL (0x7f) are not < 0x20.
EXPECT_EQ(EscapeJsonString(" \x7f"), " \x7f");
}
TEST_F(JsonTest, HighBytesAndUtf8PassThrough) {
// UTF-8 multi-byte characters have bytes >= 0x80; signed char negative values must not be
// treated as < 0x20.
const std::string utf8 = "λ — 🚀";
EXPECT_EQ(EscapeJsonString(utf8), utf8);
}
} // namespace
} // namespace dawn