1 /*
2  * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <assert.h>
7 #include <stdint.h>
8 #include <stddef.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <sys/param.h>
14 #include <stdlib.h>
15 #include "unity.h"
16 #include "unity_fixture.h"
17 
18 // unity_fixture_malloc_overrides.h defines 'free' as 'unity_free',
19 // which can only handle pointers allocated with 'unity_malloc'.
20 // This test allocates memory via 'posix_memalign' so calling 'free'
21 // for these pointers causes the heap guards check to fail.
22 #undef free
23 
24 TEST_GROUP(misc);
25 
TEST_SETUP(misc)26 TEST_SETUP(misc)
27 {
28 }
29 
TEST_TEAR_DOWN(misc)30 TEST_TEAR_DOWN(misc)
31 {
32 }
33 
TEST(misc,posix_memalign)34 TEST(misc, posix_memalign)
35 {
36     void* outptr;
37     int ret;
38 
39     ret = posix_memalign(&outptr, 4, 0);
40     TEST_ASSERT_EQUAL_PTR(NULL, outptr);
41     TEST_ASSERT_EQUAL_INT(ret, 0);
42 
43     void* magic = (void*) 0xEFEFEFEF;
44     outptr = magic;
45     ret = posix_memalign(&outptr, 0x10000000, 64);  // too big alignment - should fail on all targets
46     TEST_ASSERT_EQUAL_INT(ret, ENOMEM);
47     TEST_ASSERT_EQUAL_PTR(magic, outptr);  // was not modified
48 
49     outptr = magic;
50     ret = posix_memalign(&outptr, 16, 0x10000000);  // too big size - should fail on all targets
51     TEST_ASSERT_EQUAL_INT(ret, ENOMEM);
52     TEST_ASSERT_EQUAL_PTR(magic, outptr);  // was not modified
53 
54     outptr = magic;
55     ret = posix_memalign(&outptr, 16, 64);
56     TEST_ASSERT_TRUE(outptr != magic);
57     TEST_ASSERT_NOT_NULL(outptr);
58     TEST_ASSERT_EQUAL_INT(ret, 0);
59     free(outptr);
60 }
61 
TEST(misc,sysconf)62 TEST(misc, sysconf)
63 {
64     TEST_ASSERT_NOT_EQUAL(-1, sysconf(_SC_NPROCESSORS_ONLN));
65 }
66 
TEST(misc,realpath)67 TEST(misc, realpath)
68 {
69     char out[PATH_MAX];
70 
71     TEST_ASSERT_EQUAL_STRING("/", realpath("/", out));
72     TEST_ASSERT_EQUAL_STRING("/", realpath("//", out));
73     TEST_ASSERT_EQUAL_STRING("/", realpath(".", out));
74     TEST_ASSERT_EQUAL_STRING("/", realpath("./", out));
75     TEST_ASSERT_EQUAL_STRING("/", realpath("/.", out));
76     TEST_ASSERT_EQUAL_STRING("/", realpath("./.", out));
77     TEST_ASSERT_EQUAL_STRING("/", realpath("..", out));
78     TEST_ASSERT_EQUAL_STRING("/", realpath("../..", out));
79     TEST_ASSERT_EQUAL_STRING("/a", realpath("/a/", out));
80     TEST_ASSERT_EQUAL_STRING("/", realpath("/a/..", out));
81     TEST_ASSERT_EQUAL_STRING("/", realpath("/a/../..", out));
82     TEST_ASSERT_EQUAL_STRING("/c", realpath("/a/../b/../c", out));
83     TEST_ASSERT_EQUAL_STRING("/abc/def", realpath("/abc/./def/ghi/..", out));
84     char* out_new = realpath("/abc/./def/ghi/..", NULL);
85     TEST_ASSERT_NOT_NULL(out_new);
86     TEST_ASSERT_EQUAL_STRING("/abc/def", out_new);
87     free(out_new);
88 }
89 
TEST_GROUP_RUNNER(misc)90 TEST_GROUP_RUNNER(misc)
91 {
92     RUN_TEST_CASE(misc, posix_memalign)
93     RUN_TEST_CASE(misc, sysconf)
94     RUN_TEST_CASE(misc, realpath)
95 }
96