jolly has uploaded this change for review. (
https://gerrit.osmocom.org/c/libosmocore/+/40854?usp=email )
Change subject: osmo_io: Add unit test to verify segmentation process
......................................................................
osmo_io: Add unit test to verify segmentation process
Change-Id: I7d8feba9c8e8386c9fd144669f6ccd01d6bbbabb
---
M tests/osmo_io/osmo_io_test.c
M tests/osmo_io/osmo_io_test.ok
2 files changed, 127 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/54/40854/1
diff --git a/tests/osmo_io/osmo_io_test.c b/tests/osmo_io/osmo_io_test.c
index 553368c..5d36c05 100644
--- a/tests/osmo_io/osmo_io_test.c
+++ b/tests/osmo_io/osmo_io_test.c
@@ -247,6 +247,113 @@
for (int i = 0; i < 128; i++)
osmo_select_main(1);
}
+
+int segmentation_cb(struct osmo_io_fd *iofd, struct msgb *msg)
+{
+ printf("%s: segmentation_cb() returning %d\n", osmo_iofd_get_name(iofd), 4);
+ return 4;
+}
+
+static void segment_read_cb(struct osmo_io_fd *iofd, int rc, struct msgb *msg)
+{
+ static int seg_number = 0;
+
+ printf("%s: read() msg with rc=%d\n", osmo_iofd_get_name(iofd), rc);
+ if (rc < 0) {
+ printf("%s: error: %s\n", osmo_iofd_get_name(iofd), strerror(-rc));
+ OSMO_ASSERT(0);
+ }
+ OSMO_ASSERT(msg);
+ if (seg_number < 3) {
+ printf("%s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
+ printf("tailroom = %d\n", msgb_tailroom(msg));
+ /* Our read buffer is 6 bytes, Our segment is 4 bytes, this results in tailroom of 2
bytes.
+ * When the pending 2 bytes are combined with subsequent read of 6 bytes, an extra
buffer
+ * with 8 bytes is allocated. Our segment is 4 byte, then this results in a tailroom of
4
+ * bytes. */
+ if (seg_number == 1) {
+ OSMO_ASSERT(msgb_tailroom(msg) == 4);
+ } else {
+ OSMO_ASSERT(msgb_tailroom(msg) == 2);
+ }
+ OSMO_ASSERT(msgb_length(msg) == sizeof(TESTDATA) / 4);
+ seg_number++;
+ } else {
+ OSMO_ASSERT(rc == 0);
+ file_eof_read = true;
+ }
+ talloc_free(msg);
+}
+
+static void test_segmentation(void)
+{
+ struct osmo_io_fd *iofd;
+ struct msgb *msg;
+ uint8_t *buf;
+ int fd[2] = { 0, 0 };
+ int rc;
+ struct osmo_io_ops ioops;
+
+ TEST_START();
+
+ /* Create pipe */
+ rc = pipe(fd);
+ OSMO_ASSERT(rc == 0);
+ OSMO_ASSERT(fd[0]);
+ OSMO_ASSERT(fd[1]);
+
+ /* First test writing to the pipe: */
+ printf("Enable write\n");
+ ioops = (struct osmo_io_ops){ .write_cb = file_write_cb };
+ iofd = osmo_iofd_setup(ctx, fd[1], "seg_iofd", OSMO_IO_FD_MODE_READ_WRITE,
&ioops, NULL);
+ osmo_iofd_register(iofd, fd[1]);
+
+ msg = msgb_alloc(12, "Test data");
+ buf = msgb_put(msg, 12);
+ memcpy(buf, TESTDATA, 12);
+ osmo_iofd_write_msgb(iofd, msg);
+ /* Allow enough cycles to handle the messages */
+ file_bytes_write_compl = 0;
+ for (int i = 0; i < 128; i++) {
+ OSMO_ASSERT(file_bytes_write_compl <= 12);
+ if (file_bytes_write_compl == 12)
+ break;
+ osmo_select_main(1);
+ usleep(100 * 1000);
+ }
+ fflush(stdout);
+ OSMO_ASSERT(file_bytes_write_compl == 12);
+
+ osmo_iofd_unregister(iofd);
+ close(fd[1]);
+
+ /* Now, re-configure iofd to only read from the pipe.
+ * Reduce the read buffer size, to verify correct segmentation operation: */
+ printf("Enable read\n");
+ osmo_iofd_set_alloc_info(iofd, 6, 0);
+ osmo_iofd_register(iofd, fd[0]);
+ ioops = (struct osmo_io_ops){ .read_cb = segment_read_cb, .segmentation_cb2 =
segmentation_cb };
+ rc = osmo_iofd_set_ioops(iofd, &ioops);
+ OSMO_ASSERT(rc == 0);
+ /* Allow enough cycles to handle the message. We expect 3 reads, 4th read will return 0.
*/
+ file_bytes_read = 0;
+ file_eof_read = false;
+ for (int i = 0; i < 128; i++) {
+ if (file_eof_read)
+ break;
+ osmo_select_main(1);
+ usleep(100 * 1000);
+ }
+ fflush(stdout);
+ OSMO_ASSERT(file_eof_read);
+
+ osmo_iofd_free(iofd);
+
+ for (int i = 0; i < 128; i++)
+ osmo_select_main(1);
+}
+
+
static const struct log_info_cat default_categories[] = {
};
@@ -267,6 +374,7 @@
test_file();
test_connected();
test_unconnected();
+ test_segmentation();
return EXIT_SUCCESS;
}
diff --git a/tests/osmo_io/osmo_io_test.ok b/tests/osmo_io/osmo_io_test.ok
index 6b9e591..b24bfd2 100644
--- a/tests/osmo_io/osmo_io_test.ok
+++ b/tests/osmo_io/osmo_io_test.ok
@@ -16,3 +16,22 @@
ep1: sendto() returned rc=16
ep2: recvfrom() msg with len=16
01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10
+Running test_segmentation
+Enable write
+seg_iofd: write() returned rc=12
+01 02 03 04 05 06 07 08 09 0a 0b 0c
+Enable read
+seg_iofd: segmentation_cb() returning 4
+seg_iofd: read() msg with rc=6
+01 02 03 04
+tailroom = 2
+seg_iofd: segmentation_cb() returning 4
+seg_iofd: segmentation_cb() returning 4
+seg_iofd: read() msg with rc=6
+05 06 07 08
+tailroom = 4
+seg_iofd: segmentation_cb() returning 4
+seg_iofd: read() msg with rc=6
+09 0a 0b 0c
+tailroom = 2
+seg_iofd: read() msg with rc=0
--
To view, visit
https://gerrit.osmocom.org/c/libosmocore/+/40854?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I7d8feba9c8e8386c9fd144669f6ccd01d6bbbabb
Gerrit-Change-Number: 40854
Gerrit-PatchSet: 1
Gerrit-Owner: jolly <andreas(a)eversberg.eu>