Esempio n. 1
0
 public void CountValidTest(string input, string requiredFields, string optionalFields, int expected)
 {
     string[] requiredFieldsArr = requiredFields?.Split(",") ?? Enumerable.Empty <string>().ToArray();
     string[] optionalFieldsArr = optionalFields?.Split(",") ?? Enumerable.Empty <string>().ToArray();
     Assert.Equal(expected, PassportHelper.CountValid(input, requiredFieldsArr, optionalFieldsArr, null));
     //string input, string[] requiredFields, string[] optionalFields, Func< Dictionary<string, string>, bool> isValid = null
 }
        private void mnuCreateLiveAutoLogin_Click(object sender, RoutedEventArgs e)
        {
#if !LIVECONNECT
            if (string.IsNullOrEmpty(UserSettings.Default.AuthenticationBlob))
            {
                AuthenticationInformation info = new AuthenticationInformation();
                try
                {
                    PassportHelper.TryAuthenticate(ref info);
                    if (string.IsNullOrEmpty(info.AuthBlob))
                    {
                        MessageBox.Show(this, "Failed to get authentication blob!");
                    }
                    else
                    {
                        UserSettings.Default.AuthenticationBlob = info.AuthBlob;
                        UserSettings.Default.Save();
                        ApplySettings();
                        MessageBox.Show(this, "Authentication blob received; autologin activated.");
                    }
                }
                catch
                {
                    MessageBox.Show(this, "Failed to get authentication blob!");
                }
            }
            else
            {
                UserSettings.Default.AuthenticationBlob = string.Empty;
                UserSettings.Default.Save();
                ApplySettings();
                MessageBox.Show(this, "Authentication login disabled. You should *restart* the bridge to reset the LiveId component!");
            }
#endif
        }
Esempio n. 3
0
 public void CountValidRealTest(string input, string requiredFields, string optionalFields, int expected)
 {
     string[] requiredFieldsArr = requiredFields?.Split(",") ?? Enumerable.Empty <string>().ToArray();
     string[] optionalFieldsArr = optionalFields?.Split(",") ?? Enumerable.Empty <string>().ToArray();
     Assert.Equal(expected, PassportHelper.CountValid(input, requiredFieldsArr, optionalFieldsArr, new Dictionary <string, string>()
     {
         { "byr", @"^(19[2-9]\d|20(0[0-2]))$" },
         { "iyr", @"^20(1\d|20)$" },
         { "eyr", @"^20(2\d|30)$" },
         { "hgt", @"^(1(5\d|6\d|7\d|8\d|9(0|1|2|3))+cm|(59|6\d|7(0|1|2|3|4|5|6))in)$" },
         { "hcl", @"^#[0-9a-f]{6}$" },
         { "ecl", @"^amb|blu|brn|gry|grn|hzl|oth$" },
         { "pid", @"^\d{9}$" },
     }));
     //string input, string[] requiredFields, string[] optionalFields, Func< Dictionary<string, string>, bool> isValid = null
 }
        private static void StartBridgeInternal(MainWindow t, int port, string domainName)
        {
            //var services = new[] { "social", "answers" };
            var services = new[] { "social" };

            //var serviceModel = System.Configuration.ConfigurationManager.GetSection("system.serviceModel");

            string authenticationTicket = null;

#if LIVECONNECT
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "LiveConnect: Try authentication");
            t.DoLogin();
            authenticationTicket = t.AccessToken;
            if (string.IsNullOrEmpty(authenticationTicket))
            {
                // Reset the RefreshToken, if the authentication has failed...
                UserSettings.Default.RefreshToken = string.Empty;
                Traces.Main_TraceEvent(TraceEventType.Error, 1, "Could not authenticate with LiveConnect!");
                throw new ApplicationException("Could not authenticate with LiveConnect!");
            }
            Traces.Main_TraceEvent(TraceEventType.Information, 1, "Authenticated: AccessToken: {0}", authenticationTicket);
#else
            // Authenticate with Live Id
            var authenticationData = new AuthenticationInformation();

            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "LiveId: Try authentication");
            try
            {
                PassportHelper.TryAuthenticate(ref authenticationData, UserSettings.Default.AuthenticationBlob);
            }
            catch
            {
                // Reset the auto login, if the authentication has failed...
                UserSettings.Default.AuthenticationBlob = string.Empty;
                throw;
            }
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "LiveId: UserName: {0}, Ticket: {1}", authenticationData.UserName, authenticationData.Ticket);

            if (authenticationData.Ticket == null)
            {
                // Reset the auto login, if the authentication has failed...
                UserSettings.Default.AuthenticationBlob = string.Empty;
                Traces.Main_TraceEvent(TraceEventType.Error, 1, "Could not authenticate with LiveId!");
                throw new ApplicationException("Could not authenticate with LiveId!");
            }
            authenticationTicket = authenticationData.Ticket;
#endif

            t._forumsProviders = new MicrosoftForumsServiceProvider[services.Length];
            for (var i = 0; i < services.Length; i++)
            {
                // Create the forums-ServiceProvider
                Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Create forums service provider: {0}", services[i]);
                var provider = new MicrosoftForumsServiceProvider(services[i]);
                System.Diagnostics.Debug.WriteLine(string.Format("{0}: Uri: {1}", services[i], provider.Uri));

                // Assigne the authentication ticket
                provider.AuthenticationTicket = authenticationTicket;
                ApplyForumsDisabled(provider);

#if PASSPORT_HEADER_ANALYSIS
                // Register AuthError-Handler
                provider.AuthenticationError += t.provider_AuthenticationError;
#endif

                t._forumsProviders[i] = provider;
            }

            // Create our DataSource for the forums
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Creating datasource for NNTP server");
            t._forumsDataSource = new ForumDataSource(t._forumsProviders, domainName);
            t._forumsDataSource.UsePlainTextConverter      = UserSettings.Default.UsePlainTextConverter;
            t._forumsDataSource.AutoLineWrap               = UserSettings.Default.AutoLineWrap;
            t._forumsDataSource.HeaderEncoding             = UserSettings.Default.EncodingForClientEncoding;
            t._forumsDataSource.InMimeUseHtml              = (UserSettings.Default.InMimeUse == UserSettings.MimeContentType.TextHtml);
            t._forumsDataSource.UserDefinedTags            = UserSettings.Default.UserDefinedTags;
            t._forumsDataSource.ShowUserNamePostfix        = UserSettings.Default.ShowUserNamePostfix;
            t._forumsDataSource.PostsAreAlwaysFormatFlowed = UserSettings.Default.PostsAreAlwaysFormatFlowed;
            t._forumsDataSource.TabAsSpace          = UserSettings.Default.TabAsSpace;
            t._forumsDataSource.UseCodeColorizer    = UserSettings.Default.UseCodeColorizer;
            t._forumsDataSource.AddHistoryToArticle = UserSettings.Default.AddHistoryToArticle;

            // Now start the NNTP-Server
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Starting NNTP server");
            t._nntpServer = new NNTPServer.NntpServer(t._forumsDataSource, true);
            t._nntpServer.EncodingSend      = UserSettings.Default.EncodingForClientEncoding;
            t._nntpServer.ListGroupDisabled = UserSettings.Default.DisableLISTGROUP;
            //t._nntpServer.DetailedErrorResponse = detailedErrorResponse;
            string errorMessage;
            t._nntpServer.Start(port, 64, UserSettings.Default.BindToWorld, out errorMessage);
            if (errorMessage != null)
            {
                throw new ApplicationException(errorMessage);
            }
        }
        private static void StartBridgeInternal(MainWindow t, int port)
        {
            string authenticationTicket = null;

#if LIVECONNECT
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "LiveConnect: Try authentication");
            t.DoLogin();
            authenticationTicket = t.AccessToken;
            if (string.IsNullOrEmpty(authenticationTicket))
            {
                // Reset the RefreshToken, if the authentication has failed...
                UserSettings.Default.RefreshToken = string.Empty;
                Traces.Main_TraceEvent(TraceEventType.Error, 1, "Could not authenticate with LiveConnect!");
                throw new ApplicationException("Could not authenticate with LiveConnect!");
            }
            Traces.Main_TraceEvent(TraceEventType.Information, 1, "Authenticated: AccessToken: {0}", authenticationTicket);
#else
            // Authenticate with Live Id
            var authenticationData = new AuthenticationInformation();

            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "LiveId: Try authentication");
            try
            {
                PassportHelper.TryAuthenticate(ref authenticationData, UserSettings.Default.AuthenticationBlob);
            }
            catch
            {
                // Reset the auto login, if the authentication has failed...
                UserSettings.Default.AuthenticationBlob = string.Empty;
                throw;
            }
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "LiveId: UserName: {0}, Ticket: {1}", authenticationData.UserName, authenticationData.Ticket);

            if (authenticationData.Ticket == null)
            {
                // Reset the auto login, if the authentication has failed...
                UserSettings.Default.AuthenticationBlob = string.Empty;
                Traces.Main_TraceEvent(TraceEventType.Error, 1, "Could not authenticate with LiveId!");
                throw new ApplicationException("Could not authenticate with LiveId!");
            }
            authenticationTicket = authenticationData.Ticket;
#endif

            string baseUrl = System.Configuration.ConfigurationManager.AppSettings["BaseUrl"];

            var rest = new ForumsRestService.ServiceAccess("tZNt5SSBt1XPiWiueGaAQMnrV4QelLbm7eum1750GI4=", baseUrl);
            rest.AuthenticationTicket = authenticationTicket;
            //rest.GetForums();


            // Create the forums-ServiceProvider
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Create forums service provider");
            //var provider = new MicrosoftForumsServiceProvider(services[i]);
            //System.Diagnostics.Debug.WriteLine(string.Format("{0}: Uri: {1}", services[i], provider.Uri));

            // Assigne the authentication ticket
            //provider.AuthenticationTicket = authenticationTicket;
            //ApplyForumsDisabled(provider);

#if PASSPORT_HEADER_ANALYSIS
            // Register AuthError-Handler
            provider.AuthenticationError += t.provider_AuthenticationError;
#endif

            //t._forumsProviders[i] = provider;

            // Create our DataSource for the forums
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Creating datasource for NNTP server");
            t._forumsDataSource = new DataSourceMsdnRest(rest);
            t._forumsDataSource.UsePlainTextConverter      = UserSettings.Default.UsePlainTextConverter;
            t._forumsDataSource.AutoLineWrap               = UserSettings.Default.AutoLineWrap;
            t._forumsDataSource.HeaderEncoding             = UserSettings.Default.EncodingForClientEncoding;
            t._forumsDataSource.InMimeUseHtml              = (UserSettings.Default.InMimeUse == UserSettings.MimeContentType.TextHtml);
            t._forumsDataSource.PostsAreAlwaysFormatFlowed = UserSettings.Default.PostsAreAlwaysFormatFlowed;
            t._forumsDataSource.TabAsSpace       = UserSettings.Default.TabAsSpace;
            t._forumsDataSource.UseCodeColorizer = UserSettings.Default.UseCodeColorizer;
            t._forumsDataSource.ProgressData    += t._forumsDataSource_ProgressData;

            // Now start the NNTP-Server
            Traces.Main_TraceEvent(TraceEventType.Verbose, 1, "Starting NNTP server");
            t._nntpServer = new NNTPServer.NntpServer(t._forumsDataSource, true);
            t._nntpServer.EncodingSend      = UserSettings.Default.EncodingForClientEncoding;
            t._nntpServer.ListGroupDisabled = UserSettings.Default.DisableLISTGROUP;
            string errorMessage;
            t._nntpServer.Start(port, 64, UserSettings.Default.BindToWorld, out errorMessage);
            if (errorMessage != null)
            {
                throw new ApplicationException(errorMessage);
            }
        }
Esempio n. 6
0
        public void GetIsValidPassportTest()
        {
            var testData = new List <Tuple <List <string>, PassportPolicy, bool> >()
            {
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd",
                    "byr:1937 iyr:2017 cid:147 hgt:183cm"
                }, Day04.PassportPolicyPart01,
                    true),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884",
                    "hcl:#cfa07d byr:1929"
                }, Day04.PassportPolicyPart01,
                    false),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "hcl:#ae17e1 iyr:2013",
                    "eyr:2024",
                    "ecl:brn pid:760753108 byr:1931",
                    "hgt:179cm"
                }, Day04.PassportPolicyPart01,
                    true),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "hcl:#cfa07d eyr:2025 pid:166559648",
                    "iyr:2011 ecl:brn hgt:59in"
                }, Day04.PassportPolicyPart01,
                    false),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "eyr:1972 cid:100",
                    "hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926"
                }, Day04.PassportPolicyPart02,
                    false),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "iyr:2019",
                    "hcl:#602927 eyr:1967 hgt:170cm",
                    "ecl:grn pid:012533040 byr:1946"
                }, Day04.PassportPolicyPart02,
                    false),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "hcl:dab227 iyr:2012",
                    "ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277"
                }, Day04.PassportPolicyPart02,
                    false),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "hgt:59cm ecl:zzz",
                    "eyr:2038 hcl:74454a iyr:2023",
                    "pid:3556412378 byr:2007"
                }, Day04.PassportPolicyPart02,
                    false),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980",
                    "hcl:#623a2f"
                }, Day04.PassportPolicyPart02,
                    true),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "eyr:2029 ecl:blu cid:129 byr:1989",
                    "iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm"
                }, Day04.PassportPolicyPart02,
                    true),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "hcl:#888785",
                    "hgt:164cm byr:2001 iyr:2015 cid:88",
                    "pid:545766238 ecl:hzl",
                    "eyr:2022"
                }, Day04.PassportPolicyPart02,
                    true),
                new Tuple <List <string>, PassportPolicy, bool>(
                    new List <string>()
                {
                    "iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719"
                }, Day04.PassportPolicyPart02,
                    true),
            };

            foreach (var testExample in testData)
            {
                var passport = PassportHelper.ParsePassport(testExample.Item1);
                var actual   = PassportHelper.GetIsValidPassport(passport, testExample.Item2);
                Assert.Equal(testExample.Item3, actual);
            }
        }
Esempio n. 7
0
        public void GetNumberOfValidPassportsTest()
        {
            // Passport data is validated in batch files (your puzzle input).
            // Each passport is represented as a sequence of key:value pairs
            // separated by spaces or newlines. Passports are separated by
            // blank lines.
            var testData = new List <Tuple <List <string>, PassportPolicy, int> >()
            {
                new Tuple <List <string>, PassportPolicy, int>(
                    // The first passport is valid - all eight fields are
                    // present. The second passport is invalid - it is missing
                    // hgt (the Height field).
                    // The third passport is interesting; the only missing
                    // field is cid, so it looks like data from North Pole
                    // Credentials, not a passport at all! Surely, nobody would
                    // mind if you made the system temporarily ignore missing
                    // cid fields. Treat this "passport" as valid.
                    // The fourth passport is missing two fields, cid and byr.
                    // Missing cid is fine, but missing any other field is not,
                    // so this passport is invalid.
                    // According to the above rules, your improved system would
                    // report 2 valid passports.
                    new List <string>()
                {
                    "ecl:gry pid:860033327 eyr:2020 hcl:#fffffd",
                    "byr:1937 iyr:2017 cid:147 hgt:183cm",
                    "",
                    "iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884",
                    "hcl:#cfa07d byr:1929",
                    "",
                    "hcl:#ae17e1 iyr:2013",
                    "eyr:2024",
                    "ecl:brn pid:760753108 byr:1931",
                    "hgt:179cm",
                    "",
                    "hcl:#cfa07d eyr:2025 pid:166559648",
                    "iyr:2011 ecl:brn hgt:59in"
                }, Day04.PassportPolicyPart01,
                    2),
                new Tuple <List <string>, PassportPolicy, int>(
                    new List <string>()
                {
                    "eyr:1972 cid:100",
                    "hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926",
                    "",
                    "iyr:2019",
                    "hcl:#602927 eyr:1967 hgt:170cm",
                    "ecl:grn pid:012533040 byr:1946",
                    "",
                    "hcl:dab227 iyr:2012",
                    "ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277",
                    "",
                    "hgt:59cm ecl:zzz",
                    "eyr:2038 hcl:74454a iyr:2023",
                    "pid:3556412378 byr:2007"
                }, Day04.PassportPolicyPart02,
                    0),
                new Tuple <List <string>, PassportPolicy, int>(
                    new List <string>()
                {
                    "pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980",
                    "hcl:#623a2f",
                    "",
                    "eyr:2029 ecl:blu cid:129 byr:1989",
                    "iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm",
                    "",
                    "hcl:#888785",
                    "hgt:164cm byr:2001 iyr:2015 cid:88",
                    "pid:545766238 ecl:hzl",
                    "eyr:2022",
                    "",
                    "iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719"
                }, Day04.PassportPolicyPart02,
                    4),
            };

            foreach (var testExample in testData)
            {
                var passports = PassportHelper.ParseInputLines(testExample.Item1);
                var actual    = PassportHelper.GetNumberOfValidPassports(passports, testExample.Item2);
                Assert.Equal(testExample.Item3, actual);
            }
        }