From 196bd583b6c4959c60d3f73cdcefc9edda6a38ae Mon Sep 17 00:00:00 2001
From: david-sarah <david-sarah@jacaranda.org>
Date: Tue, 19 Jun 2012 02:05:04 +0000
Subject: [PATCH] Add assertions to make sure that
 set_default_encoding_parameters is always called, rather than using hardcoded
 3/7/10 defaults. Also update affected tests. Note that this by itself cannot
 fix the bug mentioned in ticket:1212#comment:41, but it might make it easier
 to reproduce. refs #1212

---
 src/allmydata/immutable/upload.py | 10 ++++++----
 src/allmydata/test/test_encode.py |  6 ++----
 src/allmydata/test/test_upload.py | 15 ++++++++++++---
 3 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/allmydata/immutable/upload.py b/src/allmydata/immutable/upload.py
index ba2c53b9..c6324046 100644
--- a/src/allmydata/immutable/upload.py
+++ b/src/allmydata/immutable/upload.py
@@ -16,7 +16,7 @@ from allmydata.util import base32, dictutil, idlib, log, mathutil
 from allmydata.util.happinessutil import servers_of_happiness, \
                                          shares_by_server, merge_servers, \
                                          failure_message
-from allmydata.util.assertutil import precondition
+from allmydata.util.assertutil import precondition, _assert
 from allmydata.util.rrefutil import add_version_to_remote_reference
 from allmydata.interfaces import IUploadable, IUploader, IUploadResults, \
      IEncryptedUploadable, RIEncryptedUploadable, IUploadStatus, \
@@ -624,6 +624,8 @@ class EncryptAnUploadable:
     CHUNKSIZE = 50*1024
 
     def __init__(self, original, log_parent=None):
+        precondition(original.default_params_set,
+                     "set_default_encoding_parameters not called on %r before wrapping with EncryptAnUploadable" % (original,))
         self.original = IUploadable(original)
         self._log_number = log_parent
         self._encryptor = None
@@ -1340,9 +1342,7 @@ class AssistedUploader:
 class BaseUploadable:
     # this is overridden by max_segment_size
     default_max_segment_size = DEFAULT_MAX_SEGMENT_SIZE
-    default_encoding_param_k = 3 # overridden by encoding_parameters
-    default_encoding_param_happy = 7
-    default_encoding_param_n = 10
+    default_params_set = False
 
     max_segment_size = None
     encoding_param_k = None
@@ -1368,8 +1368,10 @@ class BaseUploadable:
             self.default_encoding_param_n = default_params["n"]
         if "max_segment_size" in default_params:
             self.default_max_segment_size = default_params["max_segment_size"]
+        self.default_params_set = True
 
     def get_all_encoding_parameters(self):
+        _assert(self.default_params_set, "set_default_encoding_parameters not called on %r" % (self,))
         if self._all_encoding_parameters:
             return defer.succeed(self._all_encoding_parameters)
 
diff --git a/src/allmydata/test/test_encode.py b/src/allmydata/test/test_encode.py
index 91c2ece9..c908e974 100644
--- a/src/allmydata/test/test_encode.py
+++ b/src/allmydata/test/test_encode.py
@@ -268,10 +268,8 @@ class Encode(unittest.TestCase):
         # force use of multiple segments
         e = encode.Encoder()
         u = upload.Data(data, convergence="some convergence string")
-        u.max_segment_size = max_segment_size
-        u.encoding_param_k = 25
-        u.encoding_param_happy = 75
-        u.encoding_param_n = 100
+        u.set_default_encoding_parameters({'max_segment_size': max_segment_size,
+                                           'k': 25, 'happy': 75, 'n': 100})
         eu = upload.EncryptAnUploadable(u)
         d = e.set_encrypted_uploadable(eu)
 
diff --git a/src/allmydata/test/test_upload.py b/src/allmydata/test/test_upload.py
index 49df769f..20524ef0 100644
--- a/src/allmydata/test/test_upload.py
+++ b/src/allmydata/test/test_upload.py
@@ -20,6 +20,7 @@ from allmydata.util.happinessutil import servers_of_happiness, \
                                          shares_by_server, merge_servers
 from allmydata.storage_client import StorageFarmBroker
 from allmydata.storage.server import storage_index_to_dir
+from allmydata.client import Client
 
 MiB = 1024*1024
 
@@ -635,42 +636,52 @@ class ServerSelection(unittest.TestCase):
 class StorageIndex(unittest.TestCase):
     def test_params_must_matter(self):
         DATA = "I am some data"
+        PARAMS = Client.DEFAULT_ENCODING_PARAMETERS
+
         u = upload.Data(DATA, convergence="")
+        u.set_default_encoding_parameters(PARAMS)
         eu = upload.EncryptAnUploadable(u)
         d1 = eu.get_storage_index()
 
         # CHK means the same data should encrypt the same way
         u = upload.Data(DATA, convergence="")
+        u.set_default_encoding_parameters(PARAMS)
         eu = upload.EncryptAnUploadable(u)
         d1a = eu.get_storage_index()
 
         # but if we use a different convergence string it should be different
         u = upload.Data(DATA, convergence="wheee!")
+        u.set_default_encoding_parameters(PARAMS)
         eu = upload.EncryptAnUploadable(u)
         d1salt1 = eu.get_storage_index()
 
         # and if we add yet a different convergence it should be different again
         u = upload.Data(DATA, convergence="NOT wheee!")
+        u.set_default_encoding_parameters(PARAMS)
         eu = upload.EncryptAnUploadable(u)
         d1salt2 = eu.get_storage_index()
 
         # and if we use the first string again it should be the same as last time
         u = upload.Data(DATA, convergence="wheee!")
+        u.set_default_encoding_parameters(PARAMS)
         eu = upload.EncryptAnUploadable(u)
         d1salt1a = eu.get_storage_index()
 
         # and if we change the encoding parameters, it should be different (from the same convergence string with different encoding parameters)
         u = upload.Data(DATA, convergence="")
+        u.set_default_encoding_parameters(PARAMS)
         u.encoding_param_k = u.default_encoding_param_k + 1
         eu = upload.EncryptAnUploadable(u)
         d2 = eu.get_storage_index()
 
         # and if we use a random key, it should be different than the CHK
         u = upload.Data(DATA, convergence=None)
+        u.set_default_encoding_parameters(PARAMS)
         eu = upload.EncryptAnUploadable(u)
         d3 = eu.get_storage_index()
         # and different from another instance
         u = upload.Data(DATA, convergence=None)
+        u.set_default_encoding_parameters(PARAMS)
         eu = upload.EncryptAnUploadable(u)
         d4 = eu.get_storage_index()
 
@@ -767,9 +778,7 @@ class EncodingParameters(GridTestMixin, unittest.TestCase, SetDEPMixin,
         broker = self.g.clients[0].storage_broker
         sh     = self.g.clients[0]._secret_holder
         data = upload.Data("data" * 10000, convergence="")
-        data.encoding_param_k = 3
-        data.encoding_param_happy = 4
-        data.encoding_param_n = 10
+        data.set_default_encoding_parameters({'k': 3, 'happy': 4, 'n': 10})
         uploadable = upload.EncryptAnUploadable(data)
         encoder = encode.Encoder()
         encoder.set_encrypted_uploadable(uploadable)
-- 
2.45.2