public void RemveKeyWithValidValidatorShouldPass()
        {
            string validator = "0x00000005476534";
            string pubKey    = "TREHXCAVSOYJULSDVGFDs=";
            JsonPublicKeySource pubKeySourceObj = new JsonPublicKeySource();
            string randFileName = JsonPublicKeySourceTests.RandomString(6) + ".json";

            pubKeySourceObj.LoadFromFile(randFileName, true);

            //Independently adding key for removal test
            pubKeySourceObj.AddKey(validator, pubKey);

            ConfigurationBuilder cb = new ConfigurationBuilder();

            string[]           args      = { "--validator", validator };
            IConfigurationRoot configObj = cb.AddCommandLine(args).Build();


            KeyManagement km = new KeyManagement(configObj, pubKeySourceObj);

            km.ProcessKeyCommand("remove");

            //check the keys should be removed
            var exception = Assert.Throws <KeyNotFoundException>(() => pubKeySourceObj.GetKeyForNode(validator));

            Assert.NotNull(exception);

            exception.Message.Should().Contain("Public key not available");

            //removing temp file
            File.Delete(randFileName);
        }
        public void AddKeyShouldPassWithValidArgs()
        {
            string validator = "0x0000000965";
            string pubKey    = "ASDFASdFFDJDY4356SDFGFDs=";

            JsonPublicKeySource pubKeySourceObj = new JsonPublicKeySource();
            string randFileName = JsonPublicKeySourceTests.RandomString(6) + ".json";

            pubKeySourceObj.LoadFromFile(randFileName, true);

            ConfigurationBuilder cb = new ConfigurationBuilder();

            string[]           args      = { "--validator", validator, "--publickey", pubKey };
            IConfigurationRoot configObj = cb.AddCommandLine(args).Build();

            KeyManagement km = new KeyManagement(configObj, pubKeySourceObj);

            km.ProcessKeyCommand("add");

            //Now check file independently that key Registration was success

            JsonPublicKeySource obj = new JsonPublicKeySource();

            obj.LoadFromFile(randFileName, true);

            string key = obj.GetKeyForNode(validator);

            Assert.Equal(key, pubKey);

            //removing temp file
            File.Delete(randFileName);
        }
        public void RemveKeyWithInvalidValidatorShouldFail()
        {
            string validator = "0x00000005476534";
            string pubKey    = "TREHXCAVSOYJULSDVGFDs=";
            JsonPublicKeySource pubKeySourceObj = new JsonPublicKeySource();
            string randFileName = JsonPublicKeySourceTests.RandomString(6) + ".json";

            pubKeySourceObj.LoadFromFile(randFileName, true);

            //Independently adding key for removal test
            pubKeySourceObj.AddKey(validator, pubKey);

            ConfigurationBuilder cb = new ConfigurationBuilder();

            string[]           args      = { "test", validator };
            IConfigurationRoot configObj = cb.AddCommandLine(args).Build();


            KeyManagement km = new KeyManagement(configObj, pubKeySourceObj);

            km.ProcessKeyCommand("remove");

            //check the keys should not be removed
            pubKeySourceObj.GetKeyForNode(validator).Should().Be(pubKey);

            //removing temp file
            File.Delete(randFileName);
        }
Ejemplo n.º 4
0
        public void SaveToFileNullPathShouldFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            var exceptionStf = Assert.Throws <Exception>(() => obj.SaveToFile());

            Assert.NotNull(exceptionStf);
            Assert.Contains("Not loaded from file.", exceptionStf.Message);
        }
Ejemplo n.º 5
0
        public void PublicKeySourceNullPathShouldFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            var exception = Assert.Throws <ArgumentNullException>(() => obj.LoadFromFile(""));

            Assert.NotNull(exception);

            Assert.Contains("path can't be null or empty", exception.Message);
        }
Ejemplo n.º 6
0
        public void PublicKeySourceNoFileShouldFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            var exception = Assert.Throws <ArgumentException>(() => obj.LoadFromFile("\\etc\\file.json"));

            Assert.NotNull(exception);

            Assert.Contains("No file at path: ", exception.Message);
        }
Ejemplo n.º 7
0
        public void CreateFileFileShouldPass()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            string randFileName = RandomString(6) + ".json";

            obj.LoadFromFile(randFileName, true);
            Assert.True(File.Exists(randFileName));

            //removing temp file
            File.Delete(randFileName);
        }
Ejemplo n.º 8
0
        public void LoadFromFileShouldPass()
        {
            string randFileName = RandomString(6) + ".json";

            File.WriteAllText(randFileName, "[ {\"nodeid\": \"node-3\",\"key\": \"BdrwegfSDFG=\"},{\"nodeid\": \"node-12\",\"key\": \"BgIA345nlikrwegfSDFG=\"}]");
            IPublickeySource obj = JsonPublicKeySource.FromFile(randFileName);

            string key = obj.GetKeyForNode("node-3");

            Assert.Equal("BdrwegfSDFG=", key);

            //removing temp file
            File.Delete(randFileName);
        }
Ejemplo n.º 9
0
        public void SaveToFileNoFileExistFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            string randFileName = RandomString(6) + ".json";

            obj.LoadFromFile(randFileName, true);
            File.Delete(randFileName);


            var exceptionStf = Assert.Throws <FileNotFoundException>(() => obj.SaveToFile());

            Assert.NotNull(exceptionStf);
            Assert.Contains("Source file no longer exists.", exceptionStf.Message);
        }
Ejemplo n.º 10
0
        public void ProgramKeyCommandShouldPass()
        {
            string validator = "0x0000000965";
            string pubKey    = "ASDFASdFFDJDY4356SDFGFDs=";

            string[] args = { "--keycmd", "add", "--validator", validator, "--publickey", pubKey };
            Program.Main(args);

            JsonPublicKeySource obj = new JsonPublicKeySource();

            obj.LoadFromFile("keyfile.json", true);

            string key = obj.GetKeyForNode(validator);

            Assert.Equal(key, pubKey);
        }
Ejemplo n.º 11
0
        public void ZeroKeysShouldFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            string randFileName = RandomString(6) + ".json";

            File.WriteAllText(randFileName, "[]");

            var exception = Assert.Throws <KeyLoadException>(() => obj.LoadFromFile(randFileName));

            Assert.NotNull(exception);

            Assert.Contains("JSON contains no keys", exception.Message);

            //removing temp file
            File.Delete(randFileName);
        }
Ejemplo n.º 12
0
        public void InvalidKeysShouldFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            string randFileName = RandomString(6) + ".json";

            File.WriteAllText(randFileName, "{nodewe=w1");

            var exception = Assert.Throws <KeyLoadException>(() => obj.LoadFromFile(randFileName));

            Assert.NotNull(exception);

            Assert.Contains("Unable to load keys from json", exception.Message);

            //removing temp file
            File.Delete(randFileName);
        }
Ejemplo n.º 13
0
        public void EmptyFileShouldFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            string randFileName = RandomString(6) + ".json";

            File.WriteAllText(randFileName, "");

            var exception = Assert.Throws <FileEmptyException>(() => obj.LoadFromFile(randFileName));

            Assert.NotNull(exception);

            Assert.Contains("is empty", exception.Message);

            //removing temp file
            File.Delete(randFileName);
        }
        public void InvalidArgsShouldFail(string command)
        {
            JsonPublicKeySource pubKeySourceObj = new JsonPublicKeySource();
            string randFileName = JsonPublicKeySourceTests.RandomString(6) + ".json";

            pubKeySourceObj.LoadFromFile(randFileName, true);

            KeyManagement km = new KeyManagement(null, null);

            km.ProcessKeyCommand(command);

            string fileData = File.ReadAllText(randFileName);

            //there is no change in pub key data file
            fileData.Should().Contain("[]");
            //removing temp file
            File.Delete(randFileName);
        }
Ejemplo n.º 15
0
        public void GetKeyForNodeShouldFailForKeyNotFound()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            string randFileName = RandomString(6) + ".json";

            File.WriteAllText(randFileName, "[ {\"nodeid\": \"node-3\",\"key\": \"BgIA345nlikrwegfSDFG=\"}]");

            obj.LoadFromFile(randFileName, true);

            var exception = Assert.Throws <KeyNotFoundException>(() => obj.GetKeyForNode("Node-12"));

            Assert.NotNull(exception);
            Assert.Contains("Public key not available", exception.Message);

            //removing temp file
            File.Delete(randFileName);
        }
        public void AddKeyShouldFailWithoutValidator()
        {
            JsonPublicKeySource pubKeySourceObj = new JsonPublicKeySource();
            string randFileName = JsonPublicKeySourceTests.RandomString(6) + ".json";

            pubKeySourceObj.LoadFromFile(randFileName, true);

            IConfigurationRoot configObj = new ConfigurationBuilder().Build();

            KeyManagement km = new KeyManagement(configObj, pubKeySourceObj);

            km.ProcessKeyCommand("add");
            string fileData = File.ReadAllText(randFileName);

            fileData.Should().Contain("[]");
            //removing temp file
            File.Delete(randFileName);
        }
Ejemplo n.º 17
0
        public void AddRemoveKeyShouldPassAndNonExistintKeyShouldFail()
        {
            JsonPublicKeySource obj = new JsonPublicKeySource();

            string randFileName = RandomString(6) + ".json";

            obj.LoadFromFile(randFileName, true);

            string nodeId = "0x00000000012";
            string pubKey = "SADDFAJIONJKASD234ASDASNK34234=";

            obj.AddKey(nodeId, pubKey);

            string key = obj.GetKeyForNode(nodeId);

            Assert.Equal(key, pubKey);

            string nodeId2 = "0x00000000003";
            string pubKey2 = "SASADDFAJIONJKASD234ASDASNK34234DDFAJIONJKASD234ASDASNK34234=";

            obj.AddKey(nodeId2, pubKey2);

            string key2 = obj.GetKeyForNode(nodeId2);

            Assert.Equal(key2, pubKey2);

            //now removing key
            obj.RemoveKey(nodeId2);

            //verifying that key is removed
            var exception = Assert.Throws <KeyNotFoundException>(() => obj.RemoveKey(nodeId2));

            Assert.NotNull(exception);
            Assert.Contains("Node key is not known", exception.Message);

            //removing temp file
            File.Delete(randFileName);
        }