Merge "Add the ability to configure NTP port for tests" am: 72e04a7dc2
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2030083 Change-Id: Ib9ce60d57ccaece7a727e979fe939a80e9880399
This commit is contained in:
@@ -60,7 +60,7 @@ public class SntpClient {
|
|||||||
private static final int TRANSMIT_TIME_OFFSET = 40;
|
private static final int TRANSMIT_TIME_OFFSET = 40;
|
||||||
private static final int NTP_PACKET_SIZE = 48;
|
private static final int NTP_PACKET_SIZE = 48;
|
||||||
|
|
||||||
private static final int NTP_PORT = 123;
|
public static final int STANDARD_NTP_PORT = 123;
|
||||||
private static final int NTP_MODE_CLIENT = 3;
|
private static final int NTP_MODE_CLIENT = 3;
|
||||||
private static final int NTP_MODE_SERVER = 4;
|
private static final int NTP_MODE_SERVER = 4;
|
||||||
private static final int NTP_MODE_BROADCAST = 5;
|
private static final int NTP_MODE_BROADCAST = 5;
|
||||||
@@ -108,18 +108,21 @@ public class SntpClient {
|
|||||||
* Sends an SNTP request to the given host and processes the response.
|
* Sends an SNTP request to the given host and processes the response.
|
||||||
*
|
*
|
||||||
* @param host host name of the server.
|
* @param host host name of the server.
|
||||||
|
* @param port port of the server.
|
||||||
* @param timeout network timeout in milliseconds. the timeout doesn't include the DNS lookup
|
* @param timeout network timeout in milliseconds. the timeout doesn't include the DNS lookup
|
||||||
* time, and it applies to each individual query to the resolved addresses of
|
* time, and it applies to each individual query to the resolved addresses of
|
||||||
* the NTP server.
|
* the NTP server.
|
||||||
* @param network network over which to send the request.
|
* @param network network over which to send the request.
|
||||||
* @return true if the transaction was successful.
|
* @return true if the transaction was successful.
|
||||||
*/
|
*/
|
||||||
public boolean requestTime(String host, int timeout, Network network) {
|
public boolean requestTime(String host, int port, int timeout, Network network) {
|
||||||
final Network networkForResolv = network.getPrivateDnsBypassingCopy();
|
final Network networkForResolv = network.getPrivateDnsBypassingCopy();
|
||||||
try {
|
try {
|
||||||
final InetAddress[] addresses = networkForResolv.getAllByName(host);
|
final InetAddress[] addresses = networkForResolv.getAllByName(host);
|
||||||
for (int i = 0; i < addresses.length; i++) {
|
for (int i = 0; i < addresses.length; i++) {
|
||||||
if (requestTime(addresses[i], NTP_PORT, timeout, networkForResolv)) return true;
|
if (requestTime(addresses[i], port, timeout, networkForResolv)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
Log.w(TAG, "Unknown host: " + host);
|
Log.w(TAG, "Unknown host: " + host);
|
||||||
|
|||||||
@@ -138,6 +138,10 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private String mHostnameForTests;
|
private String mHostnameForTests;
|
||||||
|
|
||||||
|
/** An in-memory config override for use during tests. */
|
||||||
|
@Nullable
|
||||||
|
private Integer mPortForTests;
|
||||||
|
|
||||||
/** An in-memory config override for use during tests. */
|
/** An in-memory config override for use during tests. */
|
||||||
@Nullable
|
@Nullable
|
||||||
private Duration mTimeoutForTests;
|
private Duration mTimeoutForTests;
|
||||||
@@ -163,9 +167,11 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
* Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the
|
* Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the
|
||||||
* test value, i.e. so the normal value will be used next time.
|
* test value, i.e. so the normal value will be used next time.
|
||||||
*/
|
*/
|
||||||
public void setServerConfigForTests(@Nullable String hostname, @Nullable Duration timeout) {
|
public void setServerConfigForTests(
|
||||||
|
@Nullable String hostname, @Nullable Integer port, @Nullable Duration timeout) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
mHostnameForTests = hostname;
|
mHostnameForTests = hostname;
|
||||||
|
mPortForTests = port;
|
||||||
mTimeoutForTests = timeout;
|
mTimeoutForTests = timeout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,8 +201,9 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
|
if (LOGD) Log.d(TAG, "forceRefresh() from cache miss");
|
||||||
final SntpClient client = new SntpClient();
|
final SntpClient client = new SntpClient();
|
||||||
final String serverName = connectionInfo.getServer();
|
final String serverName = connectionInfo.getServer();
|
||||||
|
final int port = connectionInfo.getPort();
|
||||||
final int timeoutMillis = connectionInfo.getTimeoutMillis();
|
final int timeoutMillis = connectionInfo.getTimeoutMillis();
|
||||||
if (client.requestTime(serverName, timeoutMillis, network)) {
|
if (client.requestTime(serverName, port, timeoutMillis, network)) {
|
||||||
long ntpCertainty = client.getRoundTripTime() / 2;
|
long ntpCertainty = client.getRoundTripTime() / 2;
|
||||||
mTimeResult = new TimeResult(
|
mTimeResult = new TimeResult(
|
||||||
client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty);
|
client.getNtpTime(), client.getNtpTimeReference(), ntpCertainty);
|
||||||
@@ -297,10 +304,12 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
private static class NtpConnectionInfo {
|
private static class NtpConnectionInfo {
|
||||||
|
|
||||||
@NonNull private final String mServer;
|
@NonNull private final String mServer;
|
||||||
|
private final int mPort;
|
||||||
private final int mTimeoutMillis;
|
private final int mTimeoutMillis;
|
||||||
|
|
||||||
NtpConnectionInfo(@NonNull String server, int timeoutMillis) {
|
NtpConnectionInfo(@NonNull String server, int port, int timeoutMillis) {
|
||||||
mServer = Objects.requireNonNull(server);
|
mServer = Objects.requireNonNull(server);
|
||||||
|
mPort = port;
|
||||||
mTimeoutMillis = timeoutMillis;
|
mTimeoutMillis = timeoutMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,6 +318,11 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
return mServer;
|
return mServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public int getPort() {
|
||||||
|
return mPort;
|
||||||
|
}
|
||||||
|
|
||||||
int getTimeoutMillis() {
|
int getTimeoutMillis() {
|
||||||
return mTimeoutMillis;
|
return mTimeoutMillis;
|
||||||
}
|
}
|
||||||
@@ -317,6 +331,7 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "NtpConnectionInfo{"
|
return "NtpConnectionInfo{"
|
||||||
+ "mServer='" + mServer + '\''
|
+ "mServer='" + mServer + '\''
|
||||||
|
+ ", mPort='" + mPort + '\''
|
||||||
+ ", mTimeoutMillis=" + mTimeoutMillis
|
+ ", mTimeoutMillis=" + mTimeoutMillis
|
||||||
+ '}';
|
+ '}';
|
||||||
}
|
}
|
||||||
@@ -341,6 +356,13 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final Integer port;
|
||||||
|
if (mPortForTests != null) {
|
||||||
|
port = mPortForTests;
|
||||||
|
} else {
|
||||||
|
port = SntpClient.STANDARD_NTP_PORT;
|
||||||
|
}
|
||||||
|
|
||||||
final int timeoutMillis;
|
final int timeoutMillis;
|
||||||
if (mTimeoutForTests != null) {
|
if (mTimeoutForTests != null) {
|
||||||
timeoutMillis = (int) mTimeoutForTests.toMillis();
|
timeoutMillis = (int) mTimeoutForTests.toMillis();
|
||||||
@@ -350,7 +372,8 @@ public class NtpTrustedTime implements TrustedTime {
|
|||||||
timeoutMillis = Settings.Global.getInt(
|
timeoutMillis = Settings.Global.getInt(
|
||||||
resolver, Settings.Global.NTP_TIMEOUT, defaultTimeoutMillis);
|
resolver, Settings.Global.NTP_TIMEOUT, defaultTimeoutMillis);
|
||||||
}
|
}
|
||||||
return TextUtils.isEmpty(hostname) ? null : new NtpConnectionInfo(hostname, timeoutMillis);
|
return TextUtils.isEmpty(hostname) ? null :
|
||||||
|
new NtpConnectionInfo(hostname, port, timeoutMillis);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Prints debug information. */
|
/** Prints debug information. */
|
||||||
|
|||||||
@@ -293,7 +293,8 @@ public class SntpClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDnsResolutionFailure() throws Exception {
|
public void testDnsResolutionFailure() throws Exception {
|
||||||
assertFalse(mClient.requestTime("ntp.server.doesnotexist.example", 5000, mNetwork));
|
assertFalse(mClient.requestTime("ntp.server.doesnotexist.example",
|
||||||
|
SntpClient.STANDARD_NTP_PORT, 5000, mNetwork));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -196,13 +196,15 @@ public class NetworkTimeUpdateService extends Binder {
|
|||||||
* Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the
|
* Overrides the NTP server config for tests. Passing {@code null} to a parameter clears the
|
||||||
* test value, i.e. so the normal value will be used next time.
|
* test value, i.e. so the normal value will be used next time.
|
||||||
*/
|
*/
|
||||||
void setServerConfigForTests(@Nullable String hostname, @Nullable Duration timeout) {
|
void setServerConfigForTests(
|
||||||
|
@Nullable String hostname, @Nullable Integer port, @Nullable Duration timeout) {
|
||||||
mContext.enforceCallingPermission(
|
mContext.enforceCallingPermission(
|
||||||
android.Manifest.permission.SET_TIME, "set NTP server config for tests");
|
android.Manifest.permission.SET_TIME, "set NTP server config for tests");
|
||||||
|
|
||||||
mLocalLog.log("Setting server config for tests: hostname=" + hostname
|
mLocalLog.log("Setting server config for tests: hostname=" + hostname
|
||||||
|
+ ", port=" + port
|
||||||
+ ", timeout=" + timeout);
|
+ ", timeout=" + timeout);
|
||||||
mTime.setServerConfigForTests(hostname, timeout);
|
mTime.setServerConfigForTests(hostname, port, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPollNetworkTime(int event) {
|
private void onPollNetworkTime(int event) {
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand {
|
|||||||
*/
|
*/
|
||||||
private static final String SHELL_COMMAND_SET_SERVER_CONFIG = "set_server_config";
|
private static final String SHELL_COMMAND_SET_SERVER_CONFIG = "set_server_config";
|
||||||
private static final String SET_SERVER_CONFIG_HOSTNAME_ARG = "--hostname";
|
private static final String SET_SERVER_CONFIG_HOSTNAME_ARG = "--hostname";
|
||||||
|
private static final String SET_SERVER_CONFIG_PORT_ARG = "--port";
|
||||||
private static final String SET_SERVER_CONFIG_TIMEOUT_ARG = "--timeout_millis";
|
private static final String SET_SERVER_CONFIG_TIMEOUT_ARG = "--timeout_millis";
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@@ -87,6 +88,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand {
|
|||||||
|
|
||||||
private int runSetServerConfig() {
|
private int runSetServerConfig() {
|
||||||
String hostname = null;
|
String hostname = null;
|
||||||
|
Integer port = null;
|
||||||
Duration timeout = null;
|
Duration timeout = null;
|
||||||
String opt;
|
String opt;
|
||||||
while ((opt = getNextArg()) != null) {
|
while ((opt = getNextArg()) != null) {
|
||||||
@@ -95,6 +97,10 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand {
|
|||||||
hostname = getNextArgRequired();
|
hostname = getNextArgRequired();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case SET_SERVER_CONFIG_PORT_ARG: {
|
||||||
|
port = Integer.parseInt(getNextArgRequired());
|
||||||
|
break;
|
||||||
|
}
|
||||||
case SET_SERVER_CONFIG_TIMEOUT_ARG: {
|
case SET_SERVER_CONFIG_TIMEOUT_ARG: {
|
||||||
timeout = Duration.ofMillis(Integer.parseInt(getNextArgRequired()));
|
timeout = Duration.ofMillis(Integer.parseInt(getNextArgRequired()));
|
||||||
break;
|
break;
|
||||||
@@ -104,7 +110,7 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mNetworkTimeUpdateService.setServerConfigForTests(hostname, timeout);
|
mNetworkTimeUpdateService.setServerConfigForTests(hostname, port, timeout);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,8 +126,9 @@ class NetworkTimeUpdateServiceShellCommand extends ShellCommand {
|
|||||||
pw.printf(" Refreshes the latest time. Prints whether it was successful.\n");
|
pw.printf(" Refreshes the latest time. Prints whether it was successful.\n");
|
||||||
pw.printf(" %s\n", SHELL_COMMAND_SET_SERVER_CONFIG);
|
pw.printf(" %s\n", SHELL_COMMAND_SET_SERVER_CONFIG);
|
||||||
pw.printf(" Sets the NTP server config for tests. The config is not persisted.\n");
|
pw.printf(" Sets the NTP server config for tests. The config is not persisted.\n");
|
||||||
pw.printf(" Options: [%s <hostname>] [%s <millis>]\n",
|
pw.printf(" Options: [%s <hostname>] [%s <port>] [%s <millis>]\n",
|
||||||
SET_SERVER_CONFIG_HOSTNAME_ARG, SET_SERVER_CONFIG_TIMEOUT_ARG);
|
SET_SERVER_CONFIG_HOSTNAME_ARG, SET_SERVER_CONFIG_PORT_ARG,
|
||||||
|
SET_SERVER_CONFIG_TIMEOUT_ARG);
|
||||||
pw.printf(" Each key/value is optional and must be specified to override the\n");
|
pw.printf(" Each key/value is optional and must be specified to override the\n");
|
||||||
pw.printf(" normal value, not specifying a key causes it to reset to the original.\n");
|
pw.printf(" normal value, not specifying a key causes it to reset to the original.\n");
|
||||||
pw.println();
|
pw.println();
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ import android.net.SntpClient;
|
|||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import libcore.io.Streams;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -37,8 +39,6 @@ import java.net.InetAddress;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import libcore.io.Streams;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Test Service that tries to connect to the web via different methods and outputs the results to
|
* Test Service that tries to connect to the web via different methods and outputs the results to
|
||||||
* the log and a output file.
|
* the log and a output file.
|
||||||
@@ -146,7 +146,7 @@ public class BandwidthEnforcementTestService extends IntentService {
|
|||||||
final ConnectivityManager mCM = context.getSystemService(ConnectivityManager.class);
|
final ConnectivityManager mCM = context.getSystemService(ConnectivityManager.class);
|
||||||
final Network network = mCM.getActiveNetwork();
|
final Network network = mCM.getActiveNetwork();
|
||||||
|
|
||||||
if (client.requestTime("0.pool.ntp.org", 10000, network)) {
|
if (client.requestTime("0.pool.ntp.org", SntpClient.STANDARD_NTP_PORT, 10000, network)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user