10
10
11
11
import static junit .framework .TestCase .assertTrue ;
12
12
import static org .junit .Assert .assertEquals ;
13
- import static org .junit .Assert .fail ;
14
13
import static org .junit .Assume .assumeTrue ;
15
14
16
15
import android .content .Context ;
19
18
20
19
import androidx .test .platform .app .InstrumentationRegistry ;
21
20
21
+ import com .nextcloud .android .lib .resources .files .ToggleFileLockRemoteOperation ;
22
22
import com .nextcloud .common .NextcloudClient ;
23
23
import com .owncloud .android .lib .common .OwnCloudBasicCredentials ;
24
24
import com .owncloud .android .lib .common .OwnCloudClient ;
54
54
import java .security .NoSuchAlgorithmException ;
55
55
import java .security .cert .CertificateException ;
56
56
import java .security .cert .X509Certificate ;
57
+ import java .util .concurrent .TimeUnit ;
57
58
58
59
import okhttp3 .Credentials ;
59
60
@@ -77,7 +78,8 @@ public abstract class AbstractIT {
77
78
protected String baseFolderPath = "/test_for_build/" ;
78
79
79
80
public static final String ASSETS__TEXT_FILE_NAME = "textFile.txt" ;
80
- private static String LOCAL_TRUSTSTORE_FILENAME = "knownServers.bks" ;
81
+ private static final String LOCAL_TRUSTSTORE_FILENAME = "knownServers.bks" ;
82
+ private static final String TAG = "AbstractIT" ;
81
83
82
84
@ BeforeClass
83
85
public static void beforeAll () throws InterruptedException ,
@@ -115,26 +117,26 @@ public static void beforeAll() throws InterruptedException,
115
117
}
116
118
117
119
private static void waitForServer (OwnCloudClient client , Uri baseUrl ) {
118
- // use http
119
- Uri httpUrl = Uri . parse ( baseUrl . toString (). replaceFirst ( "https" , "http" )) ;
120
- GetMethod get = new GetMethod ( httpUrl + "/status.php" ) ;
120
+ String statusUrl = baseUrl + "/status.php" ;
121
+ GetMethod get ;
122
+ int maxRetries = 3 ;
121
123
122
- try {
123
- int i = 0 ;
124
- while (client .executeMethod (get ) != HttpStatus .SC_OK && i < 3 ) {
125
- System .out .println ("wait…" );
126
- Thread .sleep (60 * 1000 );
127
- i ++;
128
- }
124
+ for (int i = 0 ; i < maxRetries ; i ++) {
125
+ get = new GetMethod (statusUrl );
129
126
130
- if (i == 3 ) {
131
- fail ("Server not ready!" );
132
- }
127
+ try {
128
+ if (client .executeMethod (get ) == HttpStatus .SC_OK ) {
129
+ Log_OC .d (TAG , "Server is ready" );
130
+ return ;
131
+ }
133
132
134
- } catch (IOException e ) {
135
- e .printStackTrace ();
136
- } catch (InterruptedException e ) {
137
- e .printStackTrace ();
133
+ Log_OC .d (TAG , "Server not ready, retrying in 60 seconds..." );
134
+ TimeUnit .MINUTES .sleep (1 );
135
+ } catch (Exception e ) {
136
+ Log_OC .d (TAG , "Server not ready, failed: " + e );
137
+ } finally {
138
+ get .releaseConnection ();
139
+ }
138
140
}
139
141
}
140
142
@@ -265,30 +267,56 @@ public void after() {
265
267
}
266
268
267
269
private void removeOnClient (OwnCloudClient client ) {
268
- RemoteOperationResult result = new ReadFolderRemoteOperation ("/" ).execute (client );
270
+ final var result = new ReadFolderRemoteOperation ("/" ).execute (client );
269
271
assertTrue (result .getLogMessage (context ), result .isSuccess ());
270
272
271
273
for (Object object : result .getData ()) {
272
- RemoteFile remoteFile = (RemoteFile ) object ;
273
-
274
- if (!"/" .equals (remoteFile .getRemotePath ()) &&
275
- remoteFile .getMountType () != WebdavEntry .MountType .GROUP ) {
276
- if (remoteFile .isEncrypted ()) {
277
- assertTrue (new ToggleEncryptionRemoteOperation (
278
- remoteFile .getLocalId (),
279
- remoteFile .getRemotePath (),
280
- false )
281
- .execute (client )
282
- .isSuccess ());
283
- }
274
+ if (!(object instanceof RemoteFile remoteFile )) {
275
+ Log_OC .d (TAG , "Skipping removeOnClient: not instance of RemoteFile" );
276
+ continue ;
277
+ }
278
+
279
+ String remotePath = remoteFile .getRemotePath ();
280
+
281
+ if ("/" .equals (remotePath ) || remoteFile .getMountType () == WebdavEntry .MountType .GROUP ) {
282
+ Log_OC .d (TAG , "Skipping removeOnClient: remote path is root path or mount type is group" );
283
+ continue ;
284
+ }
285
+
286
+ if (remoteFile .isEncrypted ()) {
287
+ assertTrue (toggleEncryptionRemoteFile (remoteFile ));
288
+ }
284
289
285
- assertTrue ( "Failed to remove " + remoteFile . getRemotePath (),
286
- new RemoveFileRemoteOperation ( remoteFile . getRemotePath ()). execute ( client ). isSuccess () );
290
+ if ( remoteFile . isLocked () && remotePath != null ) {
291
+ unlockRemoteFile ( remotePath );
287
292
}
293
+
294
+ boolean isRemoteFileRemoved = removeRemoteFile (remotePath );
295
+ assertTrue ("Failed to remove " + remotePath , isRemoteFileRemoved );
296
+ }
297
+
298
+ boolean isKeyStoreDeleted = new File (context .getFilesDir (), LOCAL_TRUSTSTORE_FILENAME ).delete ();
299
+ Log_OC .d (TAG , "KeyStore file deletion result: " + isKeyStoreDeleted );
300
+ }
301
+
302
+ private boolean toggleEncryptionRemoteFile (RemoteFile remoteFile ) {
303
+ final var operation = new ToggleEncryptionRemoteOperation (remoteFile .getLocalId (), remoteFile .getRemotePath (), false );
304
+ final var result = operation .execute (client );
305
+ return result .isSuccess ();
306
+ }
307
+
308
+ private void unlockRemoteFile (String path ) {
309
+ final var operation = new ToggleFileLockRemoteOperation (false , path );
310
+ final var result = operation .execute (nextcloudClient );
311
+ if (result .isSuccess ()) {
312
+ Log_OC .d (TAG , "Locked file: " + path + " unlocked" );
288
313
}
314
+ }
289
315
290
- // delete keystore
291
- new File (context .getFilesDir (), LOCAL_TRUSTSTORE_FILENAME ).delete ();
316
+ private boolean removeRemoteFile (String path ) {
317
+ final var operation = new RemoveFileRemoteOperation (path );
318
+ final var result = operation .execute (client );
319
+ return result .isSuccess ();
292
320
}
293
321
294
322
public static File getFile (String filename ) throws IOException {
0 commit comments