/** Make sure we won't pass invalid characters to XML serializer. */
  public void testWriteReadNoCrash() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    XmlSerializer serializer = Xml.newSerializer();
    serializer.setOutput(os, StandardCharsets.UTF_8.name());
    serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
    serializer.startDocument(null, true);

    for (int ch = 0; ch < 0x10000; ch++) {
      checkWriteSingleSetting(
          "char=0x" + Integer.toString(ch, 16), serializer, "key", String.valueOf((char) ch));
    }
    checkWriteSingleSetting(serializer, "k", "");
    checkWriteSingleSetting(serializer, "x", "abc");
    checkWriteSingleSetting(serializer, "abc", CRAZY_STRING);
    checkWriteSingleSetting(serializer, "def", null);

    // Invlid input, but shouoldn't crash.
    checkWriteSingleSetting(serializer, null, null);
    checkWriteSingleSetting(serializer, CRAZY_STRING, null);
    SettingsState.writeSingleSetting(
        SettingsState.SETTINGS_VERSOIN_NEW_ENCODING, serializer, null, "k", "v", "package");
    SettingsState.writeSingleSetting(
        SettingsState.SETTINGS_VERSOIN_NEW_ENCODING, serializer, "1", "k", "v", null);
  }
 @Nullable
 @Override
 public SettingsState getState() {
   final SettingsState saveState = new SettingsState();
   saveState.serverContexts = getServerContextStates();
   saveState.properties = getPropertyStates();
   return saveState;
 }
  /** In version 120, value "null" meant {code NULL}. */
  public void testUpgrade() throws Exception {
    final File file = new File(getContext().getCacheDir(), "setting.xml");
    file.delete();
    final Object lock = new Object();
    final PrintStream os = new PrintStream(new FileOutputStream(file));
    os.print(
        "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>"
            + "<settings version=\"120\">"
            + "  <setting id=\"0\" name=\"k0\" value=\"null\" package=\"null\" />"
            + "  <setting id=\"1\" name=\"k1\" value=\"\" package=\"\" />"
            + "  <setting id=\"2\" name=\"k2\" value=\"v2\" package=\"p2\" />"
            + "</settings>");
    os.close();

    final SettingsState ss =
        new SettingsState(
            lock,
            file,
            1,
            SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED,
            Looper.getMainLooper());
    synchronized (lock) {
      SettingsState.Setting s;
      s = ss.getSettingLocked("k0");
      assertEquals(null, s.getValue());
      assertEquals("null", s.getPackageName());

      s = ss.getSettingLocked("k1");
      assertEquals("", s.getValue());
      assertEquals("", s.getPackageName());

      s = ss.getSettingLocked("k2");
      assertEquals("v2", s.getValue());
      assertEquals("p2", s.getPackageName());
    }
  }
  public void testIsBinary() {
    assertFalse(SettingsState.isBinary(" abc 日本語"));

    for (char ch = 0x20; ch < 0xd800; ch++) {
      assertFalse("ch=" + Integer.toString(ch, 16), SettingsState.isBinary(String.valueOf(ch)));
    }
    for (char ch = 0xe000; ch < 0xfffe; ch++) {
      assertFalse("ch=" + Integer.toString(ch, 16), SettingsState.isBinary(String.valueOf(ch)));
    }

    for (char ch = 0x0000; ch < 0x20; ch++) {
      assertTrue("ch=" + Integer.toString(ch, 16), SettingsState.isBinary(String.valueOf(ch)));
    }
    for (char ch = 0xd800; ch < 0xe000; ch++) {
      assertTrue("ch=" + Integer.toString(ch, 16), SettingsState.isBinary(String.valueOf(ch)));
    }
    assertTrue(SettingsState.isBinary("\ufffe"));
    assertTrue(SettingsState.isBinary("\uffff"));
    try {
      assertFalse(SettingsState.isBinary(null));
      fail("NullPointerException expected");
    } catch (NullPointerException expected) {
    }
  }
  /** Make sure settings can be written to a file and also can be read. */
  public void testReadWrite() {
    final File file = new File(getContext().getCacheDir(), "setting.xml");
    file.delete();
    final Object lock = new Object();

    final SettingsState ssWriter =
        new SettingsState(
            lock,
            file,
            1,
            SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED,
            Looper.getMainLooper());
    ssWriter.setVersionLocked(SettingsState.SETTINGS_VERSOIN_NEW_ENCODING);

    ssWriter.insertSettingLocked("k1", "\u0000", "package");
    ssWriter.insertSettingLocked("k2", "abc", "p2");
    ssWriter.insertSettingLocked("k3", null, "p2");
    ssWriter.insertSettingLocked("k4", CRAZY_STRING, "p3");
    synchronized (lock) {
      ssWriter.persistSyncLocked();
    }

    final SettingsState ssReader =
        new SettingsState(
            lock,
            file,
            1,
            SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED,
            Looper.getMainLooper());
    synchronized (lock) {
      assertEquals("\u0000", ssReader.getSettingLocked("k1").getValue());
      assertEquals("abc", ssReader.getSettingLocked("k2").getValue());
      assertEquals(null, ssReader.getSettingLocked("k3").getValue());
      assertEquals(CRAZY_STRING, ssReader.getSettingLocked("k4").getValue());
    }
  }
 private void checkWriteSingleSetting(
     String msg, XmlSerializer serializer, String key, String value) throws Exception {
   // Make sure the XML serializer won't crash.
   SettingsState.writeSingleSetting(
       SettingsState.SETTINGS_VERSOIN_NEW_ENCODING, serializer, "1", key, value, "package");
 }