File tree Expand file tree Collapse file tree
main/java/me/chanjar/weixin/common/util/json
java/me/chanjar/weixin/common/util/json Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -35,7 +35,12 @@ public Date read(JsonReader in) throws IOException {
3535 in .nextNull ();
3636 return null ;
3737 case NUMBER :
38- return new Date (in .nextInt () * 1000 );
38+ long seconds = in .nextLong ();
39+ try {
40+ return new Date (Math .multiplyExact (seconds , 1000L ));
41+ } catch (ArithmeticException e ) {
42+ throw new JsonParseException ("Timestamp seconds out of range: " + seconds , e );
43+ }
3944 default :
4045 throw new JsonParseException ("Expected NUMBER but was " + peek );
4146 }
Original file line number Diff line number Diff line change 1+ package me .chanjar .weixin .common .util .json ;
2+
3+ import com .google .gson .JsonParseException ;
4+ import org .testng .annotations .Test ;
5+
6+ import java .io .IOException ;
7+ import java .util .Date ;
8+
9+ import static org .assertj .core .api .Assertions .assertThat ;
10+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
11+
12+ /**
13+ * Tests for {@link WxDateTypeAdapter}.
14+ */
15+ public class WxDateTypeAdapterTest {
16+ private final WxDateTypeAdapter adapter = new WxDateTypeAdapter ();
17+
18+ @ Test
19+ public void testReadTimestampAfter2038 () throws IOException {
20+ Date date = this .adapter .fromJson ("4102444800" );
21+
22+ assertThat (date ).isEqualTo (new Date (4102444800000L ));
23+ }
24+
25+ @ Test
26+ public void testReadRejectsPositiveMillisecondOverflow () {
27+ long overflowingSeconds = Long .MAX_VALUE / 1000 + 1 ;
28+
29+ assertThatThrownBy (() -> this .adapter .fromJson (Long .toString (overflowingSeconds )))
30+ .isInstanceOf (JsonParseException .class )
31+ .hasMessageContaining ("out of range" );
32+ }
33+
34+ @ Test
35+ public void testReadRejectsNegativeMillisecondOverflow () {
36+ long overflowingSeconds = Long .MIN_VALUE / 1000 - 1 ;
37+
38+ assertThatThrownBy (() -> this .adapter .fromJson (Long .toString (overflowingSeconds )))
39+ .isInstanceOf (JsonParseException .class )
40+ .hasMessageContaining ("out of range" );
41+ }
42+
43+ @ Test
44+ public void testWriteUsesSeconds () throws IOException {
45+ assertThat (this .adapter .toJson (new Date (4102444800123L ))).isEqualTo ("4102444800" );
46+ }
47+ }
Original file line number Diff line number Diff line change 77 <class name =" me.chanjar.weixin.common.error.WxErrorTest" />
88 <class name =" me.chanjar.weixin.common.bean.WxMenuTest" />
99 <class name =" me.chanjar.weixin.common.util.crypto.WxCryptUtilTest" />
10+ <class name =" me.chanjar.weixin.common.util.json.WxDateTypeAdapterTest" />
1011 <class name =" me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateCheckerTest" />
1112 <class name =" me.chanjar.weixin.common.session.SessionTest" />
1213 </classes >
You can’t perform that action at this time.
0 commit comments