Exemplo n.º 1
0
 public void testMakeUriString() throws IOException {
   // Simple user name and command
   EasSyncService svc = setupService(USER);
   String uriString = svc.makeUriString("Sync", null);
   // These next two should now be cached
   assertNotNull(svc.mAuthString);
   assertNotNull(svc.mUserString);
   assertEquals(
       "Basic " + Base64.encodeToString((USER + ":" + PASSWORD).getBytes(), Base64.NO_WRAP),
       svc.mAuthString);
   assertEquals(
       "&User="******"&DeviceId=" + ID + "&DeviceType=" + EasSyncService.DEVICE_TYPE,
       svc.mUserString);
   assertEquals(
       "https://" + HOST + "/Microsoft-Server-ActiveSync?Cmd=Sync" + svc.mUserString, uriString);
   // User name that requires encoding
   String user = "******";
   svc = setupService(user);
   uriString = svc.makeUriString("Sync", null);
   assertEquals(
       "Basic " + Base64.encodeToString((user + ":" + PASSWORD).getBytes(), Base64.NO_WRAP),
       svc.mAuthString);
   String safeUserName = "******";
   assertEquals(
       "&User="******"&DeviceId=" + ID + "&DeviceType=" + EasSyncService.DEVICE_TYPE,
       svc.mUserString);
   assertEquals(
       "https://" + HOST + "/Microsoft-Server-ActiveSync?Cmd=Sync" + svc.mUserString, uriString);
 }
Exemplo n.º 2
0
 public void testAddHeaders() {
   HttpRequestBase method = new HttpPost();
   EasSyncService svc = new EasSyncService();
   svc.mAuthString = "auth";
   svc.mProtocolVersion = "12.1";
   svc.mAccount = null;
   // With second argument false, there should be no header
   svc.setHeaders(method, false);
   Header[] headers = method.getHeaders("X-MS-PolicyKey");
   assertEquals(0, headers.length);
   // With second argument true, there should always be a header
   // The value will be "0" without an account
   method.removeHeaders("X-MS-PolicyKey");
   svc.setHeaders(method, true);
   headers = method.getHeaders("X-MS-PolicyKey");
   assertEquals(1, headers.length);
   assertEquals("0", headers[0].getValue());
   // With an account, but null security key, the header's value should be "0"
   Account account = new Account();
   account.mSecuritySyncKey = null;
   svc.mAccount = account;
   method.removeHeaders("X-MS-PolicyKey");
   svc.setHeaders(method, true);
   headers = method.getHeaders("X-MS-PolicyKey");
   assertEquals(1, headers.length);
   assertEquals("0", headers[0].getValue());
   // With an account and security key, the header's value should be the security key
   account.mSecuritySyncKey = "key";
   svc.mAccount = account;
   method.removeHeaders("X-MS-PolicyKey");
   svc.setHeaders(method, true);
   headers = method.getHeaders("X-MS-PolicyKey");
   assertEquals(1, headers.length);
   assertEquals("key", headers[0].getValue());
 }
Exemplo n.º 3
0
 public void testSearchGal() throws Exception {
   setupAccountMailboxAndMessages(2);
   GalResult result = EasSyncService.searchGal(mProviderContext, mAccount.mId, "search", 12);
   assertNotNull(result);
   assertEquals(10, result.total);
   result = EasSyncService.searchGal(mProviderContext, mAccount.mId - 1, "search", 12);
   assertNull(result);
 }
Exemplo n.º 4
0
 private EasSyncService setupService(String user) {
   EasSyncService svc = new EasSyncService();
   svc.mUserName = user;
   svc.mPassword = PASSWORD;
   svc.mDeviceId = ID;
   svc.mHostAddress = HOST;
   return svc;
 }
Exemplo n.º 5
0
 public void testValidateAccount() throws Exception {
   setupAccountMailboxAndMessages(0);
   HostAuth auth = new HostAuth();
   auth.mAddress = mAccount.mEmailAddress;
   auth.mProtocol = "eas";
   auth.mLogin = mAccount.mDisplayName;
   auth.mPassword = "******";
   auth.mPort = 80;
   EasSyncService eas = getTestService(mAccount, mMailbox);
   Bundle bundle = eas.validateAccount(auth, mProviderContext);
   assertEquals("14.1", bundle.get(EmailServiceProxy.VALIDATE_BUNDLE_PROTOCOL_VERSION));
   assertEquals(
       MessagingException.NO_ERROR, bundle.get(EmailServiceProxy.VALIDATE_BUNDLE_RESULT_CODE));
 }
Exemplo n.º 6
0
    private boolean parseStore() throws IOException {
      EmailSyncAdapter adapter = new EmailSyncAdapter(mService);
      EasEmailSyncParser parser = new EasEmailSyncParser(this, adapter);
      ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
      boolean res = false;

      while (nextTag(Tags.SEARCH_STORE) != END) {
        if (tag == Tags.SEARCH_STATUS) {
          getValue();
        } else if (tag == Tags.SEARCH_TOTAL) {
          mTotalResults = getValueInt();
        } else if (tag == Tags.SEARCH_RESULT) {
          parseResult(parser, ops);
        } else {
          skipTag();
        }
      }

      try {
        adapter.mContentResolver.applyBatch(EmailContent.AUTHORITY, ops);
        /// M: get the current result count.
        mCurrentResults = ops.size();
        if (Eas.USER_LOG) {
          mService.userLog("Saved " + ops.size() + " search results");
        }
      } catch (RemoteException e) {
        LogUtils.d(Logging.LOG_TAG, "RemoteException while saving search results.");
      } catch (OperationApplicationException e) {
      }

      return res;
    }
Exemplo n.º 7
0
  public void testTryAutodiscover() throws RemoteException, IOException {
    String userName = "******";
    String password = "******";

    EasSyncService service = new EasSyncService();
    Bundle bundle1 = service.tryAutodiscover(userName, password);
    Bundle bundle2 = new Bundle();
    bundle2.putInt(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_ERROR_CODE, MessagingException.NO_ERROR);
    HostAuth hostAuth = new HostAuth();
    hostAuth.mAddress = "google.com";
    hostAuth.mLogin = userName;
    hostAuth.mPassword = password;
    hostAuth.mPort = 443;
    hostAuth.mProtocol = "eas";
    hostAuth.mFlags = HostAuth.FLAG_SSL | HostAuth.FLAG_AUTHENTICATE;
    bundle2.putParcelable(EmailServiceProxy.AUTO_DISCOVER_BUNDLE_HOST_AUTH, hostAuth);
    assertEquals(bundle2.toString(), bundle1.toString());
  }
Exemplo n.º 8
0
  public void testGetServiceForMailbox() throws Exception {
    ArrayList<Long> iList = setupAccountMailboxAndMessages(2);
    Body body =
        EmailContentSetupUtils.setupBody(
            "body", iList.get(0), iList.get(1), true, mProviderContext);

    setupSyncParserAndAdapter(mAccount, mMailbox);
    EasSyncService service = EasSyncService.getServiceForMailbox(mProviderContext, mMailbox);
    Thread thread = new Thread(service);
    thread.start();
    assertTrue(service instanceof EasOutboxService);
    mMailbox.mType = Mailbox.TYPE_EAS_ACCOUNT_MAILBOX;
    service = EasSyncService.getServiceForMailbox(mProviderContext, mMailbox);
    assertTrue(service instanceof EasAccountService);
    mMailbox.mType = Mailbox.TYPE_INBOX;
    service = EasSyncService.getServiceForMailbox(mProviderContext, mMailbox);
    assertTrue(service instanceof EasSyncService);
    TestUtils.sleepAndWait(5000);
  }
Exemplo n.º 9
0
  /** M: Test the sync function */
  public void testSync() throws IOException {
    ArrayList<Long> ids = setupAccountMailboxAndMessages(4);
    setupSyncParserAndAdapter(mAccount, mMailbox);
    mSyncAdapter.mService = EasSyncService.setupServiceForAccount(mProviderContext, mAccount);
    mSyncAdapter.mService.mMailbox = mMailbox;
    mSyncAdapter.mService.mContentResolver = mResolver;
    MessageMoveRequest req1 = new MessageMoveRequest(ids.get(0), mMailbox.mId);
    FetchMailRequest req2 = new FetchMailRequest(ids.get(1));
    MeetingResponseRequest req3 = new MeetingResponseRequest(ids.get(2), 2);
    Attachment att = new Attachment();
    att.mId = 1;
    att.mLocation = "location1";
    att.mMessageKey = ids.get(3);
    att.mAccountKey = mAccount.mId;
    PartRequest req4 = new PartRequest(att, "dest1", "content1");

    ContentValues cv = new ContentValues();
    cv.put(Message.MAILBOX_KEY, 2);
    mResolver.update(
        ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, ids.get(0)), cv, null, null);

    Message msg = Message.restoreMessageWithId(mProviderContext, ids.get(2));
    Exchange.sBadSyncKeyMailboxId = mMailbox.mId;
    mSyncAdapter.mService.addRequest(req4);
    mSyncAdapter.mService.sync(mSyncAdapter);
    mSyncAdapter.mService.clearRequests();
    mSyncAdapter.mService.addRequest(req1);
    mSyncAdapter.mService.addRequest(req2);
    mSyncAdapter.mService.addRequest(req3);
    mSyncAdapter.mService.mProtocolVersionDouble = 12.0;
    mSyncAdapter.mService.sync(mSyncAdapter);
    mSyncAdapter.mService.clearRequests();
    mSyncAdapter.mService.addRequest(req4);
    mSyncAdapter.mService.mProtocolVersionDouble = 2.5;
    mSyncAdapter.mService.sync(mSyncAdapter);
    assertEquals(1, mSyncAdapter.mService.getSendStatus());
    assertEquals(Eas.EAS_SYNC_RECOVER, mSyncAdapter.mService.getSnycStatus());
  }
Exemplo n.º 10
0
 public void testEasService() throws Exception, IOException {
   setupAccountMailboxAndMessages(0);
   setupSyncParserAndAdapter(mAccount, mMailbox);
   mSyncAdapter.mService.setupService();
   EasSyncService svc = mSyncAdapter.mService;
   Attachment att = new Attachment();
   att.mId = 100;
   att.mLocation = "location1";
   PartRequest req = new PartRequest(att, "dest1", "content1");
   svc.addRequest(req);
   svc.cancelPartRequest(att.mId);
   EasSyncService.trustAllHttpsCertificates();
   int num = MockEasResponse.getSettingNums();
   assertTrue(svc.getSendingStatus());
   assertEquals(num + 1, MockEasResponse.getSettingNums());
   MockEasResponse.setSettingNums(num);
   svc.resetSecurityPolicies();
   String reString = UriCodec.decode("1234%4234");
   assertEquals("1234B34", reString);
 }
Exemplo n.º 11
0
  public static int searchMessages(
      Context context, long accountId, SearchParams searchParams, long destMailboxId) {
    // Sanity check for arguments
    final int offset = searchParams.mOffset;
    final int limit = searchParams.mLimit;
    final String filter = searchParams.mFilter;
    if (limit < 0 || limit > MAX_SEARCH_RESULTS || offset < 0) return 0;
    // TODO Should this be checked in UI?  Are there guidelines for minimums?
    if (filter == null || filter.length() < MIN_QUERY_LENGTH) return 0;

    int res = 0;
    final Account account = Account.restoreAccountWithId(context, accountId);
    if (account == null) return res;
    final EasSyncService svc = EasSyncService.setupServiceForAccount(context, account);
    if (svc == null) return res;
    final Mailbox searchMailbox = Mailbox.restoreMailboxWithId(context, destMailboxId);
    // Sanity check; account might have been deleted?
    if (searchMailbox == null) return res;
    final ContentValues statusValues = new ContentValues(2);
    try {
      // Set the status of this mailbox to indicate query
      statusValues.put(Mailbox.UI_SYNC_STATUS, UIProvider.SyncStatus.LIVE_QUERY);
      searchMailbox.update(context, statusValues);

      svc.mMailbox = searchMailbox;
      svc.mAccount = account;
      final Serializer s = new Serializer();
      s.start(Tags.SEARCH_SEARCH).start(Tags.SEARCH_STORE);
      s.data(Tags.SEARCH_NAME, "Mailbox");
      s.start(Tags.SEARCH_QUERY).start(Tags.SEARCH_AND);
      s.data(Tags.SYNC_CLASS, "Email");

      // If this isn't an inbox search, then include the collection id
      final Mailbox inbox = Mailbox.restoreMailboxOfType(context, accountId, Mailbox.TYPE_INBOX);
      if (inbox == null) return 0;
      if (searchParams.mMailboxId != inbox.mId) {
        s.data(Tags.SYNC_COLLECTION_ID, inbox.mServerId);
      }

      s.data(Tags.SEARCH_FREE_TEXT, filter);

      // Add the date window if appropriate
      if (searchParams.mStartDate != null) {
        s.start(Tags.SEARCH_GREATER_THAN);
        s.tag(Tags.EMAIL_DATE_RECEIVED);
        s.data(Tags.SEARCH_VALUE, Eas.DATE_FORMAT.format(searchParams.mStartDate));
        s.end(); // SEARCH_GREATER_THAN
      }
      if (searchParams.mEndDate != null) {
        s.start(Tags.SEARCH_LESS_THAN);
        s.tag(Tags.EMAIL_DATE_RECEIVED);
        s.data(Tags.SEARCH_VALUE, Eas.DATE_FORMAT.format(searchParams.mEndDate));
        s.end(); // SEARCH_LESS_THAN
      }
      s.end().end(); // SEARCH_AND, SEARCH_QUERY
      s.start(Tags.SEARCH_OPTIONS);
      if (offset == 0) {
        s.tag(Tags.SEARCH_REBUILD_RESULTS);
      }
      if (searchParams.mIncludeChildren) {
        s.tag(Tags.SEARCH_DEEP_TRAVERSAL);
      }
      // Range is sent in the form first-last (e.g. 0-9)
      s.data(Tags.SEARCH_RANGE, offset + "-" + (offset + limit - 1));
      s.start(Tags.BASE_BODY_PREFERENCE);
      s.data(Tags.BASE_TYPE, Eas.BODY_PREFERENCE_HTML);
      s.data(Tags.BASE_TRUNCATION_SIZE, "20000");
      s.end(); // BASE_BODY_PREFERENCE
      s.end().end().end().done(); // SEARCH_OPTIONS, SEARCH_STORE, SEARCH_SEARCH
      final EasResponse resp = svc.sendHttpClientPost("Search", s.toByteArray());
      try {
        final int code = resp.getStatus();
        if (code == HttpStatus.SC_OK) {
          final InputStream is = resp.getInputStream();
          try {
            final SearchParser sp = new SearchParser(is, svc, filter);
            sp.parse();
            res = sp.getTotalResults();
            /** M: Set and update mailbox flag. @{ */
            int currentCount = offset + sp.getCurrentResults();
            boolean allMessagesLoaded = false;
            if (currentCount >= res) {
              allMessagesLoaded = true;
            }
            searchMailbox.updateAllMessageDownloadFlag(context, allMessagesLoaded);
            /** @} */
          } finally {
            is.close();
          }
        } else {
          svc.userLog("Search returned " + code);
        }
      } finally {
        resp.close();
      }
    } catch (IOException e) {
      svc.userLog("Search exception " + e);
    } finally {
      // TODO: Handle error states
      // Set the status of this mailbox to indicate query over
      statusValues.put(Mailbox.SYNC_TIME, System.currentTimeMillis());
      statusValues.put(Mailbox.UI_SYNC_STATUS, UIProvider.SyncStatus.NO_SYNC);
      searchMailbox.update(context, statusValues);
    }
    // Return the total count
    return res;
  }
 public void userLog(String... strings) {
   mService.userLog(strings);
 }
Exemplo n.º 13
0
 public void incrementChangeCount() {
   EasSyncService var1 = this.mService;
   int var2 = var1.mChangeCount + 1;
   var1.mChangeCount = var2;
 }