Skip to content

Commit

Permalink
Fix sonar (#32)
Browse files Browse the repository at this point in the history
* Fix non compliant code
* Fix code smells
  • Loading branch information
enricovianello authored Jun 21, 2023
1 parent 0e77a05 commit db00352
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ public Resource addPath(String path) throws IOException {
public void writeTo(OutputStream out, long start, long count) throws IOException {

try (InputStream in = getInputStream()) {
in.skip(start);
if (start > 0) {
long n = in.skip(start);
if (n < start) {
throw new IOException("Skipped " + start + " bytes but read " + n);
}
}
if (count < 0) {
internalCopy(in, out);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public String asPerfMarker() {
builder.append(String.format("failure: %s", getErrorMessage().orElse("")));
} else {
builder.append("Perf Marker\n");
builder.append(format("Timestamp: %d\n", instant.getEpochSecond()));
builder.append(format("Timestamp: %d%n", instant.getEpochSecond()));
builder.append("Stripe Index: 0\n");
builder.append(format("Stripe Bytes Transferred: %d\n", getTransferByteCount()));
builder.append(format("Stripe Bytes Transferred: %d%n", getTransferByteCount()));
builder.append("Total Stripe Count: 1\n");
builder.append("End\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

public class ChecksumHelper {

public final static int ADLER32_CHECKSUM_LENGTH = 8;
public static final int ADLER32_CHECKSUM_LENGTH = 8;

public static String addLeadingZero(String checksum, int maxLength) {
return ("0".repeat(maxLength) + checksum).substring(checksum.length());
}

private ChecksumHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,53 +43,53 @@ class AvoidTraceMethodFilterTest {
MockMvc mvc;

@Test
public void traceAsAnonymousLeadsTo405() throws Exception {
void traceAsAnonymousLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request(HttpMethod.TRACE, "/test/file"))
.andExpect(status().isMethodNotAllowed());
}

@Test
@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"})
public void traceAsNonAnonymousLeadsTo405() throws Exception {
void traceAsNonAnonymousLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request(HttpMethod.TRACE, "/wlcg/file"))
.andExpect(status().isMethodNotAllowed());
}

@Test
public void traceAsAnonymousOnRootLeadsTo405() throws Exception {
void traceAsAnonymousOnRootLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request(HttpMethod.TRACE, "/"))
.andExpect(status().isMethodNotAllowed());
}

@Test
@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"})
public void traceAsNonAnonymousOnRootLeadsTo405() throws Exception {
void traceAsNonAnonymousOnRootLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request(HttpMethod.TRACE, "/"))
.andExpect(status().isMethodNotAllowed());
}

@Test
public void trackAsAnonymousLeadsTo405() throws Exception {
void trackAsAnonymousLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request("TRACK", new URI("/test/file")))
.andExpect(status().isMethodNotAllowed());
}

@Test
@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"})
public void trackAsNonAnonymousLeadsTo405() throws Exception {
void trackAsNonAnonymousLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request("TRACK", new URI("/wlcg/file")))
.andExpect(status().isMethodNotAllowed());
}

@Test
public void trackAsAnonymousOnRootLeadsTo405() throws Exception {
void trackAsAnonymousOnRootLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request("TRACK", new URI("/")))
.andExpect(status().isMethodNotAllowed());
}

@Test
@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"})
public void trackAsNonAnonymousOnRootLeadsTo405() throws Exception {
void trackAsNonAnonymousOnRootLeadsTo405() throws Exception {
mvc.perform(MockMvcRequestBuilders.request("TRACK", new URI("/")))
.andExpect(status().isMethodNotAllowed());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@AutoConfigureMockMvc
@ActiveProfiles("dev")
@WithAnonymousUser
public class AuthzTest {
class AuthzTest {

@Autowired
MockMvc mvc;
Expand All @@ -49,7 +49,7 @@ public void setup() {
}

@Test
public void writeAccessAsAnonymousLeadsTo401() throws Exception {
void writeAccessAsAnonymousLeadsTo401() throws Exception {
mvc.perform(put("/test/file")).andExpect(status().isUnauthorized());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,41 +79,41 @@ public void setup() {
}

@Test
public void anonymousGetAccessToAnonymousSaWorks() throws Exception {
void anonymousGetAccessToAnonymousSaWorks() throws Exception {
mvc.perform(get(SLASH_ANONYMOUS_SLASH_FILE)).andExpect(status().isNotFound());
}

@Test
public void anonymousWriteAccessToAnonymousSaIsBlocked() throws Exception {
void anonymousWriteAccessToAnonymousSaIsBlocked() throws Exception {
mvc.perform(put(SLASH_ANONYMOUS_SLASH_FILE)).andExpect(status().isUnauthorized());
}

@Test
public void writeAccessAsAnonymousLeadsTo401() throws Exception {
void writeAccessAsAnonymousLeadsTo401() throws Exception {
mvc.perform(put(SLASH_WLCG_SLASH_FILE)).andExpect(status().isUnauthorized());
}

@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"})
@Test
public void getAccessAsVomsUserLeads404() throws Exception {
void getAccessAsVomsUserLeads404() throws Exception {
mvc.perform(get(SLASH_WLCG_SLASH_FILE)).andExpect(status().isNotFound());
}

@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"})
@Test
public void putAccessAsVomsUserIsForbidden() throws Exception {
void putAccessAsVomsUserIsForbidden() throws Exception {
mvc.perform(put(SLASH_WLCG_SLASH_FILE)).andExpect(status().isForbidden());
}

@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"}, saWritePermissions = {"wlcg"})
@Test
public void putAccessAsVomsUserIsOk() throws Exception {
void putAccessAsVomsUserIsOk() throws Exception {
mvc.perform(put(SLASH_WLCG_SLASH_FILE)).andExpect(status().isOk());
}


@Test
public void issuerChecksAreEnforcedForWlcgScopeBasedAuthz() throws Exception {
void issuerChecksAreEnforcedForWlcgScopeBasedAuthz() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(UNKNOWN_ISSUER)
Expand All @@ -126,7 +126,7 @@ public void issuerChecksAreEnforcedForWlcgScopeBasedAuthz() throws Exception {
}

@Test
public void getAccessAsJwtUserWithoutScopeLeadsToAccessDenied() throws Exception {
void getAccessAsJwtUserWithoutScopeLeadsToAccessDenied() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand All @@ -139,7 +139,7 @@ public void getAccessAsJwtUserWithoutScopeLeadsToAccessDenied() throws Exception
}

@Test
public void getAccessAsJwtUserWithTheRightScopeGrantsAccess() throws Exception {
void getAccessAsJwtUserWithTheRightScopeGrantsAccess() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand All @@ -153,7 +153,7 @@ public void getAccessAsJwtUserWithTheRightScopeGrantsAccess() throws Exception {


@Test
public void getAccessAsJwtWithReadCapabilityForWrongPathResultsInAccessDenied() throws Exception {
void getAccessAsJwtWithReadCapabilityForWrongPathResultsInAccessDenied() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand All @@ -167,7 +167,7 @@ public void getAccessAsJwtWithReadCapabilityForWrongPathResultsInAccessDenied()
}

@Test
public void getAccessAsJwtWithWriteCapabilityResultsInAccessDenied() throws Exception {
void getAccessAsJwtWithWriteCapabilityResultsInAccessDenied() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand All @@ -182,22 +182,22 @@ public void getAccessAsJwtWithWriteCapabilityResultsInAccessDenied() throws Exce

@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"})
@Test
public void localVomsCopyRequiresWithReadPermissionsGetsAccessDenied() throws Exception {
void localVomsCopyRequiresWithReadPermissionsGetsAccessDenied() throws Exception {
mvc.perform(request("COPY", URI.create("http://localhost/wlcg/source")).header("Destination",
"http://localhost/wlcg/destination"))
.andExpect(status().isForbidden());
}

@WithMockVOMSUser(vos = "wlcg", saWritePermissions = {"wlcg"}, saReadPermissions = {"wlcg"})
@Test
public void localVomsCopyRequiresReadAndWritePermissions() throws Exception {
void localVomsCopyRequiresReadAndWritePermissions() throws Exception {
mvc.perform(request("COPY", URI.create("http://localhost/wlcg/source")).header("Destination",
"http://localhost/wlcg/destination"))
.andExpect(status().isOk());
}

@Test
public void tpcJwtPullCopyBlockedWithStorageReadScope() throws Exception {
void tpcJwtPullCopyBlockedWithStorageReadScope() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand All @@ -211,7 +211,7 @@ public void tpcJwtPullCopyBlockedWithStorageReadScope() throws Exception {
}

@Test
public void tpcJwtPullCopyRequiresStorageModifyScope() throws Exception {
void tpcJwtPullCopyRequiresStorageModifyScope() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand All @@ -225,7 +225,7 @@ public void tpcJwtPullCopyRequiresStorageModifyScope() throws Exception {
}

@Test
public void tpcJwtPullCopyRequiresStorageModifyScopeWithRightPath() throws Exception {
void tpcJwtPullCopyRequiresStorageModifyScopeWithRightPath() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand All @@ -240,7 +240,7 @@ public void tpcJwtPullCopyRequiresStorageModifyScopeWithRightPath() throws Excep


@Test
public void tpcJwtLocalCopyRequiresAppropriatePermissions() throws Exception {
void tpcJwtLocalCopyRequiresAppropriatePermissions() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand Down Expand Up @@ -309,7 +309,7 @@ public void tpcJwtLocalCopyRequiresAppropriatePermissions() throws Exception {
}

@Test
public void tpcJwtLocalMoveRequiresAppropriatePermissions() throws Exception {
void tpcJwtLocalMoveRequiresAppropriatePermissions() throws Exception {
Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
.issuer(WLCG_ISSUER)
Expand Down Expand Up @@ -358,7 +358,7 @@ public void tpcJwtLocalMoveRequiresAppropriatePermissions() throws Exception {
}

@Test
public void tpcJwtFineGrainedAuthzCopyTests() throws Exception {
void tpcJwtFineGrainedAuthzCopyTests() throws Exception {

Jwt token = Jwt.withTokenValue("test")
.header("kid", "rsa1")
Expand Down Expand Up @@ -410,7 +410,7 @@ public void tpcJwtFineGrainedAuthzCopyTests() throws Exception {

@WithMockVOMSUser(vos = "wlcg", saReadPermissions = {"wlcg"}, saWritePermissions = {"wlcg"})
@Test
public void deleteOnStorageAreaRootIsForbidden() throws Exception {
void deleteOnStorageAreaRootIsForbidden() throws Exception {
mvc.perform(delete("/wlcg")).andExpect(status().isForbidden());
}

Expand Down

0 comments on commit db00352

Please sign in to comment.