VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstFileLock.cpp

Last change on this file was 106061, checked in by vboxsync, 8 weeks ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.4 KB
Line 
1/* $Id: tstFileLock.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT Testcase - File Locks.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/file.h>
42#include <iprt/stream.h>
43#include <iprt/err.h>
44#include <iprt/thread.h> /* for RTThreadSleep() */
45#include <iprt/initterm.h>
46#include <iprt/string.h>
47
48#include <stdio.h>
49
50static char achTest1[] = "Test line 1\n";
51static char achTest2[] = "Test line 2\n";
52static char achTest3[] = "Test line 3\n";
53
54static bool fRun = false;
55
56/** @todo Create a tstFileLock-2 which doesn't require interaction and counts errors. */
57
58int main()
59{
60 RTR3InitExeNoArguments(0);
61 RTPrintf("tstFileLock: TESTING\n");
62
63 RTFILE File;
64 int rc = RTFileOpen(&File, "tstLock.tst", RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
65 RTPrintf("File open: rc=%Rrc\n", rc);
66 if (RT_FAILURE(rc))
67 {
68 if (rc != VERR_FILE_NOT_FOUND && rc != VERR_OPEN_FAILED)
69 {
70 RTPrintf("FATAL\n");
71 return 1;
72 }
73
74 rc = RTFileOpen(&File, "tstLock.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE);
75 RTPrintf("File create: rc=%Rrc\n", rc);
76 if (RT_FAILURE(rc))
77 {
78 RTPrintf("FATAL\n");
79 return 2;
80 }
81 fRun = true;
82 }
83
84 /* grow file a little */
85 rc = RTFileSetSize(File, fRun ? 2048 : 20480);
86 RTPrintf("File size: rc=%Rrc\n", rc);
87
88 int buf;
89 /* read test. */
90 rc = RTFileRead(File, &buf, sizeof(buf), NULL);
91 RTPrintf("Read: rc=%Rrc\n", rc);
92
93 /* write test. */
94 rc = RTFileWrite(File, achTest1, strlen(achTest1), NULL);
95 RTPrintf("Write: rc=%Rrc\n", rc);
96
97 /* lock: read, non-blocking. */
98 rc = RTFileLock(File, RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, _4G);
99 RTPrintf("Lock: read, non-blocking, rc=%Rrc\n", rc);
100 bool fl = RT_SUCCESS(rc);
101
102 /* read test. */
103 rc = RTFileRead(File, &buf, sizeof(buf), NULL);
104 RTPrintf("Read: rc=%Rrc\n", rc);
105
106 /* write test. */
107 rc = RTFileWrite(File, achTest2, strlen(achTest2), NULL);
108 RTPrintf("Write: rc=%Rrc\n", rc);
109 RTPrintf("Lock test will change in three seconds\n");
110 for (int i = 0; i < 3; i++)
111 {
112 RTThreadSleep(1000);
113 RTPrintf(".");
114 }
115 RTPrintf("\n");
116
117 /* change lock: write, non-blocking. */
118 rc = RTFileLock(File, RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, _4G);
119 RTPrintf("Change lock: write, non-blocking, rc=%Rrc\n", rc);
120 RTPrintf("Test will unlock in three seconds\n");
121 for (int i = 0; i < 3; i++)
122 {
123 RTThreadSleep(1000);
124 RTPrintf(".");
125 }
126 RTPrintf("\n");
127
128 /* remove lock. */
129 if (fl)
130 {
131 fl = false;
132 rc = RTFileUnlock(File, 0, _4G);
133 RTPrintf("Unlock: rc=%Rrc\n", rc);
134 RTPrintf("Write test will lock in three seconds\n");
135 for (int i = 0; i < 3; i++)
136 {
137 RTThreadSleep(1000);
138 RTPrintf(".");
139 }
140 RTPrintf("\n");
141 }
142
143 /* lock: write, non-blocking. */
144 rc = RTFileLock(File, RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, _4G);
145 RTPrintf("Lock: write, non-blocking, rc=%Rrc\n", rc);
146 fl = RT_SUCCESS(rc);
147
148 /* grow file test */
149 rc = RTFileSetSize(File, fRun ? 2048 : 20480);
150 RTPrintf("File size: rc=%Rrc\n", rc);
151
152 /* read test. */
153 rc = RTFileRead(File, &buf, sizeof(buf), NULL);
154 RTPrintf("Read: rc=%Rrc\n", rc);
155
156 /* write test. */
157 rc = RTFileWrite(File, achTest3, strlen(achTest3), NULL);
158 RTPrintf("Write: rc=%Rrc\n", rc);
159 RTPrintf("Continuing to next test in three seconds\n");
160 for (int i = 0; i < 3; i++)
161 {
162 RTThreadSleep(1000);
163 RTPrintf(".");
164 }
165 RTPrintf("\n");
166
167 RTFileClose(File);
168 RTFileDelete("tstLock.tst");
169
170
171 RTPrintf("tstFileLock: I've no recollection of this testcase succeeding or not, sorry.\n");
172 return 0;
173}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette