def test_source_files(self):
        Strings = InfStrings()
        SourceFiles = InfSourceFiles("diskname", Strings)
        SourceFiles.AddFile("test.bin")
        SourceFiles.AddFile("test2.bin")
        SourceFiles.AddFile("test3.bin")

        ExpectedStr = textwrap.dedent("""\
            [SourceDisksNames]
            1 = %DiskName%

            [SourceDisksFiles]
            test.bin = 1
            test2.bin = 1
            test3.bin = 1

            [DestinationDirs]
            DefaultDestDir = %DIRID_WINDOWS%,Firmware ; %SystemRoot%\\Firmware

            """)

        self.assertEqual(ExpectedStr, str(SourceFiles))
        self.assertIn("DiskName", Strings.LocalizableStrings)
        self.assertEqual("diskname", Strings.LocalizableStrings['DiskName'])
        self.assertIn("DIRID_WINDOWS", Strings.NonLocalizableStrings)
        self.assertEqual("10", Strings.NonLocalizableStrings['DIRID_WINDOWS'])
    def test_firmware(self):
        Strings = InfStrings()
        SourceFiles = InfSourceFiles("diskname", Strings)

        Firmware = InfFirmware(
            "tag",
            "desc",
            "34e094e9-4079-44cd-9450-3f2cb7824c97",
            "0x01000001",
            "test.bin",
            Strings,
            SourceFiles)

        ExpectedStr = textwrap.dedent("""\
            [tag_Install.NT]
            CopyFiles = tag_CopyFiles

            [tag_CopyFiles]
            test.bin

            [tag_Install.NT.Hw]
            AddReg = tag_AddReg

            [tag_AddReg]
            HKR,,FirmwareId,,{34e094e9-4079-44cd-9450-3f2cb7824c97}
            HKR,,FirmwareVersion,%REG_DWORD%,0x1000001
            HKR,,FirmwareFilename,,test.bin

            """)

        self.assertEqual(ExpectedStr, str(Firmware))
        self.assertEqual(Firmware.Description, "desc")
        self.assertIn("REG_DWORD", Strings.NonLocalizableStrings)
        self.assertEqual("0x00010001", Strings.NonLocalizableStrings['REG_DWORD'])
        self.assertIn("test.bin", SourceFiles.Files)
    def test_firmware_should_throw_for_bad_input(self):
        Strings = InfStrings()
        SourceFiles = InfSourceFiles("diskname", Strings)

        with self.assertRaises(ValueError):
            InfFirmware(
                "This is not a valid tag name",
                "desc",
                "34e094e9-4079-44cd-9450-3f2cb7824c97",
                "0x01000001",
                "test.bin",
                Strings,
                SourceFiles,
                Rollback=True,
                IntegrityFile="test2.bin")

        with self.assertRaises(ValueError):
            InfFirmware(
                "Tag",
                "desc",
                "This is not a valid UUID.",
                "0x01000001",
                "test.bin",
                Strings,
                SourceFiles,
                Rollback=True,
                IntegrityFile="test2.bin")

        with self.assertRaises(ValueError):
            InfFirmware(
                "Tag",
                "desc",
                "4e094e9-4079-44cd-9450-3f2cb7824c97",  # a more subtle not-valid UUID.
                "0x01000001",
                "test.bin",
                Strings,
                SourceFiles,
                Rollback=True,
                IntegrityFile="test2.bin")

        with self.assertRaises(ValueError):
            InfFirmware(
                "tag",
                "desc",
                "34e094e9-4079-44cd-9450-3f2cb7824c97",
                "foobar",
                "test.bin",
                Strings,
                SourceFiles,
                Rollback=True,
                IntegrityFile="test2.bin")
    def test_rollback_firmware_integrity(self):
        Strings = InfStrings()
        SourceFiles = InfSourceFiles("diskname", Strings)

        Firmware = InfFirmware(
            "tag",
            "desc",
            "34e094e9-4079-44cd-9450-3f2cb7824c97",
            "0x01000001",
            "test.bin",
            Strings,
            SourceFiles,
            Rollback=True,
            IntegrityFile="test2.bin")

        ExpectedStr = textwrap.dedent("""\
            [tag_Install.NT]
            CopyFiles = tag_CopyFiles
            AddReg = tag_DowngradePolicy_AddReg

            [tag_DowngradePolicy_AddReg]
            HKLM,SYSTEM\\CurrentControlSet\\Control\\FirmwareResources\\{34e094e9-4079-44cd-9450-3f2cb7824c97},Policy,%REG_DWORD%,1

            [tag_CopyFiles]
            test.bin
            test2.bin

            [tag_Install.NT.Hw]
            AddReg = tag_AddReg

            [tag_AddReg]
            HKR,,FirmwareId,,{34e094e9-4079-44cd-9450-3f2cb7824c97}
            HKR,,FirmwareVersion,%REG_DWORD%,0x1000001
            HKR,,FirmwareFilename,,test.bin
            HKR,,FirmwareIntegrityFilename,,test2.bin

            """)

        self.assertEqual(ExpectedStr, str(Firmware))
        self.assertEqual(Firmware.Description, "desc")
        self.assertIn("REG_DWORD", Strings.NonLocalizableStrings)
        self.assertEqual("0x00010001", Strings.NonLocalizableStrings['REG_DWORD'])
        self.assertIn("test.bin", SourceFiles.Files)
        self.assertIn("test2.bin", SourceFiles.Files)
    def test_one_section(self):
        Strings = InfStrings()
        SourceFiles = InfSourceFiles("diskname", Strings)

        Firmware = InfFirmware(
            "tag",
            "desc",
            "34e094e9-4079-44cd-9450-3f2cb7824c97",
            "0x01000001",
            "test.bin",
            Strings,
            SourceFiles)

        Sections = InfFirmwareSections('amd64', Strings)
        Sections.AddSection(Firmware)

        ExpectedStr = textwrap.dedent("""\
            [Firmware.NTamd64]
            %tagDesc% = tag_Install,UEFI\\RES_{34e094e9-4079-44cd-9450-3f2cb7824c97}

            [tag_Install.NT]
            CopyFiles = tag_CopyFiles

            [tag_CopyFiles]
            test.bin

            [tag_Install.NT.Hw]
            AddReg = tag_AddReg

            [tag_AddReg]
            HKR,,FirmwareId,,{34e094e9-4079-44cd-9450-3f2cb7824c97}
            HKR,,FirmwareVersion,%REG_DWORD%,0x1000001
            HKR,,FirmwareFilename,,test.bin

            """)

        self.assertEqual(ExpectedStr, str(Sections))
        self.assertIn("tagDesc", Strings.LocalizableStrings)
        self.assertEqual("desc", Strings.LocalizableStrings['tagDesc'])
        self.assertIn("test.bin", SourceFiles.Files)

        self.assertIn("REG_DWORD", Strings.NonLocalizableStrings)
        self.assertEqual("0x00010001", Strings.NonLocalizableStrings['REG_DWORD'])
    def test_source_files_should_throw_on_bad_input(self):
        Strings = InfStrings()
        SourceFiles = InfSourceFiles("diskname", Strings)

        with self.assertRaises(ValueError):
            SourceFiles.AddFile("Who Names Files Like This?.bin")
    def test_two_sections(self):
        Strings = InfStrings()
        SourceFiles = InfSourceFiles("diskname", Strings)

        Firmware1 = InfFirmware(
            "tag1",
            "desc1",
            "34e094e9-4079-44cd-9450-3f2cb7824c97",
            "0x01000001",
            "test1.bin",
            Strings,
            SourceFiles)

        Firmware2 = InfFirmware(
            "tag2",
            "desc2",
            "bec9124f-9934-4ec0-a6ed-b8bc1c91d276",
            "0x01000002",
            "test2.bin",
            Strings,
            SourceFiles)

        Sections = InfFirmwareSections('amd64', Strings)
        Sections.AddSection(Firmware1)
        Sections.AddSection(Firmware2)

        ExpectedStr = textwrap.dedent("""\
            [Firmware.NTamd64]
            %tag1Desc% = tag1_Install,UEFI\\RES_{34e094e9-4079-44cd-9450-3f2cb7824c97}
            %tag2Desc% = tag2_Install,UEFI\\RES_{bec9124f-9934-4ec0-a6ed-b8bc1c91d276}

            [tag1_Install.NT]
            CopyFiles = tag1_CopyFiles

            [tag1_CopyFiles]
            test1.bin

            [tag1_Install.NT.Hw]
            AddReg = tag1_AddReg

            [tag1_AddReg]
            HKR,,FirmwareId,,{34e094e9-4079-44cd-9450-3f2cb7824c97}
            HKR,,FirmwareVersion,%REG_DWORD%,0x1000001
            HKR,,FirmwareFilename,,test1.bin

            [tag2_Install.NT]
            CopyFiles = tag2_CopyFiles

            [tag2_CopyFiles]
            test2.bin

            [tag2_Install.NT.Hw]
            AddReg = tag2_AddReg

            [tag2_AddReg]
            HKR,,FirmwareId,,{bec9124f-9934-4ec0-a6ed-b8bc1c91d276}
            HKR,,FirmwareVersion,%REG_DWORD%,0x1000002
            HKR,,FirmwareFilename,,test2.bin

            """)

        self.assertEqual(ExpectedStr, str(Sections))
        self.assertIn("tag1Desc", Strings.LocalizableStrings)
        self.assertEqual("desc1", Strings.LocalizableStrings['tag1Desc'])
        self.assertIn("test1.bin", SourceFiles.Files)
        self.assertIn("tag2Desc", Strings.LocalizableStrings)
        self.assertEqual("desc2", Strings.LocalizableStrings['tag2Desc'])
        self.assertIn("test2.bin", SourceFiles.Files)

        self.assertIn("REG_DWORD", Strings.NonLocalizableStrings)
        self.assertEqual("0x00010001", Strings.NonLocalizableStrings['REG_DWORD'])