43 Commits

Author SHA1 Message Date
Peter Serwylo
2b2958f89c Added explicit test for null permissions.
This wouldn'tve actually found the problem in the previous commit,
due to the null happening before checking permissions while logging perms.
However, still seems like a nice test to have so that the method itself
handles nulls correctly.
2016-10-16 20:52:42 +11:00
Hans-Christoph Steiner
6f0c9ff88a support extended 'uses-permissions' tags in APKs
<uses-permissions/> tags can have min and max SDK to take effect.  This is
not supported currently, and it necessary especially with the privileged
installer so it can properly represent the permissions that an APK is
requesting.

For example:
<uses-permission
  android:name="android.permission.MANAGE_ACCOUNTS"
  android:maxSdkVersion="22" />
<uses-permission-sdk-23
  android:name="android.permission.CAMERA" />
<uses-permission-sdk-23
  android:name="android.permission.CALL_PHONE"
  android:maxSdkVersion="23" />
2016-10-11 08:44:51 +02:00
Hans-Christoph Steiner
d7022dd498 rename Apk.permissions to requestedPermissions like PackageInfo
android.content.pm.PackageInfo is the Android class for representing data
about an APK/package.  Since Apk.permission is the same thing, we should
use the same name.
2016-10-11 08:44:51 +02:00
Hans-Christoph Steiner
4c7322d917 throw in extra test URLs for HttpDownloaderTest
These are commented out so that the tests aren't brittle when they run on
the CI builds.  But are easy to uncomment when testing locally.
2016-09-28 11:35:57 +02:00
Hans-Christoph Steiner
bad613fbc1 move HttpDownloaderTest to emulator, AndroidOS has oddities
There are oddities with the way that Android has implemented the network
stack, as compared to OpenJDK or Oracle JDK.  So running the tests on the
local JVM, i.e. Robolectric, will not provide good test coverage for real
world use cases.
2016-09-28 11:35:57 +02:00
Hans-Christoph Steiner
ecd98047da fix emulator tests on android-10, closes #769
We have to work harder to get writeable dirs in the android-10 emulator.
2016-09-19 14:56:07 +02:00
Hans-Christoph Steiner
40541fadfe clearOldFiles() test must run on the emulator
It seems that Robolectric does not implement android.system.Os so the
atime checking code cannot be tested there.  Works fine on the emulator.
2016-08-16 12:43:31 +02:00
Dominik Schürmann
9235462e34 Tests for ApkVerifier 2016-08-09 22:26:39 +02:00
Peter Serwylo
64bc13de8a Use symlink instead of hardlink, which was accidentally used on API < 19.
At the same time, also changed visibility of methods to package local
to remove need for test class.
2016-06-23 11:14:43 +10:00
Peter Serwylo
4e73d1e5e6 Finish porting tests to Robolectric, and appease PMD.
To appease PMD, we now have a three rulesets in `config/pmd/*.xml`:

 * `rules.xml`: The bulk of the rules, used by both main and test code.
 * `rules-main.xml`: Rules specific to the andoid client code.
 * `rules-test.xml`: Rules specific to test code.

The rationale is because checkstyle by default checks for "too many static
imports", which is a fair call. However in JUnit4 code, it is common to
import many `assert*` static methods.
2016-06-09 10:44:40 +10:00
Peter Serwylo
253900e927 Multi-repo updater ported to robolectric.
The tests pass, but there is a lingering message that gets logged:

```
Jun 08, 2016 7:31:13 AM com.almworks.sqlite4java.Internal log
WARNING: [sqlite] [DETACH DATABASE temp_update_db]DB[1][C]: exception when clearing
com.almworks.sqlite4java.SQLiteException: [1] DB[1] reset [no such database: temp_update_db]
	at com.almworks.sqlite4java.SQLiteConnection.throwResult(SQLiteConnection.java:1309)
	at com.almworks.sqlite4java.SQLiteConnection.throwResult(SQLiteConnection.java:1282)
	at com.almworks.sqlite4java.SQLiteConnection.cacheStatementHandle(SQLiteConnection.java:1211)
	at com.almworks.sqlite4java.SQLiteConnection.access$900(SQLiteConnection.java:54)
	at com.almworks.sqlite4java.SQLiteConnection$CachedController.dispose(SQLiteConnection.java:1606)
	at com.almworks.sqlite4java.SQLiteStatement.dispose(SQLiteStatement.java:187)
	at org.robolectric.shadows.ShadowSQLiteConnection$Connections$4.call(ShadowSQLiteConnection.java:421)
	at org.robolectric.shadows.ShadowSQLiteConnection$Connections$6.call(ShadowSQLiteConnection.java:449)
	at org.robolectric.shadows.ShadowSQLiteConnection$Connections$6.call(ShadowSQLiteConnection.java:443)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
```

The `temp_update_db` is the one used for repo updates, but I thought that it
correctly gets dropped/detached by the `TempAppProvider` when required. In fact,
given the nature of the error message (no such database: temp_update_db), that
hints at the fact that it is indeed dropped. I'm struggling to figure out what
causes this, but it should not be harmful to the running of the tests. If a test
actually fails, then it is picked up correctly by JUnit.
2016-06-09 10:44:40 +10:00
Peter Serwylo
660ebc5ec8 Migrated to robolectric.
This required changing the SAX parser to be namespace aware, as it seems
that is the default in Android, but not the default in the OpenJDK.
2016-06-09 10:44:40 +10:00
Peter Serwylo
60451a050f Remove unused test code.
Many of the `Mock*` classes are there to deal with idiosyncrosies of
the Android SDK, including `final`/package local/`@Hide` annotations/etc.
They are no longer required with robolectric tests.
2016-06-09 10:44:40 +10:00
Peter Serwylo
839ebebd87 Migrated Apk tests to robolectric.
Relatively straightforward port, nothing particularly special here.
2016-06-09 10:44:40 +10:00
Peter Serwylo
4e66bb810f Ported AppProvider tests to Robolectric.
Get around silly `final` methods in `ContentResolver` with Mockito and `delegatesTo`.

The Robolectric library presumes that people always want to test content providers by
manually invoking the `query`/`update`/`delete` methods on the `ShadowContentResolver`.
While that is a great feature for testing, we have helper methods that require testing,
and these methods accept either a _real_ `ContentResolver` or `Context`. Robolectric
did some cool magic in terms of intercepting runtime calls to content resolvers and
forwarding them to the "shadow" verison, to deal with final/package private/etc methods.
However, as a side effect, the `ShadowContentProvider` _is not a `ContentProvider` as
far as the Java compiler is concerned.

By utilising Mockito + `delegatesTo` method, we are able to achieve what is required:
 * An actual `ContentProvider` instance.
 * It forwards calls to the `ShadowContentProvider` provided by Robolectric.
2016-06-09 10:44:40 +10:00
Peter Serwylo
09fd3d188c Robolectric testing support + InstallAppProvider testing now run in JVM.
Robolectric provides testing support for Android via the JVM, including testing
of content providers. In order to get these tests to work, we need to avoid
the default behaviour of starting up FDroidApp.onCreate(). This method has a lot
of static state which fails if set multiple times. Instead of trying to ensure
we correctly zero out that state each test, it is preferable to instead never
bother with that in the first place. Expecially when that is not what is under
test (as is the case with content provider tests).
2016-06-09 10:44:40 +10:00
Peter Serwylo
fa7f57a18a Remove unused test code.
Since refactoring the installed app cache stuff, these methods are no longer
required for testing purposes. This is because the tests directly ask the
content provider to insert relevant apps, rather than testing the broadcast
receiving functionality.
2016-06-02 20:20:28 +10:00
Hans-Christoph Steiner
90467bf8bf InstalledAppProvider: store APK hash and last update time
The APK hash is useful for comparing whether something is exactly the same
file as something else.  For example, to compare whether the installed APK
matches something that f-droid.org hosts.  The "last update time" is a fast
way to check whether the information is current.
2016-06-02 10:22:07 +02:00
Hans-Christoph Steiner
d734e584f6 InstalledAppProviderService to replace InstalledAppCacheUpdater
InstalledAppCacheUpdater was a custom Service-like thing with some
threading issues.  InstalledAppProviderService is an IntentService that
relies on the built-in queue and threading of the IntentService to make
sure that things are processed nicely in the background and one at a time.

This changes the announcing so that each app added/changed/deleted triggers
a new annoucement.  This keeps the UI more updated, and makes the Installed
tab show something as soon as possible, rather than waiting for the all of
the install apps to be processed.  This becomes more important as more
stuff is added to InstalledAppProvider, like the hash of the APK.

This also strips down and simplifies the related BroadcastReceivers.
BroadcastReceivers work on the UI thread, so they should do as little work
as possible. PackageManagerReceiver just rebadges the incoming Intent and
sends it off to InstalledAppProviderService for processing.
2016-06-02 08:36:30 +02:00
Hans-Christoph Steiner
677db72bb3 Utils.getPackageUri() for creating Uris from packageNames
Since this is done a lot, might as well have a reusable method.
2016-06-02 08:36:30 +02:00
Daniel Martí
cd1c213fb2 Bump minSdkVersion to 10, remove all compat code
Summary of changes:

* Remove compat apply()
* Remove compat setReadable()
* Remove pre-10 compat code in swap
* Remove pre-10 compat code in PRNGFixes

Fixes #663.
2016-05-30 19:25:06 +01:00
Hans-Christoph Steiner
aca94bcb68 move Provider tests into same java package the Providers
This allows the tests to call more methods directly without having to use
`public` visibility.
2016-05-27 22:00:28 +02:00
Hans-Christoph Steiner
d54748ff39 SuppressLint("ParcelCreator") on MockApplicationInfo
Apparently, the CREATOR field is not (yet?) needed in the tests, since
they work without it.  This gets us closer to making lint errors fail
the CI builds.

closes #580
2016-05-24 09:24:25 +02:00
Hans-Christoph Steiner
f1a31a7fe3 RepoUpdaterTest: convert writable test to JUnit assumption
This will mark the test as ignored rather then succeeded if it cannot find
a writable dir.
2016-05-20 12:04:36 +02:00
Hans-Christoph Steiner
02b2090e53 check repo index timestamps to prevent rollback attacks
A hacked fdroid server could "replay" old index.jar files known to have
apps with vulnerabilities in it.  That provides a long window of time for
exploiting that vulnerability.  By checking that the timestamp of an update
is never older than the current index, this attack is prevented.
2016-05-20 12:04:36 +02:00
Hans-Christoph Steiner
7f10be18c6 fix index update progress using simplified ProgressListener
The Event class is no longer needed once there is specific ProgressListener
instances for each type of progress update.  The sourceUrl serves as the
unique ID, like with DownloaderService and InstallManagerService.

fixes #633 https://gitlab.com/fdroid/fdroidclient/issues/633
2016-05-11 21:56:00 +02:00
Hans-Christoph Steiner
d73dac73ad delete DownloaderServiceTest stub, it is now crashing
DownloaderServiceTest is just a stub of a test that doesn't really test
anything yet.  It is now causing a NullPointerException, so its a problem:

 java.lang.NullPointerException
 	at org.fdroid.fdroid.net.DownloaderService.notifyDownloadComplete(DownloaderService.java:313)
 	at org.fdroid.fdroid.net.DownloaderService.handleIntent(DownloaderService.java:287)
 	at org.fdroid.fdroid.net.DownloaderService$ServiceHandler.handleMessage(DownloaderService.java:104)
 	at android.os.Handler.dispatchMessage(Handler.java:99)
 	at android.os.Looper.loop(Looper.java:137)
 	at android.os.HandlerThread.run(HandlerThread.java:60)
2016-05-10 15:38:27 +02:00
Hans-Christoph Steiner
83ee0c8f0b clean up cached files in a low priority IntentService
This moves the cache file deletion to a dedicated IntentService that runs
at the lowest possible priority.  The cache cleanup does not need to happen
with any kind of priority, so it shouldn't delay the app start or take any
resources away from foreground processes.

This also changes the logic around the "Cache packages" preference. The
downloader always saves APKs, then if "Cache packages" is disabled, those
APKs are deleted when they are older than an hour.

This also simplifies Utils.deleteFiles() since the endswith arg is no
longer needed.
2016-05-02 20:38:31 +02:00
Daniel Martí
87a4cfb27c Get rid of Log.d calls in src/main
Also, Log.d in tests don't make much sense - replace by Log.i. This way
it's easier to limit all Log.d calls to Utils.java.
2016-04-24 15:06:11 +01:00
Daniel Martí
50b2e6f7a5 PMD: Enable and obey SingularField 2016-04-23 17:27:22 +01:00
Daniel Martí
68db3ae353 PMD: Enable and obey ImmutableField 2016-04-23 17:27:22 +01:00
Daniel Martí
a484c03816 PMD: Re-enable on test files
This used to be the case, which is why only minimal changes were
required to bring it back. This also makes it take the same files that
checkstyle does, which is more consistent.
2016-04-23 01:45:04 +01:00
Daniel Martí
865d537745 Rename Repo's pubkey to signingCertificate
Updates #37.
2016-04-19 22:44:22 +01:00
Daniel Martí
4d8e410176 Rename Apk.version to Apk.versionName
Updates #37.
2016-04-19 22:44:22 +01:00
Daniel Martí
1abbedc4ab Rename Apk.vercode to Apk.versionCode
Updates #37.
2016-04-19 22:44:22 +01:00
Hans-Christoph Steiner
74d1c9521d put up a notification for each completed download
This makes background installs and updates a lot easier
2016-04-13 11:08:54 -04:00
Hans-Christoph Steiner
20c66a825d use IntentService style to download all APKs via a queue
DownloaderService is based on IntentService to provide queued requests that
run in a background thread via the Handler and the HandlerThread.  It began
as the IntentService code, but it could not be a subclass because the
downloading needs to be cancelable.  IntentServices cannot be canceled and
they provide no visibility into their queue.

DownloaderService then announces relevant events via LocalBroadcastManager
and Intents with custom "action" Strings.

https://gitlab.com/fdroid/fdroidclient/issues/601 #601
2016-04-13 10:40:01 -04:00
Hans-Christoph Steiner
22e40bfee1 port all but Provider tests to JUnit4 semantics
Android recently switched from JUnit 3 to 4 for its base testing classes.
It doesn't seem to support the old JUnit3 methods with gradle and AS. So
all the tests need to be ported to JUnit4 to work again.

#607 https://gitlab.com/fdroid/fdroidclient/issues/607
2016-04-11 11:26:21 -04:00
Hans-Christoph Steiner
70864e3479 use custom proguard config for running tests
The new Android Testing Support library stuff causes proguard to freak
out, so make proguard ignore all that stuff when running tests.
2016-04-05 12:44:01 +02:00
Hans-Christoph Steiner
74274d21b4 move SanitizedFileTest into non-Android tests
It can run in plain java, so might as well.
2016-04-04 10:53:12 +02:00
Hans-Christoph Steiner
e021eb5ca7 standardize SDK version test methods
I just took the most common method, using the SDK int value, and applied
that throughout the code to have it consistent.
2016-04-02 22:46:41 +02:00
Hans-Christoph Steiner
4538cf1ed0 suppress PMD JUnit4 warnings until tests are ported to JUnit4
#607 https://gitlab.com/fdroid/fdroidclient/issues/607
2016-03-29 12:30:30 +02:00
Hans-Christoph Steiner
3fcdfe85bb move main project files into standard gradle/Android Studio layout
This makes it a lot easier to setup all the testing stuff.  Mostly,
I'm tired of fighting Android Studio's fragility, so I want to remove
as much non-standardness as possible in the hopes of improving that
situation.

closes #534 https://gitlab.com/fdroid/fdroidclient/issues/534
2016-03-28 12:12:37 +02:00