Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/FileChannel/Lock.java
38828 views
/*1* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4429043 4493595 6332756 670945725* @summary The FileChannel file locking26*/2728import java.io.*;29import java.nio.channels.*;30import static java.nio.file.StandardOpenOption.*;3132/**33* Testing FileChannel's lock method.34*/3536public class Lock {3738public static void main(String[] args) throws Exception {39if (args.length > 0) {40if(args[0].equals("1")) {41MadWriter mw = new MadWriter(args[1], false);42} else {43MadWriter mw = new MadWriter(args[1], true);44}45return;46}47File blah = File.createTempFile("blah", null);48blah.deleteOnExit();49RandomAccessFile raf = new RandomAccessFile(blah, "rw");50raf.write(1);51raf.close();52test1(blah, "1");53test1(blah, "2");54test2(blah, true);55test2(blah, false);56test3(blah);57test4(blah);58blah.delete();59}6061private static void test2(File blah, boolean b) throws Exception {62RandomAccessFile raf = new RandomAccessFile(blah, "rw");63FileChannel channel = raf.getChannel();64FileLock lock;65if (b)66lock = channel.lock();67else68lock = channel.tryLock();69lock.release();70channel.close();71}7273static void test1(File blah, String str) throws Exception {7475// Grab the lock76RandomAccessFile fis = new RandomAccessFile(blah, "rw");77FileChannel fc = fis.getChannel();78FileLock lock = null;7980if (str.equals("1")) {81lock = fc.lock(0, 10, false);82if (lock == null)83throw new RuntimeException("Lock should not return null");84try {85FileLock lock2 = fc.lock(5, 10, false);86throw new RuntimeException("Overlapping locks allowed");87} catch (OverlappingFileLockException e) {88// Correct result89}90}9192// Exec the tamperer93String command = System.getProperty("java.home") +94File.separator + "bin" + File.separator + "java";95String testClasses = System.getProperty("test.classes");96if (testClasses != null)97command += " -cp " + testClasses;98command += " Lock " + str + " " + blah;99Process p = Runtime.getRuntime().exec(command);100101BufferedReader in = new BufferedReader102(new InputStreamReader(p.getInputStream()));103104String s;105int count = 0;106while ((s = in.readLine()) != null) {107if (!s.equals("good")) {108if (File.separatorChar == '/') {109// Fails on windows over NFS...110throw new RuntimeException("Failed: "+s);111}112}113count++;114}115116if (count == 0) {117in = new BufferedReader(new InputStreamReader(p.getErrorStream()));118while ((s = in.readLine()) != null) {119System.err.println("Error output: " + s);120}121throw new RuntimeException("Failed, no output");122}123124// Clean up125if (lock != null) {126/* Check multiple releases */127lock.release();128lock.release();129}130fc.close();131fis.close();132}133134// The overlap check for file locks should be JVM-wide135private static void test3(File blah) throws Exception {136FileChannel fc1 = new RandomAccessFile(blah, "rw").getChannel();137FileChannel fc2 = new RandomAccessFile(blah, "rw").getChannel();138139// lock via one channel, and then attempt to lock the same file140// using a second channel141FileLock fl1 = fc1.lock();142try {143fc2.tryLock();144throw new RuntimeException("Overlapping locks allowed");145} catch (OverlappingFileLockException x) {146}147try {148fc2.lock();149throw new RuntimeException("Overlapping locks allowed");150} catch (OverlappingFileLockException x) {151}152153// release lock and the attempt to lock with the second channel154// should succeed.155fl1.release();156FileLock fl2 = fc2.lock();157try {158fc1.lock();159throw new RuntimeException("Overlapping locks allowed");160} catch (OverlappingFileLockException x) {161}162163fc1.close();164fc2.close();165}166167/**168* Test file locking when file is opened for append169*/170static void test4(File blah) throws Exception {171try (FileChannel fc = new FileOutputStream(blah, true).getChannel()) {172fc.tryLock().release();173fc.tryLock(0L, 1L, false).release();174fc.lock().release();175fc.lock(0L, 1L, false).release();176}177try (FileChannel fc = FileChannel.open(blah.toPath(), APPEND)) {178fc.tryLock().release();179fc.tryLock(0L, 1L, false).release();180fc.lock().release();181fc.lock(0L, 1L, false).release();182}183}184}185186class MadWriter {187public MadWriter(String s, boolean b) throws Exception {188File f = new File(s);189RandomAccessFile fos = new RandomAccessFile(f, "rw");190FileChannel fc = fos.getChannel();191if (fc.tryLock(10, 10, false) == null) {192System.out.println("bad: Failed to grab adjacent lock");193}194FileLock lock = fc.tryLock(0, 10, false);195if (lock == null) {196if (b)197System.out.println("bad");198else199System.out.println("good");200} else {201if (b)202System.out.println("good");203else204System.out.println("bad");205}206fc.close();207fos.close();208}209210}211212213