org.hyperledger.fabric.sdk.security.CryptoSuite Java Examples

The following examples show how to use org.hyperledger.fabric.sdk.security.CryptoSuite. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChaincodeManager.java    From fabric-net-server with Apache License 2.0 6 votes vote down vote up
public ChaincodeManager(String username, FabricConfig fabricConfig)
		throws CryptoException, InvalidArgumentException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, IOException, TransactionException {
	this.config = fabricConfig;

	orderers = this.config.getOrderers();
	peers = this.config.getPeers();
	chaincode = this.config.getChaincode();

	client = HFClient.createNewInstance();
	log.debug("Create instance of HFClient");
	client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
	log.debug("Set Crypto Suite of HFClient");

	fabricOrg = getFabricOrg(username, config.openCATLS());
	channel = getChannel();
	chaincodeID = getChaincodeID();

	client.setUserContext(fabricOrg.getPeerAdmin());
}
 
Example #2
Source File: HFCAClient.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Create HFCAClient from a NetworkConfig.CAInfo
 *
 * @param caInfo      created from NetworkConfig.getOrganizationInfo("org_name").getCertificateAuthorities()
 * @param cryptoSuite the specific cryptosuite to use.
 * @return HFCAClient
 * @throws MalformedURLException
 * @throws InvalidArgumentException
 */

public static HFCAClient createNewInstance(NetworkConfig.CAInfo caInfo, CryptoSuite cryptoSuite) throws MalformedURLException, InvalidArgumentException {

    if (null == caInfo) {
        throw new InvalidArgumentException("The caInfo parameter can not be null.");
    }

    if (null == cryptoSuite) {
        throw new InvalidArgumentException("The cryptoSuite parameter can not be null.");
    }

    HFCAClient ret = new HFCAClient(caInfo.getCAName(), caInfo.getUrl(), caInfo.getProperties());
    ret.setCryptoSuite(cryptoSuite);
    return ret;
}
 
Example #3
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test //(expected = InvalidArgumentException.class)
@Ignore
public void testCryptoFactory() throws Exception {
    try {
        resetConfig();
        Assert.assertNotNull(Config.getConfig().getDefaultCryptoSuiteFactory());

        HFClient client = HFClient.createNewInstance();

        client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

        MockUser mockUser = TestUtils.getMockUser(USER_NAME, USER_MSP_ID);

        Enrollment mockEnrollment = TestUtils.getMockEnrollment(null, "mockCert");
        mockUser.setEnrollment(mockEnrollment);

        client.setUserContext(mockUser);
    } finally {
        System.getProperties().remove("org.hyperledger.fabric.sdk.crypto.default_crypto_suite_factory");

    }

}
 
Example #4
Source File: ChannelTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testProposalBuilderWithNoMetaInfDir() throws Exception {
    thrown.expect(java.lang.IllegalArgumentException.class);
    thrown.expectMessage(matchesRegex("The META-INF directory does not exist in.*src.test.fixture.meta-infs.test1.META-INF"));

    InstallProposalBuilder installProposalBuilder = InstallProposalBuilder.newBuilder();

    installProposalBuilder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    installProposalBuilder.chaincodePath("github.com/example_cc");
    installProposalBuilder.setChaincodeSource(new File(SAMPLE_GO_CC));
    installProposalBuilder.chaincodeName("example_cc.go");
    installProposalBuilder.chaincodeVersion("1");
    installProposalBuilder.setChaincodeMetaInfLocation(new File("src/test/fixture/meta-infs/test1/META-INF")); // points into which is not what's expected.

    Channel channel = hfclient.newChannel("testProposalBuilderWithNoMetaInfDir");
    User user = getMockUser("rick", "rickORG");
    TransactionContext transactionContext = new TransactionContext(channel, user, CryptoSuite.Factory.getCryptoSuite());

    installProposalBuilder.context(transactionContext);

    installProposalBuilder.build(); // Build it get the proposal. Then unpack it to see if it's what we epect.
}
 
Example #5
Source File: ChannelTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testProposalBuilderWithMetaInfExistsNOT() throws Exception {
    thrown.expect(java.lang.IllegalArgumentException.class);
    thrown.expectMessage(matchesRegex("Directory to find chaincode META-INF.*tmp.fdsjfksfj.fjksfjskd.fjskfjdsk.should never exist does not exist"));

    InstallProposalBuilder installProposalBuilder = InstallProposalBuilder.newBuilder();

    installProposalBuilder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    installProposalBuilder.chaincodePath("github.com/example_cc");
    installProposalBuilder.setChaincodeSource(new File(SAMPLE_GO_CC));
    installProposalBuilder.chaincodeName("example_cc.go");
    installProposalBuilder.chaincodeVersion("1");
    installProposalBuilder.setChaincodeMetaInfLocation(new File("/tmp/fdsjfksfj/fjksfjskd/fjskfjdsk/should never exist")); // points into which is not what's expected.

    Channel channel = hfclient.newChannel("testProposalBuilderWithMetaInfExistsNOT");
    User user = getMockUser("rick", "rickORG");
    TransactionContext transactionContext = new TransactionContext(channel, user, CryptoSuite.Factory.getCryptoSuite());

    installProposalBuilder.context(transactionContext);

    installProposalBuilder.build(); // Build it get the proposal. Then unpack it to see if it's what we epect.
}
 
Example #6
Source File: HFCAClientIT.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnrollUnknownClient() throws Exception {

    thrown.expect(EnrollmentException.class);
    thrown.expectMessage("Failed to enroll user");

    CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();

    // This client does not exist
    String clientName = "test CA client";

    HFCAClient clientWithName = HFCAClient.createNewInstance(clientName,
            testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG).getCALocation(),
            testConfig.getIntegrationTestsSampleOrg(TEST_WITH_INTEGRATION_ORG).getCAProperties());
    clientWithName.setCryptoSuite(cryptoSuite);

    clientWithName.enroll(admin.getName(), TEST_ADMIN_PW);
}
 
Example #7
Source File: TestHFClient.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
public static void setupClient(HFClient hfclient) throws Exception {

        File tempFile = File.createTempFile("teststore", "properties");
        tempFile.deleteOnExit();

        File sampleStoreFile = new File(System.getProperty("user.home") + "/test.properties");
        if (sampleStoreFile.exists()) { //For testing start fresh
            sampleStoreFile.delete();
        }
        final SampleStore sampleStore = new SampleStore(sampleStoreFile);

        //src/test/fixture/sdkintegration/e2e-2Orgs/channel/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/

        //SampleUser someTestUSER = sampleStore.getMember("someTestUSER", "someTestORG");
        SampleUser someTestUSER = sampleStore.getMember("someTestUSER", "someTestORG", "mspid",
                findFileSk("src/test/fixture/sdkintegration/e2e-2Orgs/" + testConfig.getFabricConfigGenVers() + "/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore"),
                new File("src/test/fixture/sdkintegration/e2e-2Orgs/" + testConfig.getFabricConfigGenVers() + "/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]"));
        someTestUSER.setMspId("testMSPID?");

        hfclient.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
        hfclient.setUserContext(someTestUSER);
    }
 
Example #8
Source File: NetworkConfigTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadFromConfigFileYaml() throws Exception {

    // Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
    File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.yaml");
    NetworkConfig config = NetworkConfig.fromYamlFile(f);
    //HFClient client = HFClient.loadFromConfig(f);
    assertNotNull(config);

    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

    Channel channel = client.loadChannelFromConfig("foo", config);
    assertNotNull(channel);
}
 
Example #9
Source File: NetworkConfigTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoadFromConfigFileJson() throws Exception {

    // Should be able to instantiate a new instance of "Client" with a valid path to the JSON configuration
    File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.json");
    NetworkConfig config = NetworkConfig.fromJsonFile(f);
    assertNotNull(config);

    //HFClient client = HFClient.loadFromConfig(f);
    //Assert.assertNotNull(client);

    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

    Channel channel = client.loadChannelFromConfig("mychannel", config);
    assertNotNull(channel);
    final Collection<String> peersOrganizationMSPIDs = channel.getPeersOrganizationMSPIDs();
    assertEquals(2, peersOrganizationMSPIDs.size());
    assertTrue(peersOrganizationMSPIDs.contains("Org2MSP"));
    assertTrue(peersOrganizationMSPIDs.contains("Org1MSP"));

}
 
Example #10
Source File: NetworkConfigTest.java    From fabric-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetChannelNotExists() throws Exception {

    thrown.expect(NetworkConfigurationException.class);
    thrown.expectMessage("Channel MissingChannel not found in configuration file. Found channel names: foo");

    // Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
    File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.yaml");
    NetworkConfig config = NetworkConfig.fromYamlFile(f);
    //HFClient client = HFClient.loadFromConfig(f);
    assertNotNull(config);

    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

    client.loadChannelFromConfig("MissingChannel", config);

}
 
Example #11
Source File: HFCAClientIT.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void init() throws Exception {
    out("\n\n\nRUNNING: HFCAClientEnrollIT.\n");

    resetConfig();

    crypto = CryptoSuite.Factory.getCryptoSuite();
}
 
Example #12
Source File: HFCAClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnrollmentNoServerResponse() throws Exception {

    thrown.expect(EnrollmentException.class);
    thrown.expectMessage("Failed to enroll user admin");

    CryptoSuite cryptoSuite = CryptoSuite.Factory.getCryptoSuite();

    EnrollmentRequest req = new EnrollmentRequest("profile 1", "label 1", null);
    HFCAClient client = HFCAClient.createNewInstance("client", "http://localhost:99", null);
    client.setCryptoSuite(cryptoSuite);

    client.enroll(TEST_ADMIN_NAME, TEST_ADMIN_NAME, req);
}
 
Example #13
Source File: PrivateDataIT.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
public void runFabricTest(final SampleStore sampleStore) throws Exception {
    ////////////////////////////
    // Setup client

    //Create instance of client.
    HFClient client = HFClient.createNewInstance();

    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    client.setUserContext(sampleStore.getMember(TEST_ADMIN_NAME, "peerOrg2"));

    SampleOrg sampleOrg = testConfig.getIntegrationTestsSampleOrg("peerOrg2");

    Channel barChannel = sampleStore.getChannel(client, BAR_CHANNEL_NAME);

    barChannel.initialize();
    runChannel(client, barChannel, sampleOrg, 10);
    assertFalse(barChannel.isShutdown());
    assertTrue(barChannel.isInitialized());

    if (testConfig.isFabricVersionAtOrAfter("1.3")) {
        Set<String> expect = new HashSet<>(Arrays.asList("COLLECTION_FOR_A", "COLLECTION_FOR_B"));
        Set<String> got = new HashSet<>();

        CollectionConfigPackage queryCollectionsConfig = barChannel.queryCollectionsConfig(CHAIN_CODE_NAME, barChannel.getPeers().iterator().next(), sampleOrg.getPeerAdmin());
        for (CollectionConfigPackage.CollectionConfig collectionConfig : queryCollectionsConfig.getCollectionConfigs()) {
            got.add(collectionConfig.getName());

        }
        assertEquals(expect, got);
    }

    out("That's all folks!");
}
 
Example #14
Source File: RequestTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setupClient() throws Exception {
    hfclient = HFClient.createNewInstance();
    hfclient.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    hfclient.setUserContext(TestUtils.getMockUser("user", "mspId"));
    mockstream = new ByteArrayInputStream(new byte[0]);

}
 
Example #15
Source File: FabricUtils.java    From fabric-java-block with GNU General Public License v3.0 5 votes vote down vote up
public static Channel initChannel(HFClient client, BlockChainOrgUserDTO userDTO, BlockChainOrgDTO currentOrgDTO,
                                  BlockChainChannelDTO channelDTO, BaasRouteDTO routeDTO, List<BlockChainOrdererDTO> ordererList) throws Exception {
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    // 实例化用户需要先在控制台-证书管理中申请客户端证书,申请的企业名称需要与当前登录账户实名认证的企业名称相同
    FabricUser user = new FabricUser(userDTO.getOrgUserName(), new FabricEnrollment(userDTO.getOrgUserKey(), userDTO.getOrgUserCert()),
            currentOrgDTO.getMspId());

    client.setUserContext(user);

    Channel chain = client.newChannel(channelDTO.getChannelKey());

    for(BlockChainOrdererDTO ordererDTO: ordererList) {
        chain.addOrderer(client.newOrderer(ordererDTO.getOrdererKey(), routeDTO.getEndPoint(),
                FabricConfig.getOrderProperties(routeDTO.getSecretId(),routeDTO.getSecretKey(),ordererDTO.getOrdererKey())));
    }

    for(BlockChainOrgNodeDTO nodeDTO : currentOrgDTO.getNodeList()){
        chain.addPeer(client.newPeer(nodeDTO.getNodeKey(), routeDTO.getEndPoint(), FabricConfig.getPeerProperties(routeDTO.getSecretId(),
                routeDTO.getSecretKey(),nodeDTO.getNodeKey())),
                createPeerOptions().setPeerRoles(EnumSet.of(Peer.PeerRole.ENDORSING_PEER,
                        Peer.PeerRole.LEDGER_QUERY, Peer.PeerRole.CHAINCODE_QUERY, Peer.PeerRole.EVENT_SOURCE)));
        //registerEventsForFilteredBlocks()
    }


    chain.initialize();

    return chain;
}
 
Example #16
Source File: SampleUser.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
public SampleUser(String name, String org, SampleStore fs, CryptoSuite cryptoSuite) {
    this.name = name;
    this.cryptoSuite = cryptoSuite;

    this.keyValStore = fs;
    this.organization = org;
    this.keyValStoreName = toKeyValStoreName(this.name, org);
    String memberStr = keyValStore.getValue(keyValStoreName);
    if (null == memberStr) {
        saveState();
    } else {
        restoreState();
    }

}
 
Example #17
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testGoodMockUser() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));
    Orderer orderer = hfclient.newOrderer("justMockme", "grpc://localhost:99"); // test mock should work.
    Assert.assertNotNull(orderer);

}
 
Example #18
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = NullPointerException.class)
public void testBadUserContextNull() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    client.setUserContext(null);
}
 
Example #19
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = IllegalArgumentException.class)
public void testBadUserNameNull() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    MockUser mockUser = TestUtils.getMockUser(null, USER_MSP_ID);

    client.setUserContext(mockUser);
}
 
Example #20
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = IllegalArgumentException.class)
public void testBadUserNameEmpty() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    MockUser mockUser = TestUtils.getMockUser("", USER_MSP_ID);

    client.setUserContext(mockUser);
}
 
Example #21
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = IllegalArgumentException.class)
public void testBadUserMSPIDNull() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    MockUser mockUser = TestUtils.getMockUser(USER_NAME, null);

    client.setUserContext(mockUser);
}
 
Example #22
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = IllegalArgumentException.class)
public void testBadUserMSPIDEmpty() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    MockUser mockUser = TestUtils.getMockUser(USER_NAME, "");

    client.setUserContext(mockUser);
}
 
Example #23
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = IllegalArgumentException.class)
public void testBadEnrollmentNull() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    MockUser mockUser = TestUtils.getMockUser(USER_NAME, USER_MSP_ID);
    mockUser.setEnrollment(null);

    client.setUserContext(mockUser);
}
 
Example #24
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = IllegalArgumentException.class)
public void testBadEnrollmentBadCert() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    MockUser mockUser = TestUtils.getMockUser(USER_NAME, USER_MSP_ID);
    Enrollment mockEnrollment = TestUtils.getMockEnrollment(null);
    mockUser.setEnrollment(mockEnrollment);

    client.setUserContext(mockUser);
}
 
Example #25
Source File: ClientTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test (expected = IllegalArgumentException.class)
public void testBadEnrollmentBadKey() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());

    MockUser mockUser = TestUtils.getMockUser(USER_NAME, USER_MSP_ID);
    Enrollment mockEnrollment = TestUtils.getMockEnrollment(null, "mockCert");
    mockUser.setEnrollment(mockEnrollment);

    client.setUserContext(mockUser);
}
 
Example #26
Source File: ChannelTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testProposalBuilderWithMetaInf() throws Exception {
    InstallProposalBuilder installProposalBuilder = InstallProposalBuilder.newBuilder();

    installProposalBuilder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    installProposalBuilder.chaincodePath("github.com/example_cc");
    installProposalBuilder.setChaincodeSource(new File(SAMPLE_GO_CC));
    installProposalBuilder.chaincodeName("example_cc.go");
    installProposalBuilder.setChaincodeMetaInfLocation(new File("src/test/fixture/meta-infs/test1"));
    installProposalBuilder.chaincodeVersion("1");

    Channel channel = hfclient.newChannel("testProposalBuilderWithMetaInf");

    TestUtils.MockUser mockUser = getMockUser("rick", "rickORG");
    TransactionContext transactionContext = new TransactionContext(channel, mockUser, CryptoSuite.Factory.getCryptoSuite());

    installProposalBuilder.context(transactionContext);

    ProposalPackage.Proposal proposal = installProposalBuilder.build(); // Build it get the proposal. Then unpack it to see if it's what we expect.

    ProposalPackage.ChaincodeProposalPayload chaincodeProposalPayload = ProposalPackage.ChaincodeProposalPayload.parseFrom(proposal.getPayload());
    Chaincode.ChaincodeInvocationSpec chaincodeInvocationSpec = Chaincode.ChaincodeInvocationSpec.parseFrom(chaincodeProposalPayload.getInput());
    Chaincode.ChaincodeSpec chaincodeSpec = chaincodeInvocationSpec.getChaincodeSpec();
    Chaincode.ChaincodeInput input = chaincodeSpec.getInput();

    Chaincode.ChaincodeDeploymentSpec chaincodeDeploymentSpec = Chaincode.ChaincodeDeploymentSpec.parseFrom(input.getArgs(1));
    ByteString codePackage = chaincodeDeploymentSpec.getCodePackage();
    ArrayList tarBytesToEntryArrayList = tarBytesToEntryArrayList(codePackage.toByteArray());

    ArrayList<String> expect = new ArrayList<>();
    expect.add("META-INF/statedb/couchdb/indexes/MockFakeIndex.json");
    Files.walk(Paths.get(SAMPLE_GO_CC))
            .filter(Files::isRegularFile)
            .forEach((t) -> {
                String filePath = t.toString();
                expect.add(filePath.substring(filePath.lastIndexOf("src")));
            });

    assertArrayListEquals("Tar in Install Proposal's codePackage does not have expected entries. ", expect, tarBytesToEntryArrayList);
}
 
Example #27
Source File: ChannelTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testProposalBuilderWithOutMetaInf() throws Exception {
    InstallProposalBuilder installProposalBuilder = InstallProposalBuilder.newBuilder();

    installProposalBuilder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    installProposalBuilder.chaincodePath("github.com/example_cc");
    installProposalBuilder.setChaincodeSource(new File(SAMPLE_GO_CC));
    installProposalBuilder.chaincodeName("example_cc.go");
    installProposalBuilder.chaincodeVersion("1");

    Channel channel = hfclient.newChannel("testProposalBuilderWithOutMetaInf");
    User user = getMockUser("rick", "rickORG");
    TransactionContext transactionContext = new TransactionContext(channel, user, CryptoSuite.Factory.getCryptoSuite());

    installProposalBuilder.context(transactionContext);

    ProposalPackage.Proposal proposal = installProposalBuilder.build(); // Build it get the proposal. Then unpack it to see if it's what we expect.
    ProposalPackage.ChaincodeProposalPayload chaincodeProposalPayload = ProposalPackage.ChaincodeProposalPayload.parseFrom(proposal.getPayload());
    Chaincode.ChaincodeInvocationSpec chaincodeInvocationSpec = Chaincode.ChaincodeInvocationSpec.parseFrom(chaincodeProposalPayload.getInput());
    Chaincode.ChaincodeSpec chaincodeSpec = chaincodeInvocationSpec.getChaincodeSpec();
    Chaincode.ChaincodeInput input = chaincodeSpec.getInput();

    Chaincode.ChaincodeDeploymentSpec chaincodeDeploymentSpec = Chaincode.ChaincodeDeploymentSpec.parseFrom(input.getArgs(1));
    ByteString codePackage = chaincodeDeploymentSpec.getCodePackage();
    ArrayList tarBytesToEntryArrayList = tarBytesToEntryArrayList(codePackage.toByteArray());

    ArrayList<String> expect = new ArrayList<>();
    Files.walk(Paths.get(SAMPLE_GO_CC))
            .filter(Files::isRegularFile)
            .forEach((t) -> {
                String filePath = t.toString();
                expect.add(filePath.substring(filePath.lastIndexOf("src")));
            });

    assertArrayListEquals("Tar in Install Proposal's codePackage does not have expected entries. ", expect, tarBytesToEntryArrayList);
}
 
Example #28
Source File: ChannelTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testProposalBuilderWithMetaInfEmpty() throws Exception {
    thrown.expect(java.lang.IllegalArgumentException.class);
    thrown.expectMessage(matchesRegex("The META-INF directory.*src.test.fixture.meta-infs.emptyMetaInf.META-INF is empty\\."));

    File emptyINF = new File("src/test/fixture/meta-infs/emptyMetaInf/META-INF"); // make it cause git won't check in empty directory
    if (!emptyINF.exists()) {
        emptyINF.mkdirs();
        emptyINF.deleteOnExit();
    }

    InstallProposalBuilder installProposalBuilder = InstallProposalBuilder.newBuilder();

    installProposalBuilder.setChaincodeLanguage(TransactionRequest.Type.GO_LANG);
    installProposalBuilder.chaincodePath("github.com/example_cc");
    installProposalBuilder.setChaincodeSource(new File(SAMPLE_GO_CC));
    installProposalBuilder.chaincodeName("example_cc.go");
    installProposalBuilder.chaincodeVersion("1");
    installProposalBuilder.setChaincodeMetaInfLocation(new File("src/test/fixture/meta-infs/emptyMetaInf")); // points into which is not what's expected.

    Channel channel = hfclient.newChannel("testProposalBuilderWithMetaInfEmpty");
    User user = getMockUser("rick", "rickORG");
    TransactionContext transactionContext = new TransactionContext(channel, user, CryptoSuite.Factory.getCryptoSuite());

    installProposalBuilder.context(transactionContext);

    ProposalPackage.Proposal proposal = installProposalBuilder.build(); // Build it get the proposal. Then unpack it to see if it's what we epect.
}
 
Example #29
Source File: NetworkConfigTest.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadFromConfigFileYamlNOOverrides() throws Exception {

    // Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
    File f = new File("src/test/fixture/sdkintegration/network_configs/network-config.yaml");
    NetworkConfig config = NetworkConfig.fromYamlFile(f);

    //HFClient client = HFClient.loadFromConfig(f);
    assertNotNull(config);

    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    client.setUserContext(TestUtils.getMockUser(USER_NAME, USER_MSP_ID));

    Channel channel = client.loadChannelFromConfig("foo", config);
    assertNotNull(channel);

    assertTrue(!channel.getPeers().isEmpty());

    for (Peer peer : channel.getPeers()) {

        Properties properties = peer.getProperties();

        assertNotNull(properties);
        // check for default properties
        Object[] o = (Object[]) properties.get("grpc.NettyChannelBuilderOption.keepAliveTime");
        assertEquals(o[0], 2L);
        assertEquals(o[1], TimeUnit.MINUTES);

        o = (Object[]) properties.get("grpc.NettyChannelBuilderOption.keepAliveTimeout");
        assertEquals(o[0], 20L);
        assertEquals(o[1], TimeUnit.SECONDS);

        o = (Object[]) properties.get("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls");
        assertEquals(o[0], true);
    }

}
 
Example #30
Source File: TestUtils.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] sign(byte[] msg) throws CryptoException {
    try {
        return CryptoSuite.Factory.getCryptoSuite().sign(this.enrollment.getKey(), msg);
    } catch (Exception e) {
        throw new CryptoException(e.getMessage(), e);
    }
}