summaryrefslogtreecommitdiff
path: root/test/posix/regression/posix_mq_basic_test.c
blob: 2ecb1ac80b45a866550836d77cad45d124117def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/***************************************************************************/
/* Copyright (c) 2026 Eclipse ThreadX contributors                         */
/*                                                                         */
/* This program and the accompanying materials are made available under    */
/* the terms of the MIT License which is available at                      */
/* https://opensource.org/licenses/MIT.                                    */
/*                                                                         */
/* AI Disclosure: This file was largely AI-generated by Claude Code        */
/* (Opus 5). The AI-generated portions may be considered public domain     */
/* (CC0-1.0) and not subject to the project's licence. The human           */
/* contributor has reviewed and verified that the code is correct.         */
/*                                                                         */
/* SPDX-License-Identifier: MIT and CC0-1.0                                */
/***************************************************************************/

/* This test is designed to test basic POSIX message queue operation: open a
   queue, send a message through it, receive the message back and confirm both
   the contents and the priority survived the round trip.  */

#include <stdio.h>
#include "tx_api.h"
#include "pthread.h"

#define TEST_MESSAGE_SIZE   40
#define TEST_QUEUE_DEPTH    10
#define TEST_PRIORITY       3

/* Memory the POSIX layer carves its own objects out of.  */
static ULONG            posix_region[192 * 1024 / sizeof(ULONG)];

static pthread_t        worker;
static pthread_attr_t   worker_attr;
static mqd_t            queue_des;
static struct mq_attr   queue_attr;

extern void             test_control_return(UINT status);

static VOID            *worker_entry(VOID *input);


/* Define what the initial system looks like.  */

#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void posix_mq_basic_application_define(void *first_unused_memory)
#endif
{

struct sched_param  param;
VOID               *storage_ptr;

    (void) first_unused_memory;

    /* Bring the POSIX layer up.  It returns the first address beyond its own
       structures, which is where pthread stacks are taken from.  */
    storage_ptr = posix_initialize(posix_region);

    queue_attr.mq_maxmsg  = TEST_QUEUE_DEPTH;
    queue_attr.mq_msgsize = TEST_MESSAGE_SIZE;

    queue_des = mq_open("mq_basic", O_CREAT | O_RDWR, 0, &queue_attr);
    if (queue_des == (mqd_t) ERROR)
    {

        printf("Running POSIX Message Queue Basic Test............................. ERROR #1\n");
        test_control_return(1);
    }

    pthread_attr_init(&worker_attr);
    param.sched_priority = 10;
    pthread_attr_setschedparam(&worker_attr, &param);
    pthread_attr_setstackaddr(&worker_attr, storage_ptr);

    if (pthread_create(&worker, &worker_attr, worker_entry, NULL) != OK)
    {

        printf("Running POSIX Message Queue Basic Test............................. ERROR #2\n");
        test_control_return(1);
    }
}


static VOID *worker_entry(VOID *input)
{

CHAR    sent[TEST_MESSAGE_SIZE];
CHAR    received[TEST_MESSAGE_SIZE];
ULONG   received_priority;
ssize_t received_length;
INT     i;

    (void) input;

    printf("Running POSIX Message Queue Basic Test............................. ");

    /* Build a recognisable payload rather than a constant one, so a message
       that is dropped or truncated cannot pass by accident.  */
    for (i = 0; i < TEST_MESSAGE_SIZE; i++)
    {

        sent[i]     = (CHAR) (i + 1);
        received[i] = (CHAR) 0;
    }

    if (mq_send(queue_des, sent, TEST_MESSAGE_SIZE, TEST_PRIORITY) != OK)
    {

        printf("ERROR #3\n");
        test_control_return(1);
    }

    received_priority = 0;
    received_length   = mq_receive(queue_des, received, TEST_MESSAGE_SIZE, &received_priority);

    if (received_length != (ssize_t) TEST_MESSAGE_SIZE)
    {

        printf("ERROR #4\n");
        test_control_return(1);
    }

    if (received_priority != (ULONG) TEST_PRIORITY)
    {

        printf("ERROR #5\n");
        test_control_return(1);
    }

    for (i = 0; i < TEST_MESSAGE_SIZE; i++)
    {

        if (received[i] != sent[i])
        {

            printf("ERROR #6\n");
            test_control_return(1);
        }
    }

    if (mq_close(queue_des) != OK)
    {

        printf("ERROR #7\n");
        test_control_return(1);
    }

    if (mq_unlink("mq_basic") != OK)
    {

        printf("ERROR #8\n");
        test_control_return(1);
    }

    printf("SUCCESS!\n");
    test_control_return(0);

    return (NULL);
}