Example #1
0
    def test_constructor(self):
        certificate = CertificateV2()
        certificate.wireDecode(Blob(CERT, False))

        self.assertEqual(
          Name("/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"),
          certificate.getName())
        self.assertEqual(Name("/ndn/site1/KEY/ksk-1416425377094"),
          certificate.getKeyName())
        self.assertEqual(Name("/ndn/site1"), certificate.getIdentity())
        self.assertEqual(Name.Component("0123"), certificate.getIssuerId())
        self.assertEqual(Name.Component("ksk-1416425377094"),
          certificate.getKeyId())
        self.assertEqual(Name("/ndn/site1/KEY/ksk-2516425377094"),
          KeyLocator.getFromSignature(certificate.getSignature()).getKeyName())
        self.assertEqual(fromIsoString("20150814T223739"),
          certificate.getValidityPeriod().getNotBefore(), 0)
        self.assertEqual(fromIsoString("20150818T223738"),
          certificate.getValidityPeriod().getNotAfter(), 0)

        try:
          certificate.getPublicKey()
        except:
          self.fail("Error in getPublicKey");

        data = Data()
        data.wireDecode(Blob(CERT, False))
        certificate2 = CertificateV2(data)
        self.assertEqual(certificate.getName(), certificate2.getName())
        self.assertTrue(certificate.getPublicKey().equals(certificate2.getPublicKey()))
Example #2
0
    def test_setters(self):
        certificate = CertificateV2()
        certificate.setName(
          Name("/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"))
        certificate.getMetaInfo().setFreshnessPeriod(3600 * 1000.0)
        certificate.setContent(Blob(PUBLIC_KEY, False))
        certificate.setSignature(self.generateFakeSignature())

        self.assertEqual(
          Name("/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"),
          certificate.getName())
        self.assertEqual(Name("/ndn/site1/KEY/ksk-1416425377094"),
          certificate.getKeyName())
        self.assertEqual(Name("/ndn/site1"), certificate.getIdentity())
        self.assertEqual(Name.Component("0123"), certificate.getIssuerId())
        self.assertEqual(Name.Component("ksk-1416425377094"),
          certificate.getKeyId())
        self.assertEqual(Name("/ndn/site1/KEY/ksk-2516425377094"),
          KeyLocator.getFromSignature(certificate.getSignature()).getKeyName())
        self.assertEqual(fromIsoString("20141111T050000"),
          certificate.getValidityPeriod().getNotBefore(), 0)
        self.assertEqual(fromIsoString("20141111T060000"),
          certificate.getValidityPeriod().getNotAfter(), 0)

        try:
          certificate.getPublicKey()
        except:
          self.fail("Error in getPublicKey");
Example #3
0
    def test_validity_period_checking(self):
        certificate = CertificateV2()
        certificate.setName(
          Name("/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"))
        certificate.getMetaInfo().setFreshnessPeriod(3600 * 1000.0)
        certificate.setContent(PUBLIC_KEY)
        certificate.setSignature(self.generateFakeSignature())

        self.assertEqual(True,  certificate.isValid
          (fromIsoString("20141111T050000")))
        self.assertEqual(True,  certificate.isValid
          (fromIsoString("20141111T060000")))
        self.assertEqual(False, certificate.isValid
          (fromIsoString("20141111T045959")))
        self.assertEqual(False, certificate.isValid
          (fromIsoString("20141111T060001")))
Example #4
0
    def test_construction(self):
        # Construct with the right parameters.
        interval1 = Interval(fromIsoString("20150825T120000"),
                             fromIsoString("20150825T160000"))
        self.assertEqual(toIsoString(interval1.getStartTime()), "20150825T120000")
        self.assertEqual(toIsoString(interval1.getEndTime()), "20150825T160000")
        self.assertTrue(interval1.isValid())

        # Construct with the invalid interval.
        interval2 = Interval()
        self.assertTrue(not interval2.isValid())

        # Construct with the empty interval.
        interval3 = Interval(True)
        self.assertTrue(interval3.isValid())
        self.assertTrue(interval3.isEmpty())
Example #5
0
    def test_construction(self):
        # Construct with the right parameters.
        interval1 = Interval(fromIsoString("20150825T120000"),
                             fromIsoString("20150825T160000"))
        self.assertEqual(toIsoString(interval1.getStartTime()), "20150825T120000")
        self.assertEqual(toIsoString(interval1.getEndTime()), "20150825T160000")
        self.assertTrue(interval1.isValid())

        # Construct with the invalid interval.
        interval2 = Interval()
        self.assertTrue(not interval2.isValid())

        # Construct with the empty interval.
        interval3 = Interval(True)
        self.assertTrue(interval3.isValid())
        self.assertTrue(interval3.isEmpty())
Example #6
0
    def test_validity_period_checking(self):
        certificate = Certificate()
        certificate.setName(
          Name("/ndn/site1/ksk-1416425377094/KEY/0123/%FD%00%00%01I%C9%8B"))
        certificate.getMetaInfo().setFreshnessPeriod(3600 * 1000.0)
        certificate.setContent(PUBLIC_KEY)

        certificate.setSignature(Sha256WithRsaSignature())
        signatureInfo = certificate.getSignature()

        signatureInfo.getKeyLocator().setType(KeyLocatorType.KEYNAME)
        signatureInfo.getKeyLocator().setKeyName(
          Name("/ndn/site1/ksk-2516425377094/KEY"))

        notBefore = fromIsoString("20150819T120000")
        notAfter =  fromIsoString("20150823T120000")
        signatureInfo.getValidityPeriod().setPeriod(notBefore, notAfter)

        signatureInfo.setSignature(SIG_VALUE)

        self.assertEqual(False, certificate.isInValidityPeriod
          (fromIsoString("20150819T115959")))
        self.assertEqual(True,  certificate.isInValidityPeriod
          (fromIsoString("20150819T120000")))
        self.assertEqual(True,  certificate.isInValidityPeriod
          (fromIsoString("20150823T120000")))
        self.assertEqual(False, certificate.isInValidityPeriod
          (fromIsoString("20150823T120001")))
        self.assertEqual(False, certificate.isInValidityPeriod
          (fromIsoString("20150921T130000")))
Example #7
0
    def generateFakeSignature():
        signatureInfo = Sha256WithRsaSignature()

        keyLocatorName = Name("/ndn/site1/KEY/ksk-2516425377094")
        keyLocator = KeyLocator()
        keyLocator.setType(KeyLocatorType.KEYNAME)
        keyLocator.setKeyName(keyLocatorName)
        signatureInfo.setKeyLocator(keyLocator)

        period = ValidityPeriod()
        period.setPeriod(fromIsoString("20141111T050000"),
                         fromIsoString("20141111T060000"))
        signatureInfo.setValidityPeriod(period)

        block2 = Blob(SIG_VALUE, False)
        signatureInfo.setSignature(block2)

        return signatureInfo
Example #8
0
    def test_encode_and_decode(self):
        schedule = Schedule()

        interval1 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150828T000000"), 5, 10,
                                       2, RepetitiveInterval.RepeatUnit.DAY)
        interval2 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150828T000000"), 6, 8,
                                       1, RepetitiveInterval.RepeatUnit.DAY)
        interval3 = RepetitiveInterval(fromIsoString("20150827T000000"),
                                       fromIsoString("20150827T000000"), 7, 8)
        interval4 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150825T000000"), 4, 7)

        schedule.addWhiteInterval(interval1)
        schedule.addWhiteInterval(interval2)
        schedule.addWhiteInterval(interval4)
        schedule.addBlackInterval(interval3)

        encoding = schedule.wireEncode()
        encoding2 = Blob(SCHEDULE, False)
        self.assertTrue(encoding.equals(encoding2))

        schedule2 = Schedule()
        schedule2.wireDecode(encoding)

        # timePoint1 --> positive 8.25 4-10
        timePoint1 = fromIsoString("20150825T063000")
        result = schedule.getCoveringInterval(timePoint1)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T040000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150825T100000")

        # timePoint2 --> positive 8.26 6-8
        timePoint2 = fromIsoString("20150826T073000")
        result = schedule.getCoveringInterval(timePoint2)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150826T060000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150826T080000")
Example #9
0
    def test_encode_and_decode(self):
        schedule = Schedule()

        interval1 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150828T000000"), 5, 10, 2,
          RepetitiveInterval.RepeatUnit.DAY)
        interval2 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150828T000000"), 6, 8, 1,
          RepetitiveInterval.RepeatUnit.DAY)
        interval3 = RepetitiveInterval(
          fromIsoString("20150827T000000"),
          fromIsoString("20150827T000000"), 7, 8)
        interval4 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150825T000000"), 4, 7)

        schedule.addWhiteInterval(interval1)
        schedule.addWhiteInterval(interval2)
        schedule.addWhiteInterval(interval4)
        schedule.addBlackInterval(interval3)

        encoding = schedule.wireEncode()
        encoding2 = Blob(SCHEDULE, False)
        self.assertTrue(encoding.equals(encoding2))

        schedule2 = Schedule()
        schedule2.wireDecode(encoding)

        # timePoint1 --> positive 8.25 4-10
        timePoint1 = fromIsoString("20150825T063000")
        result = schedule.getCoveringInterval(timePoint1)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T040000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150825T100000")

        # timePoint2 --> positive 8.26 6-8
        timePoint2 = fromIsoString("20150826T073000")
        result = schedule.getCoveringInterval(timePoint2)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150826T060000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150826T080000")
Example #10
0
    def test_calculate_interval_without_white(self):
        schedule = Schedule()

        interval1 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150827T000000"), 5, 10,
                                       2, RepetitiveInterval.RepeatUnit.DAY)
        interval2 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150827T000000"), 6, 8,
                                       1, RepetitiveInterval.RepeatUnit.DAY)

        schedule.addBlackInterval(interval1)
        schedule.addBlackInterval(interval2)

        # timePoint1 --> negative 8.25 4-10
        timePoint1 = fromIsoString("20150825T063000")
        result = schedule.getCoveringInterval(timePoint1)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150825T100000")

        # timePoint2 --> negative 8.25 0-4
        timePoint2 = fromIsoString("20150825T013000")
        result = schedule.getCoveringInterval(timePoint2)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T000000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150826T000000")
Example #11
0
    def test_calculate_interval_without_white(self):
        schedule = Schedule()

        interval1 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150827T000000"), 5, 10, 2,
          RepetitiveInterval.RepeatUnit.DAY)
        interval2 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150827T000000"), 6, 8, 1,
          RepetitiveInterval.RepeatUnit.DAY)

        schedule.addBlackInterval(interval1)
        schedule.addBlackInterval(interval2)

        # timePoint1 --> negative 8.25 4-10
        timePoint1 = fromIsoString("20150825T063000")
        result = schedule.getCoveringInterval(timePoint1)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150825T100000")

        # timePoint2 --> negative 8.25 0-4
        timePoint2 = fromIsoString("20150825T013000")
        result = schedule.getCoveringInterval(timePoint2)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T000000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150826T000000")
Example #12
0
    def test_cover_time_point(self):
        interval = Interval(fromIsoString("20150825T120000"),
                            fromIsoString("20150825T160000"))

        timePoint1 = fromIsoString("20150825T120000")
        timePoint2 = fromIsoString("20150825T130000")
        timePoint3 = fromIsoString("20150825T170000")
        timePoint4 = fromIsoString("20150825T110000")

        self.assertTrue(interval.covers(timePoint1))
        self.assertTrue(interval.covers(timePoint2))
        self.assertTrue(not interval.covers(timePoint3))
        self.assertTrue(not interval.covers(timePoint4))
Example #13
0
    def test_cover_time_point(self):
        interval = Interval(fromIsoString("20150825T120000"),
                            fromIsoString("20150825T160000"))

        timePoint1 = fromIsoString("20150825T120000")
        timePoint2 = fromIsoString("20150825T130000")
        timePoint3 = fromIsoString("20150825T170000")
        timePoint4 = fromIsoString("20150825T110000")

        self.assertTrue(interval.covers(timePoint1))
        self.assertTrue(interval.covers(timePoint2))
        self.assertTrue(not interval.covers(timePoint3))
        self.assertTrue(not interval.covers(timePoint4))
Example #14
0
    def test_construction(self):
        repetitiveInterval1 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150825T000000"), 5, 10)
        self.assertEqual(toIsoString(repetitiveInterval1.getStartDate()), "20150825T000000")
        self.assertEqual(toIsoString(repetitiveInterval1.getEndDate()), "20150825T000000")
        self.assertEqual(repetitiveInterval1.getIntervalStartHour(), 5)
        self.assertEqual(repetitiveInterval1.getIntervalEndHour(), 10)

        repetitiveInterval2 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150827T000000"), 5, 10, 1,
          RepetitiveInterval.RepeatUnit.DAY)

        self.assertEqual(repetitiveInterval2.getNRepeats(), 1)
        self.assertEqual(
          repetitiveInterval2.getRepeatUnit(), RepetitiveInterval.RepeatUnit.DAY)

        repetitiveInterval3 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20151227T000000"), 5, 10, 2,
          RepetitiveInterval.RepeatUnit.MONTH)

        self.assertEqual(repetitiveInterval3.getNRepeats(), 2)
        self.assertEqual(
          repetitiveInterval3.getRepeatUnit(), RepetitiveInterval.RepeatUnit.MONTH)

        repetitiveInterval4 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20301227T000000"), 5, 10, 5,
          RepetitiveInterval.RepeatUnit.YEAR)

        self.assertEqual(repetitiveInterval4.getNRepeats(), 5)
        self.assertEqual(
          repetitiveInterval4.getRepeatUnit(), RepetitiveInterval.RepeatUnit.YEAR)

        repetitiveInterval5 = RepetitiveInterval()

        self.assertEqual(repetitiveInterval5.getNRepeats(), 0)
        self.assertEqual(
          repetitiveInterval5.getRepeatUnit(), RepetitiveInterval.RepeatUnit.NONE)
Example #15
0
    def test_construction(self):
        repetitiveInterval1 = RepetitiveInterval(
            fromIsoString("20150825T000000"), fromIsoString("20150825T000000"),
            5, 10)
        self.assertEqual(toIsoString(repetitiveInterval1.getStartDate()),
                         "20150825T000000")
        self.assertEqual(toIsoString(repetitiveInterval1.getEndDate()),
                         "20150825T000000")
        self.assertEqual(repetitiveInterval1.getIntervalStartHour(), 5)
        self.assertEqual(repetitiveInterval1.getIntervalEndHour(), 10)

        repetitiveInterval2 = RepetitiveInterval(
            fromIsoString("20150825T000000"), fromIsoString("20150827T000000"),
            5, 10, 1, RepetitiveInterval.RepeatUnit.DAY)

        self.assertEqual(repetitiveInterval2.getNRepeats(), 1)
        self.assertEqual(repetitiveInterval2.getRepeatUnit(),
                         RepetitiveInterval.RepeatUnit.DAY)

        repetitiveInterval3 = RepetitiveInterval(
            fromIsoString("20150825T000000"), fromIsoString("20151227T000000"),
            5, 10, 2, RepetitiveInterval.RepeatUnit.MONTH)

        self.assertEqual(repetitiveInterval3.getNRepeats(), 2)
        self.assertEqual(repetitiveInterval3.getRepeatUnit(),
                         RepetitiveInterval.RepeatUnit.MONTH)

        repetitiveInterval4 = RepetitiveInterval(
            fromIsoString("20150825T000000"), fromIsoString("20301227T000000"),
            5, 10, 5, RepetitiveInterval.RepeatUnit.YEAR)

        self.assertEqual(repetitiveInterval4.getNRepeats(), 5)
        self.assertEqual(repetitiveInterval4.getRepeatUnit(),
                         RepetitiveInterval.RepeatUnit.YEAR)

        repetitiveInterval5 = RepetitiveInterval()

        self.assertEqual(repetitiveInterval5.getNRepeats(), 0)
        self.assertEqual(repetitiveInterval5.getRepeatUnit(),
                         RepetitiveInterval.RepeatUnit.NONE)
Example #16
0
    def test_cover_time_point(self):
        ################################ With the repeat unit DAY.

        repetitiveInterval1 = RepetitiveInterval(
            fromIsoString("20150825T000000"), fromIsoString("20150925T000000"),
            5, 10, 2, RepetitiveInterval.RepeatUnit.DAY)

        timePoint1 = fromIsoString("20150825T050000")

        result = repetitiveInterval1.getInterval(timePoint1)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150825T100000")

        timePoint2 = fromIsoString("20150902T060000")

        result = repetitiveInterval1.getInterval(timePoint2)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150902T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150902T100000")

        timePoint3 = fromIsoString("20150929T040000")

        result = repetitiveInterval1.getInterval(timePoint3)
        self.assertEqual(result.isPositive, False)

        ################################ With the repeat unit MONTH.

        repetitiveInterval2 = RepetitiveInterval(
            fromIsoString("20150825T000000"), fromIsoString("20160825T000000"),
            5, 10, 2, RepetitiveInterval.RepeatUnit.MONTH)

        timePoint4 = fromIsoString("20150825T050000")

        result = repetitiveInterval2.getInterval(timePoint4)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150825T100000")

        timePoint5 = fromIsoString("20151025T060000")

        result = repetitiveInterval2.getInterval(timePoint5)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20151025T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20151025T100000")

        timePoint6 = fromIsoString("20151226T050000")

        result = repetitiveInterval2.getInterval(timePoint6)
        self.assertEqual(result.isPositive, False)

        timePoint7 = fromIsoString("20151225T040000")

        result = repetitiveInterval2.getInterval(timePoint7)
        self.assertEqual(result.isPositive, False)

        ################################ With the repeat unit YEAR.

        repetitiveInterval3 = RepetitiveInterval(
            fromIsoString("20150825T000000"), fromIsoString("20300825T000000"),
            5, 10, 3, RepetitiveInterval.RepeatUnit.YEAR)

        timePoint8 = fromIsoString("20150825T050000")

        result = repetitiveInterval3.getInterval(timePoint8)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150825T100000")

        timePoint9 = fromIsoString("20180825T060000")

        result = repetitiveInterval3.getInterval(timePoint9)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20180825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20180825T100000")

        timePoint10 = fromIsoString("20180826T050000")
        result = repetitiveInterval3.getInterval(timePoint10)
        self.assertEqual(result.isPositive, False)

        timePoint11 = fromIsoString("20210825T040000")
        result = repetitiveInterval3.getInterval(timePoint11)
        self.assertEqual(result.isPositive, False)

        timePoint12 = fromIsoString("20300825T040000")
        result = repetitiveInterval3.getInterval(timePoint12)
        self.assertEqual(result.isPositive, False)
Example #17
0
    def test_comparison(self):
        def check(small, big):
            return small.compare(big) < 0 and not (big.compare(small) < 0)

        self.assertTrue(
            check(
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 2,
                                   RepetitiveInterval.RepeatUnit.DAY),
                RepetitiveInterval(fromIsoString("20150826T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 2,
                                   RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(
            check(
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 2,
                                   RepetitiveInterval.RepeatUnit.DAY),
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 6, 10, 2,
                                   RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(
            check(
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 2,
                                   RepetitiveInterval.RepeatUnit.DAY),
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 11, 2,
                                   RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(
            check(
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 2,
                                   RepetitiveInterval.RepeatUnit.DAY),
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 3,
                                   RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(
            check(
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 2,
                                   RepetitiveInterval.RepeatUnit.DAY),
                RepetitiveInterval(fromIsoString("20150825T000000"),
                                   fromIsoString("20150828T000000"), 5, 10, 2,
                                   RepetitiveInterval.RepeatUnit.MONTH)))
Example #18
0
    def test_intersection_and_union(self):
        interval1 = Interval(fromIsoString("20150825T030000"),
                             fromIsoString("20150825T050000"))
        # No intersection.
        interval2 = Interval(fromIsoString("20150825T050000"),
                             fromIsoString("20150825T070000"))
        # No intersection.
        interval3 = Interval(fromIsoString("20150825T060000"),
                             fromIsoString("20150825T070000"))
        # There's an intersection.
        interval4 = Interval(fromIsoString("20150825T010000"),
                             fromIsoString("20150825T040000"))
        # Right in interval1, there's an intersection.
        interval5 = Interval(fromIsoString("20150825T030000"),
                             fromIsoString("20150825T040000"))
        # Wrap interval1, there's an intersection.
        interval6 = Interval(fromIsoString("20150825T010000"),
                             fromIsoString("20150825T050000"))
        # Empty interval.
        interval7 = Interval(True)

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval2)
        self.assertTrue(tempInterval.isEmpty())

        tempInterval = Interval(interval1)
        gotError = True
        try:
            tempInterval.unionWith(interval2)
            gotError = False
        except:
            pass
        if not gotError:
          self.fail("Expected error in unionWith(interval2)")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval3)
        self.assertTrue(tempInterval.isEmpty())

        tempInterval = Interval(interval1)
        gotError = True
        try:
          tempInterval.unionWith(interval3)
          gotError = False
        except:
            pass
        if not gotError:
          self.fail("Expected error in unionWith(interval3)")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval4)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()), "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()), "20150825T040000")

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval4)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()), "20150825T010000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()), "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval5)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()), "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()), "20150825T040000")

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval5)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()), "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()), "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval6)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()), "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()), "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval6)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()), "20150825T010000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()), "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval7)
        self.assertTrue(tempInterval.isEmpty())

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval7)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()), "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()), "20150825T050000")
Example #19
0
    def test_calculate_interval_with_black_and_white(self):
        schedule = Schedule()

        interval1 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150827T000000"), 5, 10, 2,
          RepetitiveInterval.RepeatUnit.DAY)
        interval2 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150827T000000"), 6, 8, 1,
          RepetitiveInterval.RepeatUnit.DAY)
        interval3 = RepetitiveInterval(
          fromIsoString("20150827T000000"),
          fromIsoString("20150827T000000"), 7, 8)
        interval4 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150825T000000"), 4, 7)

        schedule.addWhiteInterval(interval1)
        schedule.addWhiteInterval(interval2)
        schedule.addWhiteInterval(interval4)
        schedule.addBlackInterval(interval3)

        # timePoint1 --> positive 8.25 4-10
        timePoint1 = fromIsoString("20150825T063000")
        result = schedule.getCoveringInterval(timePoint1)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T040000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150825T100000")

        # timePoint2 --> positive 8.26 6-8
        timePoint2 = fromIsoString("20150826T073000")
        result = schedule.getCoveringInterval(timePoint2)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150826T060000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150826T080000")

        # timePoint3 --> positive 8.27 5-7
        timePoint3 = fromIsoString("20150827T053000")
        result = schedule.getCoveringInterval(timePoint3)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150827T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150827T070000")

        # timePoint4 --> positive 8.27 5-7
        timePoint4 = fromIsoString("20150827T063000")
        result = schedule.getCoveringInterval(timePoint4)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150827T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150827T070000")

        # timePoint5 --> negative 8.27 7-8
        timePoint5 = fromIsoString("20150827T073000")
        result = schedule.getCoveringInterval(timePoint5)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150827T070000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150827T080000")

        # timePoint6 --> negative 8.25 10-24
        timePoint6 = fromIsoString("20150825T113000")
        result = schedule.getCoveringInterval(timePoint6)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T100000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150826T000000")
Example #20
0
    def test_comparison(self):
        def check(small, big):
            return small.compare(big) < 0 and not (big.compare(small) < 0)

        self.assertTrue(check(RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 2, RepetitiveInterval.RepeatUnit.DAY),
                              RepetitiveInterval(fromIsoString("20150826T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 2, RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(check(RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 2, RepetitiveInterval.RepeatUnit.DAY),
                              RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             6, 10, 2, RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(check(RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 2, RepetitiveInterval.RepeatUnit.DAY),
                              RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 11, 2, RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(check(RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 2, RepetitiveInterval.RepeatUnit.DAY),
                              RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 3, RepetitiveInterval.RepeatUnit.DAY)))

        self.assertTrue(check(RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 2, RepetitiveInterval.RepeatUnit.DAY),
                              RepetitiveInterval(fromIsoString("20150825T000000"),
                                                 fromIsoString("20150828T000000"),
                                             5, 10, 2, RepetitiveInterval.RepeatUnit.MONTH)))
Example #21
0
    def test_cover_time_point(self):
        ################################ With the repeat unit DAY.

        repetitiveInterval1 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20150925T000000"), 5, 10, 2,
          RepetitiveInterval.RepeatUnit.DAY)

        timePoint1 = fromIsoString("20150825T050000")

        result = repetitiveInterval1.getInterval(timePoint1)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150825T100000")

        timePoint2 = fromIsoString("20150902T060000")

        result = repetitiveInterval1.getInterval(timePoint2)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150902T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150902T100000")

        timePoint3 = fromIsoString("20150929T040000")

        result = repetitiveInterval1.getInterval(timePoint3)
        self.assertEqual(result.isPositive, False)

        ################################ With the repeat unit MONTH.

        repetitiveInterval2 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20160825T000000"), 5, 10, 2,
          RepetitiveInterval.RepeatUnit.MONTH)

        timePoint4 = fromIsoString("20150825T050000")

        result = repetitiveInterval2.getInterval(timePoint4)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150825T100000")

        timePoint5 = fromIsoString("20151025T060000")

        result = repetitiveInterval2.getInterval(timePoint5)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20151025T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20151025T100000")

        timePoint6 = fromIsoString("20151226T050000")

        result = repetitiveInterval2.getInterval(timePoint6)
        self.assertEqual(result.isPositive, False)

        timePoint7 = fromIsoString("20151225T040000")

        result = repetitiveInterval2.getInterval(timePoint7)
        self.assertEqual(result.isPositive, False)

        ################################ With the repeat unit YEAR.

        repetitiveInterval3 = RepetitiveInterval(
          fromIsoString("20150825T000000"),
          fromIsoString("20300825T000000"), 5, 10, 3,
          RepetitiveInterval.RepeatUnit.YEAR)

        timePoint8 = fromIsoString("20150825T050000")

        result = repetitiveInterval3.getInterval(timePoint8)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20150825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20150825T100000")

        timePoint9 = fromIsoString("20180825T060000")

        result = repetitiveInterval3.getInterval(timePoint9)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()), "20180825T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()), "20180825T100000")

        timePoint10 = fromIsoString("20180826T050000")
        result = repetitiveInterval3.getInterval(timePoint10)
        self.assertEqual(result.isPositive, False)

        timePoint11 = fromIsoString("20210825T040000")
        result = repetitiveInterval3.getInterval(timePoint11)
        self.assertEqual(result.isPositive, False)

        timePoint12 = fromIsoString("20300825T040000")
        result = repetitiveInterval3.getInterval(timePoint12)
        self.assertEqual(result.isPositive, False)
Example #22
0
    def test_calculate_interval_without_black(self):
        schedule = Schedule()

        interval1 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150827T000000"), 5, 10,
                                       2, RepetitiveInterval.RepeatUnit.DAY)
        interval2 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150827T000000"), 6, 8,
                                       1, RepetitiveInterval.RepeatUnit.DAY)
        interval3 = RepetitiveInterval(fromIsoString("20150825T000000"),
                                       fromIsoString("20150825T000000"), 4, 7)

        schedule.addWhiteInterval(interval1)
        schedule.addWhiteInterval(interval2)
        schedule.addWhiteInterval(interval3)

        # timePoint1 --> positive 8.25 4-10
        timePoint1 = fromIsoString("20150825T063000")
        result = schedule.getCoveringInterval(timePoint1)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T040000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150825T100000")

        # timePoint2 --> positive 8.26 6-8
        timePoint2 = fromIsoString("20150826T073000")
        result = schedule.getCoveringInterval(timePoint2)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150826T060000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150826T080000")

        # timePoint3 --> positive 8.27 5-10
        timePoint3 = fromIsoString("20150827T053000")
        result = schedule.getCoveringInterval(timePoint3)
        self.assertEqual(result.isPositive, True)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150827T050000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150827T100000")

        # timePoint4 --> negative 8.25 10-24
        timePoint4 = fromIsoString("20150825T113000")
        result = schedule.getCoveringInterval(timePoint4)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T100000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150826T000000")

        # timePoint5 --> negative 8.25 0-4
        timePoint5 = fromIsoString("20150825T013000")
        result = schedule.getCoveringInterval(timePoint5)
        self.assertEqual(result.isPositive, False)
        self.assertEqual(result.interval.isEmpty(), False)
        self.assertEqual(toIsoString(result.interval.getStartTime()),
                         "20150825T000000")
        self.assertEqual(toIsoString(result.interval.getEndTime()),
                         "20150825T040000")
Example #23
0
    def test_intersection_and_union(self):
        interval1 = Interval(fromIsoString("20150825T030000"),
                             fromIsoString("20150825T050000"))
        # No intersection.
        interval2 = Interval(fromIsoString("20150825T050000"),
                             fromIsoString("20150825T070000"))
        # No intersection.
        interval3 = Interval(fromIsoString("20150825T060000"),
                             fromIsoString("20150825T070000"))
        # There's an intersection.
        interval4 = Interval(fromIsoString("20150825T010000"),
                             fromIsoString("20150825T040000"))
        # Right in interval1, there's an intersection.
        interval5 = Interval(fromIsoString("20150825T030000"),
                             fromIsoString("20150825T040000"))
        # Wrap interval1, there's an intersection.
        interval6 = Interval(fromIsoString("20150825T010000"),
                             fromIsoString("20150825T050000"))
        # Empty interval.
        interval7 = Interval(True)

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval2)
        self.assertTrue(tempInterval.isEmpty())

        tempInterval = Interval(interval1)
        gotError = True
        try:
            tempInterval.unionWith(interval2)
            gotError = False
        except:
            pass
        if not gotError:
            self.fail("Expected error in unionWith(interval2)")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval3)
        self.assertTrue(tempInterval.isEmpty())

        tempInterval = Interval(interval1)
        gotError = True
        try:
            tempInterval.unionWith(interval3)
            gotError = False
        except:
            pass
        if not gotError:
            self.fail("Expected error in unionWith(interval3)")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval4)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()),
                         "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()),
                         "20150825T040000")

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval4)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()),
                         "20150825T010000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()),
                         "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval5)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()),
                         "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()),
                         "20150825T040000")

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval5)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()),
                         "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()),
                         "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval6)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()),
                         "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()),
                         "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval6)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()),
                         "20150825T010000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()),
                         "20150825T050000")

        tempInterval = Interval(interval1)
        tempInterval.intersectWith(interval7)
        self.assertTrue(tempInterval.isEmpty())

        tempInterval = Interval(interval1)
        tempInterval.unionWith(interval7)
        self.assertTrue(not tempInterval.isEmpty())
        self.assertEqual(toIsoString(tempInterval.getStartTime()),
                         "20150825T030000")
        self.assertEqual(toIsoString(tempInterval.getEndTime()),
                         "20150825T050000")