aboutsummaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-12-14 15:59:07 +0100
committerChristian Grothoff <christian@grothoff.org>2021-12-14 15:59:12 +0100
commit99779b455ce3bf9c53dd411575766bf298a3a5f3 (patch)
tree12cd49f097fdd1947580b08ec10ca961f8c250b9 /src/json
parent8c702327dae9e504e0f6e1678884d9327321f44a (diff)
downloadgnunet-99779b455ce3bf9c53dd411575766bf298a3a5f3.tar.gz
gnunet-99779b455ce3bf9c53dd411575766bf298a3a5f3.zip
introducing GNUNET_TIME_Timestamp
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json_generator.c86
-rw-r--r--src/json/json_helper.c124
-rw-r--r--src/json/json_pack.c16
-rw-r--r--src/json/test_json.c70
4 files changed, 137 insertions, 159 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
index 0c513ca9d..eb275712c 100644
--- a/src/json/json_generator.c
+++ b/src/json/json_generator.c
@@ -27,14 +27,6 @@
27#include "gnunet_json_lib.h" 27#include "gnunet_json_lib.h"
28 28
29 29
30/**
31 * Convert binary data to a JSON string
32 * with the base32crockford encoding.
33 *
34 * @param data binary data
35 * @param size size of @a data in bytes
36 * @return json string that encodes @a data
37 */
38json_t * 30json_t *
39GNUNET_JSON_from_data (const void *data, 31GNUNET_JSON_from_data (const void *data,
40 size_t size) 32 size_t size)
@@ -57,31 +49,22 @@ GNUNET_JSON_from_data (const void *data,
57} 49}
58 50
59 51
60/**
61 * Convert absolute timestamp to a json string.
62 *
63 * @param stamp the time stamp
64 * @return a json string with the timestamp in @a stamp
65 */
66json_t * 52json_t *
67GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp) 53GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp)
68{ 54{
69 json_t *j; 55 json_t *j;
70 56
71 GNUNET_assert (GNUNET_OK ==
72 GNUNET_TIME_round_abs (&stamp));
73
74 j = json_object (); 57 j = json_object ();
75 if (NULL == j) 58 if (NULL == j)
76 { 59 {
77 GNUNET_break (0); 60 GNUNET_break (0);
78 return NULL; 61 return NULL;
79 } 62 }
80 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 63 if (GNUNET_TIME_absolute_is_never (stamp.abs_time))
81 { 64 {
82 if (0 != 65 if (0 !=
83 json_object_set_new (j, 66 json_object_set_new (j,
84 "t_ms", 67 "t_s",
85 json_string ("never"))) 68 json_string ("never")))
86 { 69 {
87 GNUNET_break (0); 70 GNUNET_break (0);
@@ -90,11 +73,17 @@ GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
90 } 73 }
91 return j; 74 return j;
92 } 75 }
76 GNUNET_assert (
77 0 ==
78 (stamp.abs_time.abs_value_us
79 % GNUNET_TIME_UNIT_SECONDS.rel_value_us));
93 if (0 != 80 if (0 !=
94 json_object_set_new (j, 81 json_object_set_new (
95 "t_ms", 82 j,
96 json_integer ((json_int_t) (stamp.abs_value_us 83 "t_s",
97 / 1000LL)))) 84 json_integer (
85 (json_int_t) (stamp.abs_time.abs_value_us
86 / GNUNET_TIME_UNIT_SECONDS.rel_value_us))))
98 { 87 {
99 GNUNET_break (0); 88 GNUNET_break (0);
100 json_decref (j); 89 json_decref (j);
@@ -104,44 +93,29 @@ GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
104} 93}
105 94
106 95
107/**
108 * Convert absolute timestamp to a json string.
109 *
110 * @param stamp the time stamp
111 * @return a json string with the timestamp in @a stamp
112 */
113json_t * 96json_t *
114GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp) 97GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp)
115{ 98{
116 return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp)); 99 return GNUNET_JSON_from_timestamp (GNUNET_TIME_timestamp_ntoh (stamp));
117} 100}
118 101
119 102
120/**
121 * Convert relative timestamp to a json string.
122 *
123 * @param stamp the time stamp
124 * @return a json string with the timestamp in @a stamp
125 */
126json_t * 103json_t *
127GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) 104GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
128{ 105{
129 json_t *j; 106 json_t *j;
130 107
131 GNUNET_assert (GNUNET_OK ==
132 GNUNET_TIME_round_rel (&stamp));
133
134 j = json_object (); 108 j = json_object ();
135 if (NULL == j) 109 if (NULL == j)
136 { 110 {
137 GNUNET_break (0); 111 GNUNET_break (0);
138 return NULL; 112 return NULL;
139 } 113 }
140 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 114 if (GNUNET_TIME_relative_is_forever (stamp))
141 { 115 {
142 if (0 != 116 if (0 !=
143 json_object_set_new (j, 117 json_object_set_new (j,
144 "d_ms", 118 "d_us",
145 json_string ("forever"))) 119 json_string ("forever")))
146 { 120 {
147 GNUNET_break (0); 121 GNUNET_break (0);
@@ -150,11 +124,17 @@ GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
150 } 124 }
151 return j; 125 return j;
152 } 126 }
127 if (stamp.rel_value_us >= (1LLU << 53))
128 {
129 /* value is larger than allowed */
130 GNUNET_break (0);
131 return NULL;
132 }
153 if (0 != 133 if (0 !=
154 json_object_set_new (j, 134 json_object_set_new (
155 "d_ms", 135 j,
156 json_integer ((json_int_t) (stamp.rel_value_us 136 "d_us",
157 / 1000LL)))) 137 json_integer ((json_int_t) stamp.rel_value_us)))
158 { 138 {
159 GNUNET_break (0); 139 GNUNET_break (0);
160 json_decref (j); 140 json_decref (j);
@@ -164,12 +144,6 @@ GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
164} 144}
165 145
166 146
167/**
168 * Convert RSA public key to JSON.
169 *
170 * @param pk public key to convert
171 * @return corresponding JSON encoding
172 */
173json_t * 147json_t *
174GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk) 148GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
175{ 149{
@@ -186,12 +160,6 @@ GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
186} 160}
187 161
188 162
189/**
190 * Convert RSA signature to JSON.
191 *
192 * @param sig signature to convert
193 * @return corresponding JSON encoding
194 */
195json_t * 163json_t *
196GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) 164GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
197{ 165{
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 3a11f205c..73f5fc00c 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -49,7 +49,7 @@ GNUNET_JSON_spec_end ()
49 * @param[out] spec where to write the data 49 * @param[out] spec where to write the data
50 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 50 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
51 */ 51 */
52static int 52static enum GNUNET_GenericReturnValue
53parse_fixed_data (void *cls, 53parse_fixed_data (void *cls,
54 json_t *root, 54 json_t *root,
55 struct GNUNET_JSON_Specification *spec) 55 struct GNUNET_JSON_Specification *spec)
@@ -579,20 +579,20 @@ GNUNET_JSON_spec_int64 (const char *name,
579/* ************ GNUnet-specific parser specifications ******************* */ 579/* ************ GNUnet-specific parser specifications ******************* */
580 580
581/** 581/**
582 * Parse given JSON object to absolute time. 582 * Parse given JSON object to a timestamp.
583 * 583 *
584 * @param cls closure, NULL 584 * @param cls closure, NULL
585 * @param root the json object representing data 585 * @param root the json object representing data
586 * @param[out] spec where to write the data 586 * @param[out] spec where to write the data
587 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 587 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
588 */ 588 */
589static int 589static enum GNUNET_GenericReturnValue
590parse_abs_time (void *cls, 590parse_timestamp (void *cls,
591 json_t *root, 591 json_t *root,
592 struct GNUNET_JSON_Specification *spec) 592 struct GNUNET_JSON_Specification *spec)
593{ 593{
594 struct GNUNET_TIME_Absolute *abs = spec->ptr; 594 struct GNUNET_TIME_Timestamp *ts = spec->ptr;
595 json_t *json_t_ms; 595 json_t *json_t_s;
596 unsigned long long int tval; 596 unsigned long long int tval;
597 597
598 if (! json_is_object (root)) 598 if (! json_is_object (root))
@@ -600,14 +600,16 @@ parse_abs_time (void *cls,
600 GNUNET_break_op (0); 600 GNUNET_break_op (0);
601 return GNUNET_SYSERR; 601 return GNUNET_SYSERR;
602 } 602 }
603 json_t_ms = json_object_get (root, "t_ms"); 603 json_t_s = json_object_get (root,
604 if (json_is_integer (json_t_ms)) 604 "t_s");
605 if (json_is_integer (json_t_s))
605 { 606 {
606 tval = json_integer_value (json_t_ms); 607 tval = json_integer_value (json_t_s);
607 /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ 608 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
608 abs->abs_value_us = tval * GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us; 609 ts->abs_time.abs_value_us
609 if ((abs->abs_value_us) 610 = tval * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
610 / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us 611 if (ts->abs_time.abs_value_us
612 / GNUNET_TIME_UNIT_SECONDS.rel_value_us
611 != tval) 613 != tval)
612 { 614 {
613 /* Integer overflow */ 615 /* Integer overflow */
@@ -616,14 +618,15 @@ parse_abs_time (void *cls,
616 } 618 }
617 return GNUNET_OK; 619 return GNUNET_OK;
618 } 620 }
619 if (json_is_string (json_t_ms)) 621 if (json_is_string (json_t_s))
620 { 622 {
621 const char *val; 623 const char *val;
622 624
623 val = json_string_value (json_t_ms); 625 val = json_string_value (json_t_s);
624 if ((0 == strcasecmp (val, "never"))) 626 if ((0 == strcasecmp (val,
627 "never")))
625 { 628 {
626 *abs = GNUNET_TIME_UNIT_FOREVER_ABS; 629 ts->abs_time = GNUNET_TIME_UNIT_FOREVER_ABS;
627 return GNUNET_OK; 630 return GNUNET_OK;
628 } 631 }
629 GNUNET_break_op (0); 632 GNUNET_break_op (0);
@@ -635,17 +638,14 @@ parse_abs_time (void *cls,
635 638
636 639
637struct GNUNET_JSON_Specification 640struct GNUNET_JSON_Specification
638GNUNET_JSON_spec_absolute_time (const char *name, 641GNUNET_JSON_spec_timestamp (const char *name,
639 struct GNUNET_TIME_Absolute *at) 642 struct GNUNET_TIME_Timestamp *t)
640{ 643{
641 struct GNUNET_JSON_Specification ret = { 644 struct GNUNET_JSON_Specification ret = {
642 .parser = &parse_abs_time, 645 .parser = &parse_timestamp,
643 .cleaner = NULL,
644 .cls = NULL,
645 .field = name, 646 .field = name,
646 .ptr = at, 647 .ptr = t,
647 .ptr_size = sizeof(struct GNUNET_TIME_Absolute), 648 .ptr_size = sizeof(struct GNUNET_TIME_Timestamp)
648 .size_ptr = NULL
649 }; 649 };
650 650
651 return ret; 651 return ret;
@@ -660,40 +660,37 @@ GNUNET_JSON_spec_absolute_time (const char *name,
660 * @param[out] spec where to write the data 660 * @param[out] spec where to write the data
661 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 661 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
662 */ 662 */
663static int 663static enum GNUNET_GenericReturnValue
664parse_abs_time_nbo (void *cls, 664parse_timestamp_nbo (void *cls,
665 json_t *root, 665 json_t *root,
666 struct GNUNET_JSON_Specification *spec) 666 struct GNUNET_JSON_Specification *spec)
667{ 667{
668 struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr; 668 struct GNUNET_TIME_TimestampNBO *ts = spec->ptr;
669 struct GNUNET_TIME_Absolute a; 669 struct GNUNET_TIME_Timestamp a;
670 struct GNUNET_JSON_Specification ispec; 670 struct GNUNET_JSON_Specification ispec;
671 671
672 ispec = *spec; 672 ispec = *spec;
673 ispec.parser = &parse_abs_time; 673 ispec.parser = &parse_timestamp;
674 ispec.ptr = &a; 674 ispec.ptr = &a;
675 if (GNUNET_OK != 675 if (GNUNET_OK !=
676 parse_abs_time (NULL, 676 parse_timestamp (NULL,
677 root, 677 root,
678 &ispec)) 678 &ispec))
679 return GNUNET_SYSERR; 679 return GNUNET_SYSERR;
680 *abs = GNUNET_TIME_absolute_hton (a); 680 *ts = GNUNET_TIME_timestamp_hton (a);
681 return GNUNET_OK; 681 return GNUNET_OK;
682} 682}
683 683
684 684
685struct GNUNET_JSON_Specification 685struct GNUNET_JSON_Specification
686GNUNET_JSON_spec_absolute_time_nbo (const char *name, 686GNUNET_JSON_spec_timestamp_nbo (const char *name,
687 struct GNUNET_TIME_AbsoluteNBO *at) 687 struct GNUNET_TIME_TimestampNBO *at)
688{ 688{
689 struct GNUNET_JSON_Specification ret = { 689 struct GNUNET_JSON_Specification ret = {
690 .parser = &parse_abs_time_nbo, 690 .parser = &parse_timestamp_nbo,
691 .cleaner = NULL,
692 .cls = NULL,
693 .field = name, 691 .field = name,
694 .ptr = at, 692 .ptr = at,
695 .ptr_size = sizeof(struct GNUNET_TIME_AbsoluteNBO), 693 .ptr_size = sizeof(struct GNUNET_TIME_TimestampNBO)
696 .size_ptr = NULL
697 }; 694 };
698 695
699 return ret; 696 return ret;
@@ -708,13 +705,13 @@ GNUNET_JSON_spec_absolute_time_nbo (const char *name,
708 * @param[out] spec where to write the data 705 * @param[out] spec where to write the data
709 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 706 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
710 */ 707 */
711static int 708static enum GNUNET_GenericReturnValue
712parse_rel_time (void *cls, 709parse_rel_time (void *cls,
713 json_t *root, 710 json_t *root,
714 struct GNUNET_JSON_Specification *spec) 711 struct GNUNET_JSON_Specification *spec)
715{ 712{
716 struct GNUNET_TIME_Relative *rel = spec->ptr; 713 struct GNUNET_TIME_Relative *rel = spec->ptr;
717 json_t *json_d_ms; 714 json_t *json_d_us;
718 unsigned long long int tval; 715 unsigned long long int tval;
719 716
720 if (! json_is_object (root)) 717 if (! json_is_object (root))
@@ -722,25 +719,27 @@ parse_rel_time (void *cls,
722 GNUNET_break_op (0); 719 GNUNET_break_op (0);
723 return GNUNET_SYSERR; 720 return GNUNET_SYSERR;
724 } 721 }
725 json_d_ms = json_object_get (root, "d_ms"); 722 json_d_us = json_object_get (root,
726 if (json_is_integer (json_d_ms)) 723 "d_us");
724 if (json_is_integer (json_d_us))
727 { 725 {
728 tval = json_integer_value (json_d_ms); 726 tval = json_integer_value (json_d_us);
729 /* Time is in milliseconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ 727 if (tval >= (1LLU << 53))
730 rel->rel_value_us = tval * 1000LL;
731 if ((rel->rel_value_us) / 1000LL != tval)
732 { 728 {
733 /* Integer overflow */ 729 /* value is larger than allowed */
734 GNUNET_break_op (0); 730 GNUNET_break_op (0);
735 return GNUNET_SYSERR; 731 return GNUNET_SYSERR;
736 } 732 }
733 rel->rel_value_us = tval;
737 return GNUNET_OK; 734 return GNUNET_OK;
738 } 735 }
739 if (json_is_string (json_d_ms)) 736 if (json_is_string (json_d_us))
740 { 737 {
741 const char *val; 738 const char *val;
742 val = json_string_value (json_d_ms); 739
743 if ((0 == strcasecmp (val, "forever"))) 740 val = json_string_value (json_d_us);
741 if ((0 == strcasecmp (val,
742 "forever")))
744 { 743 {
745 *rel = GNUNET_TIME_UNIT_FOREVER_REL; 744 *rel = GNUNET_TIME_UNIT_FOREVER_REL;
746 return GNUNET_OK; 745 return GNUNET_OK;
@@ -759,12 +758,9 @@ GNUNET_JSON_spec_relative_time (const char *name,
759{ 758{
760 struct GNUNET_JSON_Specification ret = { 759 struct GNUNET_JSON_Specification ret = {
761 .parser = &parse_rel_time, 760 .parser = &parse_rel_time,
762 .cleaner = NULL,
763 .cls = NULL,
764 .field = name, 761 .field = name,
765 .ptr = rt, 762 .ptr = rt,
766 .ptr_size = sizeof(struct GNUNET_TIME_Relative), 763 .ptr_size = sizeof(struct GNUNET_TIME_Relative)
767 .size_ptr = NULL
768 }; 764 };
769 765
770 return ret; 766 return ret;
@@ -779,7 +775,7 @@ GNUNET_JSON_spec_relative_time (const char *name,
779 * @param[out] spec where to write the data 775 * @param[out] spec where to write the data
780 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 776 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
781 */ 777 */
782static int 778static enum GNUNET_GenericReturnValue
783parse_rsa_public_key (void *cls, 779parse_rsa_public_key (void *cls,
784 json_t *root, 780 json_t *root,
785 struct GNUNET_JSON_Specification *spec) 781 struct GNUNET_JSON_Specification *spec)
@@ -864,7 +860,7 @@ GNUNET_JSON_spec_rsa_public_key (const char *name,
864 * @param[out] spec where to write the data 860 * @param[out] spec where to write the data
865 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 861 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
866 */ 862 */
867static int 863static enum GNUNET_GenericReturnValue
868parse_rsa_signature (void *cls, 864parse_rsa_signature (void *cls,
869 json_t *root, 865 json_t *root,
870 struct GNUNET_JSON_Specification *spec) 866 struct GNUNET_JSON_Specification *spec)
@@ -952,7 +948,7 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
952 * @param[out] spec where to write the data 948 * @param[out] spec where to write the data
953 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 949 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
954 */ 950 */
955static int 951static enum GNUNET_GenericReturnValue
956parse_boolean (void *cls, 952parse_boolean (void *cls,
957 json_t *root, 953 json_t *root,
958 struct GNUNET_JSON_Specification *spec) 954 struct GNUNET_JSON_Specification *spec)
diff --git a/src/json/json_pack.c b/src/json/json_pack.c
index 92f8b4535..296f56104 100644
--- a/src/json/json_pack.c
+++ b/src/json/json_pack.c
@@ -245,16 +245,16 @@ GNUNET_JSON_pack_data_varsize (const char *name,
245 245
246 246
247struct GNUNET_JSON_PackSpec 247struct GNUNET_JSON_PackSpec
248GNUNET_JSON_pack_time_abs (const char *name, 248GNUNET_JSON_pack_timestamp (const char *name,
249 struct GNUNET_TIME_Absolute at) 249 struct GNUNET_TIME_Timestamp t)
250{ 250{
251 struct GNUNET_JSON_PackSpec ps = { 251 struct GNUNET_JSON_PackSpec ps = {
252 .field_name = name 252 .field_name = name
253 }; 253 };
254 254
255 if (0 != at.abs_value_us) 255 if (! GNUNET_TIME_absolute_is_zero (t.abs_time))
256 { 256 {
257 ps.object = GNUNET_JSON_from_time_abs (at); 257 ps.object = GNUNET_JSON_from_timestamp (t);
258 GNUNET_assert (NULL != ps.object); 258 GNUNET_assert (NULL != ps.object);
259 } 259 }
260 else 260 else
@@ -266,11 +266,11 @@ GNUNET_JSON_pack_time_abs (const char *name,
266 266
267 267
268struct GNUNET_JSON_PackSpec 268struct GNUNET_JSON_PackSpec
269GNUNET_JSON_pack_time_abs_nbo (const char *name, 269GNUNET_JSON_pack_timestamp_nbo (const char *name,
270 struct GNUNET_TIME_AbsoluteNBO at) 270 struct GNUNET_TIME_TimestampNBO at)
271{ 271{
272 return GNUNET_JSON_pack_time_abs (name, 272 return GNUNET_JSON_pack_timestamp (name,
273 GNUNET_TIME_absolute_ntoh (at)); 273 GNUNET_TIME_timestamp_ntoh (at));
274} 274}
275 275
276 276
diff --git a/src/json/test_json.c b/src/json/test_json.c
index 4485a37a4..7e9025b5b 100644
--- a/src/json/test_json.c
+++ b/src/json/test_json.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2015, 2016 GNUnet e.V. 3 (C) 2015, 2016, 2021 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -34,33 +34,45 @@
34 * @return 0 on success 34 * @return 0 on success
35 */ 35 */
36static int 36static int
37test_abs_time () 37test_timestamp (void)
38{ 38{
39 json_t *j; 39 json_t *j;
40 struct GNUNET_TIME_Absolute a1; 40 struct GNUNET_TIME_Absolute a1;
41 struct GNUNET_TIME_Absolute a2; 41 struct GNUNET_TIME_Timestamp t1;
42 struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_absolute_time ( 42 struct GNUNET_TIME_Timestamp t2;
43 NULL, 43 struct GNUNET_JSON_Specification s1[] = {
44 &a2), 44 GNUNET_JSON_spec_timestamp (NULL,
45 GNUNET_JSON_spec_end () }; 45 &t2),
46 struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_absolute_time ( 46 GNUNET_JSON_spec_end ()
47 NULL, 47 };
48 &a2), 48 struct GNUNET_JSON_Specification s2[] = {
49 GNUNET_JSON_spec_end () }; 49 GNUNET_JSON_spec_timestamp (NULL,
50 &t2),
51 GNUNET_JSON_spec_end ()
52 };
50 53
51 a1 = GNUNET_TIME_absolute_get (); 54 a1 = GNUNET_TIME_absolute_get ();
52 GNUNET_TIME_round_abs (&a1); 55 GNUNET_TIME_absolute_to_timestamp (a1,
53 j = GNUNET_JSON_from_time_abs (a1); 56 &t1);
57 j = GNUNET_JSON_from_timestamp (t1);
54 GNUNET_assert (NULL != j); 58 GNUNET_assert (NULL != j);
55 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); 59 GNUNET_assert (GNUNET_OK ==
56 GNUNET_assert (a1.abs_value_us == a2.abs_value_us); 60 GNUNET_JSON_parse (j,
61 s1,
62 NULL,
63 NULL));
64 GNUNET_assert (GNUNET_TIME_timestamp_cmp (t1, ==, t2));
57 json_decref (j); 65 json_decref (j);
58 66
59 a1 = GNUNET_TIME_UNIT_FOREVER_ABS; 67 a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
60 j = GNUNET_JSON_from_time_abs (a1); 68 j = GNUNET_JSON_from_timestamp (t1);
61 GNUNET_assert (NULL != j); 69 GNUNET_assert (NULL != j);
62 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); 70 GNUNET_assert (GNUNET_OK ==
63 GNUNET_assert (a1.abs_value_us == a2.abs_value_us); 71 GNUNET_JSON_parse (j,
72 s2,
73 NULL,
74 NULL));
75 GNUNET_assert (GNUNET_TIME_timestamp_cmp (t1, ==, t2));
64 json_decref (j); 76 json_decref (j);
65 return 0; 77 return 0;
66} 78}
@@ -72,19 +84,21 @@ test_abs_time ()
72 * @return 0 on success 84 * @return 0 on success
73 */ 85 */
74static int 86static int
75test_rel_time () 87test_rel_time (void)
76{ 88{
77 json_t *j; 89 json_t *j;
78 struct GNUNET_TIME_Relative r1; 90 struct GNUNET_TIME_Relative r1;
79 struct GNUNET_TIME_Relative r2; 91 struct GNUNET_TIME_Relative r2;
80 struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_relative_time ( 92 struct GNUNET_JSON_Specification s1[] = {
81 NULL, 93 GNUNET_JSON_spec_relative_time (NULL,
82 &r2), 94 &r2),
83 GNUNET_JSON_spec_end () }; 95 GNUNET_JSON_spec_end ()
84 struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_relative_time ( 96 };
85 NULL, 97 struct GNUNET_JSON_Specification s2[] = {
86 &r2), 98 GNUNET_JSON_spec_relative_time (NULL,
87 GNUNET_JSON_spec_end () }; 99 &r2),
100 GNUNET_JSON_spec_end ()
101 };
88 102
89 r1 = GNUNET_TIME_UNIT_SECONDS; 103 r1 = GNUNET_TIME_UNIT_SECONDS;
90 j = GNUNET_JSON_from_time_rel (r1); 104 j = GNUNET_JSON_from_time_rel (r1);
@@ -211,7 +225,7 @@ int
211main (int argc, const char *const argv[]) 225main (int argc, const char *const argv[])
212{ 226{
213 GNUNET_log_setup ("test-json", "WARNING", NULL); 227 GNUNET_log_setup ("test-json", "WARNING", NULL);
214 if (0 != test_abs_time ()) 228 if (0 != test_timestamp ())
215 return 1; 229 return 1;
216 if (0 != test_rel_time ()) 230 if (0 != test_rel_time ())
217 return 1; 231 return 1;