Skip to content

Commit 7c0f687

Browse files
authored
Merge pull request #1576 from WebFuzzing/create-users
Auth handling of create users
2 parents cc28daa + 91854a7 commit 7c0f687

38 files changed

Lines changed: 1068 additions & 101 deletions

File tree

client-java/test-utils-java/src/main/java/org/evomaster/test/utils/EMTestUtils.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,79 @@ is used in the EvoMaster Core (eg, when making HTTP calls) and
2222
*/
2323
public class EMTestUtils {
2424

25+
/**
26+
* Loaded only once at class loading.
27+
* Seed is still going to incremented with ++ at each use.
28+
* The idea is to force each value unique during a session, even when generating hundreds of thousands of tests.
29+
* However, when running again in generated test suite, a new starting seed might reduce chances of clashes,
30+
* albeit cannot guarantee removal of them
31+
*/
32+
private static long seed = System.currentTimeMillis();
33+
34+
/**
35+
*
36+
* @param minLength Optional minimum length of the generated string
37+
* @param maxLength Optional maximum length of the generated string
38+
* @param prefix Optional fixed prefix shared by all generated strings
39+
* @param postfix Optional fixed postfix shared by all generated strings
40+
* @return
41+
*/
42+
public static String createString(Integer minLength, Integer maxLength, String prefix, String postfix){
43+
44+
if(minLength != null && minLength < 0){
45+
throw new IllegalArgumentException("Negative minimum length: " + minLength);
46+
}
47+
if(maxLength != null && maxLength < 0){
48+
throw new IllegalArgumentException("Negative maximum length: " + maxLength);
49+
}
50+
51+
int min = 0;
52+
if(minLength != null){
53+
min = minLength;
54+
}
55+
int len = 0;
56+
if(prefix != null){
57+
len += prefix.length();
58+
}
59+
if(postfix != null){
60+
len += postfix.length();
61+
}
62+
min = Math.max(min, len);
63+
64+
//actual check on inputs
65+
if(maxLength != null && maxLength < len){
66+
throw new IllegalArgumentException("Maximum length " + maxLength + " does not cover minimum prefix+postfix length: "+prefix+postfix);
67+
}
68+
69+
//recompute with default values if not specified
70+
if(prefix == null){
71+
prefix = "u";
72+
}
73+
if(postfix == null){
74+
postfix = "";
75+
}
76+
len = prefix.length() + postfix.length();
77+
78+
int maxDigits = 6; // 999 999 values
79+
if(maxDigits + len < min){
80+
maxDigits = min - len;
81+
}
82+
if(maxLength != null && maxDigits + len > maxLength ){
83+
maxDigits = maxLength - len;
84+
}
85+
86+
int mask = 1;
87+
for(int i = 0; i < maxDigits; i++){
88+
mask = mask * 10;
89+
}
90+
91+
long value = seed % mask;
92+
seed++;
93+
94+
return prefix + value + postfix;
95+
}
96+
97+
2598
/**
2699
*
27100
* @param locationHeader a URI-reference, coming from a "location" header. See RFC 7231.

client-java/test-utils-java/src/test/java/org/evomaster/test/utils/EMTestUtilsTest.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
package org.evomaster.test.utils;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertFalse;
5-
import static org.junit.jupiter.api.Assertions.assertTrue;
6-
73
import org.junit.jupiter.api.Test;
84

5+
import static org.junit.jupiter.api.Assertions.*;
6+
97
public class EMTestUtilsTest {
108

9+
@Test
10+
public void testCreateString(){
11+
12+
String prefix = "foo";
13+
String postfix = "bar";
14+
int min = 5;
15+
int max = 10;
16+
17+
String first = EMTestUtils.createString(min, max, prefix, postfix);
18+
assertTrue(first.startsWith(prefix));
19+
assertTrue(first.endsWith(postfix));
20+
assertTrue(first.length() >= min);
21+
assertTrue(first.length() <= max);
22+
23+
String second = EMTestUtils.createString(min, max, prefix, postfix);
24+
assertTrue(second.startsWith(prefix));
25+
assertTrue(second.endsWith(postfix));
26+
assertTrue(second.length() >= min);
27+
assertTrue(second.length() <= max);
28+
29+
assertNotEquals(first, second);
30+
}
31+
32+
1133
@Test
1234
public void testEmptyPath(){
1335

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.foo.rest.examples.bb.authcreateusers
2+
3+
class AuthDto(
4+
var email : String? = null,
5+
var token: TokenDto? = null
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.foo.rest.examples.bb.authcreateusers
2+
3+
import org.springframework.boot.SpringApplication
4+
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
6+
7+
8+
@SpringBootApplication(exclude = [SecurityAutoConfiguration::class])
9+
open class BBAuthCreateUsersApplication {
10+
11+
companion object {
12+
@JvmStatic
13+
fun main(args: Array<String>) {
14+
SpringApplication.run(BBAuthCreateUsersApplication::class.java, *args)
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.foo.rest.examples.bb.authcreateusers
2+
3+
import org.evomaster.e2etests.utils.CoveredTargets
4+
import org.springframework.http.MediaType
5+
import org.springframework.http.ResponseEntity
6+
import org.springframework.web.bind.annotation.*
7+
8+
@RestController
9+
@RequestMapping(path = ["/api/authcreateusers"])
10+
class BBAuthCreateUsersRest {
11+
12+
private val SECRET = "a complex secret - "
13+
14+
private val users = mutableMapOf<String, CreateUserDto>()
15+
16+
private val tokens = mutableMapOf<String, String>()
17+
18+
19+
@PostMapping("/users")
20+
fun createUser(@RequestBody user: CreateUserDto): ResponseEntity<Unit> {
21+
22+
if(user.email == null || user.username == null || user.password == null) {
23+
return ResponseEntity.status(400).build()
24+
}
25+
26+
if(!user.email!!.contains("@") || !user.email!!.contains(".")) {
27+
return ResponseEntity.status(400).build()
28+
}
29+
30+
if(user.password != user.repeatPassword) {
31+
return ResponseEntity.status(400).build()
32+
}
33+
34+
if(users.containsKey(user.email)){
35+
return ResponseEntity.status(403).build()
36+
}
37+
38+
users[user.email!!] = user
39+
return ResponseEntity.status(201).build()
40+
}
41+
42+
43+
@PostMapping(path = ["/users/login"], consumes = [MediaType.APPLICATION_JSON_VALUE])
44+
fun login(@RequestBody login : LoginDto) : ResponseEntity<AuthDto>{
45+
46+
val user = users[login.email!!]
47+
?: return ResponseEntity.status(404).build()
48+
49+
if(login.password != user.password){
50+
return ResponseEntity.status(400).build()
51+
}
52+
53+
val secret = "$SECRET${System.currentTimeMillis()}"
54+
55+
tokens[secret] = user.email!!
56+
57+
return ResponseEntity.ok(AuthDto(user.email, TokenDto(secret)))
58+
}
59+
60+
@GetMapping(path = ["/check"])
61+
fun check(@RequestHeader("Authorization") authorization: String?) : ResponseEntity<String>{
62+
63+
val secret = authorization!!.substring("Bearer ".length)
64+
65+
if(tokens.containsKey(secret)){
66+
CoveredTargets.cover("CHECK")
67+
return ResponseEntity.ok("OK")
68+
}
69+
70+
return ResponseEntity.status(401).build()
71+
}
72+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.foo.rest.examples.bb.authcreateusers
2+
3+
class CreateUserDto(
4+
var email: String? = null,
5+
var password: String? = null,
6+
var repeatPassword: String? = null,
7+
var username: String? = null
8+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.foo.rest.examples.bb.authcreateusers
2+
3+
class LoginDto(
4+
var email: String? = null,
5+
var password: String? = null
6+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.foo.rest.examples.bb.authcreateusers
2+
3+
class TokenDto(
4+
var authToken : String? = null
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.foo.rest.examples.bb.authcreateusers
2+
3+
import com.foo.rest.examples.bb.SpringController
4+
import com.foo.rest.examples.bb.authcookie.CookieLoginApplication
5+
import org.evomaster.client.java.controller.problem.ProblemInfo
6+
import org.evomaster.client.java.controller.problem.RestProblem
7+
8+
class AuthCreateUsersController : SpringController(BBAuthCreateUsersApplication::class.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.evomaster.e2etests.spring.rest.bb.authcreateusers
2+
3+
import com.foo.rest.examples.bb.authcookie.CookieLoginController
4+
import com.foo.rest.examples.bb.authcreateusers.AuthCreateUsersController
5+
import org.evomaster.core.output.OutputFormat
6+
import org.evomaster.core.problem.rest.data.HttpVerb
7+
import org.evomaster.e2etests.spring.rest.bb.SpringTestBase
8+
import org.evomaster.e2etests.utils.EnterpriseTestBase
9+
import org.junit.jupiter.api.Assertions.assertTrue
10+
import org.junit.jupiter.api.BeforeAll
11+
import org.junit.jupiter.params.ParameterizedTest
12+
import org.junit.jupiter.params.provider.EnumSource
13+
14+
class BBAuthCreateUsersEMTest : SpringTestBase() {
15+
16+
companion object {
17+
init {
18+
shouldApplyInstrumentation = false
19+
}
20+
21+
@BeforeAll
22+
@JvmStatic
23+
fun init() {
24+
initClass(AuthCreateUsersController())
25+
}
26+
}
27+
28+
@ParameterizedTest
29+
@EnumSource
30+
fun testBlackBoxOutput(outputFormat: OutputFormat) {
31+
32+
executeAndEvaluateBBTest(
33+
outputFormat,
34+
"authcreateusers",
35+
50,
36+
3,
37+
"CHECK"
38+
){ args: MutableList<String> ->
39+
40+
setOption(args, "configPath", "src/test/resources/config/authcreateusers.yaml")
41+
42+
val solution = initAndRun(args)
43+
44+
assertTrue(solution.individuals.size >= 1)
45+
assertHasAtLeastOne(solution, HttpVerb.GET, 200, "/api/authcreateusers/check", "OK")
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)