Skip to content

Commit 311c442

Browse files
zakariamaaraki#38 check sourcecode extension before compilation
1 parent c0f07ab commit 311c442

File tree

5 files changed

+94
-44
lines changed

5 files changed

+94
-44
lines changed

.idea/workspace.xml

+26-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/cp/compiler/models/Language.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum Language {
2323
WellKnownFiles.PYTHON_FILE_NAME,
2424
WellKnownCommands.PYTHON_COMMAND_LINE,
2525
WellKnownMetrics.PYTHON_COUNTER_NAME,
26+
".py",
2627
false),
2728
/**
2829
* C language.
@@ -31,6 +32,7 @@ public enum Language {
3132
WellKnownFiles.C_FILE_NAME,
3233
WellKnownCommands.C_COMMAND_LINE,
3334
WellKnownMetrics.C_COUNTER_NAME,
35+
".c",
3436
true),
3537
/**
3638
* Cpp language.
@@ -39,6 +41,7 @@ public enum Language {
3941
WellKnownFiles.CPP_FILE_NAME,
4042
WellKnownCommands.CPP_COMMAND_LINE,
4143
WellKnownMetrics.CPP_COUNTER_NAME,
44+
".cpp",
4245
true),
4346
/**
4447
* Java language.
@@ -47,6 +50,7 @@ public enum Language {
4750
WellKnownFiles.JAVA_FILE_NAME,
4851
WellKnownCommands.JAVA_COMMAND_LINE,
4952
WellKnownMetrics.JAVA_COUNTER_NAME,
53+
".java",
5054
true),
5155
/**
5256
* Golang language.
@@ -55,6 +59,7 @@ public enum Language {
5559
WellKnownFiles.GO_FILE_NAME,
5660
WellKnownCommands.GO_COMMAND_LINE,
5761
WellKnownMetrics.GO_COUNTER_NAME,
62+
".go",
5863
true),
5964
/**
6065
* Cs language.
@@ -63,6 +68,7 @@ public enum Language {
6368
WellKnownFiles.CS_FILE_NAME,
6469
WellKnownCommands.CS_COMMAND_LINE,
6570
WellKnownMetrics.CS_COUNTER_NAME,
71+
".cs",
6672
true),
6773
/**
6874
* Kotlin language.
@@ -71,6 +77,7 @@ public enum Language {
7177
WellKnownFiles.KOTLIN_FILE_NAME,
7278
WellKnownCommands.KOTLIN_COMMAND_LINE,
7379
WellKnownMetrics.KOTLIN_COUNTER_NAME,
80+
".kt",
7481
true),
7582

7683
/**
@@ -80,6 +87,7 @@ public enum Language {
8087
WellKnownFiles.SCALA_FILE_NAME,
8188
WellKnownCommands.SCALA_COMMAND_LINE,
8289
WellKnownMetrics.SCALA_COUNTER_NAME,
90+
".scala",
8391
true),
8492

8593
/**
@@ -89,6 +97,7 @@ public enum Language {
8997
WellKnownFiles.RUST_FILE_NAME,
9098
WellKnownCommands.RUST_COMMAND_LINE,
9199
WellKnownMetrics.RUST_COUNTER_NAME,
100+
".rs",
92101
true),
93102

94103
/**
@@ -98,6 +107,7 @@ public enum Language {
98107
WellKnownFiles.RUBY_FILE_NAME,
99108
WellKnownCommands.RUBY_COMMAND_LINE,
100109
WellKnownMetrics.RUBY_COUNTER_NAME,
110+
".rb",
101111
false),
102112

103113
/**
@@ -107,24 +117,34 @@ public enum Language {
107117
WellKnownFiles.HASKELL_FILE_NAME,
108118
WellKnownCommands.HASKELL_COMMAND_LINE,
109119
WellKnownMetrics.HASKELL_COUNTER_NAME,
120+
".hs",
110121
true);
111122

112123
/**
113124
* The execution folder name.
114125
*/
115126
private String folderName;
127+
116128
/**
117129
* The source code file name.
118130
*/
119-
private String defaultSourceCodeFileName;
131+
private String defaultSourcecodeFileName;
132+
120133
/**
121134
* The compilation command.
122135
*/
123136
private String compilationCommand;
137+
124138
/**
125139
* The execution counter name
126140
*/
127141
private String executionCounter;
142+
143+
/**
144+
* The sourcecode extension
145+
*/
146+
private String sourcecodeExtension;
147+
128148
/**
129149
* Is the language compiled or interpreted
130150
*/

src/main/java/com/cp/compiler/models/Request.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class Request {
3939

4040
public MultipartFile getSourceCode() throws IOException {
4141
return new MockMultipartFile(
42-
language.getDefaultSourceCodeFileName(),
43-
language.getDefaultSourceCodeFileName(),
42+
language.getDefaultSourcecodeFileName(),
43+
language.getDefaultSourcecodeFileName(),
4444
null,
4545
new ByteArrayInputStream(this.sourceCode.getBytes()));
4646
}

src/main/java/com/cp/compiler/services/CompilerProxy.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cp.compiler.services;
22

33
import com.cp.compiler.executions.Execution;
4+
import com.cp.compiler.models.Language;
45
import com.cp.compiler.wellknownconstants.WellKnownFiles;
56
import com.cp.compiler.wellknownconstants.WellKnownMetrics;
67
import com.cp.compiler.repositories.HooksRepository;
@@ -116,11 +117,19 @@ private ResponseEntity<Object> compileAndExecute(Execution execution) {
116117
}
117118

118119
private Optional<ResponseEntity<Object>> validateRequest(Execution execution) {
120+
119121
if (!checkFileName(execution.getSourceCodeFile().getOriginalFilename())) {
120122
return Optional.of(buildOutputError(
121-
"Bad request, source code file must match the following regex "
123+
"Bad request, sourcecode file must match the following regex "
122124
+ WellKnownFiles.FILE_NAME_REGEX));
123125
}
126+
127+
// sourcecode file format is correct, lets check the extension
128+
if (!checkFileExtension(execution.getSourceCodeFile().getOriginalFilename(), execution.getLanguage())) {
129+
return Optional.of(buildOutputError(
130+
"Bad request, sourcecode file extension is not correct, it should be: "
131+
+ execution.getLanguage().getSourcecodeExtension()));
132+
}
124133

125134
if (!checkFileName(execution.getExpectedOutputFile().getOriginalFilename())) {
126135
return Optional.of(buildOutputError(
@@ -152,9 +161,14 @@ private Optional<ResponseEntity<Object>> validateRequest(Execution execution) {
152161

153162
return Optional.of(buildOutputError(errorMessage));
154163
}
164+
155165
return Optional.ofNullable(null);
156166
}
157167

168+
private boolean checkFileExtension(String originalFilename, Language language) {
169+
return originalFilename.endsWith(language.getSourcecodeExtension());
170+
}
171+
158172
private ResponseEntity buildOutputError(String errorMessage) {
159173
log.info(errorMessage);
160174
return ResponseEntity.badRequest()

src/test/java/com/cp/compiler/services/CompilerProxyServiceTests.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.cp.compiler.repositories.HooksRepository;
77
import org.junit.jupiter.api.Assertions;
88
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.provider.ValueSource;
910
import org.mockito.Mockito;
1011
import org.springframework.beans.factory.annotation.Autowired;
1112
import org.springframework.boot.test.context.SpringBootTest;
@@ -42,11 +43,38 @@ class CompilerProxyServiceTests {
4243
(byte[]) null);
4344

4445
private MultipartFile validFileName = new MockMultipartFile(
45-
"test.txt",
46-
"test.txt",
46+
"test.java",
47+
"test.java",
4748
null,
4849
(byte[]) null);
4950

51+
@Test
52+
void extensionsInLanguageEnumAreCorrect() {
53+
for (Language language : Language.values()) {
54+
String expectedExtension = "." + language.getDefaultSourcecodeFileName().split("\\.")[1];
55+
Assertions.assertEquals(expectedExtension, language.getSourcecodeExtension());
56+
}
57+
}
58+
59+
@Test
60+
void shouldReturnBadRequestIfTheExtensionIsNotValid() {
61+
// Given
62+
MultipartFile invalidExtension = new MockMultipartFile(
63+
"test.c",
64+
"test.c",
65+
null,
66+
(byte[]) null);
67+
68+
Execution execution = ExecutionFactory.createExecution(
69+
invalidExtension, validFileName, validFileName, 10, 500, Language.JAVA);
70+
71+
// When
72+
ResponseEntity responseEntity = compilerProxy.execute(execution);
73+
74+
// Then
75+
Assertions.assertEquals(BAD_REQUEST, responseEntity.getStatusCodeValue());
76+
}
77+
5078
@Test
5179
void WhenInputFileNameIsInvalidShouldReturnBadRequest() {
5280
// Given

0 commit comments

Comments
 (0)