def PackageWindowsCapsuleFiles(OutputFolder, ProductName, ProductFmpGuid, CapsuleVersion_DotString,
    CapsuleVersion_HexString, ProductFwProvider, ProductFwMfgName, ProductFwDesc, CapsuleFileName, PfxFile=None, PfxPass=None,
    Rollback=False, Arch='amd64', OperatingSystem_String='Win10'):

      logging.debug("CapsulePackage: Create Windows Capsule Files")

      #Make INF
      InfFilePath = os.path.join(OutputFolder, ProductName + ".inf")
      InfTool = InfGenerator(ProductName, ProductFwProvider, ProductFmpGuid, Arch, ProductFwDesc, CapsuleVersion_DotString, CapsuleVersion_HexString)
      InfTool.Manufacturer = ProductFwMfgName  #optional
      ret = InfTool.MakeInf(InfFilePath, CapsuleFileName, Rollback)
      if(ret != 0):
          raise Exception("CreateWindowsInf Failed with errorcode %d" % ret)

      #Make CAT
      CatFilePath = os.path.realpath(os.path.join(OutputFolder, ProductName + ".cat"))
      CatTool = CatGenerator(Arch, OperatingSystem_String)
      ret = CatTool.MakeCat(CatFilePath)

      if(ret != 0):
          raise Exception("Creating Cat file Failed with errorcode %d" % ret)

      if(PfxFile is not None):
          #Find Signtool
          SignToolPath = FindToolInWinSdk("signtool.exe")
          if not os.path.exists(SignToolPath):
              raise Exception("Can't find signtool on this machine.")
          #dev sign the cat file
          ret = CatalogSignWithSignTool(SignToolPath, CatFilePath, PfxFile, PfxPass)
          if(ret != 0):
              raise Exception("Signing Cat file Failed with errorcode %d" % ret)

      return ret
    def test_version_hex_too_big(self):
        with self.subTest("hex string too big"):
            with self.assertRaises(ValueError):
                InfGenerator("test_name", "provider",
                             InfGeneratorTest.VALID_GUID_STRING, "x64",
                             "description", "aa.bb", "0x100000000")

        with self.subTest("decimal too big"):
            with self.assertRaises(ValueError):
                InfGenerator("test_name", "provider",
                             InfGeneratorTest.VALID_GUID_STRING, "x64",
                             "description", "aa.bb", "4294967296")
    def test_version_string_format(self):
        with self.subTest(version_string="zero ."):
            with self.assertRaises(ValueError):
                InfGenerator("test_name", "provider",
                             InfGeneratorTest.VALID_GUID_STRING, "x64",
                             "description", "1234", "0x100000000")

        with self.subTest(version_string="> 3 ."):
            with self.assertRaises(ValueError):
                InfGenerator("test_name", "provider",
                             InfGeneratorTest.VALID_GUID_STRING, "x64",
                             "description", "1.2.3.4.5", "0x100000000")
    def test_file(self):
        with tempfile.TemporaryDirectory() as tmpdir:
            inffile_path = os.path.join(tmpdir, "InfFile.inf")
            infgen = InfGenerator('TestName', 'TestProvider',
                                  InfGeneratorTest.VALID_GUID_STRING, "x64",
                                  "Test Description", "1.2.3.4", "0x01020304")
            infgen.MakeInf(inffile_path, "TestFirmwareRom.bin", False)

            with open(inffile_path, "r") as inffile:
                file_contents = inffile.read()
                # Remove all whitespace, just in case.
                file_contents = file_contents.replace("\n", "").replace(
                    "\t", "").replace(" ", "")
                test_contents = _get_test_file().replace("\n", "").replace(
                    "\t", "").replace(" ", "")
                self.assertEqual(test_contents, file_contents)
    def test_integrity_file_entry(self):
        with tempfile.TemporaryDirectory() as tmpdir:
            inffile_path = os.path.join(tmpdir, "InfFile.inf")
            infgen = InfGenerator('TestName', 'TestProvider',
                                  InfGeneratorTest.VALID_GUID_STRING, "x64",
                                  "Test Description", "1.2.3.4", "0x01020304")
            infgen.MakeInf(inffile_path, "TestFirmwareRom.bin", False)
            with open(inffile_path, "r") as inffile:
                file_contents = inffile.read()
                self.assertNotIn("SampleIntegrityFile.bin", file_contents)
                self.assertNotIn("FirmwareIntegrityFilename", file_contents)

            infgen.IntegrityFilename = "SampleIntegrityFile.bin"
            infgen.MakeInf(inffile_path, "TestFirmwareRom.bin", False)
            with open(inffile_path, "r") as inffile:
                file_contents = inffile.read()
                self.assertIn("SampleIntegrityFile.bin", file_contents)
                self.assertIn("FirmwareIntegrityFilename", file_contents)
    def test_invalid_name_symbol(self):

        InvalidChars = [
            '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', ' ',
            '{', '[', '}', ']', '+', '='
        ]
        for a in InvalidChars:
            with self.subTest(name="test{}name".format(a)):
                name = "test{}name".format(a)
                with self.assertRaises(ValueError):
                    InfGenerator(name, "provider",
                                 InfGeneratorTest.VALID_GUID_STRING, "x64",
                                 "description", "aa.bb", "0xaabbccdd")
    def test_valid(self):
        o = InfGenerator("test_name", "provider",
                         InfGeneratorTest.VALID_GUID_STRING, "x64",
                         "description", "aa.bb.cc.dd", "0xaabbccdd")
        self.assertIsInstance(o, InfGenerator)
        self.assertEqual(o.Name, "test_name")
        self.assertEqual(o.Provider, "provider")
        self.assertEqual(o.EsrtGuid, InfGeneratorTest.VALID_GUID_STRING)
        self.assertEqual(o.Arch, InfGenerator.SUPPORTED_ARCH["x64"])
        self.assertEqual(o.Description, "description")
        self.assertEqual(int(o.VersionHex, 0), int("0xaabbccdd", 0))
        self.assertEqual(o.VersionString, "aa.bb.cc.dd")
        self.assertEqual(o.Manufacturer, "provider")

        # loop thru all supported arch and make sure it works
        for a in InfGenerator.SUPPORTED_ARCH.keys():
            with self.subTest(Arch=a):
                o.Arch = a
                self.assertEqual(InfGenerator.SUPPORTED_ARCH[a], o.Arch)

        # set manufacturer
        o.Manufacturer = "manufacturer"
        self.assertEqual("manufacturer", o.Manufacturer)
 def test_invalid_guid_format(self):
     with self.assertRaises(ValueError):
         InfGenerator("test_name", "provider", "NOT A VALID GUID", "x64",
                      "description", "aa.bb", "0x1000000")
 def test_version_hex_can_support_decimal(self):
     o = InfGenerator("test_name", "provider",
                      InfGeneratorTest.VALID_GUID_STRING, "x64",
                      "description", "aa.bb.cc.dd", "12356")
     self.assertEqual(int(o.VersionHex, 0), 12356)