Update the testing suite for use with non-MACing clients.

This includes a separate non-MAC datafile, testing_input_nomac.dat, which can
be used with safebrowsing_test_server.py.

The test server can also be safely stopped and restarted within the same
Python process.



git-svn-id: http://google-safe-browsing.googlecode.com/svn/trunk@109 2195c2fd-d934-0410-ae3f-cd772a4098b8
diff --git a/testing/external_test_pb2.py b/testing/external_test_pb2.py
index 3b86228..6e8131b 100755
--- a/testing/external_test_pb2.py
+++ b/testing/external_test_pb2.py
@@ -161,7 +161,7 @@
       options=None),
     descriptor.FieldDescriptor(
       name='ClientKey', full_name='TestData.ClientKey', index=1,
-      number=2, type=12, cpp_type=9, label=2,
+      number=2, type=12, cpp_type=9, label=1,
       default_value="",
       message_type=None, enum_type=None, containing_type=None,
       is_extension=False, extension_scope=None,
diff --git a/testing/safebrowsing_test_server.py b/testing/safebrowsing_test_server.py
index d730f2a..e38786d 100755
--- a/testing/safebrowsing_test_server.py
+++ b/testing/safebrowsing_test_server.py
@@ -63,7 +63,7 @@
 # Dict of step -> Dict of hash_prefix ->
 # (full length hashes responses, num times requested)
 hash_data_by_step = {}
-client_key = ''
+client_key = None
 enforce_caching = False
 validate_database = True
 server_port = -1
@@ -89,14 +89,20 @@
   """ Load data from filename to be used by the testing server.
   """
   global response_data_by_step
+  global hash_data_by_step
   global client_key
   data_file = open(filename, 'rb')
   str_data = data_file.read()
   test_data = external_test_pb2.TestData()
   test_data.ParseFromString(str_data)
   print "Data Loaded"
-  client_key = test_data.ClientKey
+  if test_data.HasField('ClientKey'):
+    client_key = test_data.ClientKey
+  else:
+    client_key = None
   step = 0
+  response_data_by_step = {}
+  hash_data_by_step = {}
   for step_data in test_data.Steps:
     step += 1
     step_list = []
@@ -142,12 +148,15 @@
                (binascii.hexlify(prefix),
                 num_requests,
                 expression))
+        # This information is slightly redundant with what will be printed below
+        # but it is occasionally worth seeing.
+        print "Response %s" % response
         cur_index = 0
         while cur_index < len(response):
           end_header_index = response.find('\n', cur_index + 1)
           header = response[cur_index:end_header_index]
           (listname, chunk_num, hashdatalen) = header.split(":")
-          print "   List '%s' in add chunk num %d" % (listname, chunk_num)
+          print "   List '%s' in add chunk num %s" % (listname, chunk_num)
           cur_index = end_header_index + hashdatalen + 1
 
         complete = False
@@ -173,8 +182,11 @@
 
   def MACResponse(self, response, is_downloads_request):
     """ Returns the response wrapped with a MAC. Formatting will change
-    if this is a downloads_request or hashserver_request.
+    if this is a downloads_request or hashserver_request.  If no client_key
+    is set, returns the response as-is.
     """
+    if client_key is None:
+      return response
     unescaped_mac = hmac.new(client_key, response, sha).digest()
     return "%s%s\n%s" % (is_downloads_request and "m:" or "",
                        base64.urlsafe_b64encode(unescaped_mac),
@@ -317,7 +329,8 @@
                                'localhost:%d' % server_port,
                                server_response)
       # Remove the current MAC, because it's going to be wrong now.
-      server_response = server_response[server_response.find('\n')+1:]
+      if server_response.startswith('m:'):
+        server_response = server_response[server_response.find('\n')+1:]
       # Add a new correct MAC.
       server_response = self.MACResponse(server_response, True)
 
@@ -390,3 +403,9 @@
     tm.start()
 
   server.serve_forever()
+  try:
+    server.serve_forever()
+  except KeyboardInterrupt:
+    pass
+  server.server_close()
+  print "Server stopped."
diff --git a/testing/testing_input_nomac.dat b/testing/testing_input_nomac.dat
new file mode 100644
index 0000000..54cf77f
--- /dev/null
+++ b/testing/testing_input_nomac.dat
Binary files differ