]> git.proxmox.com Git - libgit2.git/blobdiff - tests/core/posix.c
New upstream version 1.0.0+dfsg.1
[libgit2.git] / tests / core / posix.c
index 1cef937cdb0a81a9182e6ea4a2a675811b547a93..1bb1e9c6b1d539be47e2e881a75718e59a88836d 100644 (file)
@@ -10,6 +10,7 @@
 #endif
 
 #include "clar_libgit2.h"
+#include "futils.h"
 #include "posix.h"
 
 void test_core_posix__initialize(void)
@@ -39,7 +40,7 @@ void test_core_posix__inet_pton(void)
        struct in_addr addr;
        struct in6_addr addr6;
        size_t i;
-       
+
        struct in_addr_data {
                const char *p;
                const uint8_t n[4];
@@ -91,9 +92,147 @@ void test_core_posix__inet_pton(void)
        cl_assert(p_inet_pton(AF_INET, "10.foo.bar.1", &addr) == 0);
 
        /* Test unsupported address families */
-       cl_git_fail(p_inet_pton(12, "52.472", NULL)); /* AF_DECnet */
+       cl_git_fail(p_inet_pton(INT_MAX-1, "52.472", &addr));
        cl_assert_equal_i(EAFNOSUPPORT, errno);
+}
 
-       cl_git_fail(p_inet_pton(5, "315.124", NULL)); /* AF_CHAOS */
-       cl_assert_equal_i(EAFNOSUPPORT, errno);
+void test_core_posix__utimes(void)
+{
+       struct p_timeval times[2];
+       struct stat st;
+       time_t curtime;
+       int fd;
+
+       /* test p_utimes */
+       times[0].tv_sec = 1234567890;
+       times[0].tv_usec = 0;
+       times[1].tv_sec = 1234567890;
+       times[1].tv_usec = 0;
+
+       cl_git_mkfile("foo", "Dummy file.");
+       cl_must_pass(p_utimes("foo", times));
+
+       cl_must_pass(p_stat("foo", &st));
+       cl_assert_equal_i(1234567890, st.st_atime);
+       cl_assert_equal_i(1234567890, st.st_mtime);
+
+
+       /* test p_futimes */
+       times[0].tv_sec = 1414141414;
+       times[0].tv_usec = 0;
+       times[1].tv_sec = 1414141414;
+       times[1].tv_usec = 0;
+
+       cl_must_pass(fd = p_open("foo", O_RDWR));
+       cl_must_pass(p_futimes(fd, times));
+       cl_must_pass(p_close(fd));
+
+       cl_must_pass(p_stat("foo", &st));
+       cl_assert_equal_i(1414141414, st.st_atime);
+       cl_assert_equal_i(1414141414, st.st_mtime);
+
+
+       /* test p_utimes with current time, assume that
+        * it takes < 5 seconds to get the time...!
+        */
+       cl_must_pass(p_utimes("foo", NULL));
+
+       curtime = time(NULL);
+       cl_must_pass(p_stat("foo", &st));
+       cl_assert((st.st_atime - curtime) < 5);
+       cl_assert((st.st_mtime - curtime) < 5);
+
+       cl_must_pass(p_unlink("foo"));
+}
+
+void test_core_posix__unlink_removes_symlink(void)
+{
+       if (!git_path_supports_symlinks(clar_sandbox_path()))
+               clar__skip();
+
+       cl_git_mkfile("file", "Dummy file.");
+       cl_git_pass(git_futils_mkdir("dir", 0777, 0));
+
+       cl_must_pass(p_symlink("file", "file-symlink"));
+       cl_must_pass(p_symlink("dir", "dir-symlink"));
+
+       cl_must_pass(p_unlink("file-symlink"));
+       cl_must_pass(p_unlink("dir-symlink"));
+
+       cl_assert(git_path_exists("file"));
+       cl_assert(git_path_exists("dir"));
+
+       cl_must_pass(p_unlink("file"));
+       cl_must_pass(p_rmdir("dir"));
+}
+
+void test_core_posix__symlink_resolves_to_correct_type(void)
+{
+       git_buf contents = GIT_BUF_INIT;
+
+       if (!git_path_supports_symlinks(clar_sandbox_path()))
+               clar__skip();
+
+       cl_must_pass(git_futils_mkdir("dir", 0777, 0));
+       cl_must_pass(git_futils_mkdir("file", 0777, 0));
+       cl_git_mkfile("dir/file", "symlink target");
+
+       cl_git_pass(p_symlink("file", "dir/link"));
+
+       cl_git_pass(git_futils_readbuffer(&contents, "dir/file"));
+       cl_assert_equal_s(contents.ptr, "symlink target");
+
+       cl_must_pass(p_unlink("dir/link"));
+       cl_must_pass(p_unlink("dir/file"));
+       cl_must_pass(p_rmdir("dir"));
+       cl_must_pass(p_rmdir("file"));
+
+       git_buf_dispose(&contents);
+}
+
+void test_core_posix__relative_symlink(void)
+{
+       git_buf contents = GIT_BUF_INIT;
+
+       if (!git_path_supports_symlinks(clar_sandbox_path()))
+               clar__skip();
+
+       cl_must_pass(git_futils_mkdir("dir", 0777, 0));
+       cl_git_mkfile("file", "contents");
+       cl_git_pass(p_symlink("../file", "dir/link"));
+       cl_git_pass(git_futils_readbuffer(&contents, "dir/link"));
+       cl_assert_equal_s(contents.ptr, "contents");
+
+       cl_must_pass(p_unlink("file"));
+       cl_must_pass(p_unlink("dir/link"));
+       cl_must_pass(p_rmdir("dir"));
+
+       git_buf_dispose(&contents);
+}
+
+void test_core_posix__symlink_to_file_across_dirs(void)
+{
+       git_buf contents = GIT_BUF_INIT;
+
+       if (!git_path_supports_symlinks(clar_sandbox_path()))
+               clar__skip();
+
+       /*
+        * Create a relative symlink that points into another
+        * directory. This used to not work on Win32, where we
+        * forgot to convert directory separators to
+        * Windows-style ones.
+        */
+       cl_must_pass(git_futils_mkdir("dir", 0777, 0));
+       cl_git_mkfile("dir/target", "symlink target");
+       cl_git_pass(p_symlink("dir/target", "link"));
+
+       cl_git_pass(git_futils_readbuffer(&contents, "dir/target"));
+       cl_assert_equal_s(contents.ptr, "symlink target");
+
+       cl_must_pass(p_unlink("dir/target"));
+       cl_must_pass(p_unlink("link"));
+       cl_must_pass(p_rmdir("dir"));
+
+       git_buf_dispose(&contents);
 }