Skip to content

Commit f5bdf97

Browse files
committed
Add test before changes.
1 parent b382177 commit f5bdf97

3 files changed

Lines changed: 207 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,9 @@ if(UNIX OR MSVC)
631631
add_executable(z_collections_test ${PROJECT_SOURCE_DIR}/tests/z_collections_test.c)
632632
add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c)
633633
add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c)
634+
if(PICO_SHARED)
635+
add_executable(z_iosli_memory_test ${PROJECT_SOURCE_DIR}/tests/z_iosli_memory_test.c)
636+
endif()
634637
add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c)
635638
add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c)
636639
add_executable(z_api_null_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_null_drop_test.c)
@@ -660,6 +663,9 @@ if(UNIX OR MSVC)
660663
target_link_libraries(z_collections_test zenohpico::lib)
661664
target_link_libraries(z_endpoint_test zenohpico::lib)
662665
target_link_libraries(z_iobuf_test zenohpico::lib)
666+
if(PICO_SHARED)
667+
target_link_libraries(z_iosli_memory_test zenohpico::lib)
668+
endif()
663669
target_link_libraries(z_msgcodec_test zenohpico::lib)
664670
target_link_libraries(z_keyexpr_test zenohpico::lib)
665671
target_link_libraries(z_api_null_drop_test zenohpico::lib)
@@ -709,6 +715,9 @@ if(UNIX OR MSVC)
709715
add_test(z_collections_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_collections_test)
710716
add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test)
711717
add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test)
718+
if(PICO_SHARED)
719+
add_test(z_iosli_memory_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iosli_test)
720+
endif()
712721
add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test)
713722
add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test)
714723
add_test(z_api_null_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_null_drop_test)

tests/z_iobuf_test.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,95 @@ void test_wbuf_wrap_bytes(void) {
609609
printf("Ok\n");
610610
}
611611

612+
static void fill_pattern(uint8_t *buf, size_t n) {
613+
for (size_t i = 0; i < n; i++) {
614+
buf[i] = (uint8_t)(0xA0u + (uint8_t)i);
615+
}
616+
}
617+
618+
void test_copy_bytes_readable_region(void) {
619+
_z_iosli_t src = _z_iosli_make(16);
620+
_z_iosli_t dst = _z_iosli_make(16);
621+
assert(src._buf);
622+
assert(dst._buf);
623+
624+
fill_pattern(src._buf, src._capacity);
625+
626+
// Make readable window be [r_pos..w_pos) = [5..12) => length 7
627+
src._r_pos = 5;
628+
src._w_pos = 12;
629+
630+
// Dirty dst positions to ensure function overwrites them
631+
dst._r_pos = 3;
632+
dst._w_pos = 9;
633+
memset(dst._buf, 0x00, dst._capacity);
634+
635+
_z_iosli_copy_bytes(&dst, &src);
636+
637+
// dst now contains exactly the readable bytes starting at offset 0
638+
assert(dst._r_pos == 0);
639+
assert(dst._w_pos == 7);
640+
assert(memcmp(dst._buf, src._buf + src._r_pos, 7) == 0);
641+
642+
_z_iosli_clear(&src);
643+
_z_iosli_clear(&dst);
644+
}
645+
646+
void test_copy_bytes_exact_capacity_fit(void) {
647+
_z_iosli_t src = _z_iosli_make(32);
648+
assert(src._buf);
649+
650+
// Create a readable region length = 10
651+
src._r_pos = 2;
652+
src._w_pos = 12; // len = 10
653+
fill_pattern(src._buf, src._capacity);
654+
655+
_z_iosli_t dst = _z_iosli_make(10);
656+
assert(dst._buf);
657+
memset(dst._buf, 0xEE, dst._capacity);
658+
659+
// Should succeed because dst capacity == readable length
660+
_z_iosli_copy_bytes(&dst, &src);
661+
662+
assert(dst._r_pos == 0);
663+
assert(dst._w_pos == 10);
664+
assert(memcmp(dst._buf, src._buf + src._r_pos, 10) == 0);
665+
666+
_z_iosli_clear(&src);
667+
_z_iosli_clear(&dst);
668+
}
669+
670+
void test_copy_bytes_does_not_alias(void) {
671+
_z_iosli_t src = _z_iosli_make(16);
672+
_z_iosli_t dst = _z_iosli_make(16);
673+
assert(src._buf && dst._buf);
674+
675+
fill_pattern(src._buf, src._capacity);
676+
src._r_pos = 4;
677+
src._w_pos = 9; // len = 5
678+
679+
_z_iosli_copy_bytes(&dst, &src);
680+
681+
assert(dst._r_pos == 0);
682+
assert(dst._w_pos == 5);
683+
assert(memcmp(dst._buf, src._buf + src._r_pos, 5) == 0);
684+
685+
// Mutate src readable region; dst should remain unchanged (deep copy)
686+
src._buf[src._r_pos + 0] ^= 0xFF;
687+
src._buf[src._r_pos + 1] ^= 0xFF;
688+
689+
// Should differ
690+
assert(memcmp(dst._buf, src._buf + src._r_pos, 5) != 0);
691+
692+
// But dst still equals original pattern:
693+
uint8_t expected_full[16];
694+
fill_pattern(expected_full, sizeof(expected_full));
695+
assert(memcmp(dst._buf, expected_full + 4, 5) == 0);
696+
697+
_z_iosli_clear(&src);
698+
_z_iosli_clear(&dst);
699+
}
700+
612701
/*=============================*/
613702
/* Main */
614703
/*=============================*/
@@ -634,4 +723,7 @@ int main(void) {
634723
wbuf_reusable_write_zbuf_read();
635724
}
636725
test_wbuf_wrap_bytes();
726+
test_copy_bytes_readable_region();
727+
test_copy_bytes_exact_capacity_fit();
728+
test_copy_bytes_does_not_alias();
637729
}

tests/z_iosli_memory_test.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// Copyright (c) 2025 ZettaScale Technology
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
//
9+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
//
11+
// Contributors:
12+
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13+
//
14+
15+
#include <assert.h>
16+
#include <stddef.h>
17+
#include <stdlib.h>
18+
#include <string.h>
19+
20+
#include "zenoh-pico/protocol/iobuf.h"
21+
22+
static int g_fail_after = -1;
23+
static int g_calls = 0;
24+
25+
void z_malloc_fail_after(int n) {
26+
g_fail_after = n;
27+
g_calls = 0;
28+
}
29+
30+
void *z_malloc(size_t size) {
31+
if (g_fail_after >= 0 && g_calls++ >= g_fail_after) {
32+
return NULL;
33+
}
34+
return malloc(size);
35+
}
36+
37+
void z_free(void *ptr) { free(ptr); }
38+
39+
static void fill_pattern(uint8_t *buf, size_t n) {
40+
for (size_t i = 0; i < n; i++) {
41+
buf[i] = (uint8_t)(0xA0u + (uint8_t)i);
42+
}
43+
}
44+
45+
void test_iosli_copy_success(void) {
46+
_z_iosli_t src = _z_iosli_make(16);
47+
assert(src._buf != NULL);
48+
assert(src._capacity == 16);
49+
assert(src._is_alloc == true);
50+
assert(src._r_pos == 0);
51+
assert(src._w_pos == 0);
52+
53+
src._r_pos = 2;
54+
src._w_pos = 10;
55+
fill_pattern(src._buf, src._capacity);
56+
57+
_z_iosli_t dst = _z_iosli_null();
58+
_z_iosli_copy(&dst, &src);
59+
60+
assert(dst._capacity == src._capacity);
61+
assert(dst._r_pos == src._r_pos);
62+
assert(dst._w_pos == src._w_pos);
63+
assert(dst._is_alloc == src._is_alloc);
64+
65+
assert(dst._buf != NULL);
66+
assert(dst._buf != src._buf);
67+
assert(memcmp(dst._buf, src._buf, src._capacity) == 0);
68+
69+
_z_iosli_clear(&src);
70+
_z_iosli_clear(&dst);
71+
}
72+
73+
void test_iosli_copy_oom(void) {
74+
_z_iosli_t src = _z_iosli_make(16);
75+
assert(src._buf != NULL);
76+
assert(src._capacity == 16);
77+
assert(src._is_alloc == true);
78+
assert(src._r_pos == 0);
79+
assert(src._w_pos == 0);
80+
81+
// Fill src with recognisable data
82+
src._w_pos = 8;
83+
memset(src._buf, 0xAA, src._capacity);
84+
85+
// Force the next allocation to fail
86+
z_malloc_fail_after(0);
87+
_z_iosli_t dst = _z_iosli_null();
88+
_z_iosli_copy(&dst, &src);
89+
90+
// Allow allocations again
91+
z_malloc_fail_after(-1);
92+
93+
assert(dst._buf == NULL);
94+
assert(dst._capacity == 0);
95+
assert(dst._r_pos == 0);
96+
assert(dst._w_pos == 0);
97+
assert(dst._is_alloc == false);
98+
99+
_z_iosli_clear(&src);
100+
_z_iosli_clear(&dst);
101+
}
102+
103+
int main(void) {
104+
test_iosli_copy_success();
105+
test_iosli_copy_oom();
106+
}

0 commit comments

Comments
 (0)