I have a contract like this: PublishService.sol, the strings.sol is taken from here.
pragma solidity ^0.4.0;
import "./strings.sol";
contract PublishService
{
using strings for *;
struct ServiceListStruct {
string _cloudID;
address _gatewayID;
string _serviceProducerID;
string _serviceProducerMetadata;
string _serviceConsumerMetaAttr;
}
ServiceListStruct[] BCServiceList;
function countEntries() public returns (uint)
{
return BCServiceList.length;
}
function addEntry(string cloudID, address gatewayID, string spID, string spMetadata, string scMetaAttr) public returns (uint)
{
require(msg.sender==gatewayID);
var sls = ServiceListStruct(cloudID, gatewayID, spID, spMetadata, scMetaAttr);
BCServiceList.push(sls);
return 1;
}
function deleteEntry(string cloudID, address gatewayID, string spID, string spMetadata, string scMetaAttr) public returns (uint)
{
require(msg.sender==gatewayID);
int pos = -1;
for(uint index = 0; index< BCServiceList.length; index++)
{
if(compareStrings(cloudID, BCServiceList[index]._cloudID))
{
if(gatewayID == BCServiceList[index]._gatewayID)
{
if(compareStrings(spID, BCServiceList[index]._serviceProducerID))
{
if(compareStrings(spMetadata, BCServiceList[index]._serviceProducerMetadata))
{
if(compareStrings(scMetaAttr, BCServiceList[index]._serviceConsumerMetaAttr))
{
pos = int(index);
}
}
}
}
}
}
if(pos > -1)
{
for(uint i = uint(pos); i < BCServiceList.length -1 ; i++)
{
BCServiceList[i] = BCServiceList[i+1];
delete BCServiceList[BCServiceList.length-1];
BCServiceList.length--;
}
return 1;
}
else
return 0;
}
function compareStrings(string s1, string s2) internal returns(bool)
{
var slice1 = s1.toSlice();
var slice2 = s2.toSlice();
var result = slice1.compare(slice2);
if(result == 0)
return true;
else
return false;
}
}
And I tried to test it using Remix IDE and it works as expected.
- tried to add a service from an account other than the one deployed the contract and deleted the service entry using the same account and it worked.
- tried to add service from one account and deleted from other account and the require statement failed as expected
But, when I try to test it using truffle, it fails. The test file is as follows:
const PublishService = artifacts.require('PublishService');
contract('PublishService', function(accounts) {
it("addEntry function call from a valid account", function() {
return PublishService.new({from: accounts[0]}).then(instance => { return instance.addEntry.call("LC1","0xca6e598bac8319860c663926f98096fdb279e55e", "LC1SP1", "SP1MD", "SC_ATTR",{"from": accounts[1]}); }).then(result => {
assert.equal(result, 1, "[E] addEntry function test failed");
});
});
it("deleteEntry function call from a valid account", function() {
return PublishService.new({from: accounts[0]}).then(instance => { return instance.deleteEntry.call("LC1","0xca6e598bac8319860c663926f98096fdb279e55e", "LC1SP1", "SP1MD", "SC_ATTR",{"from": accounts[1]}); }).then(result => {
assert.equal(result, 1, "[E] deleteEntry function test failed");
});
});
});
The second parameter of the call function is the account1 id from the testrpc network Here is the result of it.
The addEntry function succeeds but the deleteEntry
failed. I can't figure out the reason. Any help is appreciated.