예제 #1
0
 def testWireFormat(self):
   self.assertIs(
       serialization.FromWireFormat(bool, serialization.ToWireFormat(True)),
       True)
   self.assertIs(
       serialization.FromWireFormat(bool, serialization.ToWireFormat(False)),
       False)
예제 #2
0
    def testSerialization(self, sample=None):
        """Make sure the RDFValue instance can be serialized."""
        if sample is None:
            sample = self.GenerateSample()

        # Serializing to a string must produce a string.
        serialized = serialization.ToBytes(sample)
        self.assertIsInstance(serialized, bytes)

        # Ensure we can parse it again.

        rdfvalue_object = serialization.FromBytes(self.rdfvalue_class,
                                                  serialized)
        self.CheckRDFValue(rdfvalue_object, sample)

        # Serializing to data store must produce something the data store can
        # handle.
        serialized = serialization.ToWireFormat(sample)
        protobuf_type = serialization.GetProtobufType(type(sample))

        if protobuf_type == "bytes":
            self.assertIsInstance(serialized, bytes)
        elif protobuf_type == "string":
            self.assertIsInstance(serialized, Text)
        elif protobuf_type in ["unsigned_integer", "integer"]:
            self.assertIsInstance(serialized, int)
        else:
            self.fail("%s has no valid protobuf_type" % self.rdfvalue_class)

        # Ensure we can parse it again.
        rdfvalue_object = serialization.FromWireFormat(self.rdfvalue_class,
                                                       serialized)
        self.CheckRDFValue(rdfvalue_object, sample)