@Before
  public void reset() throws Exception {
    // reset Logger defaults:
    LogPersister.unsetContext();

    // clear all shared prefs
    SharedPreferences sharedPreferences =
        RuntimeEnvironment.application.getSharedPreferences(
            LogPersister.SHARED_PREF_KEY, Context.MODE_PRIVATE);
    sharedPreferences.edit().clear().commit();
    System.setProperty("http.agent", "Test user agent");

    PackageManager pm = activity.getPackageManager();
    PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_SIGNATURES);
    pi.signatures = new Signature[] {new Signature("1234567890abcdef")};

    File file = new File(activity.getFilesDir(), FILE_NAME0);
    file.delete();
    file = new File(activity.getFilesDir(), FILE_NAME1);
    file.delete();
    file = new File(activity.getFilesDir(), FILE_NAME0 + ".send");
    file.delete();
    file = new File(activity.getFilesDir(), FILE_NAME1 + ".send");
    file.delete();
    file = new File(activity.getFilesDir(), ANALYTICS_FILE_NAME0);
    file.delete();
    file = new File(activity.getFilesDir(), ANALYTICS_FILE_NAME1);
    file.delete();
    file = new File(activity.getFilesDir(), ANALYTICS_FILE_NAME0 + ".send");
    file.delete();
    file = new File(activity.getFilesDir(), ANALYTICS_FILE_NAME1 + ".send");
    file.delete();

    // some tests below use FakeHttp.addPendingHttpResponse();.  We should clear them after every
    // test.
    FakeHttp.clearPendingHttpResponses();

    // reset the static flags in Logger that prevent accidental double sending of the
    // persistent file contents
    Field f1 = LogPersister.class.getDeclaredField("sendingLogs");
    f1.setAccessible(true);
    f1.set(null, false);
    Field f2 = LogPersister.class.getDeclaredField("sendingAnalyticsLogs");
    f2.setAccessible(true);
    f2.set(null, false);

    Logger.setLogPersister(new LogPersisterDelegate());
  }
Exemplo n.º 2
0
 @Override
 public PackageInfo getPackageInfo(String packageName, int flags) throws RemoteException {
   waitForReadyInner();
   try {
     String pkg = getAndCheckCallingPkg(packageName);
     if (pkg != null) {
       enforcePluginFileExists();
       PluginPackageParser parser = mPluginCache.get(pkg);
       if (parser != null) {
         PackageInfo packageInfo = parser.getPackageInfo(flags);
         if (packageInfo != null
             && (flags & PackageManager.GET_SIGNATURES) != 0
             && packageInfo.signatures == null) {
           packageInfo.signatures = mSignatureCache.get(packageName);
         }
         return packageInfo;
       }
     }
   } catch (Exception e) {
     handleException(e);
   }
   return null;
 }
Exemplo n.º 3
0
  public boolean collectCertificates() {
    WeakReference<byte[]> readBufferRef;
    byte[] readBuffer = null;
    synchronized (this.getClass()) {
      readBufferRef = mReadBuffer;
      if (readBufferRef != null) {
        mReadBuffer = null;
        readBuffer = readBufferRef.get();
      }
      if (readBuffer == null) {
        readBuffer = new byte[8192];
        readBufferRef = new WeakReference<byte[]>(readBuffer);
      }
    }

    try {
      JarFile jarFile = new JarFile(mArchiveSourcePath);

      Certificate[] certs = null;

      Enumeration entries = jarFile.entries();
      while (entries.hasMoreElements()) {
        JarEntry je = (JarEntry) entries.nextElement();
        if (je.isDirectory()) continue;

        String name = je.getName();
        if (name.startsWith("META-INF/")) continue;

        if (mLibDir != null && name.startsWith("lib/") && !name.startsWith(mLibDir)) {
          // Ignore unused ABIs
          continue;
        }

        Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
        if (false) {
          Log.i(
              TAG,
              "File "
                  + mArchiveSourcePath
                  + " entry "
                  + name
                  + ": certs="
                  + certs
                  + " ("
                  + (certs != null ? certs.length : 0)
                  + ")");
        }
        if (localCerts == null) {
          Log.e(
              TAG,
              "Package " + mPackageName + " has no certificates at entry " + name + "; ignoring!");
          jarFile.close();
          return false;
        } else if (certs == null) {
          certs = localCerts;
        } else {
          // Ensure all certificates match.
          for (int i = 0; i < certs.length; i++) {
            boolean found = false;
            for (int j = 0; j < localCerts.length; j++) {
              if (certs[i] != null && certs[i].equals(localCerts[j])) {
                found = true;
                break;
              }
            }
            if (!found || certs.length != localCerts.length) {
              Log.e(
                  TAG,
                  "Package "
                      + mPackageName
                      + " has mismatched certificates at entry "
                      + name
                      + "; ignoring!");
              jarFile.close();
              return false;
            }
          }
        }
      }

      jarFile.close();

      synchronized (this.getClass()) {
        mReadBuffer = readBufferRef;
      }

      if (certs != null && certs.length > 0) {
        final int N = certs.length;
        mPackageInfo.signatures = new Signature[certs.length];
        for (int i = 0; i < N; i++) {
          mPackageInfo.signatures[i] = new Signature(certs[i].getEncoded());
        }
      } else {
        Log.e(TAG, "Package " + mPackageName + " has no certificates; ignoring!");
        return false;
      }
    } catch (CertificateEncodingException e) {
      Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
      return false;
    } catch (IOException e) {
      Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
      return false;
    } catch (RuntimeException e) {
      Log.w(TAG, "Exception reading " + mArchiveSourcePath, e);
      return false;
    }
    return true;
  }