• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Test that clock_settime() sets errno=EINVAL if tp has a nsec value < 0 or
9  * >= 1000 million.
10  *
11  * Test calling clock_settime() with the following tp.tv_nsec values:
12  *   MIN INT = INT32_MIN
13  *   MAX INT = INT32_MAX
14  *   MIN INT - 1 = 2147483647  (this is what gcc will set to)
15  *   MAX INT + 1 = -2147483647 (this is what gcc will set to)
16  *   unassigned value = -1073743192 (ex. of what gcc will set to)
17  *   unassigned value = 1073743192 (ex. of what gcc will set to)
18  *   -1
19  *   1000000000
20  *   1000000001
21  *
22  * The clock_id CLOCK_REALTIME is used.
23  */
24 #include <stdio.h>
25 #include <time.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdint.h>
29 #include "posixtest.h"
30 #include "helpers.h"
31 
32 #define NUMINVALIDTESTS 9
33 
34 static int invalid_tests[NUMINVALIDTESTS] = {
35 	INT32_MIN, INT32_MAX, 2147483647, -2147483647, -1073743192,
36 	1073743192, -1, 1000000000, 1000000001
37 };
38 
main(void)39 int main(void)
40 {
41 	struct timespec tsset, tscurrent, tsreset;
42 	int i;
43 	int failure = 0;
44 
45 	/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
46 	if (getuid() != 0) {
47 		printf("Run this test as ROOT, not as a Regular User\n");
48 		return PTS_UNTESTED;
49 	}
50 
51 	if (clock_gettime(CLOCK_REALTIME, &tscurrent) != 0) {
52 		printf("clock_gettime() did not return success\n");
53 		return PTS_UNRESOLVED;
54 	}
55 
56 	getBeforeTime(&tsreset);
57 	for (i = 0; i < NUMINVALIDTESTS; i++) {
58 		tsset.tv_sec = tscurrent.tv_sec;
59 		tsset.tv_nsec = invalid_tests[i];
60 
61 		printf("Test %d sec %d nsec\n",
62 		       (int)tsset.tv_sec, (int)tsset.tv_nsec);
63 		if (clock_settime(CLOCK_REALTIME, &tsset) == -1) {
64 			if (EINVAL != errno) {
65 				printf("errno != EINVAL\n");
66 				failure = 1;
67 			}
68 		} else {
69 			printf("clock_settime() did not return -1\n");
70 			failure = 1;
71 		}
72 	}
73 
74 	setBackTime(tsreset);
75 	if (failure) {
76 		printf("At least one test FAILED -- see above\n");
77 		return PTS_FAIL;
78 	} else {
79 		printf("All tests PASSED\n");
80 		return PTS_PASS;
81 	}
82 
83 	return PTS_UNRESOLVED;
84 }
85