]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
[Core] Add PRE_CLOSED flag on file handle, add unit test.
authorDragos Oancea <dragos@signalwire.com>
Thu, 21 Nov 2019 18:02:47 +0000 (18:02 +0000)
committerAndrey Volk <andywolk@gmail.com>
Thu, 5 Dec 2019 17:55:46 +0000 (21:55 +0400)
src/include/switch_types.h
src/switch_core_file.c
tests/unit/Makefile.am
tests/unit/switch_core_file.c [new file with mode: 0644]

index 9302ad5c3d4845b64dd10280f9a9ea82f44ee384..9d8445a1e7b378de93aed457275707b76d4e2280 100644 (file)
@@ -1937,7 +1937,8 @@ typedef enum {
        SWITCH_FILE_NOMUX = (1 << 17),
        SWITCH_FILE_BREAK_ON_CHANGE = (1 << 18),
        SWITCH_FILE_FLAG_VIDEO = (1 << 19),
-       SWITCH_FILE_FLAG_VIDEO_EOF = (1 << 20)
+       SWITCH_FILE_FLAG_VIDEO_EOF = (1 << 20),
+       SWITCH_FILE_PRE_CLOSED = (1 << 21)
 } switch_file_flag_enum_t;
 typedef uint32_t switch_file_flag_t;
 
index c49b151b0116e58ef5d6e2c0438bbf43a288687b..717233ce55f6ce0024067e9da76ca8bfb3595e88 100644 (file)
@@ -915,6 +915,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_pre_close(switch_file_handle_t
        }
 
        switch_clear_flag_locked(fh, SWITCH_FILE_OPEN);
+       switch_set_flag_locked(fh, SWITCH_FILE_PRE_CLOSED);
 
        if (fh->file_interface->file_pre_close) {
                status = fh->file_interface->file_pre_close(fh);
@@ -929,10 +930,12 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_close(switch_file_handle_t *fh)
 
        if (switch_test_flag(fh, SWITCH_FILE_OPEN)) {
                status = switch_core_file_pre_close(fh);
-       } else {
+       } else if (!switch_test_flag(fh, SWITCH_FILE_PRE_CLOSED)) {
                return SWITCH_STATUS_FALSE;
        }
 
+       switch_clear_flag_locked(fh, SWITCH_FILE_PRE_CLOSED);
+
        fh->file_interface->file_close(fh);
 
        if (fh->params) {
index 79df35188fed35c9a64ef1701eae5bb2144e5a43..f3e9a7d234e12048dc13d26724a88ebf580ea2f1 100644 (file)
@@ -1,6 +1,6 @@
 include $(top_srcdir)/build/modmake.rulesam
 
-noinst_PROGRAMS = switch_event switch_hash switch_ivr_originate switch_utils switch_core switch_console switch_vpx \
+noinst_PROGRAMS = switch_event switch_hash switch_ivr_originate switch_utils switch_core switch_console switch_vpx switch_core_file \
                           switch_ivr_play_say switch_core_codec
 noinst_PROGRAMS+= switch_core_video
 AM_LDFLAGS  = -avoid-version -no-undefined $(SWITCH_AM_LDFLAGS) $(openssl_LIBS)
diff --git a/tests/unit/switch_core_file.c b/tests/unit/switch_core_file.c
new file mode 100644 (file)
index 0000000..34edf7d
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005-2018, Anthony Minessale II <anthm@freeswitch.org>
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II <anthm@freeswitch.org>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Chris Rienzo <chris@signalwire.com>
+ * Seven Du <dujinfang@gmail.com>
+ * Dragos Oancea <dragos@signalwire.com>
+ *
+ * switch_core_file.c -- tests file core functions
+ *
+ */
+#include <switch.h>
+#include <stdlib.h>
+
+#include <test/switch_test.h>
+
+FST_CORE_BEGIN("./conf")
+{
+       FST_SUITE_BEGIN(switch_core_file)
+       {
+               FST_SETUP_BEGIN()
+               {
+               }
+               FST_SETUP_END()
+
+               FST_TEARDOWN_BEGIN()
+               {
+               }
+               FST_TEARDOWN_END()
+
+               FST_TEST_BEGIN(test_switch_core_file_close)
+               {
+                       switch_file_handle_t fh = { 0 };
+                       switch_status_t status = SWITCH_STATUS_FALSE;
+                       static char filename[] = "/tmp/fs_unit_test.wav";
+                       static char hdr[] = /*simplest wav file*/
+                               "\x52\x49\x46\x46"
+                               "\x24\x00\x00\x00"
+                               "\x57\x41\x56\x45"
+                               "\x66\x6d\x74\x20"
+                               "\x10\x00\x00\x00"
+                               "\x01\x00\x01\x00"
+                               "\x44\xac\x00\x00"
+                               "\x88\x58\x01\x00"
+                               "\x02\x00\x10\x00"
+                               "\x64\x61\x74\x61"
+                               "\x00\x00";
+                       FILE *f = NULL;
+
+                       f = fopen(filename, "w"); 
+                       fst_check(f != NULL);
+                       fwrite(hdr, 1, sizeof(hdr) ,f);
+                       fclose(f);
+
+                       status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
+                       fst_check(status == SWITCH_STATUS_SUCCESS);
+                       fst_check(switch_test_flag(&fh, SWITCH_FILE_OPEN));
+                       status = switch_core_file_pre_close(&fh);
+                       fst_check(!switch_test_flag(&fh, SWITCH_FILE_OPEN));
+                       fst_check(switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED));
+                       fst_check(status == SWITCH_STATUS_SUCCESS);
+                       status = switch_core_file_close(&fh);
+                       fst_check(status == SWITCH_STATUS_SUCCESS);
+                       fst_check(!switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED));
+
+                       status = switch_core_file_close(&fh);
+                       fst_check(status == SWITCH_STATUS_FALSE);
+
+                       unlink(filename);
+               }
+               FST_TEST_END()
+               FST_TEST_BEGIN(test_switch_core_file_no_pre_close)
+               {
+                       switch_file_handle_t fh = { 0 };
+                       switch_status_t status = SWITCH_STATUS_FALSE;
+                       static char filename[] = "/tmp/fs_unit_test.wav";
+                       static char hdr[] = /*simplest wav file*/
+                               "\x52\x49\x46\x46"
+                               "\x24\x00\x00\x00"
+                               "\x57\x41\x56\x45"
+                               "\x66\x6d\x74\x20"
+                               "\x10\x00\x00\x00"
+                               "\x01\x00\x01\x00"
+                               "\x44\xac\x00\x00"
+                               "\x88\x58\x01\x00"
+                               "\x02\x00\x10\x00"
+                               "\x64\x61\x74\x61"
+                               "\x00\x00";
+                       FILE *f = NULL;
+
+                       f = fopen(filename, "w"); 
+                       fst_check(f != NULL);
+                       fwrite(hdr, 1, sizeof(hdr) ,f);
+                       fclose(f);
+
+                       status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);
+                       fst_check(status == SWITCH_STATUS_SUCCESS);
+                       fst_check(switch_test_flag(&fh, SWITCH_FILE_OPEN));
+                       status = switch_core_file_close(&fh);
+                       fst_check(status == SWITCH_STATUS_SUCCESS);
+                       fst_check(!switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED));
+
+                       status = switch_core_file_close(&fh);
+                       fst_check(status == SWITCH_STATUS_FALSE);
+
+                       unlink(filename);
+               }
+               FST_TEST_END()
+
+       }
+       FST_SUITE_END()
+}
+FST_CORE_END()