package fuzion24.device.vulnerability.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.AsyncTask;
import android.os.Bundle;

import org.json.JSONObject;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import fuzion24.device.vulnerability.test.VulnerabilityTestResult;
import fuzion24.device.vulnerability.util.DeviceInfo;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityOrganizer;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityResultSerialzier;
import fuzion24.device.vulnerability.vulnerabilities.VulnerabilityTest;

/**
 * Created by fuzion24 on 11/25/15.
 */
public class ScanRunnerBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {

        //Only allow this code to be ran on debug builds, since it accepts and writes to arbitrary file
        //paths, which would allow another app to arbitrarily write anywhere in this app's context.
        // http://android-developers.blogspot.com/2010/09/securing-android-lvl-applications.html
        boolean isDebuggable = ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
        if(!isDebuggable){
            return;
        }

        Bundle intentExtras = intent.getExtras();
        final String writeResultPath = intentExtras.getString("RESULT_PATH");

        new AsyncTask<Void,Void,Void>(){
            @Override
            protected Void doInBackground(Void... params) {
                List<VulnerabilityTest> tests = VulnerabilityOrganizer.getTests(context);
                List<VulnerabilityTestResult> results = new ArrayList<VulnerabilityTestResult>();
                for(VulnerabilityTest vt : tests){
                    boolean vulnerable = false;
                    Exception x = null;
                    try {
                        vulnerable = vt.isVulnerable(context);
                    }catch(Exception e){
                      x = e;
                    }
                    results.add(new VulnerabilityTestResult(vt, vulnerable, x));

                }

                try {
                    JSONObject jobj = VulnerabilityResultSerialzier.serializeResultsToJson(results, DeviceInfo.getDeviceInfo());
                    FileOutputStream fos = new FileOutputStream(writeResultPath);
                    fos.write(jobj.toString(2).getBytes());
                    fos.close();
                }catch(Exception e){
                    e.printStackTrace();
                }

                return null;
            }
        }.execute();

    }
}

@@ -253,16 +253,41 @@
 .Bundle;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -862,16 +862,75 @@
 eiver %7B%0A
+    private static final String TAG = %22ScanRunnerReceiver%22;
 %0A    @Ov
@@ -1003,16 +1003,73 @@
 ent) %7B%0A%0A
+        Log.d(TAG, %22Received broadcast for scanrunner%22);%0A
         
@@ -1518,152 +1518,598 @@
     
-return;%0A        %7D%0A%0A        Bundle intentExtras = intent.getExtras();%0A        final String writeResultPath = intentExtras.getString(%22RESULT_PATH%22
+Log.d(TAG, %22Not running the tests because the app is not debuggable%22);%0A            return;%0A        %7D%0A%0A        Bundle intentExtras = intent.getExtras();%0A        if(intentExtras == null)%7B%0A            Log.d(TAG, %22There were no extras with the broadcast. Include RESULT_PATH%22);%0A            return;%0A        %7D%0A%0A        final String writeResultPath = intentExtras.getString(%22RESULT_PATH%22);%0A        if(writeResultPath == null %7C%7C writeResultPath.equals(%22%22))%7B%0A            Log.d(TAG, %22Result write path is null or empty%22);%0A        %7D%0A%0A        Log.d(TAG, %22Results will be written to: %22 + writeResultPath
 );%0A%0A
@@ -2470,16 +2470,79 @@
 tests)%7B%0A
+                    Log.d(TAG, %22Running: %22 + vt.getCVEorID());%0A
         
@@ -2883,17 +2883,16 @@
 e, x));%0A
-%0A
         

Add some logging and additional error checking