VirtualBox

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

Last change on this file since 4953 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/* $Id: tstFileLock.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - File Locks.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/file.h>
23#include <iprt/stream.h>
24#include <iprt/err.h>
25#include <iprt/thread.h> /* for RTThreadSleep() */
26#include <iprt/runtime.h>
27#include <iprt/string.h>
28
29#include <stdio.h>
30
31static char achTest1[] = "Test line 1\n";
32static char achTest2[] = "Test line 2\n";
33static char achTest3[] = "Test line 3\n";
34
35static bool fRun = false;
36
37/** @todo Create a tstFileLock-2 which doesn't require interaction and counts errors. */
38
39int main()
40{
41 RTR3Init();
42 RTPrintf("tstFileLock: TESTING\n");
43
44 RTFILE File;
45 int rc = RTFileOpen(&File, "tstLock.tst", RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
46 RTPrintf("File open: rc=%Vrc\n", rc);
47 if (RT_FAILURE(rc))
48 {
49 if (rc != VERR_FILE_NOT_FOUND && rc != VERR_OPEN_FAILED)
50 {
51 RTPrintf("FATAL\n");
52 return 1;
53 }
54
55 rc = RTFileOpen(&File, "tstLock.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE);
56 RTPrintf("File create: rc=%Vrc\n", rc);
57 if (RT_FAILURE(rc))
58 {
59 RTPrintf("FATAL\n");
60 return 2;
61 }
62 fRun = true;
63 }
64
65 /* grow file a little */
66 rc = RTFileSetSize(File, fRun ? 2048 : 20480);
67 RTPrintf("File size: rc=%Vrc\n", rc);
68
69 int buf;
70 /* read test. */
71 rc = RTFileRead(File, &buf, sizeof(buf), NULL);
72 RTPrintf("Read: rc=%Vrc\n", rc);
73
74 /* write test. */
75 rc = RTFileWrite(File, achTest1, strlen(achTest1), NULL);
76 RTPrintf("Write: rc=%Vrc\n", rc);
77
78 /* lock: read, non-blocking. */
79 rc = RTFileLock(File, RTFILE_LOCK_READ | RTFILE_LOCK_IMMEDIATELY, 0, _4G);
80 RTPrintf("Lock: read, non-blocking, rc=%Vrc\n", rc);
81 bool fl = RT_SUCCESS(rc);
82
83 /* read test. */
84 rc = RTFileRead(File, &buf, sizeof(buf), NULL);
85 RTPrintf("Read: rc=%Vrc\n", rc);
86
87 /* write test. */
88 rc = RTFileWrite(File, achTest2, strlen(achTest2), NULL);
89 RTPrintf("Write: rc=%Vrc\n", rc);
90 RTPrintf("Lock test will change in three seconds\n");
91 for (int i = 0; i < 3; i++)
92 {
93 RTThreadSleep(1000);
94 RTPrintf(".");
95 }
96 RTPrintf("\n");
97
98 /* change lock: write, non-blocking. */
99 rc = RTFileLock(File, RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, _4G);
100 RTPrintf("Change lock: write, non-blocking, rc=%Vrc\n", rc);
101 RTPrintf("Test will unlock in three seconds\n");
102 for (int i = 0; i < 3; i++)
103 {
104 RTThreadSleep(1000);
105 RTPrintf(".");
106 }
107 RTPrintf("\n");
108
109 /* remove lock. */
110 if (fl)
111 {
112 fl = false;
113 rc = RTFileUnlock(File, 0, _4G);
114 RTPrintf("Unlock: rc=%Vrc\n", rc);
115 RTPrintf("Write test will lock in three seconds\n");
116 for (int i = 0; i < 3; i++)
117 {
118 RTThreadSleep(1000);
119 RTPrintf(".");
120 }
121 RTPrintf("\n");
122 }
123
124 /* lock: write, non-blocking. */
125 rc = RTFileLock(File, RTFILE_LOCK_WRITE | RTFILE_LOCK_IMMEDIATELY, 0, _4G);
126 RTPrintf("Lock: write, non-blocking, rc=%Vrc\n", rc);
127 fl = RT_SUCCESS(rc);
128
129 /* grow file test */
130 rc = RTFileSetSize(File, fRun ? 2048 : 20480);
131 RTPrintf("File size: rc=%Vrc\n", rc);
132
133 /* read test. */
134 rc = RTFileRead(File, &buf, sizeof(buf), NULL);
135 RTPrintf("Read: rc=%Vrc\n", rc);
136
137 /* write test. */
138 rc = RTFileWrite(File, achTest3, strlen(achTest3), NULL);
139 RTPrintf("Write: rc=%Vrc\n", rc);
140 RTPrintf("Continuing to next test in three seconds\n");
141 for (int i = 0; i < 3; i++)
142 {
143 RTThreadSleep(1000);
144 RTPrintf(".");
145 }
146 RTPrintf("\n");
147
148 RTFileClose(File);
149 RTFileDelete("tstLock.tst");
150
151
152 RTPrintf("tstFileLock: I've no recollection of this testcase succeeding or not, sorry.\n");
153 return 0;
154}
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