Skip to content

Commit

Permalink
Extend CredentialProvider so that it can dynamically generate credent…
Browse files Browse the repository at this point in the history
…ials based on NetworkService

PiperOrigin-RevId: 433544503
Change-Id: I51274fb9ab8b7cb5e48b4a06fa5a308dfa97ec36
  • Loading branch information
Albert Cui authored and copybara-github committed Mar 9, 2022
1 parent 29fe9b2 commit 0d98aba
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ImmutableList<TestCredential> run(NetworkService networkService) {
}

Iterator<List<TestCredential>> credentialPartitions =
Iterators.partition(provider.generateTestCredentials(), this.batchSize);
Iterators.partition(provider.generateTestCredentials(networkService), this.batchSize);

return Streams.stream(credentialPartitions)
.map(batchCredentials -> tester.testValidCredentials(networkService, batchCredentials))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.tsunami.plugins.detectors.credentials.ncrack.provider;

import com.google.tsunami.proto.NetworkService;
import java.util.Iterator;

/**
Expand All @@ -30,5 +31,5 @@ public abstract class CredentialProvider {

public abstract String description();

public abstract Iterator<TestCredential> generateTestCredentials();
public abstract Iterator<TestCredential> generateTestCredentials(NetworkService networkService);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.tsunami.proto.NetworkService;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -185,7 +186,7 @@ public String description() {
}

@Override
public Iterator<TestCredential> generateTestCredentials() {
public Iterator<TestCredential> generateTestCredentials(NetworkService unused) {
return credentials.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public void setupPlugin() {

// thenReturn method will always return the same instance of iterator, which will be exhausted
// by previous testers, hence we are using thenAnswer method.
when(provider.generateTestCredentials()).thenAnswer(invocation -> TEST_CREDENTIALS.iterator());
when(provider.generateTestCredentials(any()))
.thenAnswer(invocation -> TEST_CREDENTIALS.iterator());
when(tester1.canAccept(any())).thenReturn(true);
when(tester2.canAccept(any())).thenReturn(true);
when(tester3.canAccept(any())).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class WeakCredentialsComposerTest {
public void setupComposer() {
provider = mock(CredentialProvider.class);
tester = mock(CredentialTester.class);
when(provider.generateTestCredentials()).thenReturn(TEST_CREDENTIALS.iterator());
when(provider.generateTestCredentials(any())).thenReturn(TEST_CREDENTIALS.iterator());
when(tester.testValidCredentials(any(), any()))
.thenReturn(ImmutableList.of(TestCredential.create("username1", Optional.of("password1"))))
.thenReturn(ImmutableList.of());
Expand All @@ -73,7 +73,7 @@ public void setupComposer() {
.build());

verify(tester, never()).testValidCredentials(any(), any());
verify(provider, never()).generateTestCredentials();
verify(provider, never()).generateTestCredentials(NetworkService.getDefaultInstance());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.tsunami.proto.NetworkService;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void getDescription_always_doNotReturnEmptyOrNull() {
@Test
public void genereateTestCredentials_withinExpectedValues_returnsExpectedCredentials() {
ImmutableList<TestCredential> generatedCredentials =
ImmutableList.copyOf(provider.generateTestCredentials());
ImmutableList.copyOf(provider.generateTestCredentials(NetworkService.getDefaultInstance()));

assertThat(generatedCredentials)
.containsExactly(
Expand Down

0 comments on commit 0d98aba

Please sign in to comment.