2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2023-08-16 01:24:53 +00:00
|
|
|
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-08-16 01:24:53 +00:00
|
|
|
#include "libc/calls/struct/dirent.h"
|
2023-08-16 22:53:06 +00:00
|
|
|
#include "libc/errno.h"
|
|
|
|
#include "libc/macros.internal.h"
|
2023-08-16 01:24:53 +00:00
|
|
|
#include "libc/mem/gc.internal.h"
|
2023-08-16 22:53:06 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2023-08-16 01:24:53 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2023-08-16 22:53:06 +00:00
|
|
|
#include "libc/runtime/zipos.internal.h"
|
|
|
|
#include "libc/stdio/stdio.h"
|
2022-06-15 23:19:50 +00:00
|
|
|
#include "libc/str/str.h"
|
2023-08-16 01:24:53 +00:00
|
|
|
#include "libc/sysv/consts/dt.h"
|
|
|
|
#include "libc/testlib/testlib.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-08-16 01:24:53 +00:00
|
|
|
__static_yoink("zipos");
|
|
|
|
__static_yoink("libc/testlib/hyperion.txt");
|
|
|
|
__static_yoink("libc/testlib/moby.txt");
|
|
|
|
__static_yoink("usr/share/zoneinfo/New_York");
|
|
|
|
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *ent;
|
|
|
|
|
|
|
|
TEST(zipdir, test) {
|
|
|
|
const char *path = "/zip/libc/testlib///";
|
|
|
|
ASSERT_NE(NULL, (dir = opendir(path)));
|
|
|
|
ASSERT_EQ(0, telldir(dir));
|
|
|
|
ASSERT_NE(NULL, (ent = readdir(dir)));
|
|
|
|
ASSERT_EQ(0, strcmp(ent->d_name, "."));
|
|
|
|
ASSERT_EQ(DT_DIR, ent->d_type);
|
|
|
|
ASSERT_NE(NULL, (ent = readdir(dir)));
|
|
|
|
ASSERT_EQ(0, strcmp(ent->d_name, ".."));
|
|
|
|
ASSERT_EQ(DT_DIR, ent->d_type);
|
|
|
|
ASSERT_NE(NULL, (ent = readdir(dir)));
|
|
|
|
ASSERT_EQ(0, strcmp(ent->d_name, "hyperion.txt"));
|
|
|
|
ASSERT_EQ(DT_REG, ent->d_type);
|
|
|
|
long pos = telldir(dir);
|
|
|
|
ASSERT_NE(NULL, (ent = readdir(dir)));
|
|
|
|
ASSERT_EQ(0, strcmp(ent->d_name, "moby.txt"));
|
|
|
|
ASSERT_EQ(DT_REG, ent->d_type);
|
|
|
|
ASSERT_EQ(NULL, (ent = readdir(dir)));
|
|
|
|
seekdir(dir, pos);
|
|
|
|
ASSERT_NE(NULL, (ent = readdir(dir)));
|
|
|
|
ASSERT_EQ(0, strcmp(ent->d_name, "moby.txt"));
|
|
|
|
ASSERT_EQ(DT_REG, ent->d_type);
|
|
|
|
ASSERT_EQ(NULL, (ent = readdir(dir)));
|
2023-08-16 14:54:40 +00:00
|
|
|
ASSERT_EQ(NULL, (ent = readdir(dir)));
|
2023-08-16 01:24:53 +00:00
|
|
|
ASSERT_SYS(0, 0, closedir(dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(dirstream, hasDirectoryEntry) {
|
|
|
|
bool gotsome = false;
|
|
|
|
const char *path = "/zip/usr/share/zoneinfo";
|
|
|
|
ASSERT_NE(NULL, (dir = opendir(path)));
|
|
|
|
while ((ent = readdir(dir))) {
|
|
|
|
gotsome = true;
|
2020-08-25 11:23:25 +00:00
|
|
|
}
|
2023-08-16 01:24:53 +00:00
|
|
|
ASSERT_SYS(0, 0, closedir(dir));
|
|
|
|
EXPECT_TRUE(gotsome);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-08-16 22:53:06 +00:00
|
|
|
|
|
|
|
TEST(__zipos_normpath, emptyBuf_wontNulTerminate) {
|
|
|
|
__zipos_normpath(0, "hello", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(__zipos_normpath, overflows_willNulTerminate) {
|
|
|
|
char buf[2];
|
|
|
|
ASSERT_EQ(2, __zipos_normpath(buf, "hello", 2));
|
|
|
|
ASSERT_STREQ("h", buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(__zipos_normpath, vectors) {
|
|
|
|
static const char V[][2][128] = {
|
|
|
|
{"", ""},
|
2023-08-18 14:04:55 +00:00
|
|
|
{"/", ""},
|
2023-08-16 22:53:06 +00:00
|
|
|
{"/..", ""},
|
|
|
|
{"/../", ""},
|
|
|
|
{".", ""},
|
|
|
|
{"./", ""},
|
|
|
|
{"..", ""},
|
|
|
|
{"../", ""},
|
2023-08-17 03:11:19 +00:00
|
|
|
{"foo/", "foo/"},
|
2023-08-16 22:53:06 +00:00
|
|
|
{"../abc/def", "abc/def"},
|
2023-08-17 03:11:19 +00:00
|
|
|
{"../abc/def/..", "abc/"},
|
|
|
|
{"../abc/././././def/..", "abc/"},
|
2023-08-16 22:53:06 +00:00
|
|
|
{"////../abc/def", "abc/def"},
|
|
|
|
{"/../def", "def"},
|
|
|
|
{"../def", "def"},
|
|
|
|
{"/abc////../def", "def"},
|
|
|
|
{"abc/../def/ghi", "def/ghi"},
|
|
|
|
{"/abc/def/../ghi", "abc/ghi"},
|
|
|
|
{"/abc/..abc////../def", "abc/def"},
|
|
|
|
{"/abc/..abc/../def", "abc/def"},
|
|
|
|
{"/abc/..abc/def", "abc/..abc/def"},
|
|
|
|
{"abc/../def", "def"},
|
|
|
|
{"abc/../../def", "def"},
|
|
|
|
{"abc/../../../def", "def"},
|
|
|
|
{"././", ""},
|
|
|
|
{"abc/..", ""},
|
|
|
|
{"abc/../", ""},
|
|
|
|
{"abc/../..", ""},
|
|
|
|
{"abc/../../", ""},
|
|
|
|
{"a/..", ""},
|
|
|
|
{"a/../", ""},
|
|
|
|
{"a/../..", ""},
|
|
|
|
{"a/../../", ""},
|
|
|
|
{"../../a", "a"},
|
|
|
|
{"../../../a", "a"},
|
|
|
|
{"../a../../a", "a"},
|
|
|
|
{"cccc/abc////..////.//../", ""},
|
2023-08-17 03:11:19 +00:00
|
|
|
{"aaaa/cccc/abc////..////.//../", "aaaa/"},
|
|
|
|
{"..//////.///..////..////.//////abc////.////..////def//abc/..", "def/"},
|
2023-08-16 22:53:06 +00:00
|
|
|
{"////////////..//////.///..////..////.//////abc////.////..////def//abc/"
|
|
|
|
"..",
|
2023-08-17 03:11:19 +00:00
|
|
|
"def/"},
|
2023-08-16 22:53:06 +00:00
|
|
|
};
|
|
|
|
int fails = 0;
|
|
|
|
|
|
|
|
// test non-overlapping arguments
|
|
|
|
// duplicate input string for better asan checking
|
|
|
|
for (int i = 0; i < ARRAYLEN(V); ++i) {
|
|
|
|
char tmp[128];
|
|
|
|
__zipos_normpath(tmp, gc(strdup(V[i][0])), sizeof(tmp));
|
|
|
|
if (strcmp(tmp, V[i][1])) {
|
|
|
|
if (++fails < 8) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n%s: zipos path normalization test failed\n"
|
|
|
|
"\tinput = %`'s\n"
|
|
|
|
"\t want = %`'s\n"
|
|
|
|
"\t got = %`'s\n"
|
|
|
|
"\t i = %d\n",
|
|
|
|
program_invocation_name, V[i][0], V[i][1], tmp, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test overlapping arguments
|
|
|
|
if (!fails) {
|
|
|
|
for (int i = 0; i < ARRAYLEN(V); ++i) {
|
|
|
|
char tmp[128];
|
|
|
|
strcpy(tmp, V[i][0]);
|
|
|
|
__zipos_normpath(tmp, tmp, sizeof(tmp));
|
|
|
|
if (strcmp(tmp, V[i][1])) {
|
|
|
|
if (++fails < 8) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n%s: zipos path normalization breaks w/ overlapping args\n"
|
|
|
|
"\tinput = %`'s\n"
|
|
|
|
"\t want = %`'s\n"
|
|
|
|
"\t got = %`'s\n"
|
|
|
|
"\t i = %d\n",
|
|
|
|
program_invocation_name, V[i][0], V[i][1], tmp, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fails) {
|
|
|
|
fprintf(stderr, "\n%d / %zd zipos path norm tests failed\n", fails,
|
|
|
|
ARRAYLEN(V));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|