From 698f0e37746bffe61fc59cd553d3582d0e004097 Mon Sep 17 00:00:00 2001
From: Amos Shi <ashi@openjdk.org>
Date: Fri, 16 Aug 2024 17:20:51 +0000
Subject: [PATCH] 8313674: (fc)
 java/nio/channels/FileChannel/BlockDeviceSize.java should test for more block
 devices

Reviewed-by: mbaesken
Backport-of: e91492ab4333c61f39b50eb428fa932131a5b908
---
 .../channels/FileChannel/BlockDeviceSize.java | 49 +++++++++++--------
 1 file changed, 28 insertions(+), 21 deletions(-)

diff --git a/test/jdk/java/nio/channels/FileChannel/BlockDeviceSize.java b/test/jdk/java/nio/channels/FileChannel/BlockDeviceSize.java
index 496312256be..7d430693b2f 100644
--- a/test/jdk/java/nio/channels/FileChannel/BlockDeviceSize.java
+++ b/test/jdk/java/nio/channels/FileChannel/BlockDeviceSize.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,40 +24,47 @@
 /* @test
  * @bug 8054029
  * @requires (os.family == "linux")
- * @summary Block devices should not report size=0 on Linux
+ * @summary FileChannel.size() should be equal to RandomAccessFile.size() and > 0 for block devs on Linux
  */
 
 import java.io.RandomAccessFile;
 import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.nio.channels.FileChannel;
 import java.nio.file.AccessDeniedException;
 import java.nio.file.NoSuchFileException;
+import java.util.List;
+
 import static java.nio.file.StandardOpenOption.*;
 
 
 public class BlockDeviceSize {
-    private static final String BLK_FNAME = "/dev/sda1";
-    private static final Path BLK_PATH = Paths.get(BLK_FNAME);
+    private static final List<String> BLK_FNAMES = List.of("/dev/sda1", "/dev/nvme0n1", "/dev/xvda1") ;
 
     public static void main(String[] args) throws Throwable {
-        try (FileChannel ch = FileChannel.open(BLK_PATH, READ);
-             RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) {
-
-            long size1 = ch.size();
-            long size2 = file.length();
-            if (size1 != size2) {
-                throw new RuntimeException("size differs when retrieved" +
-                        " in different ways: " + size1 + " != " + size2);
+        for (String blkFname: BLK_FNAMES) {
+            Path blkPath = Path.of(blkFname);
+            try (FileChannel ch = FileChannel.open(blkPath, READ);
+                 RandomAccessFile file = new RandomAccessFile(blkFname, "r")) {
+
+                long size1 = ch.size();
+                long size2 = file.length();
+                if (size1 != size2) {
+                    throw new RuntimeException("size differs when retrieved" +
+                            " in different ways: " + size1 + " != " + size2);
+                }
+                if (size1 <= 0) {
+                    throw new RuntimeException("size() for a block device size returns zero or a negative value");
+                }
+                System.out.println("OK");
+
+            } catch (NoSuchFileException nsfe) {
+                System.err.println("File " + blkFname + " not found." +
+                        " Skipping test");
+            } catch (AccessDeniedException ade) {
+                throw new RuntimeException("Access to " + blkFname + " is denied."
+                        + " Run test as root.", ade);
+
             }
-            System.out.println("OK");
-
-        } catch (NoSuchFileException nsfe) {
-            System.err.println("File " + BLK_FNAME + " not found." +
-                    " Skipping test");
-        } catch (AccessDeniedException ade) {
-            System.err.println("Access to " + BLK_FNAME + " is denied." +
-                    " Run test as root.");
         }
     }
 }
-- 
GitLab