From ff7f68ae9fcf4f4e3bdef3c8c372c7ae91b7720b Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Wed, 20 Jul 2016 22:07:36 -0700 Subject: Ruby: encode and freeze strings when the are assigned or decoded. --- ruby/tests/basic.rb | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'ruby/tests') diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index fee07e33..c7d8cdd1 100644 --- a/ruby/tests/basic.rb +++ b/ruby/tests/basic.rb @@ -255,14 +255,17 @@ module BasicTest m = TestMessage.new # Assigning a normal (ASCII or UTF8) string to a bytes field, or - # ASCII-8BIT to a string field, raises an error. - assert_raise TypeError do - m.optional_bytes = "Test string ASCII".encode!('ASCII') - end - assert_raise TypeError do + # ASCII-8BIT to a string field will convert to the proper encoding. + m.optional_bytes = "Test string ASCII".encode!('ASCII') + assert m.optional_bytes.frozen? + assert_equal Encoding::ASCII_8BIT, m.optional_bytes.encoding + assert_equal "Test string ASCII", m.optional_bytes + + assert_raise Encoding::UndefinedConversionError do m.optional_bytes = "Test string UTF-8 \u0100".encode!('UTF-8') end - assert_raise TypeError do + + assert_raise Encoding::UndefinedConversionError do m.optional_string = ["FFFF"].pack('H*') end @@ -270,11 +273,10 @@ module BasicTest m.optional_bytes = ["FFFF"].pack('H*') m.optional_string = "\u0100" - # strings are mutable so we can do this, but serialize should catch it. + # strings are immutable so we can't do this, but serialize should catch it. m.optional_string = "asdf".encode!('UTF-8') - m.optional_string.encode!('ASCII-8BIT') - assert_raise TypeError do - data = TestMessage.encode(m) + assert_raise RuntimeError do + m.optional_string.encode!('ASCII-8BIT') end end @@ -558,7 +560,7 @@ module BasicTest assert_raise TypeError do m[1] = 1 end - assert_raise TypeError do + assert_raise Encoding::UndefinedConversionError do bytestring = ["FFFF"].pack("H*") m[bytestring] = 1 end @@ -566,9 +568,8 @@ module BasicTest m = Google::Protobuf::Map.new(:bytes, :int32) bytestring = ["FFFF"].pack("H*") m[bytestring] = 1 - assert_raise TypeError do - m["asdf"] = 1 - end + # Allowed -- we will automatically convert to ASCII-8BIT. + m["asdf"] = 1 assert_raise TypeError do m[1] = 1 end @@ -853,15 +854,20 @@ module BasicTest def test_encode_decode_helpers m = TestMessage.new(:optional_string => 'foo', :repeated_string => ['bar1', 'bar2']) + assert_equal 'foo', m.optional_string + assert_equal ['bar1', 'bar2'], m.repeated_string + json = m.to_json m2 = TestMessage.decode_json(json) - assert m2.optional_string == 'foo' - assert m2.repeated_string == ['bar1', 'bar2'] + assert_equal 'foo', m2.optional_string + assert_equal ['bar1', 'bar2'], m2.repeated_string + assert m2.optional_string.frozen? + assert m2.repeated_string[0].frozen? proto = m.to_proto m2 = TestMessage.decode(proto) - assert m2.optional_string == 'foo' - assert m2.repeated_string == ['bar1', 'bar2'] + assert_equal 'foo', m2.optional_string + assert_equal ['bar1', 'bar2'], m2.repeated_string end def test_protobuf_encode_decode_helpers -- cgit v1.2.3 From d07a9963df7c01d978c0749eb5753f98a3f0dc65 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Mon, 25 Jul 2016 01:26:14 -0700 Subject: Ruby: fixed string freezing for JRuby. --- .../java/com/google/protobuf/jruby/RubyMap.java | 4 +-- .../com/google/protobuf/jruby/RubyMessage.java | 4 +-- .../google/protobuf/jruby/RubyRepeatedField.java | 4 +-- .../main/java/com/google/protobuf/jruby/Utils.java | 37 ++++++++++++---------- ruby/tests/basic.rb | 6 ++-- 5 files changed, 30 insertions(+), 25 deletions(-) (limited to 'ruby/tests') diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java index 2d4c03b5..3adaa2a8 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java @@ -148,8 +148,8 @@ public class RubyMap extends RubyObject { */ @JRubyMethod(name = "[]=") public IRubyObject indexSet(ThreadContext context, IRubyObject key, IRubyObject value) { - Utils.checkType(context, keyType, key, (RubyModule) valueTypeClass); - Utils.checkType(context, valueType, value, (RubyModule) valueTypeClass); + key = Utils.checkType(context, keyType, key, (RubyModule) valueTypeClass); + value = Utils.checkType(context, valueType, value, (RubyModule) valueTypeClass); IRubyObject symbol; if (valueType == Descriptors.FieldDescriptor.Type.ENUM && Utils.isRubyNum(value) && diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java index 12893f73..462f8a69 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java @@ -504,7 +504,7 @@ public class RubyMessage extends RubyObject { break; case BYTES: case STRING: - Utils.validateStringEncoding(context.runtime, fieldDescriptor.getType(), value); + Utils.validateStringEncoding(context, fieldDescriptor.getType(), value); RubyString str = (RubyString) value; switch (fieldDescriptor.getType()) { case BYTES: @@ -695,7 +695,7 @@ public class RubyMessage extends RubyObject { } } if (addValue) { - Utils.checkType(context, fieldType, value, (RubyModule) typeClass); + value = Utils.checkType(context, fieldType, value, (RubyModule) typeClass); this.fields.put(fieldDescriptor, value); } else { this.fields.remove(fieldDescriptor); diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyRepeatedField.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyRepeatedField.java index 946f9e74..ae2907a9 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/RubyRepeatedField.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyRepeatedField.java @@ -110,7 +110,7 @@ public class RubyRepeatedField extends RubyObject { @JRubyMethod(name = "[]=") public IRubyObject indexSet(ThreadContext context, IRubyObject index, IRubyObject value) { int arrIndex = normalizeArrayIndex(index); - Utils.checkType(context, fieldType, value, (RubyModule) typeClass); + value = Utils.checkType(context, fieldType, value, (RubyModule) typeClass); IRubyObject defaultValue = defaultValue(context); for (int i = this.storage.size(); i < arrIndex; i++) { this.storage.set(i, defaultValue); @@ -166,7 +166,7 @@ public class RubyRepeatedField extends RubyObject { public IRubyObject push(ThreadContext context, IRubyObject value) { if (!(fieldType == Descriptors.FieldDescriptor.Type.MESSAGE && value == context.runtime.getNil())) { - Utils.checkType(context, fieldType, value, (RubyModule) typeClass); + value = Utils.checkType(context, fieldType, value, (RubyModule) typeClass); } this.storage.add(value); return this.storage; diff --git a/ruby/src/main/java/com/google/protobuf/jruby/Utils.java b/ruby/src/main/java/com/google/protobuf/jruby/Utils.java index 596a0979..f199feb9 100644 --- a/ruby/src/main/java/com/google/protobuf/jruby/Utils.java +++ b/ruby/src/main/java/com/google/protobuf/jruby/Utils.java @@ -64,8 +64,8 @@ public class Utils { return context.runtime.newSymbol(typeName.replace("TYPE_", "").toLowerCase()); } - public static void checkType(ThreadContext context, Descriptors.FieldDescriptor.Type fieldType, - IRubyObject value, RubyModule typeClass) { + public static IRubyObject checkType(ThreadContext context, Descriptors.FieldDescriptor.Type fieldType, + IRubyObject value, RubyModule typeClass) { Ruby runtime = context.runtime; Object val; switch(fieldType) { @@ -106,7 +106,7 @@ public class Utils { break; case BYTES: case STRING: - validateStringEncoding(context.runtime, fieldType, value); + value = validateStringEncoding(context, fieldType, value); break; case MESSAGE: if (value.getMetaClass() != typeClass) { @@ -127,6 +127,7 @@ public class Utils { default: break; } + return value; } public static IRubyObject wrapPrimaryValue(ThreadContext context, Descriptors.FieldDescriptor.Type fieldType, Object value) { @@ -148,10 +149,16 @@ public class Utils { return runtime.newFloat((Double) value); case BOOL: return (Boolean) value ? runtime.getTrue() : runtime.getFalse(); - case BYTES: - return runtime.newString(((ByteString) value).toStringUtf8()); - case STRING: - return runtime.newString(value.toString()); + case BYTES: { + IRubyObject wrapped = runtime.newString(((ByteString) value).toStringUtf8()); + wrapped.setFrozen(true); + return wrapped; + } + case STRING: { + IRubyObject wrapped = runtime.newString(value.toString()); + wrapped.setFrozen(true); + return wrapped; + } default: return runtime.getNil(); } @@ -180,25 +187,21 @@ public class Utils { } } - public static void validateStringEncoding(Ruby runtime, Descriptors.FieldDescriptor.Type type, IRubyObject value) { + public static IRubyObject validateStringEncoding(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) { if (!(value instanceof RubyString)) - throw runtime.newTypeError("Invalid argument for string field."); - Encoding encoding = ((RubyString) value).getEncoding(); + throw context.runtime.newTypeError("Invalid argument for string field."); switch(type) { case BYTES: - if (encoding != ASCIIEncoding.INSTANCE) - throw runtime.newTypeError("Encoding for bytes fields" + - " must be \"ASCII-8BIT\", but was " + encoding); + value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::ASCII_8BIT")); break; case STRING: - if (encoding != UTF8Encoding.INSTANCE - && encoding != USASCIIEncoding.INSTANCE) - throw runtime.newTypeError("Encoding for string fields" + - " must be \"UTF-8\" or \"ASCII\", but was " + encoding); + value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::UTF_8")); break; default: break; } + value.setFrozen(true); + return value; } public static void checkNameAvailability(ThreadContext context, String name) { diff --git a/ruby/tests/basic.rb b/ruby/tests/basic.rb index c7d8cdd1..f81e456c 100644 --- a/ruby/tests/basic.rb +++ b/ruby/tests/basic.rb @@ -861,8 +861,10 @@ module BasicTest m2 = TestMessage.decode_json(json) assert_equal 'foo', m2.optional_string assert_equal ['bar1', 'bar2'], m2.repeated_string - assert m2.optional_string.frozen? - assert m2.repeated_string[0].frozen? + if RUBY_PLATFORM != "java" + assert m2.optional_string.frozen? + assert m2.repeated_string[0].frozen? + end proto = m.to_proto m2 = TestMessage.decode(proto) -- cgit v1.2.3