diff options
Diffstat (limited to 'test/files')
-rw-r--r-- | test/files/neg/sd128.check | 17 | ||||
-rw-r--r-- | test/files/neg/sd128/A.java | 3 | ||||
-rw-r--r-- | test/files/neg/sd128/Test.scala | 19 | ||||
-rw-r--r-- | test/files/pos/t2377b/Q.java | 13 | ||||
-rw-r--r-- | test/files/pos/t2377b/a.scala | 5 | ||||
-rw-r--r-- | test/files/run/lambda-serialization-security.scala | 47 | ||||
-rw-r--r-- | test/files/run/lambda-serialization.scala | 71 |
7 files changed, 147 insertions, 28 deletions
diff --git a/test/files/neg/sd128.check b/test/files/neg/sd128.check new file mode 100644 index 0000000000..8f6fcb1213 --- /dev/null +++ b/test/files/neg/sd128.check @@ -0,0 +1,17 @@ +Test.scala:4: error: class C1 inherits conflicting members: + method f in trait A of type ()Int and + method f in trait T of type => Int +(Note: this can be resolved by declaring an override in class C1.) +class C1 extends A with T // error + ^ +Test.scala:5: error: class C2 inherits conflicting members: + method f in trait T of type => Int and + method f in trait A of type ()Int +(Note: this can be resolved by declaring an override in class C2.) +class C2 extends T with A // error + ^ +Test.scala:14: error: overriding method f in trait A of type ()Int; + method f needs `override' modifier + def f() = 9999 // need override modifier + ^ +three errors found diff --git a/test/files/neg/sd128/A.java b/test/files/neg/sd128/A.java new file mode 100644 index 0000000000..6774deba2e --- /dev/null +++ b/test/files/neg/sd128/A.java @@ -0,0 +1,3 @@ +interface A { + default int f() { return -10; } +} diff --git a/test/files/neg/sd128/Test.scala b/test/files/neg/sd128/Test.scala new file mode 100644 index 0000000000..66ca3d0940 --- /dev/null +++ b/test/files/neg/sd128/Test.scala @@ -0,0 +1,19 @@ +trait T { + def f = 99 +} +class C1 extends A with T // error +class C2 extends T with A // error + +trait U extends A { + override def f = 999 +} +class D1 extends A with U // OK +class D2 extends U with A // OK + +class E1 extends A { + def f() = 9999 // need override modifier +} + +class E2 extends A { + override def f() = 9999 // OK +} diff --git a/test/files/pos/t2377b/Q.java b/test/files/pos/t2377b/Q.java new file mode 100644 index 0000000000..fbf9c776e9 --- /dev/null +++ b/test/files/pos/t2377b/Q.java @@ -0,0 +1,13 @@ +public class Q { + + public static class Builder {} + + public static class Inner { + public static class Builder { public void innerMethod() {} } + public Builder foo() { return new Builder(); } // this line gives an error, that Builder is ambiguous + + public Inner.Builder viaSelect() { return new Builder(); } // this line gives an error, that Builder is ambiguous + } + +} + diff --git a/test/files/pos/t2377b/a.scala b/test/files/pos/t2377b/a.scala new file mode 100644 index 0000000000..3053841589 --- /dev/null +++ b/test/files/pos/t2377b/a.scala @@ -0,0 +1,5 @@ +object Test { + (new Q.Inner).foo.innerMethod + (new Q.Inner).viaSelect.innerMethod + +} diff --git a/test/files/run/lambda-serialization-security.scala b/test/files/run/lambda-serialization-security.scala new file mode 100644 index 0000000000..08e235b1cb --- /dev/null +++ b/test/files/run/lambda-serialization-security.scala @@ -0,0 +1,47 @@ +import java.io.{ByteArrayInputStream, ObjectInputStream, ObjectOutputStream, ByteArrayOutputStream} + +trait IntToString extends java.io.Serializable { def apply(i: Int): String } + +object Test { + def main(args: Array[String]): Unit = { + roundTrip() + roundTripIndySam() + } + + def roundTrip(): Unit = { + val c = new Capture("Capture") + val lambda = (p: Param) => ("a", p, c) + val reconstituted1 = serializeDeserialize(lambda).asInstanceOf[Object => Any] + val p = new Param + assert(reconstituted1.apply(p) == ("a", p, c)) + val reconstituted2 = serializeDeserialize(lambda).asInstanceOf[Object => Any] + assert(reconstituted1.getClass == reconstituted2.getClass) + + val reconstituted3 = serializeDeserialize(reconstituted1) + assert(reconstituted3.apply(p) == ("a", p, c)) + + val specializedLambda = (p: Int) => List(p, c).length + assert(serializeDeserialize(specializedLambda).apply(42) == 2) + assert(serializeDeserialize(serializeDeserialize(specializedLambda)).apply(42) == 2) + } + + // lambda targeting a SAM, not a FunctionN (should behave the same way) + def roundTripIndySam(): Unit = { + val lambda: IntToString = (x: Int) => "yo!" * x + val reconstituted1 = serializeDeserialize(lambda).asInstanceOf[IntToString] + val reconstituted2 = serializeDeserialize(reconstituted1).asInstanceOf[IntToString] + assert(reconstituted1.apply(2) == "yo!yo!") + assert(reconstituted1.getClass == reconstituted2.getClass) + } + + def serializeDeserialize[T <: AnyRef](obj: T) = { + val buffer = new ByteArrayOutputStream + val out = new ObjectOutputStream(buffer) + out.writeObject(obj) + val in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray)) + in.readObject.asInstanceOf[T] + } +} + +case class Capture(s: String) extends Serializable +class Param diff --git a/test/files/run/lambda-serialization.scala b/test/files/run/lambda-serialization.scala index 08e235b1cb..78b4c5d58b 100644 --- a/test/files/run/lambda-serialization.scala +++ b/test/files/run/lambda-serialization.scala @@ -1,37 +1,54 @@ -import java.io.{ByteArrayInputStream, ObjectInputStream, ObjectOutputStream, ByteArrayOutputStream} +import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream} +import java.lang.invoke.{MethodHandleInfo, SerializedLambda} + +import scala.tools.nsc.util + +class C extends java.io.Serializable { + val fs = List( + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => (), + () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => (), () => () ,() => (), () => (), () => (), () => (), () => () + ) + private def foo(): Unit = { + assert(false, "should not be called!!!") + } +} -trait IntToString extends java.io.Serializable { def apply(i: Int): String } +trait FakeSam { def apply(): Unit } object Test { def main(args: Array[String]): Unit = { - roundTrip() - roundTripIndySam() + allRealLambdasRoundTrip() + fakeLambdaFailsToDeserialize() } - def roundTrip(): Unit = { - val c = new Capture("Capture") - val lambda = (p: Param) => ("a", p, c) - val reconstituted1 = serializeDeserialize(lambda).asInstanceOf[Object => Any] - val p = new Param - assert(reconstituted1.apply(p) == ("a", p, c)) - val reconstituted2 = serializeDeserialize(lambda).asInstanceOf[Object => Any] - assert(reconstituted1.getClass == reconstituted2.getClass) - - val reconstituted3 = serializeDeserialize(reconstituted1) - assert(reconstituted3.apply(p) == ("a", p, c)) - - val specializedLambda = (p: Int) => List(p, c).length - assert(serializeDeserialize(specializedLambda).apply(42) == 2) - assert(serializeDeserialize(serializeDeserialize(specializedLambda)).apply(42) == 2) + def allRealLambdasRoundTrip(): Unit = { + new C().fs.map(x => serializeDeserialize(x).apply()) } - // lambda targeting a SAM, not a FunctionN (should behave the same way) - def roundTripIndySam(): Unit = { - val lambda: IntToString = (x: Int) => "yo!" * x - val reconstituted1 = serializeDeserialize(lambda).asInstanceOf[IntToString] - val reconstituted2 = serializeDeserialize(reconstituted1).asInstanceOf[IntToString] - assert(reconstituted1.apply(2) == "yo!yo!") - assert(reconstituted1.getClass == reconstituted2.getClass) + def fakeLambdaFailsToDeserialize(): Unit = { + val fake = new SerializedLambda(classOf[C], classOf[FakeSam].getName, "apply", "()V", + MethodHandleInfo.REF_invokeVirtual, classOf[C].getName, "foo", "()V", "()V", Array(new C)) + try { + serializeDeserialize(fake).asInstanceOf[FakeSam].apply() + assert(false) + } catch { + case ex: Exception => + val stackTrace = util.stackTraceString(ex) + assert(stackTrace.contains("Illegal lambda deserialization"), stackTrace) + } } def serializeDeserialize[T <: AnyRef](obj: T) = { @@ -43,5 +60,3 @@ object Test { } } -case class Capture(s: String) extends Serializable -class Param |