Ejemplo n.º 1
0
    def test_calculateHTId(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)

        for case in self.__cases:
            self.assertEqual(distributedHT._calculateHTId(
                case.hashValue), case.htId)

        self.__cleanup()
Ejemplo n.º 2
0
    def test_calculateCollision(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        self.__insert(distributedHT)

        expected_collision = 4 / 9.0
        self.assertEqual(distributedHT.calculateCollision(), expected_collision)

        self.__cleanup()
Ejemplo n.º 3
0
    def test_read(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        
        for case in self.__cases:
            distributedHT.insert(case.hashValue)        
            self.assertEqual(distributedHT.read(case.hashValue), case.counter)

        self.__cleanup()
Ejemplo n.º 4
0
    def test_calculateCollision(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        self.__insert(distributedHT)

        expected_collision = 4 / 9.0
        self.assertEqual(distributedHT.calculateCollision(),
                         expected_collision)

        self.__cleanup()
Ejemplo n.º 5
0
    def test_exists(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        self.__insert(distributedHT)

        for case in self.__cases:
            self.assertTrue(distributedHT.exists(case.hashValue))

        for case in self.__nonExistingCases:
            self.assertFalse(distributedHT.exists(case.hashValue))

        self.__cleanup()
Ejemplo n.º 6
0
    def test_countCollision(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        self.__insert(distributedHT)

        expectedHashCount = 2
        expectedValueCount = 4
        expectedTotalCount = 9
        hashCount, valueCount, totalCount = distributedHT.countCollision()
        self.assertEqual(hashCount, expectedHashCount)
        self.assertEqual(valueCount, expectedValueCount)
        self.assertEqual(totalCount, expectedTotalCount)

        self.__cleanup()
Ejemplo n.º 7
0
    def test_countCollision(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        self.__insert(distributedHT)

        expectedHashCount = 2
        expectedValueCount = 4
        expectedTotalCount = 9
        hashCount, valueCount, totalCount = distributedHT.countCollision()
        self.assertEqual(hashCount, expectedHashCount)
        self.assertEqual(valueCount, expectedValueCount)
        self.assertEqual(totalCount, expectedTotalCount)

        self.__cleanup()
Ejemplo n.º 8
0
    def test_read(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)

        for case in self.__cases:
            distributedHT.insert(case.hashValue)
            self.assertEqual(distributedHT.read(case.hashValue), case.counter)

        self.__cleanup()
Ejemplo n.º 9
0
    def test_calculateHTId(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)

        for case in self.__cases:
            self.assertEqual(distributedHT._calculateHTId(case.hashValue),
                             case.htId)

        self.__cleanup()
Ejemplo n.º 10
0
    def test_insert(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        self.__insert(distributedHT)

        for case in self.__cases:
            self.assertTrue(self.__ensureUniquness(case.hashValue, case.htId))

        self.__cleanup()
Ejemplo n.º 11
0
    def test_exists(self):
        distributedHT = LocalDiskDHT(8, 10, self.__path)
        self.__insert(distributedHT)

        for case in self.__cases:
            self.assertTrue(distributedHT.exists(case.hashValue))

        for case in self.__nonExistingCases:
            self.assertFalse(distributedHT.exists(case.hashValue))

        self.__cleanup()
Ejemplo n.º 12
0
def main(argv):
    global COMPONENT_LIMIT
    global start_time
    global counters

    start_time = time.time()

    inputPath = ""
    dhtType = "memory"
    hashSize = ""
    dataType = ""
    try:
        opts, args = getopt.getopt(
            argv, "hi:c:d:s:t:",
            ["help", "ipath=", "climit=", "dhttype", "hsize=", "dtype="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-i", "--ipath"):
            inputPath = arg
        elif opt in ("-c", "--climit"):
            COMPONENT_LIMIT = int(arg)
        elif opt in ("-d", "--dhttype"):
            dhtType = arg
        elif opt in ("-s", "--hsize"):
            hashSize = arg
        elif opt in ("-t", "--dtype"):
            dataType = arg
        else:
            usage()
            sys.exit(2)

    if inputPath == "":
        print "Missing input path."
        usage()
        sys.exit(2)

    if os.path.isfile(inputPath):
        print "Input path must be a directory."
        usage()
        sys.exit(2)

    if hashSize == "":
        print "Missing hash size."
        usage()
        sys.exit(2)

    if dataType == "":
        print "Missing data type."
        usage()
        sys.exit(2)

    if dhtType not in ("disk", "db", "memory"):
        print "DHT type must be either 'disk', 'db', or 'memory'."
        sys.exit(2)

    if dataType not in ("raw", "obfuscated"):
        print "Data type must be either 'raw' or 'obfuscated'."
        sys.exit(2)

    sizes = []
    for size in hashSize.split(","):
        if not size in sizeUsed:
            print "Unknown hash size: '" + size + "'."
            sys.exit(2)
        else:
            sizes.append(size)

    if dataType == "obfuscated" and len(sizes) > 1:
        print "When 'obfuscated' data type is used, only one size can be " \
            "processed at a time."
        sys.exit(2)

    # Prepare DHTs.
    if dhtType == "disk":
        if os.path.isdir(DHT_PATH):
            shutil.rmtree(DHT_PATH, ignore_errors=True)

    for i in range(0, COMPONENT_LIMIT):
        for size in sizes:
            if dhtType == "disk":
                dhts[size].append(
                    LocalDiskDHT(int(size), NUM_OF_HASHTABLES,
                                 DHT_PATH + "/" + size + "/" + str(i + 1)))
            elif dhtType == "db":
                dhts[size].append(
                    LocalDbDHT(int(size), NUM_OF_HASHTABLES,
                               DHT_PATH + "/" + size + "/" + str(i + 1)))
            elif dhtType == "memory":
                dhts[size].append(LocalMemoryDHT(int(size), NUM_OF_HASHTABLES))

    # Initialize all counters to 0.
    counters = [0] * COMPONENT_LIMIT

    print "Processing input files..."
    for i in range(0, COMPONENT_LIMIT):
        processFile(os.path.join(inputPath,
                                 str(i + 1) + "-component"), sizes, dataType,
                    i)

    outputResults(sizes)
    print("--- %s seconds ---" % (time.time() - start_time))

    # Cleaning up.
    print "Cleaning up..."
    if dhtType in ("db"):
        for size in sizes:
            for i in range(0, COMPONENT_LIMIT):
                dhts[size][i].close()
    if dhtType in ("disk", "db"):
        shutil.rmtree(DHT_PATH, ignore_errors=True)
    print("--- %s seconds ---" % (time.time() - start_time))
Ejemplo n.º 13
0
def main(argv):
    global COMPONENT_LIMIT
    global start_time
    global counters

    start_time = time.time()

    inputPath = ""
    outputType = "plot"
    plot = False
    dhtType = "memory"
    hashType = "ALL"
    try:
        opts, args = getopt.getopt(
            argv, "hi:o:pc:d:s:",
            ["help", "ipath=", "otype=", "plot", "climit=", "dtype", "htype="])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-i", "--ipath"):
            inputPath = arg
        elif opt in ("-o", "--otype"):
            outputType = arg
        elif opt in ("-p", "--plot"):
            plot = True
        elif opt in ("-c", "--climit"):
            COMPONENT_LIMIT = int(arg)
        elif opt in ("-d", "--dtype"):
            dhtType = arg
        elif opt in ("-s", "--htype"):
            hashType = arg
        else:
            usage()
            sys.exit(2)

    if inputPath == "":
        print "Missing input path."
        usage()
        sys.exit(2)

    if outputType not in ("text", "plot"):
        print "Output type must be either 'text' or 'plot'."
        usage()
        sys.exit(2)

    if not (hashType == "ALL" or hashType in hashUsed):
        print "Hash type must be one of the following '" + hashUsed + "'."
        usage()
        sys.exit(2)

    if outputType == "text" and plot == True:
        print "Output type 'plot' cannot be use with -p."
        usage()
        sys.exit(2)

    if plot == True and os.path.isfile(inputPath):
        print "If -p is used, <inputPath> must be a directory."
        usage()
        sys.exit(2)

    if dhtType not in ("disk", "db", "memory"):
        print "DHT type must be either 'disk' or 'memory'."
        usage()
        sys.exit(2)

    # Prepare DHTs.
    if dhtType == "disk":
        if os.path.isdir(DHT_PATH):
            shutil.rmtree(DHT_PATH, ignore_errors=True)

    for i in range(0, COMPONENT_LIMIT):
        if hashType == "ALL":
            for key in hashUsed:
                if dhtType == "disk":
                    dhts[key].append(
                        LocalDiskDHT(obfuscators[key].size(),
                                     NUM_OF_HASHTABLES,
                                     DHT_PATH + "/" + key + "/" + str(i + 1)))
                elif dhtType == "db":
                    dhts[key].append(
                        LocalDbDHT(obfuscators[key].size(), NUM_OF_HASHTABLES,
                                   DHT_PATH + "/" + key + "/" + str(i + 1)))
                elif dhtType == "memory":
                    dhts[key].append(
                        LocalMemoryDHT(obfuscators[key].size(),
                                       NUM_OF_HASHTABLES))
        else:
            if dhtType == "disk":
                dhts[hashType].append(
                    LocalDiskDHT(obfuscators[hashType].size(),
                                 NUM_OF_HASHTABLES,
                                 DHT_PATH + "/" + hashType + "/" + str(i + 1)))
            elif dhtType == "db":
                dhts[hashType].append(
                    LocalDbDHT(obfuscators[hashType].size(), NUM_OF_HASHTABLES,
                               DHT_PATH + "/" + hashType + "/" + str(i + 1)))
            elif dhtType == "memory":
                dhts[hashType].append(
                    LocalMemoryDHT(obfuscators[hashType].size(),
                                   NUM_OF_HASHTABLES))

    # Initialize all counters to 0.
    counters = [0] * COMPONENT_LIMIT

    print "Processing input files..."
    if plot == False:
        if os.path.isfile(inputPath):
            processFile(inputPath, hashType)
        else:
            for filePath in [
                    os.path.join(inputPath, f)
                    for f in sorted(os.listdir(inputPath))
                    if os.path.isfile(os.path.join(inputPath, f))
            ]:
                processFile(filePath, hashType)

        outputResults(outputType, hashType)
        print("--- %s seconds ---" % (time.time() - start_time))
    else:
        plotResultsFromFile(inputPath, hashType)
        print("--- %s seconds ---" % (time.time() - start_time))

    # Cleaning up.
    print "Cleaning up..."
    if dhtType in ("db"):
        for key in hashUsed:
            for i in range(0, COMPONENT_LIMIT):
                dhts[key][i].close()
    if dhtType in ("disk", "db"):
        shutil.rmtree(DHT_PATH, ignore_errors=True)
    print("--- %s seconds ---" % (time.time() - start_time))