Skip to content

Commit af9d344

Browse files
committed
mvn wrapper
1 parent 77921e6 commit af9d344

File tree

14 files changed

+316
-27
lines changed

14 files changed

+316
-27
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

Path Manipulation/while File Read/java/fileread.pathmanipulation/pom.xml

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
<artifactId>spring-boot-starter-web</artifactId>
3636
</dependency>
3737

38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-test</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
3844
<dependency>
3945
<groupId>org.springframework.boot</groupId>
4046
<artifactId>spring-boot-starter-thymeleaf</artifactId>
@@ -45,7 +51,49 @@
4551
<artifactId>spring-boot-starter-test</artifactId>
4652
<scope>test</scope>
4753
</dependency>
48-
</dependencies>
54+
55+
<dependency>
56+
<groupId>org.springframework</groupId>
57+
<artifactId>spring-test</artifactId>
58+
<version>5.1.9.RELEASE</version>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>org.springframework</groupId>
63+
<artifactId>spring-test</artifactId>
64+
<version>5.1.9.RELEASE</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.springframework</groupId>
69+
<artifactId>spring-test</artifactId>
70+
<version>5.1.9.RELEASE</version>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>org.springframework</groupId>
75+
<artifactId>spring-test</artifactId>
76+
<version>5.1.9.RELEASE</version>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.springframework</groupId>
81+
<artifactId>spring-web</artifactId>
82+
<version>5.1.9.RELEASE</version>
83+
</dependency>
84+
85+
<dependency>
86+
<groupId>org.springframework</groupId>
87+
<artifactId>spring-web</artifactId>
88+
<version>5.1.9.RELEASE</version>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>org.springframework</groupId>
93+
<artifactId>spring-test</artifactId>
94+
<version>5.1.9.RELEASE</version>
95+
</dependency>
96+
</dependencies>
4997

5098
<build>
5199
<plugins>

Path Manipulation/while File Read/java/fileread.pathmanipulation/src/test/java/securecodingexamples/fileread/pathmanipulation/ApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package securecodingexamples.fileread.pathmanipulation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import org.springframework.test.web.servlet.MockMvc;
6+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
7+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
8+
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11+
12+
import org.springframework.http.ResponseEntity;
13+
import org.springframework.mock.web.MockMultipartFile;
14+
import org.springframework.web.multipart.MultipartFile;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
17+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
18+
@AutoConfigureMockMvc
19+
class FileUploadControllerTest {
20+
21+
@Autowired
22+
private MockMvc mockMvc;
23+
24+
@Test
25+
void testFileUploadSuccess() throws Exception {
26+
MockMultipartFile mockFile = new MockMultipartFile(
27+
"file", // Field name in request
28+
"testfile.txt", // Original file name
29+
"text/plain", // Content type
30+
"Sample file content".getBytes() // File content
31+
);
32+
33+
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/files/upload")
34+
.file(mockFile)
35+
.contentType(MediaType.MULTIPART_FORM_DATA))
36+
.andExpect(status().isOk())
37+
.andExpect(content().string(containsString("File uploaded successfully")));
38+
}
39+
40+
@Test
41+
void testEmptyFileUpload() throws Exception {
42+
MockMultipartFile emptyFile = new MockMultipartFile(
43+
"file", "emptyfile.txt", "text/plain", new byte[0]
44+
);
45+
46+
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/files/upload")
47+
.file(emptyFile)
48+
.contentType(MediaType.MULTIPART_FORM_DATA))
49+
.andExpect(status().isBadRequest())
50+
.andExpect(content().string(containsString("File is empty")));
51+
}
52+
53+
@Test
54+
void testFileUploadServerError() throws Exception {
55+
MockMultipartFile mockFile = new MockMultipartFile(
56+
"file", "testfile.txt", "text/plain", "Sample file content".getBytes()
57+
);
58+
59+
// Mocking server error by setting an invalid path
60+
MockMvc mockMvcWithError = MockMvcBuilders.standaloneSetup(new FileUploadController() {
61+
@Override
62+
public ResponseEntity<?> handleFileUpload(MultipartFile file) {
63+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
64+
.body("Failed to upload file: Simulated error");
65+
}
66+
}).build();
67+
68+
mockMvcWithError.perform(MockMvcRequestBuilders.multipart("/api/files/upload")
69+
.file(mockFile)
70+
.contentType(MediaType.MULTIPART_FORM_DATA))
71+
.andExpect(status().isInternalServerError())
72+
.andExpect(content().string(containsString("Failed to upload file")));
73+
}
74+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

Path Manipulation/while File Upload/java/fileupload.pathmanipulation/src/test/java/securecodingexamples/fileupload/pathmanipulation/PathmanipulationApplicationTests.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package securecodingexamples.fileupload.pathmanipulation;
2+
3+
import org.junit.jupiter.api.*;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.mock.web.MockMultipartFile;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
11+
import java.io.File;
12+
import java.nio.file.Paths;
13+
14+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
16+
17+
@SpringBootTest
18+
@AutoConfigureMockMvc
19+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
20+
class UploadControllerTest {
21+
22+
@Autowired
23+
private MockMvc mockMvc;
24+
25+
private static final String UPLOAD_DIRECTORY = System.getProperty("java.io.tmpdir") + File.separator + "Uploads";
26+
27+
@BeforeAll
28+
static void setup() {
29+
File uploadDir = new File(UPLOAD_DIRECTORY);
30+
if (!uploadDir.exists()) {
31+
uploadDir.mkdirs();
32+
}
33+
}
34+
35+
@AfterEach
36+
void cleanup() {
37+
File uploadDir = new File(UPLOAD_DIRECTORY);
38+
if (uploadDir.exists()) {
39+
for (File file : uploadDir.listFiles()) {
40+
file.delete();
41+
}
42+
}
43+
}
44+
45+
// ✅ Test: Successful File Upload
46+
@Test
47+
@Order(1)
48+
void testFileUploadSuccess() throws Exception {
49+
MockMultipartFile file = new MockMultipartFile(
50+
"file", "sample.txt", "text/plain", "Sample file content".getBytes()
51+
);
52+
53+
mockMvc.perform(multipart("/uploadFile")
54+
.file(file)
55+
.contentType(MediaType.MULTIPART_FORM_DATA))
56+
.andExpect(status().isOk())
57+
.andExpect(content().string("File Uploaded Successfully"));
58+
59+
File uploadedFile = new File(Paths.get(UPLOAD_DIRECTORY, "sample.txt").toString());
60+
Assertions.assertTrue(uploadedFile.exists(), "Uploaded file should exist in the directory.");
61+
}
62+
63+
// ✅ Test: Empty File Upload
64+
@Test
65+
@Order(2)
66+
void testEmptyFileUpload() throws Exception {
67+
MockMultipartFile emptyFile = new MockMultipartFile(
68+
"file", "emptyfile.txt", "text/plain", new byte[0]
69+
);
70+
71+
mockMvc.perform(multipart("/uploadFile")
72+
.file(emptyFile)
73+
.contentType(MediaType.MULTIPART_FORM_DATA))
74+
.andExpect(status().isBadRequest())
75+
.andExpect(content().string("No selected file"));
76+
}
77+
78+
// ✅ Test: Invalid File Extension
79+
@Test
80+
@Order(3)
81+
void testInvalidFileExtension() throws Exception {
82+
MockMultipartFile file = new MockMultipartFile(
83+
"file", "malicious.exe", "application/octet-stream", "Malicious content".getBytes()
84+
);
85+
86+
mockMvc.perform(multipart("/uploadFile")
87+
.file(file)
88+
.contentType(MediaType.MULTIPART_FORM_DATA))
89+
.andExpect(status().isBadRequest())
90+
.andExpect(content().string("Invalid Extension"));
91+
}
92+
93+
// ✅ Test: Invalid Filename Format
94+
@Test
95+
@Order(4)
96+
void testInvalidFilenameFormat() throws Exception {
97+
MockMultipartFile file = new MockMultipartFile(
98+
"file", "../../escape.txt", "text/plain", "Potential path traversal attempt".getBytes()
99+
);
100+
101+
mockMvc.perform(multipart("/uploadFile")
102+
.file(file)
103+
.contentType(MediaType.MULTIPART_FORM_DATA))
104+
.andExpect(status().isBadRequest())
105+
.andExpect(content().string("Invalid Filename"));
106+
}
107+
108+
// ✅ Test: Duplicate Filename Handling
109+
@Test
110+
@Order(5)
111+
void testDuplicateFilename() throws Exception {
112+
// First file
113+
MockMultipartFile file1 = new MockMultipartFile(
114+
"file", "duplicate.txt", "text/plain", "Original content".getBytes()
115+
);
116+
mockMvc.perform(multipart("/uploadFile").file(file1).contentType(MediaType.MULTIPART_FORM_DATA))
117+
.andExpect(status().isOk());
118+
119+
// Second file with the same name
120+
MockMultipartFile file2 = new MockMultipartFile(
121+
"file", "duplicate.txt", "text/plain", "Duplicate content".getBytes()
122+
);
123+
mockMvc.perform(multipart("/uploadFile").file(file2).contentType(MediaType.MULTIPART_FORM_DATA))
124+
.andExpect(status().isOk())
125+
.andExpect(content().string("File Uploaded Successfully"));
126+
127+
// Ensure unique filename logic worked
128+
File uploadedFile1 = new File(Paths.get(UPLOAD_DIRECTORY, "duplicate.txt").toString());
129+
File uploadedFile2 = new File(Paths.get(UPLOAD_DIRECTORY, "duplicate_1.txt").toString());
130+
131+
Assertions.assertTrue(uploadedFile1.exists(), "First uploaded file should exist");
132+
Assertions.assertTrue(uploadedFile2.exists(), "Second uploaded file should be renamed and exist");
133+
}
134+
135+
// ✅ Test: Simulate IOException During File Save
136+
@Test
137+
@Order(6)
138+
void testIOExceptionDuringUpload() throws Exception {
139+
MockMultipartFile file = new MockMultipartFile(
140+
"file", "error.txt", "text/plain", "File causing IOException".getBytes()
141+
);
142+
143+
File errorFile = new File(Paths.get(UPLOAD_DIRECTORY, "error.txt").toString());
144+
errorFile.createNewFile(); // Create a file first to simulate error
145+
146+
errorFile.setReadOnly(); // Force a write failure
147+
148+
mockMvc.perform(multipart("/uploadFile").file(file).contentType(MediaType.MULTIPART_FORM_DATA))
149+
.andExpect(status().isInternalServerError())
150+
.andExpect(content().string("File upload failed"));
151+
152+
errorFile.setWritable(true); // Reset permissions after test
153+
errorFile.delete(); // Clean up
154+
}
155+
}
Binary file not shown.

0 commit comments

Comments
 (0)