From f2a2b92a2fe2ee4cc5c822d4517aed1c612b52ec Mon Sep 17 00:00:00 2001 From: mindless Date: Mon, 1 Mar 2010 06:45:57 +0000 Subject: [PATCH] [FIXED HUDSON-5768] Avoid double decoding that broke deserialization of fields with double underscore, by replacing XStream11XmlFriendlyMapper with a custom mapper that only does "-" to "$" decoding that is needed for compatibility with old xml data written by Hudson 1.105 or older. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@28071 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/util/XStream2.java | 37 +++++++++++--- .../src/test/java/hudson/util/SecretTest.java | 15 +++--- .../test/java/hudson/util/XStream2Test.java | 48 +++++++++++++++++-- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/hudson/util/XStream2.java b/core/src/main/java/hudson/util/XStream2.java index c48581b494..05cce68c85 100644 --- a/core/src/main/java/hudson/util/XStream2.java +++ b/core/src/main/java/hudson/util/XStream2.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Alan Harder * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -38,6 +38,7 @@ import com.thoughtworks.xstream.core.JVM; import com.thoughtworks.xstream.io.HierarchicalStreamDriver; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; +import com.thoughtworks.xstream.mapper.CannotResolveClassException; import hudson.model.Hudson; import hudson.model.Result; @@ -70,11 +71,6 @@ public class XStream2 extends XStream { return super.unmarshal(reader,root,dataHolder); } - @Override - protected boolean useXStream11XmlFriendlyMapper() { - return true; - } - private void init() { registerConverter(new RobustCollectionConverter(getMapper(),getReflectionProvider()),10); registerConverter(new CopyOnWriteMap.Tree.ConverterImpl(getMapper()),10); // needs to override MapConverter @@ -93,7 +89,34 @@ public class XStream2 extends XStream { // list up types that should be marshalled out like a value, without referencial integrity tracking. ImmutableTypesMapper itm = new ImmutableTypesMapper(next); itm.addImmutableType(Result.class); - return itm; + return new CompatibilityMapper(itm); + } + + /** + * Prior to Hudson 1.106, XStream 1.1.x was used which encoded "$" in class names + * as "-" instead of "_-" that is used now. Up through Hudson 1.348 compatibility + * for old serialized data was maintained via {@code XStream11XmlFriendlyMapper}. + * However, it was found (HUDSON-5768) that this caused fields with "__" to fail + * deserialization due to double decoding. Now this class is used for compatibility. + */ + private class CompatibilityMapper extends MapperWrapper { + private CompatibilityMapper(Mapper wrapped) { + super(wrapped); + } + + @Override + public Class realClass(String elementName) { + try { + return super.realClass(elementName); + } catch (CannotResolveClassException e) { + // If a "-" is found, retry with mapping this to "$" + if (elementName.indexOf('-') >= 0) try { + return super.realClass(elementName.replace('-', '$')); + } catch (CannotResolveClassException e2) { } + // Throw original exception + throw e; + } + } } /** diff --git a/core/src/test/java/hudson/util/SecretTest.java b/core/src/test/java/hudson/util/SecretTest.java index ef52bf6d61..0385b1892d 100644 --- a/core/src/test/java/hudson/util/SecretTest.java +++ b/core/src/test/java/hudson/util/SecretTest.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -34,7 +34,7 @@ import hudson.model.Hudson; * @author Kohsuke Kawaguchi */ public class SecretTest extends TestCase { - protected void setUp() throws Exception { + @Override protected void setUp() throws Exception { SecureRandom sr = new SecureRandom(); byte[] random = new byte[32]; sr.nextBytes(random); @@ -42,7 +42,7 @@ public class SecretTest extends TestCase { } - protected void tearDown() throws Exception { + @Override protected void tearDown() throws Exception { Secret.SECRET = null; } @@ -65,11 +65,10 @@ public class SecretTest extends TestCase { public void testSerialization() { Secret s = Secret.fromString("Mr.Hudson"); String xml = Hudson.XSTREAM.toXML(s); - System.out.println(xml); - assertTrue(!xml.contains(s.toString())); - assertTrue(xml.contains(s.getEncryptedValue())); + assertTrue(xml, !xml.contains(s.toString())); + assertTrue(xml, xml.contains(s.getEncryptedValue())); Object o = Hudson.XSTREAM.fromXML(xml); - assertEquals(o,s); + assertEquals(xml, o, s); } public static class Foo { @@ -80,7 +79,7 @@ public class SecretTest extends TestCase { * Makes sure the serialization form is backward compatible with String. */ public void testCompatibilityFromString() { - String tagName = Foo.class.getName().replace("$","-"); + String tagName = Foo.class.getName().replace("$","_-"); String xml = "<"+tagName+">secret"; Foo foo = new Foo(); Hudson.XSTREAM.fromXML(xml, foo); diff --git a/core/src/test/java/hudson/util/XStream2Test.java b/core/src/test/java/hudson/util/XStream2Test.java index add7eacbcd..4a996f220c 100644 --- a/core/src/test/java/hudson/util/XStream2Test.java +++ b/core/src/test/java/hudson/util/XStream2Test.java @@ -1,7 +1,7 @@ /* * The MIT License * - * Copyright (c) 2004-2009, Sun Microsystems, Inc. + * Copyright (c) 2004-2010, Sun Microsystems, 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 @@ -29,18 +29,56 @@ import hudson.model.Run; /** * - * @author Kohsuke Kawaguchi + * @author Kohsuke Kawaguchi, Mike Dillon, Alan Harder */ public class XStream2Test extends TestCase { + public static final class Foo { Result r1,r2; } - public void testMarshalValue() throws Exception { + + public void testMarshalValue() { Foo f = new Foo(); f.r1 = f.r2 = Result.FAILURE; String xml = Run.XSTREAM.toXML(f); - System.out.println(xml); // we should find two "FAILURE"s as they should be written out twice - assertEquals(3,xml.split("FAILURE").length); + assertEquals(xml, 3, xml.split("FAILURE").length); + } + + private static class Bar { + String s; + } + + /** + * Test ability to read old XML from Hudson 1.105 or older. + */ + public void testXStream11Compatibility() { + Bar b = (Bar)new XStream2().fromXML( + "foo"); + assertEquals("foo", b.s); + } + + public static final class __Foo_Bar$Class { + String under_1 = "1", under__2 = "2", + _leadUnder1 = "L1", __leadUnder2 = "L2", + $dollar = "D1", dollar$2 = "D2"; + } + + /** + * Test marshal/unmarshal round trip for class/field names with _ and $ characters. + */ + public void testXmlRoundTrip() { + XStream2 xs = new XStream2(); + __Foo_Bar$Class b = new __Foo_Bar$Class(); + + String xml = xs.toXML(b); + __Foo_Bar$Class b2 = (__Foo_Bar$Class)xs.fromXML(xml); + + assertEquals(xml, b.under_1, b2.under_1); + assertEquals(xml, b.under__2, b2.under__2); + assertEquals(xml, b._leadUnder1, b2._leadUnder1); + assertEquals(xml, b.__leadUnder2, b2.__leadUnder2); + assertEquals(xml, b.$dollar, b2.$dollar); + assertEquals(xml, b.dollar$2, b2.dollar$2); } }