VirtualBox

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

Last change on this file since 26430 was 14831, checked in by vboxsync, 16 years ago

whole bunch: avoid runtime.h, include individual headers indead.

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