Skip to content

Commit

Permalink
feat: Add conversion of the byte range in file to file lines
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed Apr 5, 2024
1 parent 6e087d0 commit 2a040d5
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 42 deletions.
74 changes: 74 additions & 0 deletions src/main/java/com/lpvs/entity/LPVSFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Set;

/**
Expand All @@ -21,6 +24,7 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class LPVSFile {

/**
Expand All @@ -33,6 +37,11 @@ public class LPVSFile {
*/
private String filePath;

/**
* The absolute path to the file on the server.
*/
private String absoluteFilePath;

/**
* The type of snippet in the file.
*/
Expand Down Expand Up @@ -135,4 +144,69 @@ public String convertLicensesToString(LPVSVcs vcs) {
licenseNames = licenseNames.substring(0, licenseNames.length() - 2);
return licenseNames;
}

/**
* Converts byte ranges to line numbers in a file.
*
* @return A string representing the start and end line numbers corresponding to the byte ranges.
* Returns an empty string if the input does not start with "BYTES:" or if an error occurs.
*/
public String convertBytesToLinesNumbers() {

if (matchedLines != null && !matchedLines.startsWith("BYTES:")) {
return matchedLines;
}

StringBuilder result = new StringBuilder();

try (RandomAccessFile sourceFile = new RandomAccessFile(absoluteFilePath, "r")) {
// Skip "BYTES:" before splitting
String[] byteRangeTokens = matchedLines.substring(6).split(":");

for (String byteRangeToken : byteRangeTokens) {
String[] range = byteRangeToken.split("-");
int startByte = Integer.parseInt(range[0]);
int endByte = Integer.parseInt(range[1]);

// Read lines until reaching the end byte
long byteCounter = 0;
long currentLine = 1;
long startLine = 0;
long endLine = 0;

// Reset file pointer to the beginning of the file
sourceFile.seek(0);

while (byteCounter < endByte) {
String line = sourceFile.readLine();
if (line == null) {
if (startLine > 0 && endLine == 0) endLine = currentLine;
break;
}
byteCounter += line.getBytes().length + 1;
if (byteCounter > startByte && startLine == 0) {
startLine = currentLine;
}
if (byteCounter >= endByte) {
endLine = currentLine;
break;
}
currentLine++;
}

// Construct the string representing start and end line numbers in the range
if (result.length() > 0) {
result.append(",");
}
result.append(startLine);
result.append("-");
result.append(endLine);
}
} catch (IOException e) {
log.error(e.getMessage());
return "";
}

return result.toString();
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/lpvs/service/scan/LPVSDetectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.lpvs.service.scan;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -159,7 +160,12 @@ public void runOneScan() {
public List<LPVSFile> runScan(LPVSQueue webhookConfig, String path) throws Exception {
try {
scanService.runScan(webhookConfig, path);
return scanService.checkLicenses(webhookConfig);
List<LPVSFile> files = scanService.checkLicenses(webhookConfig);
for (LPVSFile file : files) {
file.setAbsoluteFilePath(path + File.separator + file.getFilePath());
file.setMatchedLines(file.convertBytesToLinesNumbers());
}
return files;
} catch (IllegalArgumentException | NullPointerException ex) {
log.error(ex.getMessage());
return new ArrayList<>();
Expand Down
19 changes: 11 additions & 8 deletions src/main/resources/database_dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ CREATE TABLE IF NOT EXISTS member (
UNIQUE (email,provider)
);

INSERT INTO license_list (license_name, license_spdx, license_usage) VALUES
('GNU General Public License v3.0 only','GPL-3.0-only','PROHIBITED'),
('OpenSSL License','OpenSSL','PERMITTED'),
('GNU Lesser General Public License v2.0 or later','LGPL-2.0-or-later','RESTRICTED'),
('MIT License', 'MIT', 'PERMITTED'),
('Apache License 2.0', 'Apache-2.0', 'PERMITTED'),
('GNU General Public License v2.0 only', 'GPL-2.0-only', 'RESTRICTED'),
('GNU Lesser General Public License v3.0 or later', 'LGPL-3.0-or-later', 'PROHIBITED');
INSERT INTO license_list (id, license_name, license_spdx, license_usage) VALUES
(1, 'GNU General Public License v3.0 only','GPL-3.0-only','PROHIBITED'),
(2, 'OpenSSL License','OpenSSL','PERMITTED'),
(3, 'GNU Lesser General Public License v2.0 or later','LGPL-2.0-or-later','RESTRICTED'),
(4, 'MIT License', 'MIT', 'PERMITTED'),
(5, 'Apache License 2.0', 'Apache-2.0', 'PERMITTED'),
(6, 'GNU General Public License v2.0 only', 'GPL-2.0-only', 'RESTRICTED'),
(7, 'GNU Lesser General Public License v3.0 or later', 'LGPL-3.0-or-later', 'PROHIBITED');

INSERT INTO license_conflicts (conflict_license_id, repository_license_id) VALUES
(1, 3), (1, 6), (2, 6), (2, 3), (3, 5), (3, 7), (5, 6), (6, 7);
52 changes: 44 additions & 8 deletions src/test/java/com/lpvs/entity/LPVSFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.lpvs.entity;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.*;

import com.lpvs.entity.enums.LPVSVcs;
Expand All @@ -20,6 +22,7 @@ public class LPVSFileTest {
LPVSFile lpvsFile;
final long baseId = 1L;
final String baseFilePath = "baseFilePath";
final String baseAbsoluteFilePath = "baseAbsoluteFilePath";
final String baseSnippetType = "baseSnippetType";
final String baseSnippetMatch = "baseSnippetMatch";
final String baseMatchedLines = "baseMatchedLines";
Expand Down Expand Up @@ -51,6 +54,14 @@ public void getterSetterFilePathTest() {
assertEquals(lpvsFile.getFilePath(), baseFilePath);
}

@Test
public void getterSetterAbsoluteFilePathTest() {
assertEquals(lpvsFile.getAbsoluteFilePath(), null);
lpvsFile.setAbsoluteFilePath(baseAbsoluteFilePath);
assertNotEquals(lpvsFile.getAbsoluteFilePath(), null);
assertEquals(lpvsFile.getAbsoluteFilePath(), baseAbsoluteFilePath);
}

@Test
public void getterSetterSnippetTypeTest() {
assertEquals(lpvsFile.getSnippetType(), null);
Expand Down Expand Up @@ -155,9 +166,6 @@ public void convertLicenseToStringCheckListUrlNullTest() {
final String baseSpdxId = "spdxId";
final String baseAccess = "access";
final String baseAlternativeName = "licenseNameAlternative";
final String baseChecklistUrl = "checklistUrl";
List<String> baseIncompatibleWith =
Arrays.asList("incompatibleWith1", "incompatibleWith2", "incompatibleWith3");

LPVSLicense lpvsLicense1 =
new LPVSLicense(
Expand Down Expand Up @@ -191,9 +199,6 @@ public void convertLicenseToStringCheckListUrlLicenseRef() {
final String baseSpdxId = "spdxId";
final String baseAccess = "access";
final String baseAlternativeName = "licenseNameAlternative";
final String baseChecklistUrl = "checklistUrl";
List<String> baseIncompatibleWith =
Arrays.asList("incompatibleWith1", "incompatibleWith2", "incompatibleWith3");

LPVSLicense lpvsLicense3 =
new LPVSLicense(
Expand Down Expand Up @@ -223,8 +228,6 @@ public void convertLicenseToStringCheckListUrlTwoTest() {
final String baseAccess = "access";
final String baseAlternativeName = "licenseNameAlternative";
final String baseChecklistUrl = "checklistUrl";
List<String> baseIncompatibleWith =
Arrays.asList("incompatibleWith1", "incompatibleWith2", "incompatibleWith3");

LPVSLicense lpvsLicense1 =
new LPVSLicense(
Expand All @@ -250,4 +253,37 @@ public void convertLicenseToStringCheckListUrlTwoTest() {
lpvsFile.convertLicensesToString(LPVSVcs.GITHUB),
"<a target=\"_blank\" href=\"checklistUrl\">spdxId</a> (access), <a target=\"_blank\" href=\"checklistUrl\">spdxId</a> (access)");
}

@Test
public void testConvertBytesToLinesNumbers() throws URISyntaxException {
// Test when matchedLines starts with "BYTES:"
LPVSFile validFile = new LPVSFile();

validFile.setAbsoluteFilePath(
Paths.get(
Objects.requireNonNull(
getClass()
.getClassLoader()
.getResource("convert1.txt"))
.toURI())
.toString());
validFile.setMatchedLines("BYTES:0-3709:6492-7819");
String result = validFile.convertBytesToLinesNumbers();
assertEquals("1-107,208-248", result);

// Test when matchedLines does not start with "BYTES:"
validFile.setMatchedLines("5-10,15-20");
result = validFile.convertBytesToLinesNumbers();
assertEquals("5-10,15-20", result);
}

@Test
public void testConvertBytesToLinesNumbers_N() throws URISyntaxException {
LPVSFile invalidFile = new LPVSFile();
// File does not exist
invalidFile.setAbsoluteFilePath("convertX.txt");
invalidFile.setMatchedLines("BYTES:0-3709:6492-7819");
String result = invalidFile.convertBytesToLinesNumbers();
assertEquals("", result);
}
}
24 changes: 16 additions & 8 deletions src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,8 @@ class TestCommentResults__ProhibitedPresentConflictsPresent {
final String file_url_1 =
"https://github.com/Samsung/LPVS/tree/main/src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";

final String absolute_file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String snippet_type_1 = "snippet";
final String snippet_match_1 =
"/**\n"
Expand Down Expand Up @@ -2239,6 +2241,7 @@ void setUp() {
new LPVSFile(
1L,
file_path_1,
absolute_file_path_1,
snippet_type_1,
snippet_match_1,
matched_lines_1,
Expand Down Expand Up @@ -2463,6 +2466,7 @@ class TestCommentResults__EmptyPresentConflictsPresent {
final String file_url_1 =
"https://github.com/Samsung/LPVS/tree/main/src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String absolute_file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String snippet_type_1 = "snippet";
final String snippet_match_1 =
"/**\n"
Expand Down Expand Up @@ -2566,6 +2570,7 @@ void setUp() {
new LPVSFile(
1L,
file_path_1,
absolute_file_path_1,
snippet_type_1,
snippet_match_1,
matched_lines_1,
Expand Down Expand Up @@ -2790,6 +2795,7 @@ class TestCommentResults__UnreviewedPresentConflictsPresent {
final String file_url_1 =
"https://github.com/Samsung/LPVS/tree/main/src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String absolute_file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String snippet_type_1 = "snippet";
final String snippet_match_1 =
"/**\n"
Expand Down Expand Up @@ -2896,6 +2902,7 @@ void setUp() {
new LPVSFile(
1L,
file_path_1,
absolute_file_path_1,
snippet_type_1,
snippet_match_1,
matched_lines_1,
Expand Down Expand Up @@ -3120,6 +3127,7 @@ class TestCommentResults__RestrictedPresentConflictsPresent {
final String file_url_1 =
"https://github.com/Samsung/LPVS/tree/main/src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String absolute_file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String snippet_type_1 = "snippet";
final String snippet_match_1 =
"/**\n"
Expand Down Expand Up @@ -3226,6 +3234,7 @@ void setUp() {
new LPVSFile(
1L,
file_path_1,
absolute_file_path_1,
snippet_type_1,
snippet_match_1,
matched_lines_1,
Expand Down Expand Up @@ -3442,6 +3451,7 @@ class TestCommentResults__ProhibitedAbsentConflictsAbsent {
final String file_url_1 =
"https://github.com/Samsung/LPVS/tree/main/src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String absolute_file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String snippet_type_1 = "snippet";
final String snippet_match_1 =
"/**\n"
Expand Down Expand Up @@ -3534,6 +3544,7 @@ void setUp() {
new LPVSFile(
1L,
file_path_1,
absolute_file_path_1,
snippet_type_1,
snippet_match_1,
matched_lines_1,
Expand Down Expand Up @@ -4100,6 +4111,7 @@ class TestGetMatchedLinesAsLink_NotAll {
// `lpvs_file_1`
LPVSFile lpvs_file_1;
final String file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String absolute_file_path_1 = "src/main/java/com/lpvs/service/LPVSGitHubService.java";
final String matched_lines_1 = "1-6";

final String expected_result =
Expand All @@ -4116,6 +4128,7 @@ void setUp() {
new LPVSFile(
1L,
file_path_1,
absolute_file_path_1,
"snippet",
null,
matched_lines_1,
Expand Down Expand Up @@ -4149,6 +4162,7 @@ class TestGetMatchedLinesAsLink_All {
// `lpvs_file_1`
LPVSFile lpvs_file_1;
final String file_path_1 = "LICENSE";
final String absolute_file_path_1 = "LICENSE";
final String matched_lines_1 = "all";

final String expected_result =
Expand All @@ -4165,6 +4179,7 @@ void setUp() {
new LPVSFile(
1L,
file_path_1,
absolute_file_path_1,
"snippet",
null,
matched_lines_1,
Expand Down Expand Up @@ -4254,6 +4269,7 @@ public void testCommentResults() throws Exception {
}
});
file.setFilePath("");
file.setAbsoluteFilePath("");
file.setComponentFilePath("");
file.setComponentName("");
file.setComponentLines("");
Expand Down Expand Up @@ -4340,14 +4356,6 @@ public void testCheckEmpty()
LPVSExitHandler exitHandler = mock(LPVSExitHandler.class);
LPVSGitHubConnectionService lpvsGitHubConnectionService =
new LPVSGitHubConnectionService("", "", "", exitHandler);

final LPVSGitHubService gh_service =
new LPVSGitHubService(
mocked_pullRequestRepository,
mocked_lpvsDetectedLicenseRepository,
mocked_lpvsLicenseRepository,
mocked_lpvsLicenseConflictRepository,
lpvsGitHubConnectionService);
Method method = lpvsGitHubConnectionService.getClass().getDeclaredMethod("checks");
method.setAccessible(true);
method.invoke(lpvsGitHubConnectionService);
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
null,
null,
null,
null,
Set.of(lpvs_license_1),
null,
null,
Expand All @@ -450,6 +451,7 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
null,
null,
null,
null,
Set.of(lpvs_license_2),
null,
null,
Expand Down
Loading

0 comments on commit 2a040d5

Please sign in to comment.