1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 // Tests for C++ wrappers.
9
10 #include <string.h>
11
12 #include <fstream>
13 #include <iostream>
14 #include <set>
15 #include <sstream>
16
17 #include "google/protobuf/timestamp.upb.h"
18 #include "google/protobuf/timestamp.upbdefs.h"
19 #include <gtest/gtest.h>
20 #include "upb/base/upcast.h"
21 #include "upb/json/decode.h"
22 #include "upb/json/encode.h"
23 #include "upb/reflection/def.h"
24 #include "upb/reflection/def.hpp"
25 #include "upb/test/test_cpp.upb.h"
26 #include "upb/test/test_cpp.upbdefs.h"
27
28 // Must be last.
29 #include "upb/port/def.inc"
30
TEST(Cpp,Iteration)31 TEST(Cpp, Iteration) {
32 upb::DefPool defpool;
33 upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr()));
34
35 // Test range-based for on both fields and oneofs (with the iterator adaptor).
36 int field_count = 0;
37 for (auto field : md.fields()) {
38 UPB_UNUSED(field);
39 field_count++;
40 }
41 EXPECT_EQ(field_count, md.field_count());
42
43 int oneof_count = 0;
44 for (auto oneof : md.oneofs()) {
45 UPB_UNUSED(oneof);
46 oneof_count++;
47 }
48 EXPECT_EQ(oneof_count, md.oneof_count());
49 }
50
TEST(Cpp,InlinedArena2)51 TEST(Cpp, InlinedArena2) {
52 upb::InlinedArena<64> arena;
53 upb_Arena_Malloc(arena.ptr(), sizeof(int));
54 }
55
TEST(Cpp,Default)56 TEST(Cpp, Default) {
57 upb::DefPool defpool;
58 upb::Arena arena;
59 upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr()));
60 upb_test_TestMessage* msg = upb_test_TestMessage_new(arena.ptr());
61 size_t size = upb_JsonEncode(UPB_UPCAST(msg), md.ptr(), nullptr, 0, nullptr,
62 0, nullptr);
63 EXPECT_EQ(2, size); // "{}"
64 }
65
TEST(Cpp,JsonNull)66 TEST(Cpp, JsonNull) {
67 upb::DefPool defpool;
68 upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(defpool.ptr()));
69 upb::FieldDefPtr i32_f = md.FindFieldByName("i32");
70 upb::FieldDefPtr str_f = md.FindFieldByName("str");
71 ASSERT_TRUE(i32_f);
72 ASSERT_TRUE(str_f);
73 EXPECT_EQ(5, i32_f.default_value().int32_val);
74 EXPECT_EQ(0, strcmp(str_f.default_value().str_val.data, "abc"));
75 EXPECT_EQ(3, str_f.default_value().str_val.size);
76 }
77
TEST(Cpp,TimestampEncoder)78 TEST(Cpp, TimestampEncoder) {
79 upb::DefPool defpool;
80 upb::Arena arena;
81 upb::MessageDefPtr md(google_protobuf_Timestamp_getmsgdef(defpool.ptr()));
82 google_protobuf_Timestamp* timestamp_upb =
83 google_protobuf_Timestamp_new(arena.ptr());
84 google_protobuf_Timestamp* timestamp_upb_decoded =
85 google_protobuf_Timestamp_new(arena.ptr());
86
87 int64_t timestamps[] = {
88 253402300799, // 9999-12-31T23:59:59Z
89 1641006000, // 2022-01-01T03:00:00Z
90 0, // 1970-01-01T00:00:00Z
91 -31525200, // 1969-01-01T03:00:00Z
92 -2208988800, // 1900-01-01T00:00:00Z
93 -62135596800, // 0000-01-01T00:00:00Z
94 };
95
96 for (int64_t timestamp : timestamps) {
97 google_protobuf_Timestamp_set_seconds(timestamp_upb, timestamp);
98
99 char json[128];
100 size_t size = upb_JsonEncode(UPB_UPCAST(timestamp_upb), md.ptr(), nullptr,
101 0, json, sizeof(json), nullptr);
102 bool result = upb_JsonDecode(json, size, UPB_UPCAST(timestamp_upb_decoded),
103 md.ptr(), nullptr, 0, arena.ptr(), nullptr);
104 const int64_t timestamp_decoded =
105 google_protobuf_Timestamp_seconds(timestamp_upb_decoded);
106
107 ASSERT_TRUE(result);
108 EXPECT_EQ(timestamp, timestamp_decoded);
109 }
110 }
111