~jamesodhunt/libnih/bug-1123588-gcc-malloc-attribute

« back to all changes in this revision

Viewing changes to nih/tests/test_watch.c

  • Committer: Scott James Remnant
  • Date: 2011-06-20 18:47:02 UTC
  • mfrom: (1048.1.1 libnih-fix-for-777097)
  • Revision ID: scott@netsplit.com-20110620184702-hqi8hfvmze7i1824
* nih/watch.c (nih_watch_handle): Handle non-directory watches;
previously a file watch resulted in an invalid file path ending in
a single slash (LP:#777097).
* nih/tests/test_watch.c: Added explicit test for watch on a file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
946
946
        nih_error_init ();
947
947
 
948
948
        TEST_FILENAME (dirname);
949
 
        mkdir (dirname, 0755);
 
949
        TEST_EQ (mkdir (dirname, 0755), 0);
 
950
 
 
951
        TEST_FEATURE ("with watched file");
 
952
        strcpy (filename, dirname);
 
953
        strcat (filename, "/foo");
 
954
 
 
955
        /* Create file first since we don't set a create handler on the
 
956
         * watch.
 
957
         */
 
958
        fd = fopen (filename, "w");
 
959
        fprintf (fd, "bar\n");
 
960
        fclose (fd);
 
961
 
 
962
        create_called = 0;
 
963
        modify_called = 0;
 
964
        delete_called = 0;
 
965
        logger_called = 0;
 
966
        last_path  = NULL;
 
967
        last_watch = NULL;
 
968
        last_data  = NULL;
 
969
 
 
970
        watch = nih_watch_new (NULL, filename, FALSE, FALSE, NULL,
 
971
                               NULL, my_modify_handler,
 
972
                               my_delete_handler, &watch);
 
973
        TEST_NE_P (watch, NULL);
 
974
 
 
975
        /* Now, modify the existing file to trigger the modify handler. */
 
976
        fd = fopen (filename, "a+");
 
977
        fprintf (fd, "baz\n");
 
978
        fclose (fd);
 
979
 
 
980
        nfds = 0;
 
981
        FD_ZERO (&readfds);
 
982
        FD_ZERO (&writefds);
 
983
        FD_ZERO (&exceptfds);
 
984
 
 
985
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
 
986
        select (nfds, &readfds, &writefds, &exceptfds, NULL);
 
987
        nih_io_handle_fds (&readfds, &writefds, &exceptfds);
 
988
 
 
989
        TEST_EQ_STR (watch->path, filename);
 
990
 
 
991
        /* Ensure no regression to old behaviour (LP:#777097) */
 
992
        TEST_NE (last_path[ strlen(last_path) - 1 ], '/');
 
993
 
 
994
        TEST_EQ_STR (last_path, filename);
 
995
        TEST_EQ (modify_called, 1);
 
996
 
 
997
        unlink (filename);
 
998
 
 
999
        nfds = 0;
 
1000
        FD_ZERO (&readfds);
 
1001
        FD_ZERO (&writefds);
 
1002
        FD_ZERO (&exceptfds);
 
1003
 
 
1004
        nih_io_select_fds (&nfds, &readfds, &writefds, &exceptfds);
 
1005
        select (nfds, &readfds, &writefds, &exceptfds, NULL);
 
1006
        nih_io_handle_fds (&readfds, &writefds, &exceptfds);
 
1007
 
 
1008
        TEST_EQ (delete_called, 1);
 
1009
 
 
1010
        rmdir (filename);
 
1011
        nih_free (last_path);
 
1012
 
 
1013
        create_called = 0;
 
1014
        modify_called = 0;
 
1015
        delete_called = 0;
 
1016
        logger_called = 0;
 
1017
        last_path  = NULL;
 
1018
        last_watch = NULL;
 
1019
        last_data  = NULL;
 
1020
 
950
1021
 
951
1022
        watch = nih_watch_new (NULL, dirname, TRUE, TRUE, my_filter,
952
1023
                               my_create_handler, my_modify_handler,
953
1024
                               my_delete_handler, &watch);
954
 
 
955
 
 
956
1025
        /* Check that creating a file within the directory being watched
957
1026
         * results in the create handler being called, and passed the full
958
1027
         * path of the created file to it.