def test_function(): """ This is an example unit test in pytest. It checks the add() function for correctness. """ assert add(2, 3) == 5 assert add(-1, 2) == 1
def test_add_element(self): n, A0, S = setup() x0 = secrets.randbelow(pow(2, 256)) x1 = secrets.randbelow(pow(2, 256)) # first addition A1 = add(A0, S, x0, n) nonce = S[x0] proof = prove_membership(A0, S, x0, n) self.assertEqual(len(S), 1) self.assertEqual(A0, proof) self.assertTrue(verify_membership(A1, x0, nonce, proof, n)) # second addition A2 = add(A1, S, x1, n) nonce = S[x1] proof = prove_membership(A0, S, x1, n) self.assertEqual(len(S), 2) self.assertEqual(A1, proof) self.assertTrue(verify_membership(A2, x1, nonce, proof, n)) # delete A1_new = delete(A0, A2, S, x0, n) proof = prove_membership(A0, S, x1, n) proof_none = prove_membership(A0, S, x0, n) self.assertEqual(len(S), 1) self.assertEqual(proof_none, None) self.assertTrue(verify_membership(A1_new, x1, nonce, proof, n))
def form_valid(self, form): url = form.cleaned_data["url"] print(f'[+] Adding URL: {url}') parser = form.cleaned_data["parser"] tag = form.cleaned_data["tag"] depth = 0 if form.cleaned_data["depth"] == "0" else 1 extractors = ','.join(form.cleaned_data["archive_methods"]) input_kwargs = { "urls": url, "tag": tag, "depth": depth, "parser": parser, "update_all": False, "out_dir": OUTPUT_DIR, } if extractors: input_kwargs.update({"extractors": extractors}) add_stdout = StringIO() with redirect_stdout(add_stdout): add(**input_kwargs) print(add_stdout.getvalue()) context = self.get_context_data() context.update({ "stdout": ansi_to_html(add_stdout.getvalue().strip()), "form": AddLinkForm() }) return render(template_name=self.template_name, request=self.request, context=context)
def test_add_value(self): """Tests the return value of the add function. """ self.assertEqual(main.add(2, 5), 7) self.assertEqual(main.add(52, 5), 57) self.assertEqual(main.add(3, 3), 6) self.assertEqual(main.add(-2, -5), -7)
def test_proof_of_exponent(self): # first, do regular accumulation n, A0, S = setup() x0 = secrets.randbelow(pow(2, 256)) x1 = secrets.randbelow(pow(2, 256)) A1 = add(A0, S, x0, n) A2 = add(A1, S, x1, n) Q, l_nonce, u = prove_membership_with_NIPoE(A0, S, x0, n, A2) is_valid = verify_exponentiation(Q, l_nonce, u, x0, S[x0], A2, n) self.assertTrue(is_valid)
def gen_testData(dire=main.pDir(), weeks=1): filename, ext = '', '.json' today = date.today() for num in range(weeks * 7): json = fj.gen_dict(today, [0,1,2,3,4,5,6], 1) for x in range(num): current = today + timedelta(days=x) formatted = fj.format_date(current) fj.update_status(json, formatted, fj.TAGS['p']) json_str = fj.dict_to_json(json) name = filename + str(num) + ext path = main.full_path_to(name, dire) main.add(json_str, path)
def test_add4(self): ''' CHECK 4: INT and no paramter value is passed ''' param1 = 5 result = add(param1) self.assertEqual(result, 5)
def test3_str1_str2(self): print("execeting test3") print(self.user) act=add("str1","str2") exp="str1str2" error = "test3_str1_str2 failed" self.assertTrue(act==exp,error)
def test1_add_10_20(self): print("execeting test1") print(self.user) act=add(10,20) exp=30 error = "test_add_10_20 failed" self.assertEqual(act,exp,error)
def test_if_positive_plus_zero_returns_positive(self): a = 2 b = 0 expected = 2 result = add(a, b) self.assertEqual(expected, result)
def test2_add_10_minus_20(self): print("execeting test2") print(self.user) act=add(10,-20) exp=-10 error = "test_add_10_minus_20 failed" self.assertEqual(act,exp,error)
def test4_str1_20(self): print("execeting test4") print(self.user) act=add("str1",20) exp=None error = "test4_str1_20 failed" self.assertTrue(act==exp,error)
def test_add_int(): a = 0 b = 100 for i in range(10): operand_one = random.randint(a, b) operand_two = random.randint(a, b) assert main.add(operand_one, operand_two) == operand_one + operand_two
def test_add(self): ''' CHECK 1: Default check with INT paramters ''' param1 = 5 param2 = 10 result = add(param1, param2) self.assertEqual(result, 15)
def test_add_long(self): element = add( 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 4) self.assertEqual( element, 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004 )
def test_add2(self): ''' CHECK2: STRING type and INT type ''' param1 = 'ABC' param2 = 5 result = add(param1, param2) self.assertIsInstance(result, TypeError)
def test_prove_non_membership(self): n, A0, S = setup() elements_list = create_list(3) A1 = add(A0, S, elements_list[0], n) A2 = add(A1, S, elements_list[1], n) A3 = add(A2, S, elements_list[2], n) proof = prove_non_membership(A0, S, elements_list[0], S[elements_list[0]], n) self.assertIsNone(proof) x = create_list(1)[0] prime, x_nonce = hash_to_prime(x) proof = prove_non_membership(A0, S, x, x_nonce, n) is_valid = verify_non_membership(A0, A3, proof[0], proof[1], x, x_nonce, n) self.assertTrue(is_valid)
def test_add3(self): ''' CHECK 3: NONE and INT parameter ''' param1 = None param2 = 5 result = add(param1, param2) self.assertIsInstance(result, TypeError)
def test_if_two_positive_intigers_return_positive_intiger(self): # given a = 2 b = 2 #then expected = a + b result = add(a, b) self.assertEqual(result, expected)
def test_add(self): # assign # act s = main.add(1, 2) # assert self.assertEqual(s, 3)
def test_shamir_trick_2(self): n, A0, S = setup() elements_list = create_list(2) A1 = add(A0, S, elements_list[0], n) A2 = add(A1, S, elements_list[1], n) prime0 = hash_to_prime(elements_list[0], nonce=S[elements_list[0]])[0] prime1 = hash_to_prime(elements_list[1], nonce=S[elements_list[1]])[0] proof0 = prove_membership(A0, S, elements_list[0], n) proof1 = prove_membership(A0, S, elements_list[1], n) agg_proof = shamir_trick(proof0, proof1, prime0, prime1, n) is_valid = pow(agg_proof, prime0 * prime1, n) == A2 self.assertTrue(is_valid)
def add_view(self, request): if not request.user.is_authenticated: return redirect(f'/admin/login/?next={request.path}') request.current_app = self.name context = { **self.each_context(request), 'title': 'Add URLs', } if request.method == 'GET': context['form'] = AddLinkForm() elif request.method == 'POST': form = AddLinkForm(request.POST) if form.is_valid(): url = form.cleaned_data["url"] print(f'[+] Adding URL: {url}') depth = 0 if form.cleaned_data["depth"] == "0" else 1 input_kwargs = { "urls": url, "depth": depth, "update_all": False, "out_dir": OUTPUT_DIR, } add_stdout = StringIO() with redirect_stdout(add_stdout): add(**input_kwargs) print(add_stdout.getvalue()) context.update({ "stdout": ansi_to_html(add_stdout.getvalue().strip()), "form": AddLinkForm() }) else: context["form"] = form return render(template_name='add.html', request=request, context=context)
def add_test(): """Unit test function""" var1 = 3 var2 = 3 result = main.add(var1, var2) if result == 6: verified = True else: verified = not True return verified
def form_valid(self, form): url = form.cleaned_data["url"] print(f'[+] Adding URL: {url}') depth = 0 if form.cleaned_data["depth"] == "0" else 1 input_kwargs = { "urls": url, "depth": depth, "update_all": False, "out_dir": OUTPUT_DIR, } add_stdout = StringIO() with redirect_stdout(add_stdout): add(**input_kwargs) print(add_stdout.getvalue()) context = self.get_context_data() context.update({ "stdout": ansi_to_html(add_stdout.getvalue().strip()), "form": AddLinkForm() }) return render(template_name=self.template_name, request=self.request, context=context)
def test_batch_proof_of_membership(self): n, A0, S = setup() elements_list = create_list(10) A = A0 for x in elements_list: A = add(A, S, x, n) A_final = A elements_to_prove_list = [elements_list[4], elements_list[7], elements_list[8]] A_intermediate = batch_prove_membership(A0, S, elements_to_prove_list, n=n) nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_to_prove_list)) is_valid = batch_verify_membership(A_final, elements_to_prove_list, nonces_list, A_intermediate, n) self.assertTrue(is_valid)
def test_batch_proof_of_membership_with_NIPoE(self): n, A0, S = setup() elements_list = create_list(10) A = A0 for x in elements_list: A = add(A, S, x, n) A_final = A elements_to_prove_list = [elements_list[4], elements_list[7], elements_list[8]] Q, l_nonce, u = batch_prove_membership_with_NIPoE(A0, S, elements_to_prove_list, n, A_final) nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_to_prove_list)) is_valid = batch_verify_membership_with_NIPoE(Q, l_nonce, u, elements_to_prove_list, nonces_list, A_final, n) self.assertTrue(is_valid)
def test_batch_delete(self): n, A0, S = setup() elements_list = create_list(5) A = A0 for i in range(len(elements_list)): A = add(A, S, elements_list[i], n) A_pre_delete = A elements_to_delete_list = [elements_list[0], elements_list[2], elements_list[4]] nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_to_delete_list)) proofs = list(map(lambda x: prove_membership(A0, S, x, n), elements_to_delete_list)) A_post_delete, nipoe = batch_delete_using_membership_proofs(A_pre_delete, S, elements_to_delete_list, proofs, n) is_valid = batch_verify_membership_with_NIPoE(nipoe[0], nipoe[1], A_post_delete, elements_to_delete_list, nonces_list, A_pre_delete, n) self.assertTrue(is_valid)
def test_non_membership_witness_update_on_add( total_utxo_set_size_for_accumulator): print("total_utxo_set_size_for_accumulator =", total_utxo_set_size_for_accumulator) print("--> initialize and fill up accumulator state") n, A0, S, order = setup() elements_for_accumulator = create_random_list( total_utxo_set_size_for_accumulator) tik = time.time() A, _ = batch_add(A0, S, elements_for_accumulator, n) tok = time.time() print("<-- Done.", tok - tik) test_element = random.randint(1, pow(2, 256)) if test_element in S.keys(): print( "test_element is included in accumulator, change it to other value!" ) print("--> generate non-membership witness for ", test_element) d, b = prove_non_membership(A0, S, test_element, 0, n) print("<-- Done.") print("--> prove non-membership without add") result = verify_non_membership(A0, A, d, b, test_element, 0, n) print("<-- Done.") print("verify result ", result) print("--> add random element and update non-membership witness for", test_element) element_add = random.randint(1, pow(2, 256)) A_add = add(A, S, element_add, n) d, b = acc_add_non_membership_witness_update(A, S, test_element, d, b, element_add, n) print("<-- Done.") print("--> prove non-membership after add", element_add) result = verify_non_membership(A0, A_add, d, b, test_element, 0, n) print("<-- Done.") acc_non_membership_update_result.append(result)
def test_add(): result = main.add(1, 2) assert result == 3
def test_add_succesfully(self, open_mock: MagicMock, domains: str): for domain in domains: main.add(domain) assert open_mock.return_value.__enter__.return_value.write.call_count == len( domains)
def test_add_if_variable_is_string(self): num = 'aaaå' result = add(num) self.assertIsInstance(result, ValueError)
def test_add(self): self.assertEqual(add(2, 3), 5)
def test_string_in_add(self): with self.assertRaises(TypeError): add('3', 3)
def test_add(self): self.assertEqual(add(2, 3), 5) self.assertEqual(add(1, -2), -1)
def test_add(self): self.assertEqual(main.add(3, 4), 7, 'addition fail')
def test_numbers_10_11(self): self.assertEqual(add(10, 11), 21)
def test_add(self): a = (4,1) b = (-2,2) expect = (2,3) got = main.add(a, b) self.assertEqual(expect, got)
def testAdd(self): self.assertEqual(4, main.add(1, 3))