| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2018 Jiri Svoboda
 | 
|---|
| 3 |  * All rights reserved.
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
| 6 |  * modification, are permitted provided that the following conditions
 | 
|---|
| 7 |  * are met:
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
| 10 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
| 11 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 12 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 13 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
| 14 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
| 15 |  *   derived from this software without specific prior written permission.
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
| 18 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
| 19 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
| 20 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
| 21 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
| 22 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
| 23 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
| 24 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
| 25 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
| 26 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #include <pcut/pcut.h>
 | 
|---|
| 30 | #include <str.h>
 | 
|---|
| 31 | #include <fcntl.h>
 | 
|---|
| 32 | #include <stdio.h>
 | 
|---|
| 33 | #include <stdlib.h>
 | 
|---|
| 34 | #include <unistd.h>
 | 
|---|
| 35 | 
 | 
|---|
| 36 | PCUT_INIT;
 | 
|---|
| 37 | 
 | 
|---|
| 38 | PCUT_TEST_SUITE(stdlib);
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #define MKSTEMP_TEMPL "/tmp/tmp.XXXXXX"
 | 
|---|
| 41 | #define MKTEMP_BAD_TEMPL "/tmp/tmp.XXXXX"
 | 
|---|
| 42 | #define MKTEMP_SHORT_TEMPL "XXXXX"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | /** mktemp function */
 | 
|---|
| 45 | PCUT_TEST(mktemp)
 | 
|---|
| 46 | {
 | 
|---|
| 47 |         int file;
 | 
|---|
| 48 |         char buf[sizeof(MKSTEMP_TEMPL)];
 | 
|---|
| 49 |         char *p;
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         str_cpy(buf, sizeof(buf), MKSTEMP_TEMPL);
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         p = mktemp(buf);
 | 
|---|
| 54 |         PCUT_ASSERT_TRUE(p == buf);
 | 
|---|
| 55 |         PCUT_ASSERT_TRUE(str_lcmp(p, MKSTEMP_TEMPL,
 | 
|---|
| 56 |             str_length(MKSTEMP_TEMPL) - 6) == 0);
 | 
|---|
| 57 | 
 | 
|---|
| 58 |         file = open(p, O_CREAT | O_EXCL | O_RDWR, 0600);
 | 
|---|
| 59 |         PCUT_ASSERT_TRUE(file >= 0);
 | 
|---|
| 60 |         close(file);
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         (void) unlink(p);
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | /** mktemp function called twice should return different names */
 | 
|---|
| 66 | PCUT_TEST(mktemp_twice)
 | 
|---|
| 67 | {
 | 
|---|
| 68 |         char buf1[sizeof(MKSTEMP_TEMPL)];
 | 
|---|
| 69 |         char buf2[sizeof(MKSTEMP_TEMPL)];
 | 
|---|
| 70 |         char *p;
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         str_cpy(buf1, sizeof(buf1), MKSTEMP_TEMPL);
 | 
|---|
| 73 |         str_cpy(buf2, sizeof(buf2), MKSTEMP_TEMPL);
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         p = mktemp(buf1);
 | 
|---|
| 76 |         PCUT_ASSERT_TRUE(p == buf1);
 | 
|---|
| 77 |         PCUT_ASSERT_TRUE(str_lcmp(p, MKSTEMP_TEMPL,
 | 
|---|
| 78 |             str_length(MKSTEMP_TEMPL) - 6) == 0);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         p = mktemp(buf2);
 | 
|---|
| 81 |         PCUT_ASSERT_TRUE(p == buf2);
 | 
|---|
| 82 |         PCUT_ASSERT_TRUE(str_lcmp(p, MKSTEMP_TEMPL,
 | 
|---|
| 83 |             str_length(MKSTEMP_TEMPL) - 6) == 0);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         PCUT_ASSERT_TRUE(str_cmp(buf1, buf2) != 0);
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | /** mktemp function with malformed template */
 | 
|---|
| 89 | PCUT_TEST(mktemp_bad_templ)
 | 
|---|
| 90 | {
 | 
|---|
| 91 |         char buf[sizeof(MKTEMP_BAD_TEMPL)];
 | 
|---|
| 92 |         char *p;
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         str_cpy(buf, sizeof(buf), MKTEMP_BAD_TEMPL);
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         p = mktemp(buf);
 | 
|---|
| 97 |         PCUT_ASSERT_TRUE(p == buf);
 | 
|---|
| 98 |         PCUT_ASSERT_TRUE(p[0] == '\0');
 | 
|---|
| 99 | }
 | 
|---|
| 100 | 
 | 
|---|
| 101 | /** mktemp function with too short template */
 | 
|---|
| 102 | PCUT_TEST(mktemp_short_templ)
 | 
|---|
| 103 | {
 | 
|---|
| 104 |         char buf[sizeof(MKTEMP_SHORT_TEMPL)];
 | 
|---|
| 105 |         char *p;
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         str_cpy(buf, sizeof(buf), MKTEMP_SHORT_TEMPL);
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         p = mktemp(buf);
 | 
|---|
| 110 |         PCUT_ASSERT_TRUE(p == buf);
 | 
|---|
| 111 |         PCUT_ASSERT_TRUE(p[0] == '\0');
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | /** mkstemp function */
 | 
|---|
| 115 | PCUT_TEST(mkstemp)
 | 
|---|
| 116 | {
 | 
|---|
| 117 |         int file;
 | 
|---|
| 118 |         char buf[sizeof(MKSTEMP_TEMPL)];
 | 
|---|
| 119 |         ssize_t rc;
 | 
|---|
| 120 |         char c;
 | 
|---|
| 121 | 
 | 
|---|
| 122 |         str_cpy(buf, sizeof(buf), MKSTEMP_TEMPL);
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         file = mkstemp(buf);
 | 
|---|
| 125 |         PCUT_ASSERT_TRUE(file >= 0);
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         (void) unlink(buf);
 | 
|---|
| 128 | 
 | 
|---|
| 129 |         c = 'x';
 | 
|---|
| 130 |         rc = write(file, &c, sizeof(c));
 | 
|---|
| 131 |         PCUT_ASSERT_TRUE(rc == sizeof(c));
 | 
|---|
| 132 | 
 | 
|---|
| 133 |         (void) lseek(file, 0, SEEK_SET);
 | 
|---|
| 134 |         c = '\0';
 | 
|---|
| 135 |         rc = read(file, &c, sizeof(c));
 | 
|---|
| 136 |         PCUT_ASSERT_TRUE(rc == sizeof(c));
 | 
|---|
| 137 |         PCUT_ASSERT_INT_EQUALS('x', c);
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         close(file);
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | PCUT_EXPORT(stdlib);
 | 
|---|