Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JENKINS-35206] - Make the InstallState object deserialization robust…
… against corrupted files (#2387) * [JENKINS-35206] - Add unit tests for the deserialization logic * [JENKINS-35206] - Install state should be robust against messed statuses when deserializing objects * [JENKINS-35206] - Fix the license header in thre test file
- Loading branch information
1 parent
967fe84
commit 1b2711c
Showing
2 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2016 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package jenkins.install; | ||
|
||
import hudson.ExtensionList; | ||
import jenkins.model.Jenkins; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* Tests of {@link InstallState}. | ||
* Effectively the most of the tests do not need the Jenkins instance, but we want to | ||
* honor Jenkins extension points and hooks, which may influence the behavior. | ||
* @author Oleg Nenashev | ||
*/ | ||
public class InstallStateTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
public void shouldPefromCorrectConversionForAllNames() { | ||
ExtensionList<InstallState> states = InstallState.all(); | ||
for (InstallState state : states) { | ||
InstallState afterRoundtrip = forName(state.name()); | ||
// It also prevents occasional name duplications | ||
assertThat("State after the roundtrip must be equal to the original state", | ||
afterRoundtrip, equalTo(state)); | ||
assertTrue("State " + state + " should return the extension point instance after deserialization", | ||
afterRoundtrip == state); | ||
} | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-35206") | ||
public void shouldNotFailOnNullXMLField() { | ||
String xml = "<jenkins.install.InstallState>\n" + | ||
" <isSetupComplete>true</isSetupComplete>\n" + | ||
"</jenkins.install.InstallState>"; | ||
final InstallState state = forXml(xml); | ||
assertThat(state, equalTo(InstallState.UNKNOWN)); | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-35206") | ||
public void shouldNotFailOnEmptyName() { | ||
final InstallState state = forName(""); | ||
assertThat(state, equalTo(InstallState.UNKNOWN)); | ||
} | ||
|
||
@Test | ||
@Issue("JENKINS-35206") | ||
public void shouldReturnUnknownStateForUnknownName() { | ||
final InstallState state = forName("NonExistentStateName"); | ||
assertThat(state, equalTo(InstallState.UNKNOWN)); | ||
} | ||
|
||
private static InstallState forName(String name) throws AssertionError { | ||
String xml = "<jenkins.install.InstallState>\n" + | ||
" <isSetupComplete>true</isSetupComplete>\n" + | ||
" <name>" + name + "</name>\n" + | ||
"</jenkins.install.InstallState>"; | ||
return forXml(xml); | ||
} | ||
|
||
private static InstallState forXml(String xml) throws AssertionError { | ||
Object read = Jenkins.XSTREAM2.fromXML(xml); | ||
assertThat(read, instanceOf(InstallState.class)); | ||
InstallState state = (InstallState) read; | ||
return state; | ||
} | ||
} |