-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(EC-512): Handle Camera Leakage when started but not stopped #28
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
swift-lang/src/main/java/io/ecocode/ios/swift/checks/camera/CameraLeak.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 CameraLeak 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; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
"EC547", | ||
"EC523", | ||
"EC503", | ||
"EC603" | ||
"EC603", | ||
"EC512" | ||
] | ||
} |
44 changes: 44 additions & 0 deletions
44
swift-lang/src/main/resources/io/ecocode/rules/swift/EC512.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
swift-lang/src/main/resources/io/ecocode/rules/swift/EC512.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
swift-lang/src/test/java/io/ecocode/ios/swift/checks/camera/CameraLeakCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
swift-lang/src/test/resources/checks/camera/CameraLeak_no_trigger.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
swift-lang/src/test/resources/checks/camera/CameraLeak_trigger.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to CameraLeakCheck