Skip to content
/ server Public

MDEV-18318 Unit tests for the Json_writer#4731

Open
varundeepsaini wants to merge 1 commit intoMariaDB:10.11from
varundeepsaini:MDEV-18318-json-writer-unit-tests
Open

MDEV-18318 Unit tests for the Json_writer#4731
varundeepsaini wants to merge 1 commit intoMariaDB:10.11from
varundeepsaini:MDEV-18318-json-writer-unit-tests

Conversation

@varundeepsaini
Copy link
Contributor

@varundeepsaini varundeepsaini commented Mar 4, 2026

Summary

Add 104 TAP unit tests for the Json_writer class, plus two small production code fixes discovered during test development.

Unit tests (unittest/sql/my_json_writer-t.cc)

  • Invalid JSON detection: unnamed values in objects, named values in arrays, mismatched open/close
  • Output verification for all value types: strings, integers, unsigned integers, booleans, null, doubles, sizes
  • Edge cases: LLONG_MAX/LLONG_MIN, ULLONG_MAX, special doubles (0.0, -0.0), Latin-1 and UTF-8 mb4 encoded strings, embedded NUL bytes in keys and values, duplicate key names
  • add_member(name, len) explicit key length semantics
  • Single-line formatting helper including long-element fallback and embedded-NUL disable behavior
  • Nested structures: objects in objects, arrays of objects, mixed nesting
  • RAII wrappers (Json_writer_object, Json_writer_array) including ulonglong add overload preserving unsigned range
  • String_with_limit truncation behavior
  • Json_writer size limit enforcement with exact truncation count
  • add_size formatting (bytes, Kb, Mb) with boundary tests
  • Positive-path test for add(name, value, num_bytes) overload

Production code fixes

  1. sql/my_json_writer.h: Fix missing my_writer NULL guard in Json_writer_object::add(const char*, const char*, size_t) which would crash when used with a NULL writer (ignore mode).

  2. sql/my_json_writer.cc: Harden Single_line_formatting_helper against embedded NUL bytes — on_add_member() now rejects keys containing NUL, and on_add_str() disables single-line mode and flushes when a value contains NUL.

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Mar 4, 2026
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! This is a preliminary review.

Please rebase to 10.11: this is enhanced test coverage and as such it should go to the earliest maintained version where the functionality was first pushed.

Please also make sure the commit message of your commit complies with CODING_STANDARDS.md.

Few testing ideas to increase coverage are listed below.

@varundeepsaini varundeepsaini force-pushed the MDEV-18318-json-writer-unit-tests branch from 7717e5e to cab7a58 Compare March 4, 2026 11:41
@CLAassistant
Copy link

CLAassistant commented Mar 4, 2026

CLA assistant check
All committers have signed the CLA.

@varundeepsaini varundeepsaini changed the base branch from main to 10.11 March 4, 2026 11:42
@varundeepsaini varundeepsaini force-pushed the MDEV-18318-json-writer-unit-tests branch from cab7a58 to 1f21dc3 Compare March 4, 2026 13:03
@varundeepsaini varundeepsaini requested a review from gkodinov March 4, 2026 13:04
@varundeepsaini varundeepsaini force-pushed the MDEV-18318-json-writer-unit-tests branch 2 times, most recently from 67a2427 to 6bce763 Compare March 4, 2026 13:06
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Let's leave the rest of it for the final reviewer.

Please stand by for the final review.

@varundeepsaini varundeepsaini force-pushed the MDEV-18318-json-writer-unit-tests branch 2 times, most recently from e68f29a to 7afb4c5 Compare March 4, 2026 17:18
@grooverdan
Copy link
Member

And notably correct the Windows compiling error

       (ClCompile target) -> 
         C:\buildbot\workers\prod\amd64-windows\build\unittest\sql\my_json_writer-t.cc(447,37): error C2124: divide or mod by zero [C:\buildbot\workers\prod\amd64-windows\build\unittest\sql\my_json_writer-t.vcxproj]

@varundeepsaini varundeepsaini force-pushed the MDEV-18318-json-writer-unit-tests branch 8 times, most recently from be7c298 to cb1cf0f Compare March 5, 2026 08:55
Add 104 TAP unit tests for the Json_writer class covering:

- Invalid JSON detection (unnamed values in objects, named values
  in arrays, mismatched open/close)
- Output verification for all value types (strings, integers,
  unsigned integers, booleans, null, doubles, sizes)
- Edge cases: LLONG_MAX/MIN, ULLONG_MAX, special doubles (0.0,
  -0.0), Latin-1 and UTF-8 mb4 encoded strings, embedded NUL
  bytes in keys and values, duplicate key names
- add_member(name, len) explicit key length
- Single-line formatting helper including long-element fallback
  and embedded-NUL disable behavior
- Nested structures (objects in objects, arrays of objects, mixed)
- RAII wrappers (Json_writer_object, Json_writer_array) including
  ulonglong add overload preserving unsigned range
- String_with_limit truncation behavior
- Json_writer size limit enforcement with exact truncation count
- add_size formatting (bytes, Kb, Mb) with boundary tests
- Positive-path test for add(name, value, num_bytes) overload

Also fix a missing my_writer NULL guard in
Json_writer_object::add(const char*, const char*, size_t)
which would crash when used with a NULL writer.

Harden Single_line_formatting_helper against embedded NUL bytes:
on_add_member() now rejects keys containing NUL, and on_add_str()
disables single-line mode and flushes when a value contains NUL.

Signed-off-by: Varun Deep Saini <varun.23bcs10048@ms.sst.scaler.com>
@varundeepsaini varundeepsaini force-pushed the MDEV-18318-json-writer-unit-tests branch from cb1cf0f to 86ed877 Compare March 5, 2026 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

5 participants