def test5_marshalAndParseJSON(): """Test 5 creates a macaroon and tests the marshal and parse to JSON functions """ printTestDesc("marshalToJSON") id = "abc" key = "234324" location = "DC" M = mlib.CreateMacaroon(key, id, location) caveat_cid = "123" caveat_vid = "0" caveat_cl = "NYC" M.addCaveatHelper(caveat_cid , caveat_vid, caveat_cl) json_string = mlib.marshalToJSON(M) print(json_string) printTestDesc("parseToJSON") M_returned = mlib.parseFromJSON(json_string) #assert(M == M_returned) for key in M.__dict__.keys(): object_of_M = M.__dict__[key] object_of_M_returned = M_returned.__dict__[key] print(key, object_of_M) if(type(object_of_M) == type([])):#list of strings assert(set(object_of_M) == set(object_of_M_returned)) else:#string type assert(object_of_M_returned == object_of_M) #print(M_returned) printTestResult("test5_marshalAndParseJSON" , "SUCCESS")
def test3_addCaveatHelper(): """Test 3 creates a simple macaroon and tests the caveat helper function """ printTestDesc("addCaveatHelper") id = "abc" key = "234324" location = "DC" M = mlib.CreateMacaroon(key, id, location) global oldMacaroonCopy oldMacaroonCopy = mlib.parseFromJSON(mlib.marshalToJSON(M)) assert(M.sig == oldMacaroonCopy.sig) caveat_cid = "123" caveat_vid = "sdfd" caveat_cl = "NYC" ## test addCaveatHelper M.addCaveatHelper(caveat_cid , caveat_vid, caveat_cl) assert(M.sig != oldMacaroonCopy.sig) #### what to verify #### test if the caveat was properly added string_caveat = caveat_cid + ":" + caveat_vid + ":" + caveat_cl assert(M.caveats[-1] == string_caveat) #### test if the caveat signature "evolved" correctly new_sig = hmac.new(oldMacaroonCopy.sig, caveat_vid+caveat_cid , hashlib.sha256) assert(M.sig == new_sig.hexdigest()) printTestResult("addCaveatHelper" , "SUCCESS")
def mint_macaroon(arr): public_id = arr[0] private_key = arr[1] location = arr[2] #### use library to compute HMAC M = mlib.CreateMacaroon(private_key, public_id, location) #M.addFirstPartyCaveat("chunk E 100 ... 500") #M.addFirstPartyCaveat("op E read, write") #M.addFirstPartyCaveat("time < 5/1/13 3pm") return M
def test5_marshalAndParseJSON(): printTestDesc("marshalToJSON") id = "abc" key = "234324" location = "DC" M = mlib.CreateMacaroon(key, id, location) json_string = marshalToJSON(M) print(json_string) printTestDesc("parseToJSON") M_returned = parseFromJSON(json_string) print(M_returned)
def testVerifyOfFirstPartyCaveats(): K_TargetService1 = "this is the secret key " K_TargetService2 = "this is also the secret key " random_nonce = str(433242342) location = "Catonsville, 21228" caveat1 = "level of coolness == high" caveat2 = "champions == USA Women's Team" M = mlib.CreateMacaroon(K_TargetService1, random_nonce, location) M.addFirstPartyCaveat(caveat1) M.addFirstPartyCaveat(caveat2) receivedMacaroon = M # M2 = mlib.CreateMacaroon(K_TargetService1, random_nonce, location) # M2.addFirstPartyCaveat(caveat1) # M2.addFirstPartyCaveat(caveat2) assert (mlib.verify(receivedMacaroon, K_TargetService2) == False) assert (mlib.verify(receivedMacaroon, K_TargetService1) == True)
def test1_CreateMacaroon(): #### Input: data id = "abc" key = "234324" location = "DC" #### Output: compute hmac on the outside hmac_value = hmac.new(key, id, hashlib.sha256) hmac_value_digest = hmac_value.hexdigest() #### use library to compute HMAC M = mlib.CreateMacaroon(key, id, location) #### Assertion: Does the library's output equal the expected value #print(M.sig) #print(hmac_value_digest) printTestDesc("CreateMacaroon") assert (M.sig == hmac_value_digest) assert (M.id == id) printTestResult("CreateMacaroon", "SUCCESS")
def test4_addFirstPartyCaveat(): printTestDesc("addFirstPartyCaveat") id = "abc" key = "234324" location = "DC" M = mlib.CreateMacaroon(key, id, location) caveat_cid = "123" caveat_vid = "0" caveat_cl = "NYC" M.addCaveatHelper(caveat_cid, caveat_vid, caveat_cl) assert (M.sig != oldMacaroonCopy.sig) #### what to verify #### test if the caveat was properly added string_caveat = caveat_cid + ":" + caveat_vid + ":" + caveat_cl assert (M.caveats[-1] == string_caveat) #### test if the caveat signature "evolved" correctly new_sig = hmac.new(oldMacaroonCopy.sig, caveat_vid + caveat_cid, hashlib.sha256) assert (M.sig == new_sig.hexdigest()) printTestResult("addFirstPartyCaveat", "SUCCESS")
The name of the test being run testName : str The results of the test """ print("------------------------------------------ "+ testName + ":"+ string ) def test1_VerifyOfFirstPartyCaveats(): """Test 1 creates a macaroon and add first party caveats then tests the verify function """ K_TargetService1 = "this is the secret key " K_TargetService2 = "this is also the secret key " random_nonce = str(433242342) location = "Catonsville, 21228" caveat1 = "level of coolness == high" caveat2 = "champions == USA Women's Team" M = mlib.CreateMacaroon(K_TargetService1, random_nonce, location) M.addFirstPartyCaveat(caveat1) M.addFirstPartyCaveat(caveat2) receivedMacaroon = M # M2 = mlib.CreateMacaroon(K_TargetService1, random_nonce, location) # M2.addFirstPartyCaveat(caveat1) # M2.addFirstPartyCaveat(caveat2) assert(mlib.verify(receivedMacaroon, K_TargetService2 ) == False) assert(mlib.verify(receivedMacaroon, K_TargetService1 ) == True) def test2_CreateMacaroon(): """Test 2 creates a simple macaroon and tests its creation """ #### Input: data id = "abc" key = "234324"