[kdbus] Add SipHash algorithm
[platform/upstream/glib.git] / tests / file-test.c
index adab1f2..0a3316f 100644 (file)
@@ -12,9 +12,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
@@ -24,8 +22,6 @@
  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include "config.h"
-
 #undef G_DISABLE_ASSERT
 #undef G_LOG_DOMAIN
 
 
 #include <gstdio.h>
 
-#ifdef HAVE_UNISTD_H
+#include <fcntl.h>             /* For open() */
+
+#ifdef G_OS_UNIX
 #include <unistd.h>
 #endif
-
 #ifdef G_OS_WIN32
 #include <io.h>                        /* For read(), write() etc */
 #endif
@@ -60,14 +57,18 @@ test_mkstemp (void)
   strcpy (template, "foobar");
   fd = g_mkstemp (template);
   if (fd != -1)
-    g_warning ("g_mkstemp works even if template doesn't contain XXXXXX");
-  close (fd);
+    {
+      g_warning ("g_mkstemp works even if template doesn't contain XXXXXX");
+      close (fd);
+    }
 
   strcpy (template, "foobarXXX");
   fd = g_mkstemp (template);
   if (fd != -1)
-    g_warning ("g_mkstemp works even if template contains less than six X");
-  close (fd);
+    {
+      g_warning ("g_mkstemp works even if template contains less than six X");
+      close (fd);
+    }
 
   strcpy (template, "fooXXXXXX");
   fd = g_mkstemp (template);
@@ -96,6 +97,55 @@ test_mkstemp (void)
 }
 
 static void
+test_mkdtemp (void)
+{
+  char template[32], *retval;
+  int fd;
+  int i;
+
+  strcpy (template, "foodir");
+  retval = g_mkdtemp (template);
+  if (retval != NULL)
+    {
+      g_warning ("g_mkdtemp works even if template doesn't contain XXXXXX");
+      g_rmdir (retval);
+    }
+
+  strcpy (template, "foodir");
+  retval = g_mkdtemp (template);
+  if (retval != NULL)
+    {
+      g_warning ("g_mkdtemp works even if template contains less than six X");
+      g_rmdir (retval);
+    }
+
+  strcpy (template, "fooXXXXXX");
+  retval = g_mkdtemp (template);
+  g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX");
+  g_assert (retval == template && "g_mkdtemp allocated the resulting string?");
+  g_assert (!g_file_test (template, G_FILE_TEST_IS_REGULAR));
+  g_assert (g_file_test (template, G_FILE_TEST_IS_DIR));
+
+  strcat (template, "/abc");
+  fd = g_open (template, O_WRONLY | O_CREAT, 0600);
+  g_assert (fd != -1 && "couldn't open file in temporary directory");
+  close (fd);
+  g_assert (g_file_test (template, G_FILE_TEST_IS_REGULAR));
+  i = g_unlink (template);
+  g_assert (i != -1 && "couldn't unlink file in temporary directory");
+
+  template[9] = '\0';
+  i = g_rmdir (template);
+  g_assert (i != -1 && "couldn't remove temporary directory");
+
+  strcpy (template, "fooXXXXXX.dir");
+  retval = g_mkdtemp (template);
+  g_assert (retval != NULL && "g_mkdtemp didn't work for template fooXXXXXX.dir");
+  g_assert (g_file_test (template, G_FILE_TEST_IS_DIR));
+  g_rmdir (template);
+}
+
+static void
 test_readlink (void)
 {
 #ifdef HAVE_SYMLINK
@@ -133,12 +183,14 @@ test_readlink (void)
   data = g_file_read_link (link3, &error);
   g_assert (data == NULL && "could read link3");
   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
+  g_error_free (error);
 
   error = NULL;
   data = g_file_read_link (filename, &error);
   g_assert (data == NULL && "could read regular file as link");
   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL);
-  
+  g_error_free (error);
+
   remove (filename);
   remove (link1);
   remove (link2);
@@ -173,6 +225,7 @@ int
 main (int argc, char *argv[])
 {
   test_mkstemp ();
+  test_mkdtemp ();
   test_readlink ();
   test_get_contents ();