aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Fung <stevefung@google.com>2016-04-25 16:15:53 -0700
committerSteve Fung <stevefung@google.com>2016-04-26 00:04:42 +0000
commit495768f04fea119e1c7ae175217169006cc73da6 (patch)
treec8bea1291f1d22a91526efb1d84510d2ba43271c
parent6b0a22211dfbfe3eb7336314df5d3a3abfcf816a (diff)
downloadbdk-495768f04fea119e1c7ae175217169006cc73da6.tar.gz
Clean up hit_store to use stat permissions
Change the os.chmod() call to use the stat constants rather than 0o600, and add a unit test to make sure that the permissions are being set properly. Bug: 27718898 Test: `python test_runner.py` passes Change-Id: Ifea8b99e21f4914b8235733e575d948d5d183b86
-rw-r--r--cli/lib/metrics/hit_store.py3
-rw-r--r--cli/lib/metrics/hit_store_unittest.py28
2 files changed, 30 insertions, 1 deletions
diff --git a/cli/lib/metrics/hit_store.py b/cli/lib/metrics/hit_store.py
index e8e0e55..8e341b4 100644
--- a/cli/lib/metrics/hit_store.py
+++ b/cli/lib/metrics/hit_store.py
@@ -17,6 +17,7 @@
import os
import sqlite3
+import stat
from core import util
@@ -47,7 +48,7 @@ class Backup(object):
self._conn.execute(
'CREATE TABLE IF NOT EXISTS %s (unused)' % self._table)
if self._path != ':memory:':
- os.chmod(self._path, 0o600)
+ os.chmod(self._path, stat.S_IRUSR | stat.S_IWUSR)
self._initialized = True
def _add_fields(self, fields):
diff --git a/cli/lib/metrics/hit_store_unittest.py b/cli/lib/metrics/hit_store_unittest.py
index 5ab9a4f..a49dcd6 100644
--- a/cli/lib/metrics/hit_store_unittest.py
+++ b/cli/lib/metrics/hit_store_unittest.py
@@ -17,6 +17,10 @@
"""Unittests for hit_store.py."""
+import os
+import shutil
+import stat
+import tempfile
import unittest
from metrics import hit_store
@@ -43,3 +47,27 @@ class HitStoreTest(unittest.TestCase):
self.hit_store.save(field3)
retrieved = self.hit_store.retrieve_all()
self.assertEqual(retrieved, [field3])
+
+
+class HitStoreSetupTest(unittest.TestCase):
+ """Test that a hit store is set up correctly."""
+
+ def setUp(self):
+ """Set up the test."""
+ self.tmpdir = tempfile.mkdtemp()
+ self.db = os.path.join(self.tmpdir, 'test.db')
+ self.hit_store = hit_store.Backup(file_path=self.db)
+
+ # pylint: disable=protected-access
+ self.hit_store._setup()
+
+ def tearDown(self):
+ """Clean up after the test."""
+ shutil.rmtree(self.tmpdir)
+
+ def test_file_permissions(self):
+ """Test that the database file has the correct permissions."""
+ self.assertTrue(os.path.isfile(self.db))
+ stat_ = os.stat(self.db)
+ permissions = oct(stat_.st_mode & 0o777)
+ self.assertEqual(permissions, oct(stat.S_IRUSR | stat.S_IWUSR))