|
| 1 | +package com.example.arInfra.validator.file; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.springframework.util.unit.DataSize.ofBytes; |
| 8 | +import static org.springframework.util.unit.DataSize.ofMegabytes; |
| 9 | + |
| 10 | +import com.example.arInfra.InfraGenerated; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties; |
| 14 | + |
| 15 | +@InfraGenerated |
| 16 | +class MultipartPropertiesValidatorTest { |
| 17 | + |
| 18 | + private MultipartPropertiesValidator subject; |
| 19 | + |
| 20 | + @BeforeEach |
| 21 | + void setUp() { |
| 22 | + subject = new MultipartPropertiesValidator(); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void validate_with_valid_configuration_should_succeed() { |
| 27 | + MultipartProperties properties = new MultipartProperties(); |
| 28 | + properties.setMaxFileSize(ofMegabytes(8)); |
| 29 | + properties.setMaxRequestSize(ofMegabytes(8)); |
| 30 | + |
| 31 | + assertDoesNotThrow(() -> subject.validate(properties)); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void validate_with_5mb_limit_should_succeed() { |
| 36 | + MultipartProperties properties = new MultipartProperties(); |
| 37 | + properties.setMaxFileSize(ofMegabytes(5)); |
| 38 | + properties.setMaxRequestSize(ofMegabytes(5)); |
| 39 | + |
| 40 | + assertDoesNotThrow(() -> subject.validate(properties)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void validate_with_10mb_limit_should_succeed_with_warning() { |
| 45 | + MultipartProperties properties = new MultipartProperties(); |
| 46 | + properties.setMaxFileSize(ofMegabytes(10)); |
| 47 | + properties.setMaxRequestSize(ofMegabytes(10)); |
| 48 | + |
| 49 | + assertDoesNotThrow(() -> subject.validate(properties)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void validate_with_null_max_file_size_should_throw_exception() { |
| 54 | + MultipartProperties properties = new MultipartProperties(); |
| 55 | + properties.setMaxFileSize(null); |
| 56 | + properties.setMaxRequestSize(ofMegabytes(8)); |
| 57 | + |
| 58 | + SecurityException exception = |
| 59 | + assertThrows(SecurityException.class, () -> subject.validate(properties)); |
| 60 | + |
| 61 | + assertTrue(exception.getMessage().contains("max-file-size")); |
| 62 | + assertTrue(exception.getMessage().contains("explicitly configured")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void validate_with_zero_max_file_size_should_throw_exception() { |
| 67 | + MultipartProperties properties = new MultipartProperties(); |
| 68 | + properties.setMaxFileSize(ofBytes(0)); |
| 69 | + properties.setMaxRequestSize(ofMegabytes(8)); |
| 70 | + |
| 71 | + SecurityException exception = |
| 72 | + assertThrows(SecurityException.class, () -> subject.validate(properties)); |
| 73 | + |
| 74 | + assertTrue(exception.getMessage().contains("max-file-size")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void validate_with_negative_max_file_size_should_throw_exception() { |
| 79 | + MultipartProperties properties = new MultipartProperties(); |
| 80 | + properties.setMaxFileSize(ofBytes(-1)); |
| 81 | + properties.setMaxRequestSize(ofMegabytes(8)); |
| 82 | + |
| 83 | + SecurityException exception = |
| 84 | + assertThrows(SecurityException.class, () -> subject.validate(properties)); |
| 85 | + |
| 86 | + assertTrue(exception.getMessage().contains("max-file-size")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void validate_with_null_max_request_size_should_throw_exception() { |
| 91 | + MultipartProperties properties = new MultipartProperties(); |
| 92 | + properties.setMaxFileSize(ofMegabytes(8)); |
| 93 | + properties.setMaxRequestSize(null); |
| 94 | + |
| 95 | + SecurityException exception = |
| 96 | + assertThrows(SecurityException.class, () -> subject.validate(properties)); |
| 97 | + |
| 98 | + assertTrue(exception.getMessage().contains("max-request-size")); |
| 99 | + assertTrue(exception.getMessage().contains("explicitly configured")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void validate_with_excessive_max_file_size_should_throw_exception() { |
| 104 | + MultipartProperties properties = new MultipartProperties(); |
| 105 | + properties.setMaxFileSize(ofMegabytes(150)); |
| 106 | + properties.setMaxRequestSize(ofMegabytes(8)); |
| 107 | + |
| 108 | + SecurityException exception = |
| 109 | + assertThrows(SecurityException.class, () -> subject.validate(properties)); |
| 110 | + |
| 111 | + assertTrue(exception.getMessage().contains("exceeds absolute maximum")); |
| 112 | + assertTrue(exception.getMessage().contains("DoS risk")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void validate_with_excessive_max_request_size_should_throw_exception() { |
| 117 | + MultipartProperties properties = new MultipartProperties(); |
| 118 | + properties.setMaxFileSize(ofMegabytes(8)); |
| 119 | + properties.setMaxRequestSize(ofMegabytes(150)); |
| 120 | + |
| 121 | + SecurityException exception = |
| 122 | + assertThrows(SecurityException.class, () -> subject.validate(properties)); |
| 123 | + |
| 124 | + assertTrue(exception.getMessage().contains("exceeds absolute maximum")); |
| 125 | + assertTrue(exception.getMessage().contains("DoS risk")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void validate_with_exactly_100mb_limit_should_succeed() { |
| 130 | + MultipartProperties properties = new MultipartProperties(); |
| 131 | + properties.setMaxFileSize(ofMegabytes(100)); |
| 132 | + properties.setMaxRequestSize(ofMegabytes(100)); |
| 133 | + |
| 134 | + assertDoesNotThrow(() -> subject.validate(properties)); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void validate_with_101mb_limit_should_throw_exception() { |
| 139 | + MultipartProperties properties = new MultipartProperties(); |
| 140 | + properties.setMaxFileSize(ofMegabytes(101)); |
| 141 | + properties.setMaxRequestSize(ofMegabytes(8)); |
| 142 | + |
| 143 | + SecurityException exception = |
| 144 | + assertThrows(SecurityException.class, () -> subject.validate(properties)); |
| 145 | + |
| 146 | + assertTrue(exception.getMessage().contains("exceeds absolute maximum")); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void get_validated_type_should_return_multipart_properties_class() { |
| 151 | + assertEquals(MultipartProperties.class, subject.getValidatedType()); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + void validate_with_null_properties_should_throw_exception() { |
| 156 | + assertThrows(NullPointerException.class, () -> subject.validate(null)); |
| 157 | + } |
| 158 | +} |
0 commit comments