<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.sshd;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.ProcessBuilder.Redirect;
import java.security.Key;
import java.security.KeyPair;
import java.security.cert.CertificateEncodingException;

import org.apache.sshd.common.KeyPairProvider;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.command.UnknownCommand;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.util.BaseTest;
import org.apache.sshd.util.BogusPublickeyAuthenticator;
import org.apache.sshd.util.PemUtil;
import org.apache.sshd.util.Utils;
import org.bouncycastle.openssl.PEMWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.cvarjao.jenkins.sshd.command.FalseCommand;
import com.cvarjao.jenkins.sshd.command.SetCommand;
import com.cvarjao.jenkins.sshd.command.TrueCommand;

/**
 * TODO Add javadoc
 *
 * @author <a href="mailto:<EMAIL>">Apache MINA SSHD Project</a>
 */
public class PlinkClientTest extends BaseTest {

    private SshServer sshd;
    private int port;
    private String username="userx";
    
    @Before
    public void setUp() throws Exception {
        port = Utils.getFreePort();

        sshd = SshServer.setUpDefaultServer();
        sshd.setPort(port);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
        
        sshd.setCommandFactory(new CommandFactory() {
            public Command createCommand(String command) {
            	if (TrueCommand.COMMAND.equals(command)){
            		return new TrueCommand();
            	}else if (FalseCommand.COMMAND.equals(command)){
            		return new FalseCommand();
            	}else if (SetCommand.COMMAND.equals(command)){
            		return new SetCommand(new String[0]);
            	}
                return new UnknownCommand(command);
            }
        });
        sshd.setPublickeyAuthenticator(new BogusPublickeyAuthenticator());

        sshd.start();
    }

    @After
    public void tearDown() throws Exception {
        if (sshd != null) {
            sshd.stop(true);
            Thread.sleep(50);
        }
    }
    
    public static String convertToPem(Key cert) throws IOException{
    	StringWriter writer=new StringWriter();
        PEMWriter pw = new PEMWriter(writer);
        pw.writeObject(cert);
        pw.flush();
        pw.close();
        return writer.toString();
    }
    
    @Test
    public void testTrileadClient() throws IOException, InterruptedException, CertificateEncodingException{
    	KeyPair pair = Utils.createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA);
    	String pubkey=PemUtil.convertToPem(pair.getPrivate());
    	
    	File keyFile= new File("test.pem");
    	FileWriter writer=new FileWriter(keyFile);
    	writer.write(pubkey);
    	writer.close();
    	
    	int exitValue=-1;
		try {
			ProcessBuilder builder = new ProcessBuilder("plink", "-ssh", "localhost", "-P", Integer.toString(port), "-l", username, "-N", "-i", keyFile.getAbsolutePath(), "-batch", "true");
			builder.redirectError(Redirect.INHERIT);
			builder.redirectOutput(Redirect.INHERIT);
			builder.redirectInput(Redirect.INHERIT);
			Process process = builder.start();        
			exitValue = process.waitFor();
		} finally{
			keyFile.delete();
		}
        assertEquals(1, exitValue);
    }
}<commit_msg>checking stderr<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.sshd;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.lang.ProcessBuilder.Redirect;
import java.security.Key;
import java.security.KeyPair;
import java.security.cert.CertificateEncodingException;

import org.apache.sshd.common.KeyPairProvider;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.command.UnknownCommand;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.util.BaseTest;
import org.apache.sshd.util.BogusPublickeyAuthenticator;
import org.apache.sshd.util.PemUtil;
import org.apache.sshd.util.Utils;
import org.bouncycastle.openssl.PEMWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.cvarjao.jenkins.sshd.command.FalseCommand;
import com.cvarjao.jenkins.sshd.command.SetCommand;
import com.cvarjao.jenkins.sshd.command.TrueCommand;

/**
 * TODO Add javadoc
 *
 * @author <a href="mailto:<EMAIL>">Apache MINA SSHD Project</a>
 */
public class PlinkClientTest extends BaseTest {

    private SshServer sshd;
    private int port;
    private String username="userx";
    
    @Before
    public void setUp() throws Exception {
        port = Utils.getFreePort();

        sshd = SshServer.setUpDefaultServer();
        sshd.setPort(port);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
        //sshd.setKeyExchangeFactories(Arrays.<NamedFactory<KeyExchange>>asList(new DHG1.Factory()));
        sshd.setCommandFactory(new CommandFactory() {
            public Command createCommand(String command) {
            	if (TrueCommand.COMMAND.equals(command)){
            		return new TrueCommand();
            	}else if (FalseCommand.COMMAND.equals(command)){
            		return new FalseCommand();
            	}else if (SetCommand.COMMAND.equals(command)){
            		return new SetCommand(new String[0]);
            	}
                return new UnknownCommand(command);
            }
        });
        sshd.setPublickeyAuthenticator(new BogusPublickeyAuthenticator());

        sshd.start();
    }

    @After
    public void tearDown() throws Exception {
        if (sshd != null) {
            sshd.stop(true);
            Thread.sleep(50);
        }
    }
    
    public static String convertToPem(Key cert) throws IOException{
    	StringWriter writer=new StringWriter();
        PEMWriter pw = new PEMWriter(writer);
        pw.writeObject(cert);
        pw.flush();
        pw.close();
        return writer.toString();
    }
    
    @Test
    public void testTrileadClient() throws IOException, InterruptedException, CertificateEncodingException{
    	KeyPair pair = Utils.createTestHostKeyProvider().loadKey(KeyPairProvider.SSH_RSA);
    	String pubkey=PemUtil.convertToPem(pair.getPrivate());
    	
    	File keyFile= new File("test.pem");
    	FileWriter writer=new FileWriter(keyFile);
    	writer.write(pubkey);
    	writer.close();
    	
    	int exitValue=-1;
    	long stderrSize=-1;
    	File stderr=new File("stderr.out");
    	File stdout=new File("stdout.out");
		try {
			
			ProcessBuilder builder = new ProcessBuilder("plink", "-ssh", "localhost", "-P", Integer.toString(port), "-l", username, "-N", "-i", keyFile.getAbsolutePath(), "-batch", "true");
			builder.redirectError(stderr);
			builder.redirectOutput(stdout);
			builder.redirectInput(Redirect.INHERIT);
			Process process = builder.start();        
			exitValue = process.waitFor();
			stderrSize=stderr.length();
		} finally{
			keyFile.delete();
			stderr.delete();
			stdout.delete();
		}
		assertEquals("plink stderr should be empty", 0, stderrSize);
		assertEquals(1, exitValue);
    }
}
{'fraction_non_alphanumeric': 0.10583129312263521, 'fraction_numerical': 0.002336968617849989, 'mean_word_length': 3.375365141187926, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '366218', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2568, 'n_words': 649}