@@ -414,6 +414,42 @@ defmodule Protobuf.JSON.EncodeTest do
414414 assert catch_throw ( encode ( message ) ) ==
415415 { :bad_duration , :seconds_outside_of_range , - max_duration - 1 }
416416 end
417+
418+ test "returns an error if the nanos of the duration are too large" do
419+ message = % TestAllTypesProto3 {
420+ optional_duration: % Google.Protobuf.Duration { seconds: 1 , nanos: 1_000_000_000 }
421+ }
422+
423+ assert catch_throw ( encode ( message ) ) ==
424+ { :bad_duration , :nanos_outside_of_range , 1_000_000_000 }
425+ end
426+
427+ test "returns an error if the nanos of the duration are too small" do
428+ message = % TestAllTypesProto3 {
429+ optional_duration: % Google.Protobuf.Duration { seconds: - 1 , nanos: - 1_000_000_000 }
430+ }
431+
432+ assert catch_throw ( encode ( message ) ) ==
433+ { :bad_duration , :nanos_outside_of_range , - 1_000_000_000 }
434+ end
435+
436+ test "returns an error if the seconds and nanos have different signs (positive seconds)" do
437+ message = % TestAllTypesProto3 {
438+ optional_duration: % Google.Protobuf.Duration { seconds: 1 , nanos: - 1 }
439+ }
440+
441+ assert catch_throw ( encode ( message ) ) ==
442+ { :bad_duration , :seconds_and_nanos_different_signs , { 1 , - 1 } }
443+ end
444+
445+ test "returns an error if the seconds and nanos have different signs (negative seconds)" do
446+ message = % TestAllTypesProto3 {
447+ optional_duration: % Google.Protobuf.Duration { seconds: - 1 , nanos: 1 }
448+ }
449+
450+ assert catch_throw ( encode ( message ) ) ==
451+ { :bad_duration , :seconds_and_nanos_different_signs , { - 1 , 1 } }
452+ end
417453 end
418454
419455 describe "Google.Protobuf.Timestamp" do
@@ -437,6 +473,22 @@ defmodule Protobuf.JSON.EncodeTest do
437473 assert catch_throw ( encode ( message ) ) ==
438474 { :invalid_timestamp , timestamp , "timestamp is outside of allowed range" }
439475 end
476+
477+ test "returns an error if the nanos of the timestamp are negative" do
478+ timestamp = % Google.Protobuf.Timestamp { seconds: 0 , nanos: - 1 }
479+ message = % TestAllTypesProto3 { optional_timestamp: timestamp }
480+
481+ assert catch_throw ( encode ( message ) ) ==
482+ { :invalid_timestamp , timestamp , "nanos outside of allowed range 0..999999999" }
483+ end
484+
485+ test "returns an error if the nanos of the timestamp are too large" do
486+ timestamp = % Google.Protobuf.Timestamp { seconds: 0 , nanos: 1_000_000_000 }
487+ message = % TestAllTypesProto3 { optional_timestamp: timestamp }
488+
489+ assert catch_throw ( encode ( message ) ) ==
490+ { :invalid_timestamp , timestamp , "nanos outside of allowed range 0..999999999" }
491+ end
440492 end
441493
442494 describe "Google.Protobuf.Value" do
0 commit comments