Home | History | Annotate | Download | only in mq_send
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7  */
      8 
      9 /*
     10  * Basic test that mq_send() actually places a message into mqdes.  Verify
     11  * by ensuring the message can be received.
     12  *
     13  * 3/13/03 - Added fix from Gregoire Pichon for specifying an attr
     14  *           with a mq_maxmsg >= BUFFER.
     15  */
     16 
     17 #include <stdio.h>
     18 #include <mqueue.h>
     19 #include <fcntl.h>
     20 #include <sys/stat.h>
     21 #include <sys/types.h>
     22 #include <unistd.h>
     23 #include <string.h>
     24 #include "posixtest.h"
     25 
     26 #define NAMESIZE 50
     27 #define MSGSTR "0123456789"
     28 #define BUFFER 40
     29 #define MAXMSG 10
     30 
     31 int main(void)
     32 {
     33 	char qname[NAMESIZE], msgrcd[BUFFER];
     34 	const char *msgptr = MSGSTR;
     35 	mqd_t queue;
     36 	struct mq_attr attr;
     37 	int unresolved = 0, failure = 0;
     38 	unsigned pri;
     39 
     40 	sprintf(qname, "/mq_send_1-1_%d", getpid());
     41 
     42 	attr.mq_msgsize = BUFFER;
     43 	attr.mq_maxmsg = MAXMSG;
     44 	queue = mq_open(qname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
     45 	if (queue == (mqd_t) - 1) {
     46 		perror("mq_open() did not return success");
     47 		return PTS_UNRESOLVED;
     48 	}
     49 
     50 	if (mq_send(queue, msgptr, strlen(msgptr), 1) != 0) {
     51 		perror("mq_send() did not return success");
     52 		failure = 1;
     53 	}
     54 
     55 	if (mq_receive(queue, msgrcd, BUFFER, &pri) == -1) {
     56 		perror("mq_receive() returned failure");
     57 		failure = 1;
     58 	}
     59 
     60 	if (strncmp(msgptr, msgrcd, strlen(msgptr)) != 0) {
     61 		printf("FAIL:  sent %s received %s\n", msgptr, msgrcd);
     62 		failure = 1;
     63 	}
     64 
     65 	if (mq_close(queue) != 0) {
     66 		perror("mq_close() did not return success");
     67 		unresolved = 1;
     68 	}
     69 
     70 	if (mq_unlink(qname) != 0) {
     71 		perror("mq_unlink() did not return success");
     72 		unresolved = 1;
     73 	}
     74 
     75 	if (failure == 1) {
     76 		printf("Test FAILED\n");
     77 		return PTS_FAIL;
     78 	}
     79 
     80 	if (unresolved == 1) {
     81 		printf("Test UNRESOLVED\n");
     82 		return PTS_UNRESOLVED;
     83 	}
     84 
     85 	printf("Test PASSED\n");
     86 	return PTS_PASS;
     87 }
     88