Added a helper APK to help develop AccountCheckHostSideTest.

Test: Build.  The app is not used or installed unless manually
installed following the attached README.txt.
Bug 28928996

Change-Id: I2219878fe1b00e8fb23ef266541109340e788d74
diff --git a/hostsidetests/devicepolicy/app/AccountCheck/Tester/Android.mk b/hostsidetests/devicepolicy/app/AccountCheck/Tester/Android.mk
new file mode 100644
index 0000000..8036348
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/AccountCheck/Tester/Android.mk
@@ -0,0 +1,34 @@
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+# Tag this module as a cts test artifact
+LOCAL_COMPATIBILITY_SUITE := cts
+
+LOCAL_PACKAGE_NAME := CtsAccountCheckAuthAppTester
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 ctstestrunner ub-uiautomator android-support-test
+
+LOCAL_SDK_VERSION := test_current
+
+include $(BUILD_CTS_PACKAGE)
diff --git a/hostsidetests/devicepolicy/app/AccountCheck/Tester/AndroidManifest.xml b/hostsidetests/devicepolicy/app/AccountCheck/Tester/AndroidManifest.xml
new file mode 100644
index 0000000..a6ea4aa
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/AccountCheck/Tester/AndroidManifest.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.cts.devicepolicy.accountcheck.tester">
+
+    <!-- GET_ACCOUNTS may stop working.  Targeting at 25 may prevent it. -->
+    <uses-sdk android:minSdkVersion="25" android:targetSdkVersion="25" />
+
+    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
+    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
+    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
+
+    <application android:label="@string/label">
+        <service android:name=".TestAuthenticator"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.accounts.AccountAuthenticator" />
+            </intent-filter>
+
+            <meta-data android:name="android.accounts.AccountAuthenticator"
+                       android:resource="@xml/authenticator" />
+        </service>
+    </application>
+
+    <instrumentation
+        android:name="android.support.test.runner.AndroidJUnitRunner"
+        android:targetPackage="com.android.cts.devicepolicy.accountcheck.auth" />
+</manifest>
+
diff --git a/hostsidetests/devicepolicy/app/AccountCheck/Tester/README.txt b/hostsidetests/devicepolicy/app/AccountCheck/Tester/README.txt
new file mode 100644
index 0000000..bfb6e3a
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/AccountCheck/Tester/README.txt
@@ -0,0 +1,31 @@
+This package helps creating accounts with DPM.ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED / DISALLOWED.
+
+
+Note: AccountCheckHostSideTest should pass even with a pre-existing ALLOWED account.  Meaning, even
+after you followed the below steps to add an ALLOWED account, AccountCheckHostSideTest should
+still pass.
+
+- Build
+$ mmma -j cts/hostsidetests/devicepolicy/app/AccountCheck/Tester/
+
+- Install
+$ adb install  -r -g  ${ANDROID_PRODUCT_OUT}/data/app/CtsAccountCheckAuthAppTester/CtsAccountCheckAuthAppTester.apk
+
+
+- Add an account with DEVICE_OR_PROFILE_OWNER_ALLOWED.
+adb shell am startservice -a add_account \
+    --esa features android.account.DEVICE_OR_PROFILE_OWNER_ALLOWED \
+    com.android.cts.devicepolicy.accountcheck.tester/.TestAuthenticator
+
+- Add an account with DEVICE_OR_PROFILE_OWNER_DISALLOWED.
+adb shell am startservice -a add_account \
+    --esa features android.account.DEVICE_OR_PROFILE_OWNER_DISALLOWED \
+    com.android.cts.devicepolicy.accountcheck.tester/.TestAuthenticator
+
+- Verify
+$ dumpsys-account
+User UserInfo{0:Owner:13}:
+  Accounts: 1
+    Account {name=8894956487610:android.account.DEVICE_OR_PROFILE_OWNER_ALLOWED, type=com.android.cts.devicepolicy.authcheck.tester}
+
+
diff --git a/hostsidetests/devicepolicy/app/AccountCheck/Tester/res/values/strings.xml b/hostsidetests/devicepolicy/app/AccountCheck/Tester/res/values/strings.xml
new file mode 100644
index 0000000..97496df
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/AccountCheck/Tester/res/values/strings.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources
+    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <string name="label">accountcheck.tester</string>
+</resources>
\ No newline at end of file
diff --git a/hostsidetests/devicepolicy/app/AccountCheck/Tester/res/xml/authenticator.xml b/hostsidetests/devicepolicy/app/AccountCheck/Tester/res/xml/authenticator.xml
new file mode 100644
index 0000000..e139b6e
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/AccountCheck/Tester/res/xml/authenticator.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- The attributes in this XML file provide configuration information -->
+<!-- for the Account Manager. -->
+
+<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:accountType="com.android.cts.devicepolicy.authcheck.tester"
+    android:label="@string/label"
+/>
diff --git a/hostsidetests/devicepolicy/app/AccountCheck/Tester/src/com/android/cts/devicepolicy/accountcheck/tester/TestAuthenticator.java b/hostsidetests/devicepolicy/app/AccountCheck/Tester/src/com/android/cts/devicepolicy/accountcheck/tester/TestAuthenticator.java
new file mode 100644
index 0000000..8ad32c1
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/AccountCheck/Tester/src/com/android/cts/devicepolicy/accountcheck/tester/TestAuthenticator.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.devicepolicy.accountcheck.tester;
+
+import android.accounts.AbstractAccountAuthenticator;
+import android.accounts.Account;
+import android.accounts.AccountAuthenticatorResponse;
+import android.accounts.AccountManager;
+import android.accounts.NetworkErrorException;
+import android.app.Service;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.os.SystemClock;
+import android.text.TextUtils;
+import android.util.Log;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class TestAuthenticator extends Service {
+    private static final String TAG = "TestAuthenticator";
+
+    private static final String ACCOUNT_TYPE = "com.android.cts.devicepolicy.authcheck.tester";
+
+
+    private static Authenticator sInstance;
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        if (sInstance == null) {
+            sInstance = new Authenticator(getApplicationContext());
+
+        }
+        return sInstance.getIBinder();
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        if ("add_account".equals(intent.getAction())) {
+            final String[] features = intent.getStringArrayExtra("features");
+            createAccount(this, ACCOUNT_TYPE, features);
+        }
+
+        stopSelf();
+        return 0;
+    }
+
+    private static Account createAccount(Context context, String accountType,
+            String[] requiredFeatures) {
+        if (requiredFeatures == null) {
+            requiredFeatures = new String[0];
+        }
+
+        final String name = SystemClock.elapsedRealtimeNanos()
+                + ":" + TextUtils.join(",", requiredFeatures);
+
+        Log.i(TAG, "Adding account '" + name + "' for " + accountType
+                + "... " + Arrays.asList(requiredFeatures));
+
+        final Account account = new Account(name, accountType);
+        context.getSystemService(AccountManager.class).addAccountExplicitly(
+                account, "password", new Bundle());
+        return account;
+    }
+
+    public static class Authenticator extends AbstractAccountAuthenticator {
+
+        private final Context mContxet;
+
+        public Authenticator(Context context) {
+            super(context);
+            mContxet = context;
+        }
+
+        @Override
+        public Bundle addAccount(AccountAuthenticatorResponse response, String accountType,
+                String authTokenType, String[] requiredFeatures, Bundle options) {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account,
+                String authTokenType, Bundle options) throws NetworkErrorException {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
+                Bundle options) throws NetworkErrorException {
+            return new Bundle();
+        }
+
+        @Override
+        public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
+                String authTokenType, Bundle options) throws NetworkErrorException {
+            return new Bundle();
+        }
+
+        @Override
+        public String getAuthTokenLabel(String authTokenType) {
+            return "token_label";
+        }
+
+        @Override
+        public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account,
+                String[] features) throws NetworkErrorException {
+
+            final int p = account.name.indexOf(':');
+
+            boolean hasAll = true;
+            final List<String> hasFeatures =
+                    Arrays.asList(TextUtils.split(account.name.substring(p + 1), ","));
+            for (String requested : features) {
+                if (!hasFeatures.contains(requested)) {
+                    hasAll = false;
+                    break;
+                }
+            }
+
+            Log.i(TAG, "Checking feature for account '" + account + "' for features="
+                    + Arrays.asList(features) + " result=" + hasAll);
+
+            Bundle result = new Bundle();
+            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, hasAll);
+            return result;
+        }
+    }
+}