Exemplo n.º 1
0
        public void createAValidListOfTemporaryExposureKeys_Have15Keys_Only14ShouldBeKept()
        {
            // Create a list of 15 keys
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>();

            for (int i = 0; i < 15; i++)
            {
                temporaryExposureKeys = temporaryExposureKeys.Append(new ExposureKeyModel(new byte[i + 1], june1.AddDays(i), TimeSpan.FromDays(1), RiskLevel.Medium));
            }

            // Process them
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // There should be 14 left
            Assert.Equal(14, processedKeys.Count());
        }
Exemplo n.º 2
0
        public void createAValidListOfTemporaryExposureKeys_HaveDateGap_OnlyNewestShouldBeKept()
        {
            // Create keys
            ExposureKeyModel tek1 = new ExposureKeyModel(new byte[1], june1, TimeSpan.FromDays(1), RiskLevel.Medium);
            ExposureKeyModel tek2 = new ExposureKeyModel(new byte[2], june1.AddDays(1), TimeSpan.FromDays(1), RiskLevel.Medium);
            ExposureKeyModel tek3 = new ExposureKeyModel(new byte[3], june1.AddDays(3), TimeSpan.FromDays(1), RiskLevel.Medium);

            // Process a list of copies
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>()
            {
                CopyTek(tek1), CopyTek(tek2), CopyTek(tek3)
            };
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // Only tek3 should be left
            Assert.Single(processedKeys);
            Assert.True(ContainsTek(tek3, processedKeys));
        }
Exemplo n.º 3
0
        public void createAValidListOfTemporaryExposureKeys_HaveMultipleWithSameDate_OnlyOneWithEachDateShouldBeKept()
        {
            // Create keys
            ExposureKeyModel tek1 = new ExposureKeyModel(new byte[1], june1, TimeSpan.FromDays(1), RiskLevel.Medium);
            ExposureKeyModel tek2 = new ExposureKeyModel(new byte[2], june1, TimeSpan.FromDays(1), RiskLevel.Medium);
            ExposureKeyModel tek3 = new ExposureKeyModel(new byte[3], june1.AddDays(1), TimeSpan.FromDays(1), RiskLevel.Medium);

            // Process a list of copies
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>()
            {
                CopyTek(tek1), CopyTek(tek2), CopyTek(tek3)
            };
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // The only difference should be that tek2 is not contained in the result
            Assert.Equal(2, processedKeys.Count());
            Assert.True(ContainsTek(tek1, processedKeys));
            Assert.True(ContainsTek(tek3, processedKeys));
        }
        // === Methods used both by EN API v1 and EN API v2 ===

        public async Task UploadSelfExposureKeysToServerAsync(IEnumerable <TemporaryExposureKey> tempKeys)
        {
            // Convert to ExposureKeyModel list as it is extended with DaysSinceOnsetOfSymptoms value
            IEnumerable <ExposureKeyModel> temporaryExposureKeys =
                tempKeys?.Select(key => new ExposureKeyModel(key)) ?? new List <ExposureKeyModel>();

            if (FakeGatewayUtils.IsFakeGatewayTest)
            {
                FakeGatewayUtils.LastPulledExposureKeys = temporaryExposureKeys;
                return;
            }

            if (AuthenticationState.PersonalData?.Access_token == null)
            {
                throw new AccessTokenMissingFromIDPortenException("The token from ID Porten is not set");
            }

            if (AuthenticationState.PersonalData?.VisitedCountries == null)
            {
                throw new VisitedCountriesMissingException(
                          "The visited countries list is missing. Possibly garbage collection removed it.");
            }

            if (!DateToSetDSOS.HasValue)
            {
                throw new DSOSDateMissingException("The symptom onset date is not set from the calling view model");
            }

            DateTime dateToSetDSOSAsUniversalTime = DateToSetDSOS.Value.ToUniversalTime();

            List <ExposureKeyModel> validKeys =
                UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            validKeys = UploadDiagnosisKeysHelper.SetTransmissionRiskAndDSOS(validKeys, dateToSetDSOSAsUniversalTime);

            bool success = await _exposureNotificationWebService.PostSelfExposureKeys(validKeys);

            if (!success)
            {
                throw new FailedToPushToServerException("Failed to push keys to the server");
            }
        }
Exemplo n.º 5
0
        public void createAValidListOfTemporaryExposureKeys_HaveBadRollingDurations_RollingDurationsShouldBeSetToOneDay()
        {
            // Create keys
            ExposureKeyModel tek1 = new ExposureKeyModel(new byte[1], june1, TimeSpan.FromDays(0), RiskLevel.Medium);
            ExposureKeyModel tek2 = new ExposureKeyModel(new byte[2], june1.AddDays(1), TimeSpan.FromDays(10), RiskLevel.Medium);
            ExposureKeyModel tek3 = new ExposureKeyModel(new byte[3], june1.AddDays(2), TimeSpan.FromDays(-10), RiskLevel.Medium);

            // Process a list of copies
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>()
            {
                CopyTek(tek1), CopyTek(tek2), CopyTek(tek3)
            };
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // RollingDurations should be 1 day
            foreach (ExposureKeyModel tek in processedKeys)
            {
                Assert.Equal(TimeSpan.FromDays(1), tek.RollingDuration);
            }
        }
        public async void createAValidListOfTemporaryExposureKeys_HaveDateGap_AllShouldBeKept()
        {
            SystemTime.ResetDateTime();

            // Create keys
            ExposureKeyModel tek1 = new ExposureKeyModel(new byte[1], SystemTime.Now(), TimeSpan.FromDays(1), RiskLevel.Medium);
            ExposureKeyModel tek2 = new ExposureKeyModel(new byte[2], SystemTime.Now().AddDays(-1), TimeSpan.FromDays(1), RiskLevel.Medium);
            ExposureKeyModel tek3 = new ExposureKeyModel(new byte[3], SystemTime.Now().AddDays(-3), TimeSpan.FromDays(1), RiskLevel.Medium);

            // Process a list of copies
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>()
            {
                CopyTek(tek1), CopyTek(tek2), CopyTek(tek3)
            };
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // No keys should be filtered out
            Assert.Equal(processedKeys.Count(), temporaryExposureKeys.Count());
            Assert.True(ContainsTek(tek3, processedKeys));
            Assert.False((await _logManager.GetLogs(10)).Any());
        }
        public void createAValidListOfTemporaryExposureKeys_HaveMultipleWithSameDate_AllShouldBeKept()
        {
            SystemTime.ResetDateTime();

            // Create keys
            ExposureKeyModel tek1 = new ExposureKeyModel(new byte[1], SystemTime.Now(), TimeSpan.FromDays(0.7), RiskLevel.Medium);
            ExposureKeyModel tek2 = new ExposureKeyModel(new byte[2], SystemTime.Now(), TimeSpan.FromDays(0.3), RiskLevel.Medium);
            ExposureKeyModel tek3 = new ExposureKeyModel(new byte[3], SystemTime.Now().AddDays(-1), TimeSpan.FromDays(1), RiskLevel.Medium);

            // Process a list of copies
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>()
            {
                CopyTek(tek1), CopyTek(tek2), CopyTek(tek3)
            };
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // No keys should be filtered out
            Assert.True(ContainsTek(tek1, processedKeys));
            Assert.True(ContainsTek(tek2, processedKeys));
            Assert.True(ContainsTek(tek3, processedKeys));
        }
        public async void createAValidListOfTemporaryExposureKeys_GeneratesProperLogIfKeysAreFiltered(int ExtraKeys)
        {
            SystemTime.ResetDateTime();
            await _logManager.DeleteAll();

            // Create a list of 15 keys
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>();

            for (int i = 0; i < 14 + ExtraKeys; i++)
            {
                temporaryExposureKeys = temporaryExposureKeys.Append(new ExposureKeyModel(new byte[i + 1], SystemTime.Now().AddDays(0 - i), TimeSpan.FromDays(1), RiskLevel.Medium));
            }

            // Process them
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // Check if log is generated
            string logStatement = _logManager.GetLogs(1).Result.ElementAt(0).Description;

            Assert.Equal(ExtraKeys.ToString(), logStatement.Last().ToString());
        }
Exemplo n.º 9
0
        public async Task UploadSelfExposureKeysToServerAsync(IEnumerable <TemporaryExposureKey> tempKeys)
        {
            // Convert to ExposureKeyModel list as it is extended with DaysSinceOnsetOfSymptoms value
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = tempKeys?.Select(key => new ExposureKeyModel(key)) ?? new List <ExposureKeyModel>();

            // There is a better behaviour of uploading keys when scanning is Stoped/Started (UIAlert for permission is always shown then),
            // The IF-check just toggles the scanning status on/off or off/on to keep the scanning status
            // the same as it was before method is called

            try
            {
                if (ServiceLocator.Current.GetInstance <IDeviceInfo>().Platform == DevicePlatform.iOS)
                {
                    if (await ExposureNotification.IsEnabledAsync())
                    {
                        await ExposureNotification.StopAsync();

                        await ExposureNotification.StartAsync();
                    }
                    else
                    {
                        await ExposureNotification.StartAsync();

                        await ExposureNotification.StopAsync();
                    }
                }
            }
            catch (Exception e)
            {
                if (!e.HandleExposureNotificationException(nameof(ExposureNotificationHandler), nameof(UploadSelfExposureKeysToServerAsync)))
                {
                    throw e;
                }
            }

            if (FakeGatewayUtils.IsFakeGatewayTest)
            {
                FakeGatewayUtils.LastPulledExposureKeys = temporaryExposureKeys;
                return;
            }

            if (AuthenticationState.PersonalData?.Access_token == null)
            {
                throw new AccessTokenMissingFromIDPortenException("The token from ID Porten is not set");
            }

            if (AuthenticationState.PersonalData?.VisitedCountries == null)
            {
                throw new VisitedCountriesMissingException(
                          "The visited countries list is missing. Possibly garbage collection removed it.");
            }

            if (!MiBaDate.HasValue)
            {
                throw new MiBaDateMissingException("The symptom onset date is not set from the calling view model");
            }

            DateTime MiBaDateAsUniversalTime = MiBaDate.Value.ToUniversalTime();

            List <ExposureKeyModel> validKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);

            // Here all keys are extended with DaysSinceOnsetOfSymptoms value
            validKeys = UploadDiagnosisKeysHelper.SetTransmissionRiskLevel(validKeys, MiBaDateAsUniversalTime);

            bool success = await exposureNotificationWebService.PostSelfExposureKeys(validKeys);

            if (!success)
            {
                throw new FailedToPushToServerException("Failed to push keys to the server");
            }
        }
Exemplo n.º 10
0
        public async void calculateTransmissionRiskbasedOnDateDifference()
        {
            // Create keys with different dates
            ExposureKeyModel tekminus3 = new ExposureKeyModel(new byte[1], june1.AddDays(-3), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tekminus2 = new ExposureKeyModel(new byte[1], june1.AddDays(-2), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tekminus1 = new ExposureKeyModel(new byte[1], june1.AddDays(-1), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek2      = new ExposureKeyModel(new byte[1], june1.AddDays(2), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek3      = new ExposureKeyModel(new byte[1], june1.AddDays(3), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek4      = new ExposureKeyModel(new byte[1], june1.AddDays(4), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek5      = new ExposureKeyModel(new byte[1], june1.AddDays(5), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek6      = new ExposureKeyModel(new byte[1], june1.AddDays(6), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek7      = new ExposureKeyModel(new byte[1], june1.AddDays(7), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek8      = new ExposureKeyModel(new byte[1], june1.AddDays(8), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek9      = new ExposureKeyModel(new byte[1], june1.AddDays(9), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek10     = new ExposureKeyModel(new byte[1], june1.AddDays(10), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek11     = new ExposureKeyModel(new byte[1], june1.AddDays(11), TimeSpan.FromDays(1), RiskLevel.Invalid);
            ExposureKeyModel tek12     = new ExposureKeyModel(new byte[1], june1.AddDays(12), TimeSpan.FromDays(1), RiskLevel.Invalid);


            // Process a list of copies
            IEnumerable <ExposureKeyModel> temporaryExposureKeys = new List <ExposureKeyModel>()
            {
                tek2, tek3, tek4, tek5, tek6, tek7, tek8, tek9, tek10, tek11, tek12
            };
            IEnumerable <ExposureKeyModel> processedKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(temporaryExposureKeys);
            List <ExposureKeyModel>        validKeys     = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(processedKeys);

            List <ExposureKeyModel> resultKeys = UploadDiagnosisKeysHelper.SetTransmissionRiskLevel(validKeys, MiBaDate);

            for (int i = 1; i < 11; i++)
            {
                if (i == 0)
                {
                    Assert.Equal("Highest", resultKeys[i].TransmissionRiskLevel.ToString());
                }
                if (i > 0 && i < 3)
                {
                    Assert.Equal("VeryHigh", resultKeys[i].TransmissionRiskLevel.ToString());
                }
                if (i > 2 && i < 5)
                {
                    Assert.Equal("High", resultKeys[i].TransmissionRiskLevel.ToString());
                }
                if (i > 4 && i < 9)
                {
                    Assert.Equal("MediumHigh", resultKeys[i].TransmissionRiskLevel.ToString());
                }
                if (i > 8)
                {
                    Assert.Equal("Medium", resultKeys[i].TransmissionRiskLevel.ToString());
                }
            }

            IEnumerable <ExposureKeyModel> negativeDifferenceExposureKeys = new List <ExposureKeyModel>()
            {
                tekminus1, tekminus2, tekminus3
            };
            IEnumerable <ExposureKeyModel> processedNegativeDifferenceExposureKeys = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(negativeDifferenceExposureKeys);
            List <ExposureKeyModel>        validNegativeDifferenceExposureKeys     = UploadDiagnosisKeysHelper.CreateAValidListOfTemporaryExposureKeys(processedNegativeDifferenceExposureKeys);
            List <ExposureKeyModel>        resultKeysNegativeDifference            = UploadDiagnosisKeysHelper.SetTransmissionRiskLevel(validNegativeDifferenceExposureKeys, MiBaDate);

            for (int i = 1; i < 11; i++)
            {
                if (i == 0)
                {
                    Assert.Equal("Medium", resultKeysNegativeDifference[i].TransmissionRiskLevel.ToString());
                }
                if (i == 1)
                {
                    Assert.Equal("MediumLow", resultKeysNegativeDifference[i].TransmissionRiskLevel.ToString());
                }
                if (i == 2)
                {
                    Assert.Equal("Low", resultKeysNegativeDifference[i].TransmissionRiskLevel.ToString());
                }
            }
        }