• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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/util/sql_argument.h"
18 
19 #include <optional>
20 #include <string>
21 #include <vector>
22 
23 #include "perfetto/base/status.h"
24 #include "perfetto/trace_processor/basic_types.h"
25 #include "test/gtest_and_gmock.h"
26 
27 namespace perfetto::trace_processor::sql_argument {
28 namespace {
29 
ParseArgsSuccessfully(const std::string & args,std::vector<ArgumentDefinition> expected)30 void ParseArgsSuccessfully(const std::string& args,
31                            std::vector<ArgumentDefinition> expected) {
32   std::vector<ArgumentDefinition> actual;
33   base::Status status = ParseArgumentDefinitions(args, actual);
34   ASSERT_TRUE(status.ok()) << status.c_message();
35   ASSERT_EQ(expected, actual);
36 }
37 
ParseArgsWithFailure(const std::string & args)38 void ParseArgsWithFailure(const std::string& args) {
39   std::vector<ArgumentDefinition> actual;
40   ASSERT_FALSE(ParseArgumentDefinitions(args, actual).ok());
41 }
42 
TEST(SqlArgumentTest,IsValidName)43 TEST(SqlArgumentTest, IsValidName) {
44   ASSERT_TRUE(IsValidName("foo"));
45   ASSERT_TRUE(IsValidName("bar"));
46   ASSERT_TRUE(IsValidName("foo_bar"));
47   ASSERT_TRUE(IsValidName("foo1234"));
48   ASSERT_TRUE(IsValidName("1234Foo"));
49   ASSERT_FALSE(IsValidName("foo-bar"));
50   ASSERT_FALSE(IsValidName("foo#123"));
51 }
52 
TEST(SqlArgumentTest,ParseType)53 TEST(SqlArgumentTest, ParseType) {
54   ASSERT_EQ(ParseType("PROTO"), Type::kProto);
55   ASSERT_EQ(ParseType("BOOL"), Type::kBool);
56   ASSERT_EQ(ParseType("UNKNOWN"), std::nullopt);
57   ASSERT_EQ(ParseType("UINT"), Type::kUint);
58 }
59 
TEST(SqlArgumentTest,TypeToFriendlyString)60 TEST(SqlArgumentTest, TypeToFriendlyString) {
61   ASSERT_STREQ(TypeToHumanFriendlyString(Type::kProto), "PROTO");
62   ASSERT_STREQ(TypeToHumanFriendlyString(Type::kBool), "BOOL");
63   ASSERT_STREQ(TypeToHumanFriendlyString(Type::kUint), "UINT");
64 }
65 
TEST(SqlArgumentTest,TypeToSqlValueType)66 TEST(SqlArgumentTest, TypeToSqlValueType) {
67   ASSERT_EQ(TypeToSqlValueType(Type::kProto), SqlValue::Type::kBytes);
68   ASSERT_EQ(TypeToSqlValueType(Type::kBool), SqlValue::Type::kLong);
69   ASSERT_EQ(TypeToSqlValueType(Type::kUint), SqlValue::Type::kLong);
70 }
71 
TEST(SqlArgumentTest,ParseArguments)72 TEST(SqlArgumentTest, ParseArguments) {
73   ParseArgsSuccessfully("", {});
74   ParseArgsSuccessfully("foo UINT", {ArgumentDefinition("$foo", Type::kUint)});
75   ParseArgsSuccessfully("foo UINT, bar LONG, baz PROTO",
76                         {ArgumentDefinition("$foo", Type::kUint),
77                          ArgumentDefinition("$bar", Type::kLong),
78                          ArgumentDefinition("$baz", Type::kProto)});
79   ParseArgsSuccessfully("\nfoo UINT,\n bar LONG, baz PROTO\n",
80                         {ArgumentDefinition("$foo", Type::kUint),
81                          ArgumentDefinition("$bar", Type::kLong),
82                          ArgumentDefinition("$baz", Type::kProto)});
83   ParseArgsSuccessfully("foo123 UINT",
84                         {ArgumentDefinition("$foo123", Type::kUint)});
85 
86   ParseArgsWithFailure("foo");
87   ParseArgsWithFailure("foo bar UINT, baz UINT");
88   ParseArgsWithFailure("foo UINT32");
89   ParseArgsWithFailure("foo#bar UINT");
90   ParseArgsWithFailure("foo-bar UINT");
91 }
92 
93 }  // namespace
94 }  // namespace perfetto::trace_processor::sql_argument
95