Exemple #1
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)
Exemple #2
0
 def payload(self, payload):
   self.name = payload.__class__.__name__
   self.data = serialization.ToBytes(payload)
Exemple #3
0
 def testBytes(self):
     self.assertIs(
         serialization.FromBytes(bool, serialization.ToBytes(True)), True)
     self.assertIs(
         serialization.FromBytes(bool, serialization.ToBytes(False)), False)