def test_oneline_file_is_detected(self): filename = self.write_tamper_evident(b"Am I OK?") with open(filename, "wb") as f: f.write(b"tamper") tef = TamperEvidentFile(filename) self.assertFalse(tef.validate())
def test_appending_is_detected(self): filename = self.write_tamper_evident(b"Am I OK?") with open(filename, "ab") as f: f.write(b"tamper\n") tef = TamperEvidentFile(filename) self.assertFalse(tef.validate())
def write_main(argv): """ write FILENAME Write a local copy of FILENAME using FILENAME_tweaks for local tweaks. """ if len(argv) != 1: print("Please provide the name of a file to write.") return 1 filename = argv[0] resource_name = "files/" + filename tweaks_name = amend_filename(filename, "_tweaks") if not pkg_resources.resource_exists("edx_lint", resource_name): print("Don't have file %r to write." % filename) return 2 if os.path.exists(filename): print("Checking existing copy of %s" % filename) tef = TamperEvidentFile(filename) if not tef.validate(): bak_name = amend_filename(filename, "_backup") print( "Your copy of %s seems to have been edited, renaming it to %s" % (filename, bak_name)) if os.path.exists(bak_name): print("A previous %s exists, deleting it" % bak_name) os.remove(bak_name) os.rename(filename, bak_name) print("Reading edx_lint/files/%s" % filename) cfg = configparser.RawConfigParser() # pkg_resources always reads binary data (in both python2 and python3). # ConfigParser.read_string only exists in python3, so we have to wrap the string # from pkg_resources in a cStringIO so that we can pass it into ConfigParser.readfp. resource_string = pkg_resources.resource_string( "edx_lint", resource_name).decode("ascii") cfg.readfp(cStringIO(resource_string), resource_name) if os.path.exists(tweaks_name): print("Applying local tweaks from %s" % tweaks_name) cfg_tweaks = configparser.RawConfigParser() cfg_tweaks.read([tweaks_name]) merge_configs(cfg, cfg_tweaks) print("Writing %s" % filename) output_text = cStringIO() output_text.write( WARNING_HEADER.format(filename=filename, tweaks_name=tweaks_name)) cfg.write(output_text) out_tef = TamperEvidentFile(filename) out_tef.write(str(output_text.getvalue())) return 0
def test_editing_is_detected(self): filename = self.write_tamper_evident(b"Line 1\nLine 2\nLine 3\n") with open(filename, "rb") as f: text = f.read() with open(filename, "wb") as f: f.write(b"X") f.write(text[1:]) tef = TamperEvidentFile(filename) self.assertFalse(tef.validate())
def write_main(argv): """ write FILENAME Write a local copy of FILENAME using FILENAME_tweaks for local tweaks. """ if len(argv) != 1: print("Please provide the name of a file to write.") return 1 filename = argv[0] resource_name = "files/" + filename tweaks_name = amend_filename(filename, "_tweaks") if not pkg_resources.resource_exists("edx_lint", resource_name): print("Don't have file %r to write." % filename) return 2 if os.path.exists(filename): print("Checking existing copy of %s" % filename) tef = TamperEvidentFile(filename) if not tef.validate(): bak_name = amend_filename(filename, "_backup") print("Your copy of %s seems to have been edited, renaming it to %s" % (filename, bak_name)) if os.path.exists(bak_name): print("A previous %s exists, deleting it" % bak_name) os.remove(bak_name) os.rename(filename, bak_name) print("Reading edx_lint/files/%s" % filename) cfg = configparser.RawConfigParser() resource_string = pkg_resources.resource_string("edx_lint", resource_name).decode("utf8") # pkg_resources always reads binary data (in both python2 and python3). # ConfigParser.read_string only exists in python3, so we have to wrap the string # from pkg_resources in a cStringIO so that we can pass it into ConfigParser.readfp. cfg.readfp(cStringIO(resource_string), resource_name) # pylint: disable=deprecated-method if os.path.exists(tweaks_name): print("Applying local tweaks from %s" % tweaks_name) cfg_tweaks = configparser.RawConfigParser() cfg_tweaks.read([tweaks_name]) merge_configs(cfg, cfg_tweaks) print("Writing %s" % filename) output_text = cStringIO() output_text.write(WARNING_HEADER.format(filename=filename, tweaks_name=tweaks_name)) cfg.write(output_text) out_tef = TamperEvidentFile(filename) if six.PY2: output_bytes = output_text.getvalue() else: output_bytes = output_text.getvalue().encode("utf8") out_tef.write(output_bytes) return 0
def write_main(argv): """ write FILENAME Write a local copy of FILENAME using FILENAME_tweaks for local tweaks. """ if len(argv) != 1: print("Please provide the name of a file to write.") return 1 filename = argv[0] resource_name = "files/" + filename tweaks_name = amend_filename(filename, "_tweaks") if not pkg_resources.resource_exists("edx_lint", resource_name): print(u"Don't have file %r to write." % filename) return 2 if os.path.exists(filename): print(u"Checking existing copy of %s" % filename) tef = TamperEvidentFile(filename) if not tef.validate(): bak_name = amend_filename(filename, "_backup") print(u"Your copy of %s seems to have been edited, renaming it to %s" % (filename, bak_name)) if os.path.exists(bak_name): print(u"A previous %s exists, deleting it" % bak_name) os.remove(bak_name) os.rename(filename, bak_name) print(u"Reading edx_lint/files/%s" % filename) cfg = configparser.RawConfigParser() resource_string = pkg_resources.resource_string("edx_lint", resource_name).decode("utf8") cfg.read_string(resource_string, resource_name) if os.path.exists(tweaks_name): print(u"Applying local tweaks from %s" % tweaks_name) cfg_tweaks = configparser.RawConfigParser() cfg_tweaks.read([tweaks_name]) merge_configs(cfg, cfg_tweaks) print(u"Writing %s" % filename) output_text = cStringIO() output_text.write(WARNING_HEADER.format(filename=filename, tweaks_name=tweaks_name, version=VERSION)) cfg.write(output_text) out_tef = TamperEvidentFile(filename) if six.PY2: output_bytes = output_text.getvalue() else: output_bytes = output_text.getvalue().encode("utf8") out_tef.write(output_bytes) return 0
def check_main(argv): if len(argv) != 1: print("Please provide the name of a file to check.") return 1 filename = argv[0] if os.path.exists(filename): print("Checking existing copy of %s" % filename) tef = TamperEvidentFile(filename) if tef.validate(): print("Your copy of %s is good" % filename) else: print("Your copy of %s seems to have been edited" % filename) else: print("You don't have a copy of %s" % filename) return 0
def check_main(argv): if len(argv) != 1: print "Please provide the name of a file to check." return 1 filename = argv[0] if os.path.exists(filename): print "Checking existing copy of %s" % filename tef = TamperEvidentFile(filename) if tef.validate(): print "Your copy of %s is good" % filename else: print "Your copy of %s seems to have been edited" % filename else: print "You don't have a copy of %s" % filename return 0
def write_main(argv): if len(argv) != 1: print "Please provide the name of a file to write." return 1 filename = argv[0] resource_name = "files/" + filename tweaks_name = amend_filename(filename, "_tweaks") if not pkg_resources.resource_exists("edx_lint", resource_name): print "Don't have file %r to write." % filename return 2 if os.path.exists(filename): print "Checking existing copy of %s" % filename tef = TamperEvidentFile(filename) if not tef.validate(): bak_name = amend_filename(filename, "_backup") print "Your copy of %s seems to have been edited, renaming it to %s" % (filename, bak_name) if os.path.exists(bak_name): print "A previous %s exists, deleting it" % bak_name os.remove(bak_name) os.rename(filename, bak_name) print "Reading edx_lint/files/%s" % filename cfg = ConfigParser.RawConfigParser() cfg.readfp(pkg_resources.resource_stream("edx_lint", resource_name), resource_name) if os.path.exists(tweaks_name): print "Applying local tweaks from %s" % tweaks_name cfg_tweaks = ConfigParser.RawConfigParser() cfg_tweaks.read([tweaks_name]) merge_configs(cfg, cfg_tweaks) print "Writing %s" % filename output_text = StringIO.StringIO() output_text.write(WARNING_HEADER) cfg.write(output_text) out_tef = TamperEvidentFile(filename) out_tef.write(output_text.getvalue()) return 0
def check_main(argv): """ check FILENAME Check that FILENAME has not been edited since writing. """ if len(argv) != 1: print("Please provide the name of a file to check.") return 1 filename = argv[0] if os.path.exists(filename): print("Checking existing copy of %s" % filename) tef = TamperEvidentFile(filename) if tef.validate(): print("Your copy of %s is good" % filename) else: print("Your copy of %s seems to have been edited" % filename) else: print("You don't have a copy of %s" % filename) return 0
def test_validating_a_good_file(self): filename = self.write_tamper_evident(b"Am I OK?") tef = TamperEvidentFile(filename) self.assertTrue(tef.validate())