public void SignAndSave_TwoModifiedAssemblies_KeyChangeInReferenced()
        {
            _assemblyDefinition2.Name.PublicKeyToken = AssemblyNameReferenceObjectMother.PublicKeyToken2;
            // we use clone because otherwise the name would be set in place and the test would always work.
            _assemblyDefinition1.MainModule.AssemblyReferences.Add(_assemblyDefinition2.Name.Clone());

            using (_writerMock.GetMockRepository().Ordered())
            {
                _writerMock.Expect(mock => mock.WriteModule(_assemblyDefinition2.MainModule)).WhenCalled(
                    mi => ChangePublicKeyToken(_assemblyDefinition2.MainModule, AssemblyNameReferenceObjectMother.PublicKeyToken1));

                _writerMock
                .Expect(mock => mock.WriteModule(_assemblyDefinition1.MainModule))
                .WhenCalled(
                    mi =>
                    AssemblyNameReferenceChecker.CheckNameReferences(_assemblyDefinition2.Name, _assemblyDefinition1.MainModule.AssemblyReferences[0]));
            }

            _writerMock.Replay();
            var tracker = CreateTracker(_assemblyDefinition1, _assemblyDefinition2);

            _assemblySigner.SignAndSave(tracker);

            _writerMock.VerifyAllExpectations();
            AssemblyNameReferenceChecker.CheckNameReferences(_assemblyDefinition2.Name, _assemblyDefinition1.MainModule.AssemblyReferences[0]);
        }
        public void SignAndSave_TwoModifiedAssemblies_KeyChangeInReferenced_WithCycle_KeyChangedTwice()
        {
            _assemblyDefinition1.Name.PublicKeyToken = AssemblyNameReferenceObjectMother.PublicKeyToken1;
            _assemblyDefinition2.Name.PublicKeyToken = AssemblyNameReferenceObjectMother.PublicKeyToken2;
            // we use clone because otherwise the name would be set in place and the test would always work.
            _assemblyDefinition1.MainModule.AssemblyReferences.Add(_assemblyDefinition2.Name.Clone());
            _assemblyDefinition2.MainModule.AssemblyReferences.Add(_assemblyDefinition1.Name.Clone());

            using (_writerMock.GetMockRepository().Ordered())
            {
                _writerMock
                .Expect(mock => mock.WriteModule(_assemblyDefinition2.MainModule))
                .WhenCalled(
                    mi => ChangePublicKeyToken(_assemblyDefinition2.MainModule, AssemblyNameReferenceObjectMother.PublicKeyToken3))
                .Message("First, _assemblyDefinition2 is saved and changes its public key.");

                _writerMock
                .Expect(mock => mock.WriteModule(_assemblyDefinition1.MainModule))
                .WhenCalled(
                    mi =>
                {
                    AssemblyNameReferenceChecker.CheckNameReferences(_assemblyDefinition2.Name, _assemblyDefinition1.MainModule.AssemblyReferences[0]);
                    ChangePublicKeyToken(_assemblyDefinition1.MainModule, AssemblyNameReferenceObjectMother.PublicKeyToken4);
                })
                .Message("Second, _assemblyDefinition1 is saved (with updated reference to _assemblyDefinition2) and also changes its public key.");

                _writerMock
                .Expect(mock => mock.WriteModule(_assemblyDefinition2.MainModule))
                .WhenCalled(
                    mi =>
                {
                    AssemblyNameReferenceChecker.CheckNameReferences(_assemblyDefinition1.Name, _assemblyDefinition2.MainModule.AssemblyReferences[0]);
                    ChangePublicKeyToken(_assemblyDefinition2.MainModule, AssemblyNameReferenceObjectMother.PublicKeyToken5);
                })
                .Message("Third, _assemblyDefinition2 is saved again (with updated reference to _assemblyDefinition1), key changes a second time.");

                _writerMock
                .Expect(mock => mock.WriteModule(_assemblyDefinition1.MainModule))
                .WhenCalled(
                    mi => AssemblyNameReferenceChecker.CheckNameReferences(_assemblyDefinition2.Name, _assemblyDefinition1.MainModule.AssemblyReferences[0]))
                .Message("Fourth, _assemblyDefinition1 is saved again (with updated reference to _assemblyDefinition2), no key changes, recursion terminates.");
            }

            _writerMock.Replay();
            var tracker = CreateTracker(_assemblyDefinition1, _assemblyDefinition2);

            _assemblySigner.SignAndSave(tracker);

            _writerMock.VerifyAllExpectations();

            AssemblyNameReferenceChecker.CheckNameReferences(_assemblyDefinition1.Name, _assemblyDefinition2.MainModule.AssemblyReferences[0]);
            AssemblyNameReferenceChecker.CheckNameReferences(_assemblyDefinition2.Name, _assemblyDefinition1.MainModule.AssemblyReferences[0]);
        }