44import static org .hamcrest .Matchers .is ;
55import static org .jenkinsci .plugins .gitclient .verifier .KnownHostsTestUtil .nonGitHubHost ;
66import static org .jenkinsci .plugins .gitclient .verifier .KnownHostsTestUtil .runKnownHostsTests ;
7+ import static org .junit .jupiter .api .Assumptions .assumeFalse ;
78import static org .junit .jupiter .api .Assumptions .assumeTrue ;
89
910import hudson .model .StreamBuildListener ;
@@ -62,7 +63,7 @@ void connectWhenHostKeyProvidedThenShouldNotFail() throws Exception {
6263 ManuallyProvidedKeyVerifier .ManuallyProvidedKeyJGitHostKeyVerifier jGitHostKeyVerifier =
6364 (ManuallyProvidedKeyVerifier .ManuallyProvidedKeyJGitHostKeyVerifier )
6465 verifier .forJGit (StreamBuildListener .fromStdout ());
65- Path tempKnownHosts = Files .createTempFile ("known_hosts" , "" );
66+ Path tempKnownHosts = Files .createTempFile (testFolder . toPath (), "known_hosts" , "" );
6667 Files .writeString (tempKnownHosts , hostKey + System .lineSeparator ());
6768 KnownHostsTestUtil .connectToHost (
6869 "github.com" , 22 , tempKnownHosts .toFile (), jGitHostKeyVerifier , "ecdsa-sha2-nistp256" , s -> {
@@ -84,7 +85,7 @@ void connectWhenWrongHostKeyProvidedThenShouldFail() throws Exception {
8485 ManuallyProvidedKeyVerifier .ManuallyProvidedKeyJGitHostKeyVerifier jGitHostKeyVerifier =
8586 (ManuallyProvidedKeyVerifier .ManuallyProvidedKeyJGitHostKeyVerifier )
8687 verifier .forJGit (StreamBuildListener .fromStdout ());
87- Path tempKnownHosts = Files .createTempFile ("known_hosts" , "" );
88+ Path tempKnownHosts = Files .createTempFile (testFolder . toPath (), "known_hosts" , "" );
8889 Files .writeString (tempKnownHosts , key + System .lineSeparator ());
8990 KnownHostsTestUtil .connectToHost (
9091 "github.com" , 22 , tempKnownHosts .toFile (), jGitHostKeyVerifier , "ssh-ed25519" , s -> {
@@ -106,7 +107,7 @@ void connectWhenHostKeyProvidedWithPortThenShouldNotFail() throws Exception {
106107 ManuallyProvidedKeyVerifier .ManuallyProvidedKeyJGitHostKeyVerifier jGitHostKeyVerifier =
107108 (ManuallyProvidedKeyVerifier .ManuallyProvidedKeyJGitHostKeyVerifier )
108109 verifier .forJGit (StreamBuildListener .fromStdout ());
109- Path tempKnownHosts = Files .createTempFile ("known_hosts" , "" );
110+ Path tempKnownHosts = Files .createTempFile (testFolder . toPath (), "known_hosts" , "" );
110111 Files .writeString (tempKnownHosts , key + System .lineSeparator ());
111112 KnownHostsTestUtil .connectToHost (
112113 "github.com" , 22 , tempKnownHosts .toFile (), jGitHostKeyVerifier , "ecdsa-sha2-nistp256" , s -> {
@@ -120,30 +121,112 @@ void connectWhenHostKeyProvidedWithPortThenShouldNotFail() throws Exception {
120121
121122 @ Test
122123 void testGetVerifyHostKeyOption () throws Exception {
123- if (isWindows ()) {
124- return ; // Skip test without generating a Maven surefire warning
124+ Path tempFile = Files .createTempFile (testFolder .toPath (), "manual-host-key" , "" );
125+ String actual = new ManuallyProvidedKeyVerifier (hostKey )
126+ .forCliGit (TaskListener .NULL )
127+ .getVerifyHostKeyOption (tempFile );
128+ String fileArg = tempFile .toAbsolutePath ().toString ();
129+ if (!isWindows ()) {
130+ fileArg = "'" + fileArg + "'" ;
125131 }
126- Path tempFile = File .createTempFile ("junit" , null , testFolder ).toPath ();
132+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
133+ assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
134+ }
135+
136+ @ Test
137+ void testGetVerifyHostKeyOptionSpace () throws Exception {
138+ assumeFalse (isWindows (), "Embedded space not allowed on Windows for git plugin temporary files" );
139+ Path tempFile = Files .createTempFile (testFolder .toPath (), "x y" , "" );
127140 String actual = new ManuallyProvidedKeyVerifier (hostKey )
128141 .forCliGit (TaskListener .NULL )
129142 .getVerifyHostKeyOption (tempFile );
130- assertThat (
131- actual ,
132- is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=\\ \" \" \" " + tempFile .toAbsolutePath ()
133- + "\\ \" \" \" " ));
143+ String fileArg = tempFile .toAbsolutePath ().toString ();
144+ fileArg = "'\" " + fileArg + "\" '" ;
145+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
134146 assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
135147 }
136148
137149 @ Test
138- void testGetVerifyHostKeyOptionOnWindows () throws Exception {
150+ void testGetVerifyHostKeyOptionParentheses () throws Exception {
151+ Path tempFile = Files .createTempFile (testFolder .toPath (), "paren(1)" , "" );
152+ String actual = new ManuallyProvidedKeyVerifier (hostKey )
153+ .forCliGit (TaskListener .NULL )
154+ .getVerifyHostKeyOption (tempFile );
155+ String fileArg = tempFile .toAbsolutePath ().toString ();
139156 if (!isWindows ()) {
140- return ; // Skip test without generating a Maven surefire warning
157+ fileArg = "'" + fileArg + "'" ;
141158 }
142- Path tempFile = File .createTempFile ("junit" , null , testFolder ).toPath ();
159+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
160+ assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
161+ }
162+
163+ @ Test
164+ void testGetVerifyHostKeyOptionParenthesesAndSpace () throws Exception {
165+ assumeFalse (isWindows (), "Embedded space not allowed on Windows for git plugin temporary files" );
166+ Path tempFile = Files .createTempFile (testFolder .toPath (), "paren (2)" , "" );
167+ String actual = new ManuallyProvidedKeyVerifier (hostKey )
168+ .forCliGit (TaskListener .NULL )
169+ .getVerifyHostKeyOption (tempFile );
170+ String fileArg = tempFile .toAbsolutePath ().toString ();
171+ fileArg = "'\" " + fileArg + "\" '" ;
172+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
173+ assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
174+ }
175+
176+ @ Test
177+ void testGetVerifyHostKeyOptionShellEscape () throws Exception {
178+ Path tempFile = Files .createTempFile (testFolder .toPath (), "shell-$(date)" , "" );
143179 String actual = new ManuallyProvidedKeyVerifier (hostKey )
144180 .forCliGit (TaskListener .NULL )
145181 .getVerifyHostKeyOption (tempFile );
146- assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + tempFile .toAbsolutePath ()));
182+ String fileArg = tempFile .toAbsolutePath ().toString ();
183+ if (!isWindows ()) {
184+ fileArg = "'" + fileArg + "'" ;
185+ }
186+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
187+ assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
188+ }
189+
190+ @ Test
191+ void testGetVerifyHostKeyOptionShellEscape1 () throws Exception {
192+ Path tempFile = Files .createTempFile (testFolder .toPath (), "shell-`date`" , "" );
193+ String actual = new ManuallyProvidedKeyVerifier (hostKey )
194+ .forCliGit (TaskListener .NULL )
195+ .getVerifyHostKeyOption (tempFile );
196+ String fileArg = tempFile .toAbsolutePath ().toString ();
197+ if (!isWindows ()) {
198+ fileArg = "'" + fileArg + "'" ;
199+ }
200+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
201+ assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
202+ }
203+
204+ @ Test
205+ void testGetVerifyHostKeyOptionSingleQuoteAndSpace () throws Exception {
206+ assumeFalse (isWindows (), "Embedded space not allowed on Windows for git plugin temporary files" );
207+ Path tempFile = Files .createTempFile (testFolder .toPath (), "Bob's Job" , "" );
208+ String actual = new ManuallyProvidedKeyVerifier (hostKey )
209+ .forCliGit (TaskListener .NULL )
210+ .getVerifyHostKeyOption (tempFile );
211+ String fileArg = tempFile .toAbsolutePath ().toString ();
212+ fileArg = fileArg .replace ("'" , "\\ '" ); // Expect escaped single quotes inside the string
213+ fileArg = "'\" " + fileArg + "\" '" ;
214+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
215+ assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
216+ }
217+
218+ @ Test
219+ void testGetVerifyHostKeyOptionSingleQuote () throws Exception {
220+ Path tempFile = Files .createTempFile (testFolder .toPath (), "Bob's-Job" , "" );
221+ String actual = new ManuallyProvidedKeyVerifier (hostKey )
222+ .forCliGit (TaskListener .NULL )
223+ .getVerifyHostKeyOption (tempFile );
224+ String fileArg = tempFile .toAbsolutePath ().toString ();
225+ if (!isWindows ()) {
226+ fileArg = fileArg .replace ("'" , "\\ '" ); // Expect escaped single quotes inside the string
227+ fileArg = "'" + fileArg + "'" ;
228+ }
229+ assertThat (actual , is ("-o StrictHostKeyChecking=yes -o UserKnownHostsFile=" + fileArg ));
147230 assertThat (Files .readAllLines (tempFile ), is (Collections .singletonList (hostKey )));
148231 }
149232
0 commit comments