Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try/catch around trying to access the connectivityManager APIs. #160

Merged
merged 6 commits into from
Sep 27, 2021

Conversation

jkwatson
Copy link
Contributor

See https://issuetracker.google.com/issues/175055271 for the root Android bug.

@jkwatson jkwatson requested review from a team as code owners September 27, 2021 17:03
Copy link
Contributor

@breedx-splk breedx-splk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I read in a book one time that a method should only do one thing, and that exception handling is also a thing. I think that means you might have cleaner code if you factored out the try body to a method...but dealer's choice.


Network network = mock(Network.class);
when(connectivityManager.getActiveNetwork()).thenReturn(network);
when(connectivityManager.getNetworkCapabilities(network)).thenThrow(new SecurityException("bug"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

ConnectivityManager connectivityManager =
mock(ConnectivityManager.class);

when(connectivityManager.getActiveNetworkInfo()).thenThrow(new SecurityException("bug"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

return new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, activeNetwork.getSubtypeName());
case ConnectivityManager.TYPE_WIFI: // Deprecated in API 28
return new CurrentNetwork(NetworkState.TRANSPORT_WIFI, activeNetwork.getSubtypeName());
case ConnectivityManager.TYPE_VPN:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outside the scope of this specific change, but static imports on these would be nice for readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when I wrote this originally, I wanted to make sure it was clear which of these things were Android's, and which were ours, and keeping the class name prefixes made that a little more clear to me.

if (capabilities == null) {
return NO_NETWORK;
}
String subType = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this change, but looks like the subType should be moved into the if scope block.

@jkwatson
Copy link
Contributor Author

Looks good to me. I read in a book one time that a method should only do one thing, and that exception handling is also a thing. I think that means you might have cleaner code if you factored out the try body to a method...but dealer's choice.

Heh. That method would have to be named something silly, though, wouldn't it? Like "doDetectCurrentNetwork"? or "detectCurrentNetworkUnlessAndroidIsBuggy"? :)

@jkwatson
Copy link
Contributor Author

Looks good to me. I read in a book one time that a method should only do one thing, and that exception handling is also a thing. I think that means you might have cleaner code if you factored out the try body to a method...but dealer's choice.

Heh. That method would have to be named something silly, though, wouldn't it? Like "doDetectCurrentNetwork"? or "detectCurrentNetworkUnlessAndroidIsBuggy"? :)

I guess the other option (probably better) would be to move the exception handling up a level into the ConnectionUtil call. I can make that change if you'd like.

Comment on lines +19 to +20
import static com.splunk.rum.ConnectionUtil.NO_NETWORK;
import static com.splunk.rum.ConnectionUtil.UNKNOWN_NETWORK;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea why IDEA decided to reorder these imports. 🤷🏽

@breedx-splk
Copy link
Contributor

Looks good to me. I read in a book one time that a method should only do one thing, and that exception handling is also a thing. I think that means you might have cleaner code if you factored out the try body to a method...but dealer's choice.

Heh. That method would have to be named something silly, though, wouldn't it? Like "doDetectCurrentNetwork"? or "detectCurrentNetworkUnlessAndroidIsBuggy"? :)

I guess the other option (probably better) would be to move the exception handling up a level into the ConnectionUtil call. I can make that change if you'd like.

I think it's an improvement for sure. I would have gotten rid of the local variable there tho.

@jkwatson
Copy link
Contributor Author

jkwatson commented Sep 27, 2021

I think it's an improvement for sure. I would have gotten rid of the local variable there tho.

oh yeah, that's totally a good change. I had missed altogether that it wasn't even necessary.

@jkwatson jkwatson merged commit 1fd6ea5 into main Sep 27, 2021
@jkwatson jkwatson deleted the guard_against_network_security_exception branch September 27, 2021 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants