Skip to content

Commit

Permalink
Add validation for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
gjackson12 committed Aug 22, 2018
1 parent 8ce7d43 commit 54922d6
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 17 deletions.
Empty file added avoiding_common_mistakes.md
Empty file.
91 changes: 80 additions & 11 deletions contracts/MediaGallery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ pragma solidity ^0.4.24;
// import Open Zeppelin contract for Owner function authorization
import "../node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol";

/** @title Media Gallery. */
contract MediaGallery is Ownable {

//Author: Graham Jackson
//Date: 27 August 2018
//Version: MediaGallery v1.0 Rinkeby
/*
This contract provides the ability to create a library of media assets
/// @title Media Gallery, upload media asset representations to Ethereum
/// @author Graham Jackson
//Date: 27 August 2018
//Version: MediaGallery v1.0 Rinkeby
/**
@dev This contract provides the ability to create a library of media assets
which are referenced via a hash and are meant to be stored/retrieved via
the inter-planetary file systme (IPFS).
*/
*/
contract MediaGallery is Ownable {

bool public isStopped; //State variabe used to stop/start the contract
uint public mediaCounter; //A count of the total media assets added to the contract
uint public maximumNameLength = 125; //The maximum allowed length of a media asset name
uint public maximumDescLength = 250; //The maximum allowed length of a media asset description
uint public maximumTagsLength = 150; //The maximum allowed length of a media asset tags
uint public hashLength = 46; //The valid length of an IPFS hash

//Each poster address consists of an array of MediaAsset structs
//Used to store media assets, including the metadata associated with it, and to retrieve media
Expand Down Expand Up @@ -64,7 +67,8 @@ contract MediaGallery is Ownable {
isStopped = false;
}

/** @dev Add mew media asset and trigger associted event.
/**
* @dev Add mew media asset and trigger associted event.
* @param _name Name of the media asset.
* @param _description Description of the media asset.
* @param _mediaHash Multi-haash of the media asset.
Expand All @@ -80,6 +84,10 @@ contract MediaGallery is Ownable {
string _mediaType,
string _extension
) public stoppedInEmergency {
require(validateName(_name), "Name is too long...");
require(validateDesc(_description), "Description is too long...");
require(validateTags(_tags), "Tags is too long...");
require(validateHashLength(_mediaHash), "Hash is too long...");
//Store media asset information in memory
MediaAsset memory currentMedia;
/*
Expand Down Expand Up @@ -119,7 +127,8 @@ contract MediaGallery is Ownable {
mediaCounter++;
}

/** @dev Retrieve unique identifiers for media assets for a particular address.
/**
* @dev Retrieve unique identifiers for media assets for a particular address.
* @param _user Address to retrieve media identifiers for.
* @return mediaAssetIds An array of media identifiers for provided address.
*/
Expand All @@ -137,4 +146,64 @@ contract MediaGallery is Ownable {
}
return mediaAssetIds;
}

/**
* @dev Validate length of the name for a media asset.
* @param _name Proposed name for a media asset.
* @return A boolean value.
*/
function validateName(string _name) private view returns(bool) {
bytes memory nameBytes = bytes(_name);
uint lengthBytes = nameBytes.length;
if (lengthBytes > maximumNameLength) {
return false;
}

return true;
}

/**
* @dev Validate length of the description for a media asset.
* @param _description Proposed description for a media asset.
* @return A boolean value.
*/
function validateDesc(string _description) private view returns(bool) {
bytes memory descBytes = bytes(_description);
uint lengthBytes = descBytes.length;
if (lengthBytes > maximumDescLength) {
return false;
}

return true;
}

/**
* @dev Validate length of the tags for a media asset.
* @param _tags Proposed tags for a media asset.
* @return A boolean value.
*/
function validateTags(string _tags) private view returns(bool) {
bytes memory tagsBytes = bytes(_tags);
uint lengthTags = tagsBytes.length;
if (lengthTags > maximumTagsLength) {
return false;
}

return true;
}

/**
* @dev Validate length of the IPFS hash for a media asset.
* @param _hash Proposed hash for a media asset.
* @return A boolean value.
*/
function validateHashLength(string _hash) private view returns(bool) {
bytes memory hashBytes = bytes(_hash);
uint lengthHash = hashBytes.length;
if (lengthHash > hashLength) {
return false;
}

return true;
}
}
Empty file added design_pattern_decisions.md
Empty file.
96 changes: 90 additions & 6 deletions test/MediaGalleryExceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ contract('MediaGallery', (accounts) => {
var mediaTags1 = "tag1,,;tag2,,;tag3";
var mediaType1 = "image";
var mediaExtension1 = "png";
var owner = accounts[0];
var invalidName = "30rJKcVF2e15Bbo24Qp0JAmOA9zMjg6I76m3NhgCeqwORnBodGHMGtGK" +
"KtoRFzqly7gWYRLjKkv037sp3X0lBKu5m2D78FnSTN9Rh6IqhMnG5uMqr0JbWbeaDmv6P6s9qa36a4wh4q3L4qQ6LOAutKI";
var invalidDesc = "37d5ouTdD3v8Hzwg71Z5nTtGkwrvgrEAQTFLNiBTMy1sHuEMoKAxdY667a9sXCXQpVxjnTtwbeZ0k" +
"VKtOPZXkhnvDZimaINnJD38qX1F0PkUMXUPm1WMHtxWLdeVh2JClEPyFJkeLkp2V" +
"Q0ImeWBiAtFuHPIPKM50SV5xhbyLIzOzXrcXeXiEUc45RAueM7GnyeinwYbnuzyEPh5Najsog7" +
"4K4lSOYYIExkTwWdXyCq3Y6spPy7iCAKlWq2";
var invalidTags = "NViPFmcshbGAnPlyj6vD7DkeKY4VLutq0SNY8zkofOuvZl662mDcW8dPsGez9rLcmCNpx5AnO8niXBgy" +
"zXJGOenfbToAdG3RWxV26mMRBoaMGNtWFW1p9pDPEiQYW6CQylDLr5qFvYDZFQUWs1QNZkS";
var invalidHash = "Uusz8nU1MY0rbsLadys5Ug5xIoU0fsa4UEThaYFRxEpCczx";

it("should throw an exception if a user don't have any media yet", () => {
return MediaGallery.deployed().then((instance) => {
Expand All @@ -18,7 +26,7 @@ contract('MediaGallery', (accounts) => {
return instance.getMediaByAddress(poster);
}).then(assert.fail)
.catch((error) => {
assert(true);
assert.include(error.message, "revert", "The error message should contain 'revert'");
});
});

Expand All @@ -29,8 +37,8 @@ contract('MediaGallery', (accounts) => {
return mediaGalleryInstance.stopContract({from: poster});
}).then(assert.fail)
.catch((error) => {
assert(true);
})
assert.include(error.message, "revert", "The error message should contain 'revert'");
});
});

it("should throw an exception if someone other than the owner tries to re-enable the contract", () => {
Expand All @@ -40,7 +48,83 @@ contract('MediaGallery', (accounts) => {
return mediaGalleryInstance.resumeContract({from: poster});
}).then(assert.fail)
.catch((error) => {
assert(true);
})
assert.include(error.message, "revert", "The error message should contain 'revert'");
});
});

it("should throw an exception if the media name submitted is too long", () => {
return MediaGallery.deployed().then((instance) => {
mediaGalleryInstance = instance;

return mediaGalleryInstance.addMedia(
invalidName,
mediaDescription1,
mediaHash1,
mediaTags1,
mediaType1,
mediaExtension1,
{from: poster}
);
}).then(assert.fail)
.catch((error) => {
assert.include(error.message, "revert", "The error message should contain 'revert'");
});
});

it("should throw an exception if the media description submitted is too long", () => {
return MediaGallery.deployed().then((instance) => {
mediaGalleryInstance = instance;

return mediaGalleryInstance.addMedia(
mediaName1,
invalidDesc,
mediaHash1,
mediaTags1,
mediaType1,
mediaExtension1,
{from: poster}
);
}).then(assert.fail)
.catch((error) => {
assert.include(error.message, "revert", "The error message should contain 'revert'");
});
});

it("should throw an exception if the media tags submitted is too long", () => {
return MediaGallery.deployed().then((instance) => {
mediaGalleryInstance = instance;

return mediaGalleryInstance.addMedia(
mediaName1,
mediaDescription1,
mediaHash1,
invalidTags,
mediaType1,
mediaExtension1,
{from: poster}
);
}).then(assert.fail)
.catch((error) => {
assert.include(error.message, "revert", "The error message should contain 'revert'");
});
});

it("should throw an exception if the media hash submitted is not a valid length", () => {
return MediaGallery.deployed().then((instance) => {
mediaGalleryInstance = instance;

return mediaGalleryInstance.addMedia(
mediaName1,
mediaDescription1,
invalidHash,
mediaTags1,
mediaType1,
mediaExtension1,
{from: poster}
);
}).then(assert.fail)
.catch((error) => {
assert.include(error.message, "revert", "The error message should contain 'revert'");
});
});
});

0 comments on commit 54922d6

Please sign in to comment.