Add testcase for Any type (#163)
diff --git a/tests/any_type/SConscript b/tests/any_type/SConscript
new file mode 100644
index 0000000..7e27703
--- /dev/null
+++ b/tests/any_type/SConscript
@@ -0,0 +1,23 @@
+# Test usage of Any type
+
+Import("env")
+
+incpath = env.Clone()
+incpath.Append(PROTOCPATH = '#any_type')
+incpath.Append(CPPPATH = '$BUILD/any_type')
+
+incpath.NanopbProto("anytest")
+incpath.NanopbProto(["google/protobuf/any", "google/protobuf/any.options"])
+incpath.NanopbProto("google/protobuf/duration")
+
+enc = incpath.Program(["encode_any.c", "anytest.pb.c", "google/protobuf/any.pb.c",
+                       "google/protobuf/duration.pb.c",
+                       "$COMMON/pb_encode.o", "$COMMON/pb_common.o"])
+
+dec = incpath.Program(["decode_any.c", "anytest.pb.c", "google/protobuf/any.pb.c",
+                       "google/protobuf/duration.pb.c",
+                       "$COMMON/pb_decode.o", "$COMMON/pb_common.o"])
+
+env.RunTest(enc)
+env.RunTest([dec, "encode_any.output"])
+
diff --git a/tests/any_type/anytest.proto b/tests/any_type/anytest.proto
new file mode 100644
index 0000000..0b86957
--- /dev/null
+++ b/tests/any_type/anytest.proto
@@ -0,0 +1,27 @@
+// This file is an example and test case on handling the Any type in nanopb.
+// The Any type is defined in Google-provided any.proto file:
+// message Any {
+//      string type_url = 1;
+//      bytes value = 2;
+// }
+//
+// The type_url field identifies the type of message, and the message data
+// is inside the bytes field.
+//
+// The encoding follows the basic format of protobuf and doesn't require
+// special support to handle. In this example, we just set maximum size for
+// the type_url and value fields, and then call pb_decode() again on the value.
+//
+// This does result in unnecessarily copying the data around, so for larger
+// values it is preferrable to use callbacks on the fields instead.
+
+syntax = "proto3";
+import "google/protobuf/any.proto";
+
+message BaseMessage {
+    int32 start = 1;
+    google.protobuf.Any details = 2;
+    int32 end = 3;
+}
+
+
diff --git a/tests/any_type/decode_any.c b/tests/any_type/decode_any.c
new file mode 100644
index 0000000..6aa4b22
--- /dev/null
+++ b/tests/any_type/decode_any.c
@@ -0,0 +1,49 @@
+#include <assert.h>
+#include <pb_decode.h>
+#include <string.h>
+#include <stdio.h>
+#include "test_helpers.h"
+#include "anytest.pb.h"
+#include "google/protobuf/duration.pb.h"
+
+int main()
+{
+    BaseMessage msg = BaseMessage_init_zero;
+    uint8_t buffer[256];
+    pb_istream_t stream;
+    size_t count;
+    bool status;
+    
+    /* Read the data into buffer */
+    SET_BINARY_MODE(stdin);
+    count = fread(buffer, 1, sizeof(buffer), stdin);
+    stream = pb_istream_from_buffer(buffer, count);
+    
+    /* Decode the base message */
+    if (!pb_decode(&stream, BaseMessage_fields, &msg))
+    {
+        printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
+        return 1;
+    }
+
+    assert(msg.start == 1234);
+    assert(msg.end == 5678);
+
+    /* Decode the Any message if we know the type */
+    if (strcmp(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration") == 0)
+    {
+        google_protobuf_Duration duration = google_protobuf_Duration_init_zero;
+        stream = pb_istream_from_buffer(msg.details.value.bytes, msg.details.value.size);
+        status = pb_decode(&stream, google_protobuf_Duration_fields, &duration);
+        assert(status);
+        assert(duration.seconds == 99999);
+        assert(duration.nanos = 100);
+        return 0;
+    }
+    else
+    {
+        fprintf(stderr, "Unknown Any type\n");
+        return 2;
+    }
+}
+
diff --git a/tests/any_type/encode_any.c b/tests/any_type/encode_any.c
new file mode 100644
index 0000000..dbd4e3d
--- /dev/null
+++ b/tests/any_type/encode_any.c
@@ -0,0 +1,46 @@
+#include <assert.h>
+#include <pb_encode.h>
+#include <string.h>
+#include <stdio.h>
+#include "test_helpers.h"
+#include "anytest.pb.h"
+#include "google/protobuf/duration.pb.h"
+
+int main()
+{
+    BaseMessage msg = BaseMessage_init_zero;
+    google_protobuf_Duration duration = google_protobuf_Duration_init_zero;
+    uint8_t buffer[256];
+    pb_ostream_t stream;
+    bool status;
+    
+    msg.start = 1234;
+    msg.end = 5678;
+
+    /* Fill in the Any message header */
+    msg.has_details = true;
+    strncpy(msg.details.type_url, "type.googleapis.com/google.protobuf.Duration", sizeof(msg.details.type_url));
+    
+    /* Encode a Duration message inside the Any message. */
+    duration.seconds = 99999;
+    duration.nanos = 100;
+    stream = pb_ostream_from_buffer(msg.details.value.bytes, sizeof(msg.details.value.bytes));
+    status = pb_encode(&stream, google_protobuf_Duration_fields, &duration);
+    assert(status);
+    msg.details.value.size = stream.bytes_written;
+    
+    /* Then encode the outer message */
+    stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
+    if (pb_encode(&stream, BaseMessage_fields, &msg))
+    {    
+        /* Write the result data to stdout */
+        SET_BINARY_MODE(stdout);
+        fwrite(buffer, 1, stream.bytes_written, stdout);
+        return 0;
+    }
+    else
+    {
+        fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
+        return 1;
+    }
+}
diff --git a/tests/any_type/google/protobuf/any.options b/tests/any_type/google/protobuf/any.options
new file mode 100644
index 0000000..c88410e
--- /dev/null
+++ b/tests/any_type/google/protobuf/any.options
@@ -0,0 +1,2 @@
+google.protobuf.Any.type_url max_size:64
+google.protobuf.Any.value max_size:64
diff --git a/tests/any_type/google/protobuf/any.proto b/tests/any_type/google/protobuf/any.proto
new file mode 100644
index 0000000..4932942
--- /dev/null
+++ b/tests/any_type/google/protobuf/any.proto
@@ -0,0 +1,154 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc.  All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto3";
+
+package google.protobuf;
+
+option csharp_namespace = "Google.Protobuf.WellKnownTypes";
+option go_package = "github.com/golang/protobuf/ptypes/any";
+option java_package = "com.google.protobuf";
+option java_outer_classname = "AnyProto";
+option java_multiple_files = true;
+option objc_class_prefix = "GPB";
+
+// `Any` contains an arbitrary serialized protocol buffer message along with a
+// URL that describes the type of the serialized message.
+//
+// Protobuf library provides support to pack/unpack Any values in the form
+// of utility functions or additional generated methods of the Any type.
+//
+// Example 1: Pack and unpack a message in C++.
+//
+//     Foo foo = ...;
+//     Any any;
+//     any.PackFrom(foo);
+//     ...
+//     if (any.UnpackTo(&foo)) {
+//       ...
+//     }
+//
+// Example 2: Pack and unpack a message in Java.
+//
+//     Foo foo = ...;
+//     Any any = Any.pack(foo);
+//     ...
+//     if (any.is(Foo.class)) {
+//       foo = any.unpack(Foo.class);
+//     }
+//
+//  Example 3: Pack and unpack a message in Python.
+//
+//     foo = Foo(...)
+//     any = Any()
+//     any.Pack(foo)
+//     ...
+//     if any.Is(Foo.DESCRIPTOR):
+//       any.Unpack(foo)
+//       ...
+//
+//  Example 4: Pack and unpack a message in Go
+//
+//      foo := &pb.Foo{...}
+//      any, err := ptypes.MarshalAny(foo)
+//      ...
+//      foo := &pb.Foo{}
+//      if err := ptypes.UnmarshalAny(any, foo); err != nil {
+//        ...
+//      }
+//
+// The pack methods provided by protobuf library will by default use
+// 'type.googleapis.com/full.type.name' as the type URL and the unpack
+// methods only use the fully qualified type name after the last '/'
+// in the type URL, for example "foo.bar.com/x/y.z" will yield type
+// name "y.z".
+//
+//
+// JSON
+// ====
+// The JSON representation of an `Any` value uses the regular
+// representation of the deserialized, embedded message, with an
+// additional field `@type` which contains the type URL. Example:
+//
+//     package google.profile;
+//     message Person {
+//       string first_name = 1;
+//       string last_name = 2;
+//     }
+//
+//     {
+//       "@type": "type.googleapis.com/google.profile.Person",
+//       "firstName": <string>,
+//       "lastName": <string>
+//     }
+//
+// If the embedded message type is well-known and has a custom JSON
+// representation, that representation will be embedded adding a field
+// `value` which holds the custom JSON in addition to the `@type`
+// field. Example (for message [google.protobuf.Duration][]):
+//
+//     {
+//       "@type": "type.googleapis.com/google.protobuf.Duration",
+//       "value": "1.212s"
+//     }
+//
+message Any {
+  // A URL/resource name that uniquely identifies the type of the serialized
+  // protocol buffer message. The last segment of the URL's path must represent
+  // the fully qualified name of the type (as in
+  // `path/google.protobuf.Duration`). The name should be in a canonical form
+  // (e.g., leading "." is not accepted).
+  //
+  // In practice, teams usually precompile into the binary all types that they
+  // expect it to use in the context of Any. However, for URLs which use the
+  // scheme `http`, `https`, or no scheme, one can optionally set up a type
+  // server that maps type URLs to message definitions as follows:
+  //
+  // * If no scheme is provided, `https` is assumed.
+  // * An HTTP GET on the URL must yield a [google.protobuf.Type][]
+  //   value in binary format, or produce an error.
+  // * Applications are allowed to cache lookup results based on the
+  //   URL, or have them precompiled into a binary to avoid any
+  //   lookup. Therefore, binary compatibility needs to be preserved
+  //   on changes to types. (Use versioned type names to manage
+  //   breaking changes.)
+  //
+  // Note: this functionality is not currently available in the official
+  // protobuf release, and it is not used for type URLs beginning with
+  // type.googleapis.com.
+  //
+  // Schemes other than `http`, `https` (or the empty scheme) might be
+  // used with implementation specific semantics.
+  //
+  string type_url = 1;
+
+  // Must be a valid serialized protocol buffer of the above specified type.
+  bytes value = 2;
+}
diff --git a/tests/any_type/google/protobuf/duration.proto b/tests/any_type/google/protobuf/duration.proto
new file mode 100644
index 0000000..975fce4
--- /dev/null
+++ b/tests/any_type/google/protobuf/duration.proto
@@ -0,0 +1,117 @@
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc.  All rights reserved.
+// https://developers.google.com/protocol-buffers/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto3";
+
+package google.protobuf;
+
+option csharp_namespace = "Google.Protobuf.WellKnownTypes";
+option cc_enable_arenas = true;
+option go_package = "github.com/golang/protobuf/ptypes/duration";
+option java_package = "com.google.protobuf";
+option java_outer_classname = "DurationProto";
+option java_multiple_files = true;
+option objc_class_prefix = "GPB";
+
+// A Duration represents a signed, fixed-length span of time represented
+// as a count of seconds and fractions of seconds at nanosecond
+// resolution. It is independent of any calendar and concepts like "day"
+// or "month". It is related to Timestamp in that the difference between
+// two Timestamp values is a Duration and it can be added or subtracted
+// from a Timestamp. Range is approximately +-10,000 years.
+//
+// # Examples
+//
+// Example 1: Compute Duration from two Timestamps in pseudo code.
+//
+//     Timestamp start = ...;
+//     Timestamp end = ...;
+//     Duration duration = ...;
+//
+//     duration.seconds = end.seconds - start.seconds;
+//     duration.nanos = end.nanos - start.nanos;
+//
+//     if (duration.seconds < 0 && duration.nanos > 0) {
+//       duration.seconds += 1;
+//       duration.nanos -= 1000000000;
+//     } else if (durations.seconds > 0 && duration.nanos < 0) {
+//       duration.seconds -= 1;
+//       duration.nanos += 1000000000;
+//     }
+//
+// Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
+//
+//     Timestamp start = ...;
+//     Duration duration = ...;
+//     Timestamp end = ...;
+//
+//     end.seconds = start.seconds + duration.seconds;
+//     end.nanos = start.nanos + duration.nanos;
+//
+//     if (end.nanos < 0) {
+//       end.seconds -= 1;
+//       end.nanos += 1000000000;
+//     } else if (end.nanos >= 1000000000) {
+//       end.seconds += 1;
+//       end.nanos -= 1000000000;
+//     }
+//
+// Example 3: Compute Duration from datetime.timedelta in Python.
+//
+//     td = datetime.timedelta(days=3, minutes=10)
+//     duration = Duration()
+//     duration.FromTimedelta(td)
+//
+// # JSON Mapping
+//
+// In JSON format, the Duration type is encoded as a string rather than an
+// object, where the string ends in the suffix "s" (indicating seconds) and
+// is preceded by the number of seconds, with nanoseconds expressed as
+// fractional seconds. For example, 3 seconds with 0 nanoseconds should be
+// encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
+// be expressed in JSON format as "3.000000001s", and 3 seconds and 1
+// microsecond should be expressed in JSON format as "3.000001s".
+//
+//
+message Duration {
+
+  // Signed seconds of the span of time. Must be from -315,576,000,000
+  // to +315,576,000,000 inclusive. Note: these bounds are computed from:
+  // 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
+  int64 seconds = 1;
+
+  // Signed fractions of a second at nanosecond resolution of the span
+  // of time. Durations less than one second are represented with a 0
+  // `seconds` field and a positive or negative `nanos` field. For durations
+  // of one second or more, a non-zero value for the `nanos` field must be
+  // of the same sign as the `seconds` field. Must be from -999,999,999
+  // to +999,999,999 inclusive.
+  int32 nanos = 2;
+}