1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "rtc_base/stringutils.h"
12 #include "rtc_base/gunit.h"
13 
14 namespace rtc {
15 
16 // Tests for string_match().
17 
TEST(string_matchTest,Matches)18 TEST(string_matchTest, Matches) {
19   EXPECT_TRUE( string_match("A.B.C.D", "a.b.c.d"));
20   EXPECT_TRUE( string_match("www.TEST.GOOGLE.COM", "www.*.com"));
21   EXPECT_TRUE( string_match("127.0.0.1",  "12*.0.*1"));
22   EXPECT_TRUE( string_match("127.1.0.21", "12*.0.*1"));
23   EXPECT_FALSE(string_match("127.0.0.0",  "12*.0.*1"));
24   EXPECT_FALSE(string_match("127.0.0.0",  "12*.0.*1"));
25   EXPECT_FALSE(string_match("127.1.1.21", "12*.0.*1"));
26 }
27 
28 // It's not clear if we will ever use wchar_t strings on unix.  In theory,
29 // all strings should be Utf8 all the time, except when interfacing with Win32
30 // APIs that require Utf16.
31 
32 #if defined(WEBRTC_WIN)
33 
34 // Tests for ascii_string_compare().
35 
36 // Tests null input.
TEST(ascii_string_compareTest,NullInput)37 TEST(ascii_string_compareTest, NullInput) {
38   // The following results in an access violation in
39   // ascii_string_compare.  Is this a bug or by design?  stringutils.h
40   // should document the expected behavior in this case.
41 
42   // EXPECT_EQ(0, ascii_string_compare(nullptr, nullptr, 1, identity));
43 }
44 
45 // Tests comparing two strings of different lengths.
TEST(ascii_string_compareTest,DifferentLengths)46 TEST(ascii_string_compareTest, DifferentLengths) {
47   EXPECT_EQ(-1, ascii_string_compare(L"Test", "Test1", 5, identity));
48 }
49 
50 // Tests the case where the buffer size is smaller than the string
51 // lengths.
TEST(ascii_string_compareTest,SmallBuffer)52 TEST(ascii_string_compareTest, SmallBuffer) {
53   EXPECT_EQ(0, ascii_string_compare(L"Test", "Test1", 3, identity));
54 }
55 
56 // Tests the case where the buffer is not full.
TEST(ascii_string_compareTest,LargeBuffer)57 TEST(ascii_string_compareTest, LargeBuffer) {
58   EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 10, identity));
59 }
60 
61 // Tests comparing two eqaul strings.
TEST(ascii_string_compareTest,Equal)62 TEST(ascii_string_compareTest, Equal) {
63   EXPECT_EQ(0, ascii_string_compare(L"Test", "Test", 5, identity));
64   EXPECT_EQ(0, ascii_string_compare(L"TeSt", "tEsT", 5, tolowercase));
65 }
66 
67 // Tests comparing a smller string to a larger one.
TEST(ascii_string_compareTest,LessThan)68 TEST(ascii_string_compareTest, LessThan) {
69   EXPECT_EQ(-1, ascii_string_compare(L"abc", "abd", 4, identity));
70   EXPECT_EQ(-1, ascii_string_compare(L"ABC", "abD", 5, tolowercase));
71 }
72 
73 // Tests comparing a larger string to a smaller one.
TEST(ascii_string_compareTest,GreaterThan)74 TEST(ascii_string_compareTest, GreaterThan) {
75   EXPECT_EQ(1, ascii_string_compare(L"xyz", "xy", 5, identity));
76   EXPECT_EQ(1, ascii_string_compare(L"abc", "ABB", 5, tolowercase));
77 }
78 #endif  // WEBRTC_WIN
79 
TEST(string_trim_Test,Trimming)80 TEST(string_trim_Test, Trimming) {
81   EXPECT_EQ("temp", string_trim("\n\r\t temp \n\r\t"));
82   EXPECT_EQ("temp\n\r\t temp", string_trim(" temp\n\r\t temp "));
83   EXPECT_EQ("temp temp", string_trim("temp temp"));
84   EXPECT_EQ("", string_trim(" \r\n\t"));
85   EXPECT_EQ("", string_trim(""));
86 }
87 
TEST(string_startsTest,StartsWith)88 TEST(string_startsTest, StartsWith) {
89   EXPECT_TRUE(starts_with("foobar", "foo"));
90   EXPECT_TRUE(starts_with("foobar", "foobar"));
91   EXPECT_TRUE(starts_with("foobar", ""));
92   EXPECT_TRUE(starts_with("", ""));
93   EXPECT_FALSE(starts_with("foobar", "bar"));
94   EXPECT_FALSE(starts_with("foobar", "foobarbaz"));
95   EXPECT_FALSE(starts_with("", "f"));
96 }
97 
TEST(string_endsTest,EndsWith)98 TEST(string_endsTest, EndsWith) {
99   EXPECT_TRUE(ends_with("foobar", "bar"));
100   EXPECT_TRUE(ends_with("foobar", "foobar"));
101   EXPECT_TRUE(ends_with("foobar", ""));
102   EXPECT_TRUE(ends_with("", ""));
103   EXPECT_FALSE(ends_with("foobar", "foo"));
104   EXPECT_FALSE(ends_with("foobar", "foobarbaz"));
105   EXPECT_FALSE(ends_with("", "f"));
106 }
107 
108 } // namespace rtc
109