1+ #!/usr/bin/env python
2+ """Tests for GCS output plugin."""
3+
4+ import json
5+ import requests
6+
7+ from absl import app
8+
9+ from unittest import mock
10+ from unittest .mock import MagicMock
11+
12+ from grr_response_core .lib import rdfvalue
13+ from grr_response_core .lib .rdfvalues import client as rdf_client
14+ from grr_response_core .lib .rdfvalues import client_fs as rdf_client_fs
15+ from grr_response_core .lib .rdfvalues import file_finder as rdf_file_finder
16+ from grr_response_core .lib .rdfvalues import paths as rdf_paths
17+
18+ from grr_response_server .output_plugins import gcs_plugin
19+ from grr_response_server .rdfvalues import flow_objects as rdf_flow_objects
20+
21+ from grr .test_lib import flow_test_lib
22+ from grr .test_lib import test_lib
23+
24+
25+ class GcsOutputPluginTest (flow_test_lib .FlowTestsBaseclass ):
26+ """Tests GCS output plugin."""
27+
28+ def setUp (self ):
29+ super ().setUp ()
30+
31+ self .client_id = self .SetupClient (0 )
32+ self .flow_id = '12345678'
33+ self .source_id = (
34+ rdf_client .ClientURN (self .client_id )
35+ .Add ('Results' )
36+ .RelativeName ('aff4:/' )
37+ )
38+
39+
40+ def _CallPlugin (self , plugin_args = None , responses = None ):
41+ plugin_cls = gcs_plugin .GcsOutputPlugin
42+ plugin , plugin_state = plugin_cls .CreatePluginAndDefaultState (
43+ source_urn = self .source_id , args = plugin_args
44+ )
45+
46+ plugin_cls .UploadBlobFromStream = MagicMock ()
47+
48+ messages = []
49+ for response in responses :
50+ messages .append (
51+ rdf_flow_objects .FlowResult (
52+ client_id = self .client_id , flow_id = self .flow_id , payload = response
53+ )
54+ )
55+
56+ with test_lib .FakeTime (1445995873 ):
57+ plugin .ProcessResponses (plugin_state , messages )
58+ plugin .Flush (plugin_state )
59+ plugin .UpdateState (plugin_state )
60+
61+ return plugin .uploaded_files
62+
63+
64+ def testClientFileFinderResponseUploaded (self ):
65+ rdf_payload = rdf_file_finder .FileFinderResult ()
66+ rdf_payload .stat_entry .st_size = 1234
67+ rdf_payload .stat_entry .pathspec .path = ("/var/log/test.log" )
68+ rdf_payload .stat_entry .pathspec .pathtype = "OS"
69+ rdf_payload .transferred_file = rdf_client_fs .BlobImageDescriptor (chunks = [])
70+
71+ with test_lib .FakeTime (rdfvalue .RDFDatetime .FromSecondsSinceEpoch (15 )):
72+ uploaded_files = self ._CallPlugin (
73+ plugin_args = gcs_plugin .GcsOutputPluginArgs (
74+ project_id = "test-project-id" ,
75+ gcs_bucket = "text-gcs-bucket"
76+ ),
77+ responses = [rdf_payload ],
78+ )
79+
80+ self .assertEqual (uploaded_files , 1 )
81+
82+
83+ def testClientFileFinderResponseNoProjectId (self ):
84+ rdf_payload = rdf_file_finder .FileFinderResult ()
85+ rdf_payload .stat_entry .st_size = 1234
86+ rdf_payload .stat_entry .pathspec .path = ("/var/log/test.log" )
87+ rdf_payload .stat_entry .pathspec .pathtype = "OS"
88+ rdf_payload .transferred_file = rdf_client_fs .BlobImageDescriptor (chunks = [])
89+
90+ with test_lib .FakeTime (rdfvalue .RDFDatetime .FromSecondsSinceEpoch (15 )):
91+ uploaded_files = self ._CallPlugin (
92+ plugin_args = gcs_plugin .GcsOutputPluginArgs (
93+ project_id = "" ,
94+ gcs_bucket = "text-gcs-bucket"
95+ ),
96+ responses = [rdf_payload ],
97+ )
98+
99+ self .assertEqual (uploaded_files , 0 )
100+
101+
102+ def testClientFileFinderResponseNoGcsBucket (self ):
103+ rdf_payload = rdf_file_finder .FileFinderResult ()
104+ rdf_payload .stat_entry .st_size = 1234
105+ rdf_payload .stat_entry .pathspec .path = ("/var/log/test.log" )
106+ rdf_payload .stat_entry .pathspec .pathtype = "OS"
107+ rdf_payload .transferred_file = rdf_client_fs .BlobImageDescriptor (chunks = [])
108+
109+ with test_lib .FakeTime (rdfvalue .RDFDatetime .FromSecondsSinceEpoch (15 )):
110+ uploaded_files = self ._CallPlugin (
111+ plugin_args = gcs_plugin .GcsOutputPluginArgs (
112+ project_id = "test-project-id" ,
113+ gcs_bucket = ""
114+ ),
115+ responses = [rdf_payload ],
116+ )
117+
118+ self .assertEqual (uploaded_files , 0 )
119+
120+
121+ def testClientFileFinderResponseEmptyFile (self ):
122+ rdf_payload = rdf_file_finder .FileFinderResult ()
123+ rdf_payload .stat_entry .st_size = 0
124+ rdf_payload .stat_entry .pathspec .path = ("/var/log/test.log" )
125+ rdf_payload .stat_entry .pathspec .pathtype = "OS"
126+ rdf_payload .transferred_file = rdf_client_fs .BlobImageDescriptor (chunks = [])
127+
128+ with test_lib .FakeTime (rdfvalue .RDFDatetime .FromSecondsSinceEpoch (15 )):
129+ uploaded_files = self ._CallPlugin (
130+ plugin_args = gcs_plugin .GcsOutputPluginArgs (
131+ project_id = "test-project-id" ,
132+ gcs_bucket = "text-gcs-bucket"
133+ ),
134+ responses = [rdf_payload ],
135+ )
136+
137+ self .assertEqual (uploaded_files , 0 )
138+
139+
140+ def testClientFileFinderResponseNoTransferredFile (self ):
141+ rdf_payload = rdf_file_finder .FileFinderResult ()
142+ rdf_payload .stat_entry .st_size = 1234
143+ rdf_payload .stat_entry .pathspec .path = ("/var/log/test.log" )
144+ rdf_payload .stat_entry .pathspec .pathtype = "OS"
145+
146+ with test_lib .FakeTime (rdfvalue .RDFDatetime .FromSecondsSinceEpoch (15 )):
147+ uploaded_files = self ._CallPlugin (
148+ plugin_args = gcs_plugin .GcsOutputPluginArgs (
149+ project_id = "test-project-id" ,
150+ gcs_bucket = "text-gcs-bucket"
151+ ),
152+ responses = [rdf_payload ],
153+ )
154+
155+ self .assertEqual (uploaded_files , 0 )
156+
157+
158+ if __name__ == '__main__' :
159+ app .run (test_lib .main )
0 commit comments