Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#752: Add getKeys to JSch which makes access to all the config values… #753

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/com/jcraft/jsch/JSch.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
package com.jcraft.jsch;

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.Vector;

public class JSch {
Expand Down Expand Up @@ -639,6 +642,23 @@ public static void setConfig(String key, String value) {
}
}

/**
* Gets the configuration
*/
public static Map<String, String> getConfig() {
Map<String, String> ret = new HashMap<>();
synchronized (config) {
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);
}

/**
* Sets the logger
*
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/jcraft/jsch/JSchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -64,6 +67,26 @@ void checkLoggerBehavior() throws Exception {
assertSame(JSch.DEVNULL, jsch.getInstanceLogger(), "instance logger should be DEVNULL");
}

@Test
void getConfig() {
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 = map.size();
assertTrue(before > 150);
assertTrue(map.containsKey("diffie-hellman-group14-sha256"));
assertTrue(map.containsKey("HashKnownHosts"));

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

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

static final class TestLogger implements Logger {
@Override
public boolean isEnabled(int level) {
Expand Down
Loading