1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/json_trace_tokenizer.h"
18
19 #include <json/value.h>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23
24 namespace perfetto {
25 namespace trace_processor {
26 namespace {
27
TEST(JsonTraceTokenizerTest,Success)28 TEST(JsonTraceTokenizerTest, Success) {
29 const char* start = R"({ "foo": "bar" })";
30 const char* end = start + strlen(start);
31 const char* next = nullptr;
32 Json::Value value;
33 ReadDictRes result = ReadOneJsonDict(start, end, &value, &next);
34
35 ASSERT_EQ(result, kFoundDict);
36 ASSERT_EQ(next, end);
37 ASSERT_EQ(value["foo"].asString(), "bar");
38 }
39
TEST(JsonTraceTokenizerTest,QuotedBraces)40 TEST(JsonTraceTokenizerTest, QuotedBraces) {
41 const char* start = R"({ "foo": "}\"bar{\\" })";
42 const char* end = start + strlen(start);
43 const char* next = nullptr;
44 Json::Value value;
45 ReadDictRes result = ReadOneJsonDict(start, end, &value, &next);
46
47 ASSERT_EQ(result, kFoundDict);
48 ASSERT_EQ(next, end);
49 ASSERT_EQ(value["foo"].asString(), "}\"bar{\\");
50 }
51
TEST(JsonTraceTokenizerTest,TwoDicts)52 TEST(JsonTraceTokenizerTest, TwoDicts) {
53 const char* start = R"({"foo": 1}, {"bar": 2})";
54 const char* middle = start + strlen(R"({"foo": 1})");
55 const char* end = start + strlen(start);
56 const char* next = nullptr;
57 Json::Value value;
58
59 ASSERT_EQ(ReadOneJsonDict(start, end, &value, &next), kFoundDict);
60 ASSERT_EQ(next, middle);
61 ASSERT_EQ(value["foo"].asInt(), 1);
62
63 ASSERT_EQ(ReadOneJsonDict(next, end, &value, &next), kFoundDict);
64 ASSERT_EQ(next, end);
65 ASSERT_EQ(value["bar"].asInt(), 2);
66 }
67
TEST(JsonTraceTokenizerTest,NeedMoreData)68 TEST(JsonTraceTokenizerTest, NeedMoreData) {
69 const char* start = R"({"foo": 1)";
70 const char* end = start + strlen(start);
71 const char* next = nullptr;
72 Json::Value value;
73
74 ASSERT_EQ(ReadOneJsonDict(start, end, &value, &next), kNeedsMoreData);
75 ASSERT_EQ(next, nullptr);
76 }
77
TEST(JsonTraceTokenizerTest,FatalError)78 TEST(JsonTraceTokenizerTest, FatalError) {
79 const char* start = R"({helloworld})";
80 const char* end = start + strlen(start);
81 const char* next = nullptr;
82 Json::Value value;
83
84 ASSERT_EQ(ReadOneJsonDict(start, end, &value, &next), kFatalError);
85 ASSERT_EQ(next, nullptr);
86 }
87
88 } // namespace
89 } // namespace trace_processor
90 } // namespace perfetto
91