xref: /aosp_15_r20/external/cronet/base/test/gtest_links.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/test/gtest_links.h"
6 
7 #include "base/check.h"
8 #include "base/strings/string_util.h"
9 #include "base/test/gtest_xml_unittest_result_printer.h"
10 
11 namespace base {
12 namespace {
13 
IsValidUrl(const std::string & url)14 bool IsValidUrl(const std::string& url) {
15   // https://www.ietf.org/rfc/rfc3986.txt
16   std::set<char> valid_characters{'-', '.', '_', '~', ':', '/', '?',  '#',
17                                   '[', ']', '@', '!', '$', '&', '\'', '(',
18                                   ')', '*', '+', ',', ';', '%', '='};
19   for (const char& c : url) {
20     if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
21           (c >= '0' && c <= '9') ||
22           valid_characters.find(c) != valid_characters.end()))
23       return false;
24   }
25   return true;
26 }
27 
IsValidName(const std::string & name)28 bool IsValidName(const std::string& name) {
29   for (const char& c : name) {
30     if (!(IsAsciiAlpha(c) || IsAsciiDigit(c) || c == '/' || c == '_'))
31       return false;
32   }
33   return true;
34 }
35 
36 }  // namespace
37 
AddLinkToTestResult(const std::string & name,const std::string & url)38 void AddLinkToTestResult(const std::string& name, const std::string& url) {
39   DCHECK(IsValidName(name)) << name << " is not a valid name";
40   DCHECK(IsValidUrl(url)) << url << " is not a valid link";
41   XmlUnitTestResultPrinter::Get()->AddLink(name, url);
42 }
43 
44 }  // namespace base
45