Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
TestsudoFileUtils02.java
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package jau.test.fs;
26
27import java.util.List;
28
29import org.jau.fs.CopyOptions;
30import org.jau.fs.DirItem;
31import org.jau.fs.FMode;
32import org.jau.fs.FileStats;
33import org.jau.fs.FileUtil;
34import org.jau.fs.MountFlags;
35import org.jau.fs.TraverseOptions;
36import org.jau.fs.UnmountFlags;
37import org.jau.io.PrintUtil;
38import org.jau.sys.PlatformProps;
39import org.jau.sys.PlatformTypes;
40import org.junit.Assert;
41import org.junit.FixMethodOrder;
42import org.junit.Test;
43import org.junit.runners.MethodSorters;
44
45import jau.pkg.PlatformRuntime;
46
47@FixMethodOrder(MethodSorters.NAME_ASCENDING)
49 static final boolean DEBUG = false;
50
51 private static boolean isFolderEmpty(final String path) {
52 final List<DirItem> files = FileUtil.get_dir_content(path);
53 return null == files || 0 == files.size();
54 }
55
56 @Test(timeout = 10000)
57 public void test50_mount_copy_r_p() {
58 PlatformRuntime.checkInitialized();
59 PrintUtil.println(System.err, "test50_mount_copy_r_p\n");
60
61 final FileStats root_orig_stats = getTestDataDirStats();
62 Assert.assertTrue( true == root_orig_stats.exists() );
63 Assert.assertTrue( true == root_orig_stats.is_dir() );
64
65 {
66
67 final FileStats image_stats = getTestDataImageFile();
68 Assert.assertTrue( true == image_stats.exists() );
69
70 final String mount_point = root+"_mount";
71 FileUtil.remove(mount_point, TraverseOptions.recursive); // start fresh
72 Assert.assertTrue( true == FileUtil.mkdir(mount_point, FMode.def_dir) );
73
74 final MountFlags mount_flags;
75 if( PlatformProps.OS == PlatformTypes.OSType.LINUX ) {
76 mount_flags = new org.jau.fs.linux.MountFlags();
77 mount_flags.set(org.jau.fs.linux.MountFlags.Bit.rdonly);
78 } else {
79 mount_flags = new MountFlags();
80 }
81 PrintUtil.println(System.err, "MountFlags for "+PlatformProps.OS+": "+mount_flags);
82 final long mctx = FileUtil.mount_image(image_stats.path(), mount_point, "squashfs", mount_flags, "");
83 Assert.assertTrue( 0 != mctx );
84 Assert.assertFalse(isFolderEmpty(mount_point));
85
86 final CopyOptions copts = new CopyOptions();
87 copts.set(CopyOptions.Bit.recursive);
88 copts.set(CopyOptions.Bit.preserve_all);
89 copts.set(CopyOptions.Bit.sync);
90 copts.set(CopyOptions.Bit.verbose);
91
92 final String root_copy = root+"_copy_test50";
93 FileUtil.remove(root_copy, TraverseOptions.recursive);
94 testxx_copy_r_p("test50_mount_copy_r_p", new FileStats(mount_point), 1 /* source_added_dead_links */, root_copy, copts, false /* dest_is_vfat */);
95 Assert.assertTrue( true == FileUtil.remove(root_copy, TraverseOptions.recursive) );
96
97 final UnmountFlags umount_flags;
98 if( PlatformProps.OS == PlatformTypes.OSType.LINUX ) {
99 umount_flags = new org.jau.fs.linux.UnmountFlags();
100 umount_flags.set(org.jau.fs.linux.UnmountFlags.Bit.detach); // lazy
101 } else {
102 umount_flags = new UnmountFlags();
103 }
104 PrintUtil.println(System.err, "UnmountFlags for "+PlatformProps.OS+": "+umount_flags);
105 final boolean umount_ok = FileUtil.umount(mctx, umount_flags);
106 Assert.assertTrue( true == umount_ok );
107 Assert.assertTrue(isFolderEmpty(mount_point));
108
109 Assert.assertTrue( true == FileUtil.remove(mount_point, TraverseOptions.recursive) );
110 }
111 }
112
113 /* pp */ static String test51_devname = null;
114 /* pp */ static boolean test51_umount = true;
115
116 @Test(timeout = 10000)
117 public void test51_mount_device() {
118 PlatformRuntime.checkInitialized();
119 PrintUtil.println(System.err, "test51_mount_device\n");
120 if( null == test51_devname ) {
121 return;
122 }
123 {
124 final String device_file = test51_devname;
125 final FileStats device_stats = new FileStats(device_file);
126 Assert.assertTrue( true == device_stats.exists() );
127 PrintUtil.println(System.err, "Mount "+device_stats);
128
129 final String mount_point = root+"_mount";
130 Assert.assertTrue(isFolderEmpty(mount_point));
131 FileUtil.remove(mount_point, TraverseOptions.recursive); // start fresh
132 Assert.assertTrue( true == FileUtil.mkdir(mount_point, FMode.def_dir) );
133
134 final MountFlags mount_flags;
135 if( PlatformProps.OS == PlatformTypes.OSType.LINUX ) {
136 mount_flags = new org.jau.fs.linux.MountFlags();
137 mount_flags.set(org.jau.fs.linux.MountFlags.Bit.rdonly); // failed: MS_PRIVATE, MS_UNBINDABLE
138 } else {
139 mount_flags = new MountFlags();
140 }
141 PrintUtil.println(System.err, "MountFlags for "+PlatformProps.OS+": "+mount_flags);
142 final long mctx = FileUtil.mount(device_stats.path(), mount_point, "vfat", mount_flags, "errors=continue,rodir,sys_immutable,usefree");
143 Assert.assertTrue( 0 != mctx );
144 Assert.assertFalse(isFolderEmpty(mount_point));
145
146 final List<DirItem> files = FileUtil.get_dir_content(mount_point);
147 Assert.assertNotNull(files);
148 Assert.assertTrue(files.size() > 0 );
149 int count=0;
150 for(final DirItem item : files) {
151 ++count;
152 PrintUtil.fprintf_td(System.err, "%,4d: %s\n", count, item);
153 }
154
155 if( test51_umount ) {
156 final UnmountFlags umount_flags;
157 if( PlatformProps.OS == PlatformTypes.OSType.LINUX ) {
158 umount_flags = new org.jau.fs.linux.UnmountFlags();
159 umount_flags.set(org.jau.fs.linux.UnmountFlags.Bit.detach); // lazy
160 } else {
161 umount_flags = new UnmountFlags();
162 }
163 PrintUtil.println(System.err, "UnmountFlags for "+PlatformProps.OS+": "+umount_flags);
164 final boolean umount_ok = FileUtil.umount(mount_point, umount_flags);
165 Assert.assertTrue( true == umount_ok );
166 Assert.assertTrue(isFolderEmpty(mount_point));
167
168 Assert.assertTrue( true == FileUtil.remove(mount_point, TraverseOptions.recursive) );
169 }
170 }
171 }
172
173 public static void main(final String args[]) {
174 final int argc = args.length;
175 for(int i=0; i< argc; i++) {
176 final String arg = args[i];
177 if( arg.equals("-mount_device") && args.length > (i+1) ) {
178 test51_devname = args[++i];
179 } else if( arg.equals("-no_umount") ) {
180 test51_umount = false;
181 }
182 }
183 org.junit.runner.JUnitCore.main(TestsudoFileUtils02.class.getName());
184 }
185}
static void main(final String args[])