Skip to content

Commit

Permalink
feat(EC-512): Handle Camera Leakage when started but not stopped (#28)
Browse files Browse the repository at this point in the history
* feat(EC-512): Handle Camera Leakage when started but not stopped

* feat(EC-512): Handle Camera Leakage when started but not stopped
  • Loading branch information
ExalTomiche authored May 29, 2024
1 parent 4e19075 commit 3992707
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
* Copyright © 2023 green-code-initiative (https://www.ecocode.io/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.ios.swift.checks.camera;

import io.ecocode.ios.swift.SwiftRuleCheck;
import io.ecocode.ios.swift.antlr.generated.Swift5Parser;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
import org.sonar.check.Rule;

@Rule(key = "EC512")
public class CameraLeakCheck extends SwiftRuleCheck {
private static final String DEFAULT_ISSUE_MESSAGE = "Any started capture session should be stopped.";
private boolean captureSessionStarted = false;
private boolean captureSessionStopped = false;
Swift5Parser.ExpressionContext id;

@Override
public void apply(ParseTree tree) {
if (tree instanceof Swift5Parser.ExpressionContext && tree.getText().contains("startRunning")) {
id = (Swift5Parser.ExpressionContext) tree;
captureSessionStarted = true;
}

if (tree instanceof Swift5Parser.ExpressionContext
&& (tree.getText().contains("stopRunning"))) {
captureSessionStopped = true;
}

if (tree instanceof TerminalNodeImpl && tree.getText().equals("<EOF>")) {
if (captureSessionStarted && !captureSessionStopped) {
this.recordIssue(id.getStart().getStartIndex(), DEFAULT_ISSUE_MESSAGE);
}
captureSessionStarted = false;
captureSessionStopped = false;
}
}
}

3 changes: 2 additions & 1 deletion swift-lang/src/main/resources/ecocode_swift_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"EC547",
"EC523",
"EC503",
"EC603"
"EC603",
"EC512"
]
}
44 changes: 44 additions & 0 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EC512.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<img src="http://www.neomades.com/extern/partage/ecoCode/2sur5_1x.png">
<p>Most iOS devices come equipped with a variety of sensors that measure motion, orientation, and various environmental
conditions. Additionally, these devices include advanced sensors such as the image sensor (commonly referred to as
the Camera) and the geo-positioning sensor (commonly referred to as GPS).

The common point of all these sensors is that they are power-intensive while in use. A typical issue arises when
these sensors continue to process data unnecessarily after the application enters an idle state, like when it is
backgrounded or the user stops interacting with it.

As a result, calls to manage these sensors must be carefully paired: <code>AVCaptureSession.startRunning()</code> and
<code>AVCaptureSession.stopRunning()</code>. Failure to properly manage these calls can lead to significant battery drain within
a few hours.</p>
<h2>Noncompliant Code Example</h2>
<pre>
import AVFoundation

class CameraManager {
var captureSession: AVCaptureSession?

func activateCamera() {
captureSession = AVCaptureSession()
captureSession?.startRunning() // Camera starts capturing
// Missing corresponding stopRunning
}
}
</pre>
<h2>Compliant Code Example</h2>
<pre>
import AVFoundation

class CameraManager {
var captureSession: AVCaptureSession?

func activateCamera() {
captureSession = AVCaptureSession()
captureSession?.startRunning() // Camera starts capturing
}

func deactivateCamera() {
captureSession?.stopRunning() // Camera stops capturing
}
}
</pre>

18 changes: 18 additions & 0 deletions swift-lang/src/main/resources/io/ecocode/rules/swift/EC512.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"key": "EC512",
"title": "Camera Leakage",
"defaultSeverity": "Major",
"description": "Any started capture session should be stopped.",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5min"
},
"tags": [
"ecocode",
"environment",
"sobriety",
"eco-design"
],
"type": "CODE_SMELL"
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void testMetadata() {
}

@Test
public void testRegisteredRules() {
assertThat(repository.rules()).hasSize(10);
public void testRegistredRules() {
assertThat(repository.rules()).hasSize(11);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* ecoCode iOS plugin - Help the earth, adopt this green plugin for your applications
* Copyright © 2023 green-code-initiative (https://www.ecocode.io/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.ecocode.ios.swift.checks.camera;

import io.ecocode.ios.swift.checks.CheckTestHelper;
import org.junit.Test;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.batch.sensor.issue.IssueLocation;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

public class CameraLeakCheckTest {
@Test
public void CameraLeak_trigger() {
SensorContextTester context = CheckTestHelper.analyzeTestFile("checks/camera/CameraLeak_trigger.swift");
assertThat(context.allIssues()).hasSize(1);
Optional<Issue> issue = context.allIssues().stream().findFirst();
issue.ifPresent(i -> {
assertThat(i.ruleKey().rule()).isEqualTo("EC512");

assertThat(i.ruleKey().repository()).isEqualTo("ecoCode-swift");
IssueLocation location = i.primaryLocation();
assertThat(location.textRange().start().line()).isEqualTo(8);

});
}

@Test
public void CameraLeak_no_trigger() {
SensorContextTester context = CheckTestHelper.analyzeTestFile("checks/camera/CameraLeak_no_trigger.swift");
assertThat(context.allIssues()).isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import AVFoundation

class CameraManager {
var captureSession: AVCaptureSession?

func activateCamera() {
captureSession = AVCaptureSession()
captureSession?.startRunning() // Camera starts capturing
}

func deactivateCamera() {
captureSession?.stopRunning() // Camera stops capturing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AVFoundation

class CameraManager {
var captureSession: AVCaptureSession?

func activateCamera() {
captureSession = AVCaptureSession()
captureSession?.startRunning() // Camera starts capturing
// Missing corresponding stopRunning
}
}

0 comments on commit 3992707

Please sign in to comment.