Merge pull request #10877 from nextcloud/fix/ssl-untrusted-nullpointer

SslUntrustedCertDialog: fix inflation to avoid NPE and add test to ensure it doesn't happen again
This commit is contained in:
Álvaro Brey 2022-10-21 16:13:23 +02:00 committed by GitHub
commit 2e49782298
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 19 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View file

@ -25,9 +25,12 @@ package com.owncloud.android.ui.dialog;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Intent;
import android.net.http.SslCertificate;
import android.net.http.SslError;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.widget.TextView;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
@ -59,7 +62,6 @@ import com.owncloud.android.lib.resources.users.Status;
import com.owncloud.android.lib.resources.users.StatusType;
import com.owncloud.android.ui.activity.FileDisplayActivity;
import com.owncloud.android.ui.fragment.OCFileListBottomSheetActions;
import com.owncloud.android.ui.fragment.OCFileListBottomSheetDialog;
import com.owncloud.android.ui.fragment.OCFileListBottomSheetDialogFragment;
import com.owncloud.android.ui.fragment.ProfileBottomSheetDialog;
import com.owncloud.android.utils.MimeTypeUtil;
@ -69,6 +71,7 @@ import com.owncloud.android.utils.theme.CapabilityUtils;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import java.net.URI;
import java.util.ArrayList;
@ -464,6 +467,23 @@ public class DialogFragmentIT extends AbstractIT {
screenshot(sut.getWindow().getDecorView());
}
@Test
@ScreenshotTest
public void testSslUntrustedCertDialog() {
if (Looper.myLooper() == null) {
Looper.prepare();
}
final SslCertificate certificate = new SslCertificate("foo", "bar", "2022/01/10", "2022/01/30");
final SslError sslError = new SslError(SslError.SSL_UNTRUSTED, certificate);
final SslErrorHandler handler = Mockito.mock(SslErrorHandler.class);
SslUntrustedCertDialog sut = SslUntrustedCertDialog.newInstanceForEmptySslError(sslError, handler);
showDialog(sut);
}
private FileDisplayActivity showDialog(DialogFragment dialog) {
Intent intent = new Intent(targetContext, FileDisplayActivity.class);

View file

@ -24,10 +24,8 @@ import android.app.Activity;
import android.app.Dialog;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.widget.Button;
@ -135,17 +133,14 @@ public class SslUntrustedCertDialog extends DialogFragment implements Injectable
binding = null;
}
@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log_OC.d(TAG, "onCreateView, savedInsanceState is " + savedInstanceState);
// Create a view by inflating desired layout
if (binding == null) {
binding = SslUntrustedCertLayoutBinding.inflate(inflater, container, false);
binding.detailsScroll.setVisibility(View.GONE);
mErrorViewAdapter.updateErrorView(binding);
} else {
((ViewGroup) binding.getRoot().getParent()).removeView(binding.getRoot());
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log_OC.d(TAG, "onCreateDialog, savedInstanceState is " + savedInstanceState);
binding = SslUntrustedCertLayoutBinding.inflate(getLayoutInflater(), null, false);
binding.detailsScroll.setVisibility(View.GONE);
mErrorViewAdapter.updateErrorView(binding);
binding.ok.setOnClickListener(new OnCertificateTrusted());
@ -163,13 +158,7 @@ public class SslUntrustedCertDialog extends DialogFragment implements Injectable
}
});
return binding.getRoot();
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log_OC.d(TAG, "onCreateDialog, savedInstanceState is " + savedInstanceState);
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(binding.getRoot().getContext());
builder.setView(binding.getRoot());