Skip to content

Commit

Permalink
#752: Add getConfig() to JSch which makes access to all the config va…
Browse files Browse the repository at this point in the history
…lues possible for troubleshooting.
  • Loading branch information
davsclaus committed Jan 24, 2025
1 parent cac34ea commit 78aae2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
21 changes: 13 additions & 8 deletions src/main/java/com/jcraft/jsch/JSch.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import java.io.InputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

Expand Down Expand Up @@ -642,17 +644,20 @@ public static void setConfig(String key, String value) {
}

/**
* Returns the config keys for all the stored configurations.
*
* For example this can be used for troubleshooting to enumerate all the configuration values to
* be logged.
*
* @return config keys
* Gets the configuration
*/
public static Set<String> getConfigKeys() {
public static Map<String, String> getConfig() {
Map<String, String> ret = new HashMap<>();
synchronized (config) {
return Collections.unmodifiableSet(config.keySet());
for (Map.Entry<String, String> entry : config.entrySet()) {
String key = entry.getKey();
if (key.equals("PubkeyAcceptedKeyTypes")) {
key = "PubkeyAcceptedAlgorithms";
}
ret.put(key, entry.getValue());
}
}
return Collections.unmodifiableMap(ret);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/jcraft/jsch/JSchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -69,22 +70,21 @@ void checkLoggerBehavior() throws Exception {

@Test
void getConfigKeys() throws Exception {
Set<String> keys = JSch.getConfigKeys();
Map<String, String> map = JSch.getConfig();

// there are many keys so just assert a high number in case new keys
// are added so this test still passes

int before = keys.size();

int before = map.size();
assertTrue(before > 150);
assertTrue(keys.contains("diffie-hellman-group14-sha256"));
assertTrue(keys.contains("HashKnownHosts"));
assertTrue(map.containsKey("diffie-hellman-group14-sha256"));
assertTrue(map.containsKey("HashKnownHosts"));

// add new key
JSch.setConfig("mySpecialKey", "mySpecialValue");

// add 1 new key
keys = JSch.getConfigKeys();
int after = keys.size();
map = JSch.getConfig();
int after = map.size();
assertEquals(before + 1, after);
}

Expand Down

0 comments on commit 78aae2c

Please sign in to comment.