1 /*
2  *  Copyright 2019 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 #include "system_wrappers/include/field_trial.h"
11 
12 #include "rtc_base/checks.h"
13 #include "test/gtest.h"
14 #include "test/testsupport/rtc_expect_death.h"
15 
16 namespace webrtc {
17 namespace field_trial {
18 #if GTEST_HAS_DEATH_TEST && RTC_DCHECK_IS_ON && !defined(WEBRTC_ANDROID) && \
19     !defined(WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT)
TEST(FieldTrialValidationTest,AcceptsValidInputs)20 TEST(FieldTrialValidationTest, AcceptsValidInputs) {
21   InitFieldTrialsFromString("");
22   InitFieldTrialsFromString("Audio/Enabled/");
23   InitFieldTrialsFromString("Audio/Enabled/Video/Disabled/");
24   EXPECT_TRUE(FieldTrialsStringIsValid(""));
25   EXPECT_TRUE(FieldTrialsStringIsValid("Audio/Enabled/"));
26   EXPECT_TRUE(FieldTrialsStringIsValid("Audio/Enabled/Video/Disabled/"));
27 
28   // Duplicate trials with the same value is fine
29   InitFieldTrialsFromString("Audio/Enabled/Audio/Enabled/");
30   InitFieldTrialsFromString("Audio/Enabled/B/C/Audio/Enabled/");
31   EXPECT_TRUE(FieldTrialsStringIsValid("Audio/Enabled/Audio/Enabled/"));
32   EXPECT_TRUE(FieldTrialsStringIsValid("Audio/Enabled/B/C/Audio/Enabled/"));
33 }
34 
TEST(FieldTrialValidationDeathTest,RejectsBadInputs)35 TEST(FieldTrialValidationDeathTest, RejectsBadInputs) {
36   // Bad delimiters
37   RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/EnabledVideo/Disabled/"),
38                    "Invalid field trials string:");
39   RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled//Video/Disabled/"),
40                    "Invalid field trials string:");
41   RTC_EXPECT_DEATH(InitFieldTrialsFromString("/Audio/Enabled/Video/Disabled/"),
42                    "Invalid field trials string:");
43   RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled/Video/Disabled"),
44                    "Invalid field trials string:");
45   RTC_EXPECT_DEATH(
46       InitFieldTrialsFromString("Audio/Enabled/Video/Disabled/garbage"),
47       "Invalid field trials string:");
48   EXPECT_FALSE(FieldTrialsStringIsValid("Audio/EnabledVideo/Disabled/"));
49   EXPECT_FALSE(FieldTrialsStringIsValid("Audio/Enabled//Video/Disabled/"));
50   EXPECT_FALSE(FieldTrialsStringIsValid("/Audio/Enabled/Video/Disabled/"));
51   EXPECT_FALSE(FieldTrialsStringIsValid("Audio/Enabled/Video/Disabled"));
52   EXPECT_FALSE(
53       FieldTrialsStringIsValid("Audio/Enabled/Video/Disabled/garbage"));
54 
55   // Empty trial or group
56   RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio//"),
57                    "Invalid field trials string:");
58   RTC_EXPECT_DEATH(InitFieldTrialsFromString("/Enabled/"),
59                    "Invalid field trials string:");
60   RTC_EXPECT_DEATH(InitFieldTrialsFromString("//"),
61                    "Invalid field trials string:");
62   RTC_EXPECT_DEATH(InitFieldTrialsFromString("//Enabled"),
63                    "Invalid field trials string:");
64   EXPECT_FALSE(FieldTrialsStringIsValid("Audio//"));
65   EXPECT_FALSE(FieldTrialsStringIsValid("/Enabled/"));
66   EXPECT_FALSE(FieldTrialsStringIsValid("//"));
67   EXPECT_FALSE(FieldTrialsStringIsValid("//Enabled"));
68 
69   // Duplicate trials with different values is not fine
70   RTC_EXPECT_DEATH(InitFieldTrialsFromString("Audio/Enabled/Audio/Disabled/"),
71                    "Invalid field trials string:");
72   RTC_EXPECT_DEATH(
73       InitFieldTrialsFromString("Audio/Enabled/B/C/Audio/Disabled/"),
74       "Invalid field trials string:");
75   EXPECT_FALSE(FieldTrialsStringIsValid("Audio/Enabled/Audio/Disabled/"));
76   EXPECT_FALSE(FieldTrialsStringIsValid("Audio/Enabled/B/C/Audio/Disabled/"));
77 }
78 
TEST(FieldTrialMergingTest,MergesValidInput)79 TEST(FieldTrialMergingTest, MergesValidInput) {
80   EXPECT_EQ(MergeFieldTrialsStrings("Video/Enabled/", "Audio/Enabled/"),
81             "Audio/Enabled/Video/Enabled/");
82   EXPECT_EQ(MergeFieldTrialsStrings("Audio/Disabled/Video/Enabled/",
83                                     "Audio/Enabled/"),
84             "Audio/Enabled/Video/Enabled/");
85   EXPECT_EQ(
86       MergeFieldTrialsStrings("Audio/Enabled/Video/Enabled/", "Audio/Enabled/"),
87       "Audio/Enabled/Video/Enabled/");
88   EXPECT_EQ(
89       MergeFieldTrialsStrings("Audio/Enabled/Audio/Enabled/", "Video/Enabled/"),
90       "Audio/Enabled/Video/Enabled/");
91 }
92 
TEST(FieldTrialMergingDeathTest,DchecksBadInput)93 TEST(FieldTrialMergingDeathTest, DchecksBadInput) {
94   RTC_EXPECT_DEATH(MergeFieldTrialsStrings("Audio/Enabled/", "garbage"),
95                    "Invalid field trials string:");
96 }
97 
TEST(FieldTrialMergingTest,HandlesEmptyInput)98 TEST(FieldTrialMergingTest, HandlesEmptyInput) {
99   EXPECT_EQ(MergeFieldTrialsStrings("", "Audio/Enabled/"), "Audio/Enabled/");
100   EXPECT_EQ(MergeFieldTrialsStrings("Audio/Enabled/", ""), "Audio/Enabled/");
101   EXPECT_EQ(MergeFieldTrialsStrings("", ""), "");
102 }
103 #endif  // GTEST_HAS_DEATH_TEST && RTC_DCHECK_IS_ON && !defined(WEBRTC_ANDROID)
104         // && !defined(WEBRTC_EXCLUDE_FIELD_TRIAL_DEFAULT)
105 
106 }  // namespace field_trial
107 }  // namespace webrtc
108