• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
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 "net/traffic_annotation/network_traffic_annotation.h"
6 
7 #include "dummy_classes.h"
8 
9 // This file provides samples for testing traffic_annotation_extractor clang
10 // tool.
11 namespace {
12 
13 net::NetworkTrafficAnnotationTag kTrafficAnnotation =
14     net::DefineNetworkTrafficAnnotation("id1", R"(
15         semantics {
16           sender: "sender1"
17           description: "desc1"
18           trigger: "trigger1"
19           data: "data1"
20           destination: GOOGLE_OWNED_SERVICE
21         }
22         policy {
23           cookies_allowed: NO
24           setting: "setting1"
25           chrome_policy {
26             SpellCheckServiceEnabled {
27                 SpellCheckServiceEnabled: false
28             }
29           }
30         }
31         comments: "comment1")");
32 }
33 
TestAnnotations()34 void TestAnnotations() {
35   net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation =
36       net::DefinePartialNetworkTrafficAnnotation("id2", "completing_id2", R"(
37         semantics {
38           sender: "sender2"
39           description: "desc2"
40           trigger: "trigger2"
41           data: "data2"
42           destination: WEBSITE
43         })");
44 
45   net::NetworkTrafficAnnotationTag completed_traffic_annotation =
46       net::CompleteNetworkTrafficAnnotation("id3", partial_traffic_annotation,
47                                             R"(
48         policy {
49           cookies_allowed: YES
50           cookie_store: "user"
51           setting: "setting3"
52           chrome_policy {
53             SpellCheckServiceEnabled {
54                 SpellCheckServiceEnabled: false
55             }
56           }
57         }
58         comments: "comment3")");
59 
60   net::NetworkTrafficAnnotationTag completed_branch_traffic_annotation =
61       net::BranchedCompleteNetworkTrafficAnnotation(
62           "id4", "branch4", partial_traffic_annotation, R"(
63         policy {
64           cookies_allowed: YES
65           cookie_store: "user"
66           setting: "setting4"
67           policy_exception_justification: "justification"
68         })");
69 }
70 
TestURLFetcherCreate()71 void TestURLFetcherCreate() {
72   net::URLFetcherDelegate* delegate = nullptr;
73   net::URLFetcher::Create(GURL(), net::URLFetcher::RequestType::TEST_VALUE,
74                           delegate);
75 
76   net::URLFetcher::Create(0, GURL(), net::URLFetcher::RequestType::TEST_VALUE,
77                           delegate);
78 
79   net::URLFetcher::Create(GURL(), net::URLFetcher::RequestType::TEST_VALUE,
80                           delegate, kTrafficAnnotation);
81 
82   net::URLFetcher::Create(0, GURL(), net::URLFetcher::RequestType::TEST_VALUE,
83                           delegate, NO_TRAFFIC_ANNOTATION_YET);
84 }
85 
TestCreateRequest()86 void TestCreateRequest() {
87   net::URLRequest::Delegate* delegate = nullptr;
88   net::URLRequestContext context;
89 
90   context.CreateRequest(GURL(), net::RequestPriority::TEST_VALUE, delegate);
91   context.CreateRequest(GURL(), net::RequestPriority::TEST_VALUE, delegate,
92                         kTrafficAnnotation);
93 }
94 
TestInitList()95 void TestInitList() {
96   net::NetworkTrafficAnnotationTag({-1});
97   net::MutableNetworkTrafficAnnotationTag({-2});
98   net::PartialNetworkTrafficAnnotationTag({-1});
99   net::MutablePartialNetworkTrafficAnnotationTag({-2});
100   int i = 0;
101   net::NetworkTrafficAnnotationTag({i});
102 }
103 
TestAssignment()104 void TestAssignment() {
105   net::MutableNetworkTrafficAnnotationTag tag1;
106   tag1.unique_id_hash_code = 1;
107 
108   net::MutablePartialNetworkTrafficAnnotationTag tag2;
109   tag2.unique_id_hash_code = 2;
110 
111   // Test if assignment to |unique_id_hash_code| of another structure is not
112   // caught.
113   struct something_else {
114     int unique_id_hash_code;
115   } x;
116 
117   x.unique_id_hash_code = 3;
118 }
119 
DummyFunction(net::NetworkTrafficAnnotationTag traffic_annotation)120 void DummyFunction(net::NetworkTrafficAnnotationTag traffic_annotation) {}
121 
TestMacroExpansion()122 void TestMacroExpansion() {
123   DummyFunction(NO_TRAFFIC_ANNOTATION_YET);
124 }