Home | History | Annotate | Download | only in mq_open
      1 /*
      2  * Copyright (c) 2003, 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  * Test that mq_open() fails with EINVAL if O_CREAT was set in oflag,
     11  * attr != NULL, and either mq_maxmsg <= 0 or mq_msgsize <= 0.
     12  */
     13 
     14 #include <stdio.h>
     15 #include <mqueue.h>
     16 #include <fcntl.h>
     17 #include <sys/stat.h>
     18 #include <sys/types.h>
     19 #include <unistd.h>
     20 #include <string.h>
     21 #include <stdint.h>
     22 #include <errno.h>
     23 #include "posixtest.h"
     24 
     25 #define NAMESIZE 50
     26 #define VALIDVAL 10
     27 
     28 #define NUMTESTS 3
     29 
     30 static int invalid_values[NUMTESTS] = { 0, -1, INT32_MIN };
     31 
     32 int main(void)
     33 {
     34 	char qname[NAMESIZE];
     35 	mqd_t queue;
     36 	struct mq_attr attr;
     37 	int i, failed = 0;
     38 
     39 	sprintf(qname, "/mq_open_25-1_%d", getpid());
     40 
     41 	attr.mq_msgsize = VALIDVAL;
     42 	for (i = 0; i < NUMTESTS; i++) {
     43 		attr.mq_maxmsg = invalid_values[i];
     44 		queue = mq_open(qname, O_CREAT | O_RDWR,
     45 				S_IRUSR | S_IWUSR, &attr);
     46 		if (queue != (mqd_t) - 1) {
     47 			printf("mq_open() succeeded w/invalid mq_maxmsg %ld\n",
     48 			       attr.mq_maxmsg);
     49 			mq_close(queue);
     50 			mq_unlink(qname);
     51 			failed++;
     52 #ifdef DEBUG
     53 		} else {
     54 			printf("mq_maxmsg %ld failed as expected\n",
     55 			       attr.mq_maxmsg);
     56 #endif
     57 		}
     58 
     59 		if (errno != EINVAL) {
     60 			printf("errno != EINVAL for mq_maxmsg %ld\n",
     61 			       attr.mq_maxmsg);
     62 			failed++;
     63 #ifdef DEBUG
     64 		} else {
     65 			printf("mq_maxmsg %ld set errno==EINVAL as expected\n",
     66 			       attr.mq_maxmsg);
     67 #endif
     68 		}
     69 	}
     70 
     71 	attr.mq_maxmsg = VALIDVAL;
     72 	for (i = 0; i < NUMTESTS; i++) {
     73 		attr.mq_msgsize = invalid_values[i];
     74 		queue = mq_open(qname, O_CREAT | O_RDWR,
     75 				S_IRUSR | S_IWUSR, &attr);
     76 		if (queue != (mqd_t) - 1) {
     77 			printf("mq_open() succeeded w/invalid mq_msgsize %ld\n",
     78 			       attr.mq_msgsize);
     79 			mq_close(queue);
     80 			mq_unlink(qname);
     81 			failed++;
     82 #ifdef DEBUG
     83 		} else {
     84 			printf("mq_msgsize %ld failed as expected\n",
     85 			       attr.mq_msgsize);
     86 #endif
     87 		}
     88 
     89 		if (errno != EINVAL) {
     90 			printf("errno != EINVAL for mq_msgsize %ld\n",
     91 			       attr.mq_msgsize);
     92 			failed++;
     93 #ifdef DEBUG
     94 		} else {
     95 			printf("mq_msgsize %ld set errno==EINVAL as expected\n",
     96 			       attr.mq_msgsize);
     97 #endif
     98 		}
     99 	}
    100 
    101 	if (failed > 0) {
    102 		printf("Test FAILED\n");
    103 		return PTS_FAIL;
    104 	}
    105 
    106 	printf("Test PASSED\n");
    107 	return PTS_PASS;
    108 }
    109