�����JFIF��XX����������    $.' ",#(7),01444'9=82<.342  2!!22222222222222222222222222222222222222222222222222�����"����4���������������������������� ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������,�PG"Z_�4�˷����kjز�Z�,F+��_z�,�© �����zh6�٨�ic�fu������������������������������������#ډb���_�N��?�����������wQ���5-�~�I���8���������������������������������TK<5o�Iv-������������������k�_U_������������������������������~b�M��d��������Ӝ�U�Hh��?]��E�w��Q���k�{��_}qFW7HTՑ��Y��F�����?_�'ϔ��_�Ջt������������������������=||I �����6�έ"�����D���/[�k�9����Y�8������ds|\���Ҿp6�Ҵ���]��.����6���z<�v��@]�i%������������������������$j��~����g��J>��no����pM[me�i$[�����������s�o�ᘨ�˸ nɜG-�ĨU�ycP���3.DB�li�;���������������������hj���x����7Z^�N�h��������N3u{�:j�����x�힞��#M��&��jL P@��_���� P�������������������&��o8��������9������@Sz���6�t7#O�ߋ �����s}Yf�T������lmr����Z)'N��k�۞p�����w\�T���������������ȯ?�8`���O��i{wﭹW�[�r�� ��Q4F�׊������3m&L�=��h3�������z~��#����\�l :�F,j@�� ʱ�wQT����8�"kJO����6�֚l������������������}����R�>ډK���]��y����&����p�}b������;N�1�m�r$����|��7�>e�@���B�TM*-i�H��g�D�)� E�m�|�ؘbҗ�a���Ҿ����������������t4�����o���G��*oCN�rP���Q��@z,|?W[0���������:�n,j���WiE��W������$~/�hp\��?��{(�0���+�Y8rΟ�+����>S-S���������������VN;���}�s?.����� w��9��˟<���Mq4�Wv'������{)0�1mB����V����W[��������8�/<� �%���wT^�5���b��)iM� p�g�N�&ݝ������������VO~��q���u���9��� ����!��J27�����$����O-���! �:���%H��� ـ�������y�ΠM=t{!S�� �oK8�������t<����è��������:a��������[������ա�H���~��w��Qz`�p����o�^ ������Q��n����� �,uu�C��$ ^���,�������8�#��:�6��e�|~�����������!�3��3.�\0�����q��o�4`.|� ����y�Q�`~;�d�ׯ,��O�Zw�������`73�v�܋�<�����Ȏ�� ـ4k��5�K�a�u�=9Yd��$>x�A�&�� j0� ���vF��� Y���|�y��� ~�6�@c��1vOp��������Ig�����4��l�OD�����L����� R���c���j�_�uX�6��3?nk��Wy�f;^*B� ��@���~a�`��Eu�������+�����6�L��.ü>��}y���}_�O�6�͐�:�Yr���G�X��kG������l^w����������~㒶sy���Iu�!���� W ��X��N�7BV��O��!X�2����wvG�R�f�T#�����t�/?���%8�^�W�aT����G�cL�M���I��(J����1~�8�?aT ���]����AS�E��(��*E}� 2������#I/�׍qz��^t�̔���������b�Yz4x����t�){ OH�����+(E��A&�N�������XT��o��"�XC����'���)}�J�z�p� ����~5�}�^����+�6����w��c��Q�|�Lp�d�H��}�(�.|����k��c4^�����"�����Z?ȕ ��a<�������L�!0�39C� �Eu�����C�F�Ew�ç ;�n?�*o���B�8�bʝ���'#Rqf����M}7����]�������s2tcS{�\icTx;�\��7K���P������ʇ Z O-��~�������c>"��?��������P�����E��O�8��@�8��G��Q�g�a�Վ���󁶠��䧘��_%#r�>�����1�z�a���eb��qcP��ѵ��n���#L��� =��׀t� L�7�`�����V����A{�C:�g���e@�����w1 Xp�3�c3�ġ�������p��M"'-�@n4���fG���B3�DJ�8[Jo�ߐ���gK)ƛ��$���� �������8�3�����+���� �����6�ʻ���� ���S�kI�*KZlT _`�������?��K�����QK�d���������B`�s}�>���`������*�>��,*@J�d�oF*�����弝��O}�k��s��]��y�ߘ�������c1G�V���<=�7��7����6��q�PT��tXԀ�!9*4�4Tހ���3XΛex�46�������Y��D ����� ����BdemDa����\�_l,����G�/���֌7���Y�](�xTt^%�GE�����4�}bT����ڹ�����;��Y)���B�Q��u��>J/J ���⮶.�XԄ��j�ݳ������+E��d ���r�5�_D�����1 ���o�� �B�x�΢�#����<��W�����8���R6�@���g�M�.��� dr�D��>(otU��@�x=��~v���2� ӣ�d�oBd�����3�eO�6�㣷����������ݜ�6��6Y��Qz`����S��{���\P��~z m5{J/L��1������<�e�ͅPu���b�]�ϔ��������'�������f�b� Zpw��c`"��i���BD@:)ִ�:�]��h���v�E��w���T�l�������P����"Ju�}��وV ��J��G6��. J/�Qgl߭�e�����@�z�Zev2u����)]կ���������7x�������s�M�-<ɯ�c��r��v�����@��$�ޮ}lk���a����'����>x��O\�Z������Fu>������ck#��&:��`�$��ai�>2Δ����l���oF[h�������lE�ܺ�Π���k:)���`������� $[6�����9�����kOw�\|�����8}������ބ:��񶐕��������I�A1/���=�2[�,�!��.}gN#�u����b���� ~���������݊��}34q�����d�E��L��������c��$���"�[q�U�硬g^��%B ��z���r�p�������J�ru%v\h�����1Y�ne`������ǥ:g����pQM~�^��Xi� ��`S�:V2������9.�P���V������?B�k�� ��������AEvw%�_�9C�Q����wKekP�ؠ�\������;Io d�{ ߞo�c1eP�����\� `����E=���@K<�Y��������eڼ�J����w����{av�F�'�M�@��������������/J��+9p����|]���������Iw &`���8���&�M�hg���[�{�������Xj���%��Ӓ�������������������$��(�����ʹN�������<>�I���RY�����K2�NPlL�ɀ�)��&e��������B+ь����(������������������� � �JTx����_?EZ� }@���� 6�U���뙢ط�z��dWI��n` D����噥�[��uV��"�G&�����Ú����2�g�}&m���������������������?ċ���"����Om#�������������������������� ��{���������������������ON��"S�X���Ne��ysQ���@�������������Fn��Vg�����dX�~nj����������������������]J�<�K]:����FW���b�������62����������=��5f����JKw����bf�X������������������������55��~J �%^�������:�-�QIE��P��v�nZum� z � ~ə ���� ���ة����;�f��\v�������g�8�1��f2�������������������������4;�V���ǔ�)�������������������9���1\������������������������������c��v�/'Ƞ�w������������������$�4�R-��t����������������������������������� e�6�/�ġ �̕Ecy�J���u�B���<�W�ַ~�w[B1L۲�-JS΂�{���΃�������������������������������������������A��20�c#���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������@���� 0!1@AP"#2Q`$3V�%45a6�FRUq����� ������^7ׅ,$n��������+��F�`��2X'��0vM��p�L=�������5��8������u�p~���.�`r�����\����O��,ư�0oS ��_�M�����l���4�kv\JSd���x���SW�<��Ae�IX����������$I���w�:S���y���›R��9�Q[���,�5�;�@]�%���u�@ *ro�lbI �� ��+���%m:�͇ZV�����u�̉����θau<�fc�.����{�4Ա� �Q����*�Sm��8\ujqs]{kN���)qO�y�_*dJ�b�7���yQqI&9�ԌK!�M}�R�;�������S�T���1���i[U�ɵz�]��U)V�S6���3$K{��ߊ<�(� E]Զ[ǼENg�����'�\?#)Dkf��J���o��v���'�%ƞ�&K�u��!��b�35LX�Ϸ��63$K�a�;�9>,R��W��3�3� d�JeTYE.Mϧ��-�o�j3+y��y^�c�������VO�9NV\nd�1 ��!͕_)a�v;����թ�M�lWR1��)El��P;��yوÏ�u 3�k�5Pr6<�⒲l�!˞*��u־�n�!�l:����UNW ��%��Chx8vL'��X�@��*��)���̮��ˍ��� ����D-M�+J�U�kvK����+�x8��cY������?�Ԡ��~3mo��|�u@[XeY�C�\Kp�x8�oC�C�&����N�~3-H���� ��MX�s�u<`���~"WL��$8ξ��3���a�)|:@�m�\���^�`�@ҷ)�5p+��6���p�%i)P M���ngc�����#0Aruz���RL+xSS?���ʮ}()#�t��mˇ!��0}}y����<�e� �-ή�Ԩ��X������ MF���ԙ~l L.3���}�V뽺�v������멬��Nl�)�2����^�Iq��a��M��qG��T�����c3#������3U�Ǎ���}��לS�|qa��ڃ�+���-��2�f����/��bz��ڐ�� �ݼ[2�ç����k�X�2�* �Z�d���J�G����M*9W���s{��w���T��x��y,�in�O�v��]���n����P�$��JB@=4�OTI�n��e�22a\����q�d���%�$��(���:���: /*�K[PR�fr\nڙdN���F�n�$�4��[�� U�zƶ����� �mʋ���,�ao�u 3�z� �x��Kn����\[��VFmbE;�_U��&V�Gg�]L�۪&#n%�$ɯ��dG���D�TI=�%+AB�Ru#��b4�1�»x�cs�YzڙJG��f��Il���d�eF'T� iA��T���uC�$����Y��H?����[!G`}���ͪ� �纤Hv\������j�Ex�K���!���OiƸ�Yj�+u-<���'q����uN�*�r\��+�]���<�wOZ.fp�ێ��,-*)V?j-kÊ#�`�r��dV����(�ݽBk�����G�ƛk�QmUڗe��Z���f}|����8�8��a���i��3'J�����~G_�^���d�8w������ R�`(�~�.��u���l�s+g�bv���W���lGc}��u���afE~1�Ue������Z�0�8�=e�� f@/�jqEKQQ�J���oN��J���W5~M>$6�Lt�;$ʳ{���^��6�{����v6���ķܰg�V�cnn �~z�x�«�,2�u�?cE+Ș�H؎�%�Za�)���X>uW�Tz�Nyo����s���FQƤ��$��*�&�LLXL)�1�" L��eO��ɟ�9=���:t��Z���c��Ž���Y?�ӭV�wv�~,Y��r�ۗ�|�y��GaF�����C�����.�+� ���v1���fήJ�����]�S��T��B��n5sW}y�$��~z�'�c ��8 ��� ,! �p��VN�S��N�N�q��y8z˱�A��4��*��'������2n<�s���^ǧ˭P�Jޮɏ�U�G�L�J�*#��<�V��t7�8����TĜ>��i}K%,���)[��z�21z ?�N�i�n1?T�I�R#��m-�����������������1����lA�`��fT5+��ܐ�c�q՝��ʐ��,���3�f2U�եmab��#ŠdQ�y>\��)�SLY����w#��.���ʑ�f��� ,"+�w�~�N�'�c�O�3F�������N<���)j��&��,-� �љ���֊�_�zS���TǦ����w�>��?�������n��U仆�V���e�����0���$�C�d���rP �m�׈e�Xm�Vu� �L��.�bֹ��� �[Դaզ���*��\y�8�Է:�Ez\�0�Kq�C b��̘��cө���Q��=0Y��s�N��S.����3.���O�o:���#���v7�[#߫ ��5�܎�L���Er4���9n��COWlG�^��0k�%<���ZB���aB_���������'=��{i�v�l�$�uC���mƎҝ{�c㱼�y]���W�i ��ߧc��m�H� m�"�"�����;Y�ߝ�Z�Ǔ�����:S#��|}�y�,/k�Ld� TA�(�AI$+I3��;Y*���Z��}|��ӧO��d�v��..#:n��f>�>���ȶI�TX��� 8��y����"d�R�|�)0���=���n4��6ⲑ�+��r<�O�܂~zh�z����7ܓ�HH�Ga롏���nCo�>������a ���~]���R���̲c?�6(�q�;5%� |�uj�~z8R�=X��I�V=�|{v�Gj\gc��q����z�؋%M�ߍ����1y��#��@f^���^�>N������#x#۹��6�Y~�?�dfPO��{��P�4��V��u1E1J �*|���%����JN��`eWu�zk M6���q t[�� ��g�G���v��WIG��u_ft����5�j�"�Y�:T��ɐ���*�;� e5���4����q$C��2d�}���� _S�L#m�Yp��O�.�C�;��c����Hi#֩%+) �Ӎ��ƲV���SYź��g |���tj��3�8���r|���V��1#;.SQ�A[���S������#���`n�+���$��$�I �P\[�@�s��(�ED�z���P��])8�G#��0B��[ى��X�II�q<��9�~[Z멜�Z�⊔IWU&A>�P~�#��dp<�?����7���c��'~���5 ��+$���lx@�M�dm��n<=e�dyX��?{�|Aef ,|n3�<~z�ƃ�uۧ�����P��Y,�ӥQ�*g�#먙R�\���;T��i,��[9Qi歉����c>]9�� ��"�c��P�� �Md?٥��If�ت�u��k��/����F��9�c*9��Ǎ:�ØF���z�n*�@|I�ށ9����N3{'��[�'ͬ�Ҳ4��#}��!�V� Fu��,�,mTIk���v C�7v���B�6k�T9��1�*l� '~��ƞF��lU��'�M ����][ΩũJ_�{�i�I�n��$����L�� j��O�dx�����kza۪��#�E��Cl����x˘�o�����V���ɞ�ljr��)�/,�߬h�L��#��^��L�ф�,íMƁe�̩�NB�L�����iL����q�}��(��q��6IçJ$�W�E$��:������=#����(�K�B����zђ <��K(�N�۫K�w��^O{!����)��H���>x�������lx�?>Պ�+�>�W���,Ly!_�D���Ō�l���Q�!�[ �S����J��1��Ɛ�Y}��b,+�Lo�x�ɓ)����=�y�oh�@�꥟/��I��ѭ=��P�y9��� �ۍYӘ�e+�p�Jnϱ?V\SO%�(�t� ���=?MR�[Ș�����d�/ ��n�l��B�7j� ��!�;ӥ�/�[-���A�>��dN�sLj ��,ɪv��=1c�.SQ�O3�U���ƀ�ܽ�E����������̻��9G�ϷD�7(�}��Ävӌ\��y�_0[w ���<΍>����a_��[0+�L��F.�޺��f�>oN�T����q;���y\��bՃ��y�jH�<|q-eɏ�_?_9+P���Hp$�����[ux�K w�Mw��N�ی'$Y2�=��q���KB��P��~�������Yul:�[<����F1�2�O���5=d����]Y�sw:���Ϯ���E��j,_Q��X��z`H1,#II ��d�wr��P˂@�ZJV����y$�\y�{}��^~���[:N����ߌ�U�������O��d�����ؾe��${p>G��3c���Ė�lʌ�� ת��[��`ϱ�-W����dg�I��ig2��� ��}s ��ؤ(%#sS@���~���3�X�nRG�~\jc3�v��ӍL��M[JB�T��s3}��j�Nʖ��W����;7���ç?=X�F=-�=����q�ߚ���#���='�c��7���ڑW�I(O+=:uxq�������������e2�zi+�kuG�R��������0�&e�n���iT^J����~\jy���p'dtG��s����O��3����9* �b#Ɋ�� p������[Bws�T�>d4�ۧs���nv�n���U���_�~,�v����ƜJ1��s�� �QIz���)�(lv8M���U=�;����56��G���s#�K���MP�=��LvyGd��}�VwWBF�'�à �?MH�U�g2�� ����!�p�7Q��j��ڴ����=��j�u��� Jn�A s���uM������e��Ɔ�Ҕ�!)�'��8Ϣ�ٔ���ޝ(��Vp���צ֖d=�IC�J�Ǡ{q������kԭ�߸���i��@K����u�|�p=..�*+����x�����z[Aqġ#s2a�Ɗ���RR�)*HRsi�~�a &f��M��P����-K�L@��Z��Xy�'x�{}��Zm+���:�)�) IJ�-i�u���� ���ܒH��'��L(7�y�GӜq���� j��� 6ߌg1�g�o���,kر���tY�?W,���p���e���f�OQS��!K�۟cҒA�|ս�j�>��=⬒��˧L[�� �߿2JaB~R��u�:��Q�] �0H~���]�7��Ƽ�I���(�}��cq '�ήET���q�?f�ab���ӥvr� �)o��-Q��_'����ᴎo��K������;��V���o��%���~OK ����*��b�f:���-ťIR��`B�5!RB@���ï�� �u �̯e\�_U�_������� g�ES��3��������QT��a�����x����U<~�c?�*�#]�MW,[8O�a�x��]�1bC|踤�P��lw5V%�)�{t�<��d��5���0i�XSU��m:��Z�┵�i�"��1�^B�-��P�hJ��&)O��*�D��c�W��vM��)����}���P��ܗ-q����\mmζZ-l@�}��a��E�6��F�@��&Sg@���ݚ�M����� ȹ 4����#p�\H����dYDo�H���"��\��..R�B�H�z_�/5˘����6��KhJR��P�mƶi�m���3��,#c�co��q�a)*P�t����R�m�k�7x�D�E�\Y�閣_X�<���~�)���c[[�BP����6�Yq���S��0����%_����;��Àv�~�| VS؇ ��'O0��F0��\���U�-�d@�����7�SJ*z��3n��y��P����O����������m�~�P�3|Y��ʉr#�C�<�G~�.,! ���bqx���h~0=��!ǫ�jy����l��O,�[B��~��|9��ٱ����Xly�#�i�B��g%�S��������tˋ���e���ې��\[d�t)��.+u�|1 ������#�~Oj����hS�%��i.�~X���I�H�m��0n���c�1uE�q��cF�RF�o���7� �O�ꮧ� ���ۛ{��ʛi5�rw?׌#Qn�TW��~?y$��m\�\o����%W� ?=>S�N@�� �Ʈ���R����N�)�r"C�:��:����� �����#��qb��Y�. �6[��2K����2u�Ǧ�HYR��Q�MV��� �G�$��Q+.>�����nNH��q�^��� ����q��mM��V��D�+�-�#*�U�̒ ���p욳��u:�������IB���m����PV@O���r[b= �� ��1U�E��_Nm�yKbN�O���U�}�the�`�|6֮P>�\2�P�V���I�D�i�P�O;�9�r�mAHG�W�S]��J*�_�G��+kP�2����Ka�Z���H�'K�x�W�MZ%�O�YD�Rc+o��?�q��Ghm��d�S�oh�\�D�|:W������UA�Qc yT�q��������~^�H��/��#p�CZ���T�I�1�ӏT����4��"�ČZ�����}��`w�#�*,ʹ�� ��0�i��課�Om�*�da��^gJ݅{���l�e9uF#T�ֲ��̲�ٞC"�q���ߍ ոޑ�o#�XZTp����@ o�8��(jd��xw�]�,f���`~��|,s��^����f�1���t��|��m�򸄭/ctr��5s��7�9Q�4�H1꠲BB@�l9@���C�����+�wp�xu�£Yc�9��?`@#�o�mH�s2��)�=��2�.�l����jg�9$�Y�S�%*L������R�Y������7Z���,*=�䷘$�������arm�o�ϰ���UW.|�r�uf����IGw�t����Zwo��~5 ��YյhO+=8fF�)�W�7�L9lM�̘·Y���֘YLf�큹�pRF���99.A �"wz��=E\Z���'a� 2��Ǚ�#;�'}�G���*��l��^"q��+2FQ� hj��kŦ��${���ޮ-�T�٭cf�|�3#~�RJ����t��$b�(R��(����r���dx� >U b�&9,>���%E\� Ά�e�$��'�q't��*�א���ެ�b��-|d���SB�O�O��$�R+�H�)�܎�K��1m`;�J�2�Y~9��O�g8=vqD`K[�F)k�[���1m޼c��n���]s�k�z$@��)!I �x՝"v��9=�ZA=`Ɠi �:�E��)`�7��vI��}d�YI�_ �o�:ob���o ���3Q��&D&�2=�� �Ά��;>�h����y.*ⅥS������Ӭ�+q&����j|UƧ�����}���J0��WW< ۋS�)jQR�j���Ư��rN)�Gű�4Ѷ(�S)Ǣ�8��i��W52���No˓� ۍ%�5brOn�L�;�n��\G����=�^U�dI���8$�&���h��'���+�(������cȁ߫k�l��S^���cƗjԌE�ꭔ��gF���Ȓ��@���}O���*;e�v�WV���YJ\�]X'5��ղ�k�F��b 6R�o՜m��i N�i�����>J����?��lPm�U��}>_Z&�KK��q�r��I�D�Չ~�q�3fL�:S�e>���E���-G���{L�6p�e,8��������QI��h��a�Xa��U�A'���ʂ���s�+טIjP�-��y�8ۈZ?J$��W�P� ��R�s�]��|�l(�ԓ��sƊi��o(��S0���Y� 8�T97.�����WiL��c�~�dxc�E|�2!�X�K�Ƙਫ਼�$((�6�~|d9u+�qd�^3�89��Y�6L�.I�����?���iI�q���9�)O/뚅����O���X��X�V��ZF[�یgQ�L��K1���RҖr@v�#��X�l��F���Нy�S�8�7�kF!A��sM���^rkp�jP�DyS$N���q���nxҍ!U�f�!eh�i�2�m����`�Y�I�9r�6� �TF���C}/�y�^���Η���5d�'��9A-��J��>{�_l+�`��A���[�'��յ�ϛ#w:݅�%��X�}�&�PSt�Q�"�-��\縵�/����$Ɨh�Xb�*�y��BS����;W�ջ_mc�����vt?2}1�;qS�d�d~u:2k5�2�R�~�z+|HE!)�Ǟl��7`��0�<�,�2*���Hl-��x�^����'_TV�gZA�'j� ^�2Ϊ��N7t�����?w�� �x1��f��Iz�C-Ȗ��K�^q�;���-W�DvT�7��8�Z�������� hK�(P:��Q- �8�n�Z���܃e貾�<�1�YT<�,�����"�6{�/ �?�͟��|1�:�#g��W�>$����d��J��d�B���=��jf[��%rE^��il:��B���x���Sּ�1հ��,�=��*�7 fcG��#q� �eh?��2�7�����,�!7x��6�n�LC�4x��},Geǝ�tC.��vS �F�43��zz\��;QYC,6����~;RYS/6���|2���5���v��T��i����������mlv��������&� �nRh^ejR�LG�f���? �ۉҬܦƩ��|��Ȱ����>3����!v��i�ʯ�>�v��オ�X3e���_1z�Kȗ\<������!�8���V��]��?b�k41�Re��T�q��mz��TiOʦ�Z��Xq���L������q"+���2ۨ��8}�&N7XU7Ap�d�X��~�׿��&4e�o�F��� �H�����O���č�c�� 懴�6���͉��+)��v;j��ݷ�� �UV�� i��� j���Y9GdÒJ1��詞�����V?h��l�����l�cGs�ځ�������y�Ac������\V3�? �� ܙg�>qH�S,�E�W�[�㺨�uch�⍸�O�}���a��>�q�6�n6�����N6�q��������N� ���! 1AQaq�0@����"2BRb�#Pr���3C`��Scst���$4D���%Td���� ?�����N����a��3��m���C���w��������xA�m�q�m����m������$����4n淿t'��C"w��zU=D�\R+w�p+Y�T�&�պ@��ƃ��3ޯ?�Aﶂ��aŘ���@-�����Q�=���9D��ռ�ѻ@��M�V��P��܅�G5�f�Y<�u=,EC)�<�Fy'�"�&�չ�X~f��l�KԆV��?�� �W�N����=(� �;���{�r����ٌ�Y���h{�١������jW����P���Tc�����X�K�r��}���w�R��%��?���E��m�� �Y�q|����\lEE4����r���}�lsI�Y������f�$�=�d�yO����p�����yBj8jU�o�/�S��?�U��*������ˍ�0�������u�q�m [�?f����a�� )Q�>����6#������� ?����0UQ����,IX���(6ڵ[�DI�MNލ�c&���υ�j\��X�R|,4��� j������T�hA�e��^���d���b<����n�� �즇�=!���3�^�`j�h�ȓr��jẕ�c�,ٞX����-����a�ﶔ���#�$��]w�O��Ӫ�1y%��L�Y<�wg#�ǝ�̗`�x�xa�t�w��»1���o7o5��>�m뭛C���Uƃߜ}�C���y1Xνm�F8�jI���]����H���ۺиE@I�i;r�8ӭ�����V�F�Շ| ��&?�3|x�B�MuS�Ge�=Ӕ�#BE5G������Y!z��_e��q�р/W>|-�Ci߇�t�1ޯќd�R3�u��g�=0 5��[?�#͏��q�cf���H��{ ?u�=?�?ǯ���}Z��z���hmΔ�BFTW�����<�q��(v� ��!��z���iW]*�J�V�z��gX֧A�q�&��/w���u�gYӘa���; �i=����g:��?2�dž6�ى�k�4�>�Pxs����}������G�9���3 ���)gG�R<>r h�$��'nc�h�P��Bj��J�ҧH� -��N1���N��?��~��}-q!=��_2hc�M��l�vY%UE�@|�v����M2�.Y[|y�"Eï��K�ZF,�ɯ?,q�?v�M 80jx�"�;�9vk�����+ ֧�� �ȺU��?�%�vcV��mA�6��Qg^M�����A}�3�nl� QRN�l8�kkn�'�����(��M�7m9و�q���%ޟ���*h$Zk"��$�9��: �?U8�Sl��,,|ɒ��xH(ѷ����Gn�/Q�4�P��G�%��Ա8�N��!� �&�7�;���eKM7�4��9R/%����l�c>�x;������>��C�:�����t��h?aKX�bhe�ᜋ^�$�Iհ �hr7%F$�E��Fd���t��5���+�(M6�t����Ü�UU|zW�=a�Ts�Tg������dqP�Q����b'�m���1{|Y����X�N��b �P~��F^F:����k6�"�j!�� �I�r�`��1&�-$�Bevk:y���#y�w��I0��x��=D�4��tU���P�ZH��ڠ底taP��6����b>�xa�����Q�#� WeF��ŮNj�p�J* mQ�N�����*I�-*�ȩ�F�g�3 �5��V�ʊ�ɮ�a��5F���O@{���NX��?����H�]3��1�Ri_u��������ѕ�� ����0��� F��~��:60�p�͈�S��qX#a�5>���`�o&+�<2�D����: �������ڝ�$�nP���*)�N�|y�Ej�F�5ټ�e���ihy�Z �>���k�bH�a�v��h�-#���!�Po=@k̆IEN��@��}Ll?j�O������߭�ʞ���Q|A07x���wt!xf���I2?Z��<ץ�T���cU�j��]���陎Ltl �}5�ϓ��$�,��O�mˊ�;�@O��jE��j(�ا,��LX���LO���Ц�90�O �.����a��nA���7������j4 ��W��_ٓ���zW�jcB������y՗+EM�)d���N�g6�y1_x��p�$Lv�:��9�"z��p���ʙ$��^��JԼ*�ϭ����o���=x�Lj�6�J��u82�A�H�3$�ٕ@�=Vv�]�'�qEz�;I˼��)��=��ɯ���x �/�W(V���p�����$ �m�������u�����񶤑Oqˎ�T����r��㠚x�sr�GC��byp�G��1ߠ�w e�8�$⿄����/�M{*}��W�]˷.�CK\�ުx���/$�WP�w���r� |i���&�}�{�X� �>��$-��l���?-z���g����lΆ���(F���h�vS*���b���߲ڡn,|)mrH[���a�3�ר�[1��3o_�U�3�TC�$��(�=�)0�kgP���� ��u�^=��4 �WYCҸ:��vQ�ר�X�à��tk�m,�t*��^�,�}D*�� �"(�I��9R����>`�`��[~Q]�#af��i6l��8���6�:,s�s�N6�j"�A4���IuQ��6E,�GnH��zS�HO�uk�5$�I�4��ؤ�Q9�@��C����wp��BGv[]�u�Ov����0I4���\��y�����Q�Ѹ��~>Z��8�T��a��q�ޣ;z��a���/��S��I:�ܫ_�|������>=Z����8:�S��U�I�J��"IY���8%b8���H��:�QO�6�;7�I�S��J��ҌAά3��>c���E+&jf$eC+�z�;��V����� �r���ʺ������my�e���aQ�f&��6�ND���.:��NT�vm�<- u���ǝ\MvZY�N�NT��-A�>jr!S��n�O 1�3�Ns�%�3D@���`������ܟ 1�^c<���� �a�ɽ�̲�Xë#�w�|y�cW�=�9I*H8�p�^(4���՗�k��arOcW�tO�\�ƍR��8����'�K���I�Q�����?5�>[�}��yU�ײ -h��=��% q�ThG�2�)���"ו3]�!kB��*p�FDl�A���,�eEi�H�f�Ps�����5�H:�Փ~�H�0Dت�D�I����h�F3�������c��2���E��9�H��5�zԑ�ʚ�i�X�=:m�xg�hd(�v����׊�9iS��O��d@0ڽ���:�p�5�h-��t�&���X�q�ӕ,��ie�|���7A�2���O%P��E��htj��Y1��w�Ѓ!����  ���� ࢽ��My�7�\�a�@�ţ�J ��4�Ȼ�F�@o�̒?4�wx��)��]�P��~�����u�����5�����7X ��9��^ܩ�U;Iꭆ 5 �������eK2�7(�{|��Y׎ �V��\"���Z�1� Z�����}��(�Ǝ"�1S���_�vE30>���p;� ΝD��%x�W�?W?v����o�^V�i�d��r[��/&>�~`�9Wh��y�;���R���� ;;ɮT��?����r$�g1�K����A��C��c��K��l:�'��3 c�ﳯ*"t8�~l��)���m��+U,z��`(��>yJ�?����h>��]��v��ЍG*�{`��;y]��I�T� ;c��NU�fo¾h���/$���|NS���1�S�"�H��V���T���4��uhǜ�]�v;���5�͠x��'C\�SBpl���h}�N����� A�Bx���%��ޭ�l��/����T��w�ʽ]D�=����K���ž�r㻠l4�S�O?=�k �M:� ��c�C�a�#ha���)�ѐxc�s���gP�iG���{+���x���Q���I= �� z��ԫ+ �8"�k�ñ�j=|����c ��y��CF��/���*9ж�h{ �?4�o� ��k�m�Q�N�x��;�Y��4膚�a�w?�6�>�e]�����Q�r�:����g�,i"�����ԩA��*M�<�G��b�if��l^M��5�� �Ҩ�{����6J��ZJ�����P�*�����Y���ݛu�_4�9�I8�7���������,^ToR���m4�H��?�N�S�ѕw��/S��甍�@�9H�S�T��t�ƻ���ʒU��*{Xs�@����f������֒Li�K{H�w^���������Ϥm�tq���s� ���ք��f:��o~s��g�r��ט� �S�ѱC�e]�x���a��) ���(b-$(�j>�7q�B?ӕ�F��hV25r[7 Y� }L�R��}����*sg+��x�r�2�U=�*'WS��ZDW]�WǞ�<��叓���{�$�9Ou4��y�90-�1�'*D`�c�^o?(�9��u���ݐ��'PI&� f�Jݮ�������:wS����jfP1F:X �H�9dԯ����˝[�_54 �}*;@�ܨ�� ð�yn�T���?�ןd�#���4rG�ͨ��H�1�|-#���Mr�S3��G�3�����)�.᧏3v�z֑��r����$G"�`j �1t��x0<Ɔ�Wh6�y�6��,œ�Ga��gA����y��b��)���h�D��ß�_�m��ü �gG;��e�v��ݝ�nQ� ��C����-�*��o���y�a��M��I�>�<���]obD��"�:���G�A��-\%LT�8���c�)��+y76���o�Q�#*{�(F�⽕�y����=���rW�\p���۩�c���A���^e6��K������ʐ�cVf5$�'->���ՉN"���F�"�UQ@�f��Gb~��#�&�M=��8�ט�JNu9��D��[̤�s�o�~������� G��9T�tW^g5y$b��Y'��س�Ǵ�=��U-2 #�MC�t(�i� �lj�@Q 5�̣i�*�O����s�x�K�f��}\��M{E�V�{�υ��Ƈ�����);�H����I��fe�Lȣr�2��>��W��I�Ȃ6������i��k�� �5�YOxȺ����>��Y�f5'��|��H+��98pj�n�.O�y�������jY��~��i�w'������l�;�s�2��Y��:'lg�ꥴ)o#'Sa�a�K��Z� �m��}�`169�n���"���x��I ��*+� }F<��cГ���F�P�������ֹ*�PqX�x۩��,� ��N�� �4<-����%����:��7����W���u�`����� $�?�I��&����o��o��`v�>��P��"��l���4��5'�Z�gE���8���?��[�X�7(��.Q�-��*���ތL@̲����v��.5���[��=�t\+�CNܛ��,g�SQnH����}*F�G16���&:�t��4ُ"A��̣��$�b �|����#rs��a�����T�� ]�<�j��B�S�('$�ɻ� �wP;�/�n��?�ݜ��x�F��yUn�~mL*-�������Xf�wd^�a�}��f�,=t�׵i�.2/wpN�Ep8�OР���•��R�FJ� 55TZ��T �ɭ�<��]��/�0�r�@�f��V��V����Nz�G��^���7hZi����k��3�,kN�e|�vg�1{9]_i��X5y7� 8e]�U����'�-2,���e"����]ot�I��Y_��n�(JҼ��1�O ]bXc���Nu�No��pS���Q_���_�?i�~�x h5d'�(qw52] ��'ޤ�q��o1�R!���`ywy�A4u���h<קy���\[~�4�\ X�Wt/� 6�����n�F�a8��f���z �3$�t(���q��q�x��^�XWeN'p<-v�!�{�(>ӽDP7��ո0�y)�e$ٕv�Ih'Q�EA�m*�H��RI��=:��� ���4牢) �%_iN�ݧ�l]� �Nt���G��H�L��� ɱ�g<���1V�,�J~�ٹ�"K��Q�� 9�HS�9�?@��k����r�;we݁�]I�!{ �@�G�[�"��`���J:�n]�{�cA�E����V��ʆ���#��U9�6����j�#Y�m\��q�e4h�B�7��C�������d<�?J����1g:ٳ���=Y���D�p�ц� ׈ǔ��1�]26؜oS�'��9�V�FVu�P�h�9�xc�oq�X��p�o�5��Ա5$�9W�V(�[Ak�aY錎qf;�'�[�|���b�6�Ck��)��#a#a˙��8���=äh�4��2��C��4tm^ �n'c����]GQ$[Wҿ��i���vN�{Fu ��1�gx��1┷���N�m��{j-,��x�� Ūm�ЧS�[�s���Gna���䑴�� x�p 8<������97�Q���ϴ�v�aϚG��Rt�Һ׈�f^\r��WH�JU�7Z���y)�vg=����n��4�_)y��D'y�6�]�c�5̪��\� �PF�k����&�c;��cq�$~T�7j ���nç]�<�g ":�to�t}�159�<�/�8������m�b�K#g'I'.W������6��I/��>v��\�MN��g���m�A�yQL�4u�Lj�j9��#44�t��l^�}L����n��R��!��t��±]��r��h6ٍ>�yҏ�N��fU�� ���� Fm@�8}�/u��jb9������he:A�y�ծw��GpΧh�5����l}�3p468��)U��d��c����;Us/�֔�YX�1�O2��uq�s��`hwg�r~�{ R��mhN��؎*q 42�*th��>�#���E����#��Hv�O����q�}������6�e��\�,Wk�#���X��b>��p}�դ��3���T5��†��6��[��@��P�y*n��|'f�֧>�lư΂�̺����SU�'*�q�p�_S�����M�� '��c�6������m�� ySʨ;M��r���Ƌ�m�Kxo,���Gm�P��A�G�:��i��w�9�}M(�^�V��$ǒ�ѽ�9���|���� �a����J�SQ�a���r�B;����}���ٻ֢�2�%U���c�#�g���N�a�ݕ�'�v�[�OY'��3L�3�;,p�]@�S��{ls��X�'���c�jw��k'a�.��}�}&�� �dP�*�bK=ɍ!����;3n�gΊU�ߴmt�'*{,=SzfD� A��ko~�G�aoq�_mi}#�m�������P�Xhύ�����mxǍ�΂���巿zf��Q���c���|kc�����?���W��Y�$���_Lv����l߶��c���`?����l�j�ݲˏ!V��6����U�Ђ(A���4y)H���p�Z_�x��>���e���R��$�/�`^'3qˏ�-&Q�=?��CFVR �D�fV�9��{�8g�������n�h�(P"��6�[�D���< E�����~0<@�`�G�6����Hг�cc�� �c�K.5��D��d�B���`?�XQ��2��ٿyqo&+�1^� DW�0�ꊩ���G�#��Q�nL3��c���������/��x ��1�1�[y�x�პCW��C�c�UĨ80�m�e�4.{�m��u���I=��f�����0QRls9���f���������9���~f�����Ǩ��a�"@�8���ȁ�Q����#c�ic������G��$���G���r/$W�(��W���V�"��m�7�[m�A�m����bo��D� j����۳� l���^�k�h׽����� ��#� iXn�v��eT�k�a�^Y�4�BN���ĕ���0������� !01@Q"2AaPq3BR�������?�����@4�Q�����T3,���㺠�W�[=JK�Ϟ���2�r^7��vc�:�9 �E�ߴ�w�S#d���Ix��u��:��Hp��9E!�� V 2;73|F��9Y���*ʬ�F��D����u&���y؟��^EA��A��(ɩ���^��GV:ݜDy�`��Jr29ܾ�㝉��[���E;Fzx��YG��U�e�Y�C���� ����v-tx����I�sם�Ę�q��Eb�+P\ :>�i�C'�;�����k|z�رn�y]�#ǿb��Q��������w�����(�r|ӹs��[�D��2v-%��@;�8<a���[\o[ϧw��I!��*0�krs)�[�J9^��ʜ��p1)� "��/_>��o��<1����A�E�y^�C��`�x1'ܣn�p��s`l���fQ��):�l����b>�Me�jH^?�kl3(�z:���1ŠK&?Q�~�{�ٺ�h�y���/�[��V�|6��}�KbX����mn[-��7�5q�94�������dm���c^���h� X��5��<�eޘ>G���-�}�دB�ޟ� ��|�rt�M��V+�]�c?�-#ڛ��^ǂ}���Lkr���O��u�>�-D�ry� D?:ޞ�U��ǜ�7�V��?瓮�"�#���r��չģVR;�n���/_� ؉v�ݶe5d�b9��/O��009�G���5n�W����JpA�*�r9�>�1��.[t���s�F���nQ� V 77R�]�ɫ8����_0<՜�IF�u(v��4��F�k�3��E)��N:��yڮe��P�`�1}�$WS��J�SQ�N�j��ٺ��޵�#l���ј(�5=��5�lǏmoW�v-�1����v,W�mn��߀$x�<����v�j(����c]��@#��1������Ǔ���o'��u+����;G�#�޸��v-lη��/(`i⣍Pm^����ԯ̾9Z��F��������n��1��� ��]�[��)�'�������:�֪�W��FC����� �B9،!?���]��V��A�Վ�M��b�w��G F>_DȬ0¤�#�QR�[V��kz���m�w�"��9ZG�7'[��=�Q����j8R?�zf�\a�=��O�U����*oB�A�|G���2�54 �p��.w7� �� ���&������ξxGHp� B%��$g�����t�Џ򤵍z���HN�u�Я�-�'4��0���;_���3������� !01"@AQa2Pq#3BR�������?����ʩca��en��^��8���<�u#��m*08r��y�N"�<�Ѳ0��@\�p��� �����Kv�D��J8�Fҽ� �f�Y��-m�ybX�NP����}�!*8t(�OqѢ��Q�wW�K��ZD��Δ^e��!� ��B�K��p~�����e*l}z#9ң�k���q#�Ft�o��S�R����-�w�!�S���Ӥß|M�l޶V��!eˈ�8Y���c�ЮM2��tk���� ������J�fS����Ö*i/2�����n]�k�\���|4yX�8��U�P.���Ы[���l��@"�t�<������5�lF���vU�����W��W��;�b�cД^6[#7@vU�xgZv��F�6��Q,K�v��� �+Ъ��n��Ǣ��Ft���8��0��c�@�!�Zq s�v�t�;#](B��-�nῃ~���3g������5�J�%���O������n�kB�ĺ�.r��+���#�N$?�q�/�s�6��p��a����a��J/��M�8��6�ܰ"�*������ɗud"\w���aT(����[��F��U՛����RT�b���n�*��6���O��SJ�.�ij<�v�MT��R\c��5l�sZB>F��<7�;EA��{��E���Ö��1U/�#��d1�a�n.1ě����0�ʾR�h��|�R��Ao�3�m3 ��%�� ���28Q�� ��y��φ���H�To�7�lW>����#i`�q���c����a��� �m,B�-j����݋�'mR1Ήt�>��V��p���s�0IbI�C.���1R�ea�����]H�6�����������4B>��o��](��$B���m�����a�!=���?�B� K�Ǿ+�Ծ"�n���K��*��+��[T#�{�E�J�S����Q�����s�5�:�U�\wĐ�f�3����܆&�)�����I���Ԇw��E T�lrTf6Q|R�h:��[K�� �z��c֧�G�C��%\��_�a��84��HcO�bi��ؖV��7H �)*ģK~Xhչ0��4?�0��� �E<���}3���#���u�?�� ��|g�S�6ꊤ�|�I#Hڛ� �ա��w�X��9��7���Ŀ%�SL��y6č��|�F�a 8���b���$�sק�h���b9RAu7�˨p�Č�_\*w��묦��F ����4D~�f����|(�"m���NK��i�S�>�$d7SlA��/�²����SL��|6N�}���S�˯���g��]6��; �#�.��<���q'Q�1|KQ$�����񛩶"�$r�b:���N8�w@��8$�� �AjfG|~�9F ���Y��ʺ��Bwؒ������M:I岎�G��`s�YV5����6��A �b:�W���G�q%l�����F��H���7�������Fsv7���k�� 403WebShell
403Webshell
Server IP : 92.112.183.186  /  Your IP : 216.73.216.10
Web Server : LiteSpeed
System : Linux lt-bnk-web922.main-hosting.eu 4.18.0-553.70.1.lve.el8.x86_64 #1 SMP Wed Aug 20 14:42:18 UTC 2025 x86_64
User : u970350538 ( 970350538)
PHP Version : 7.4.33
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : OFF  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/go/pkg/mod/github.com/golang/protobuf@v1.5.2/proto/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/go/pkg/mod/github.com/golang/protobuf@v1.5.2/proto/proto_test.go
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proto_test

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"math"
	"math/rand"
	"reflect"
	"runtime/debug"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/golang/protobuf/proto"
	"google.golang.org/protobuf/testing/protopack"

	pb2 "github.com/golang/protobuf/internal/testprotos/proto2_proto"
	pb3 "github.com/golang/protobuf/internal/testprotos/proto3_proto"
	tspb "github.com/golang/protobuf/ptypes/timestamp"
)

func initGoTestField() *pb2.GoTestField {
	f := new(pb2.GoTestField)
	f.Label = proto.String("label")
	f.Type = proto.String("type")
	return f
}

// These are all structurally equivalent but the tag numbers differ.
// (It's remarkable that required, optional, and repeated all have
// 8 letters.)
func initGoTest_RequiredGroup() *pb2.GoTest_RequiredGroup {
	return &pb2.GoTest_RequiredGroup{
		RequiredField: proto.String("required"),
	}
}

func initGoTest_OptionalGroup() *pb2.GoTest_OptionalGroup {
	return &pb2.GoTest_OptionalGroup{
		RequiredField: proto.String("optional"),
	}
}

func initGoTest_RepeatedGroup() *pb2.GoTest_RepeatedGroup {
	return &pb2.GoTest_RepeatedGroup{
		RequiredField: proto.String("repeated"),
	}
}

func initGoTest(setdefaults bool) *pb2.GoTest {
	pb := new(pb2.GoTest)
	if setdefaults {
		pb.F_BoolDefaulted = proto.Bool(pb2.Default_GoTest_F_BoolDefaulted)
		pb.F_Int32Defaulted = proto.Int32(pb2.Default_GoTest_F_Int32Defaulted)
		pb.F_Int64Defaulted = proto.Int64(pb2.Default_GoTest_F_Int64Defaulted)
		pb.F_Fixed32Defaulted = proto.Uint32(pb2.Default_GoTest_F_Fixed32Defaulted)
		pb.F_Fixed64Defaulted = proto.Uint64(pb2.Default_GoTest_F_Fixed64Defaulted)
		pb.F_Uint32Defaulted = proto.Uint32(pb2.Default_GoTest_F_Uint32Defaulted)
		pb.F_Uint64Defaulted = proto.Uint64(pb2.Default_GoTest_F_Uint64Defaulted)
		pb.F_FloatDefaulted = proto.Float32(pb2.Default_GoTest_F_FloatDefaulted)
		pb.F_DoubleDefaulted = proto.Float64(pb2.Default_GoTest_F_DoubleDefaulted)
		pb.F_StringDefaulted = proto.String(pb2.Default_GoTest_F_StringDefaulted)
		pb.F_BytesDefaulted = pb2.Default_GoTest_F_BytesDefaulted
		pb.F_Sint32Defaulted = proto.Int32(pb2.Default_GoTest_F_Sint32Defaulted)
		pb.F_Sint64Defaulted = proto.Int64(pb2.Default_GoTest_F_Sint64Defaulted)
		pb.F_Sfixed32Defaulted = proto.Int32(pb2.Default_GoTest_F_Sfixed32Defaulted)
		pb.F_Sfixed64Defaulted = proto.Int64(pb2.Default_GoTest_F_Sfixed64Defaulted)
	}

	pb.Kind = pb2.GoTest_TIME.Enum()
	pb.RequiredField = initGoTestField()
	pb.F_BoolRequired = proto.Bool(true)
	pb.F_Int32Required = proto.Int32(3)
	pb.F_Int64Required = proto.Int64(6)
	pb.F_Fixed32Required = proto.Uint32(32)
	pb.F_Fixed64Required = proto.Uint64(64)
	pb.F_Uint32Required = proto.Uint32(3232)
	pb.F_Uint64Required = proto.Uint64(6464)
	pb.F_FloatRequired = proto.Float32(3232)
	pb.F_DoubleRequired = proto.Float64(6464)
	pb.F_StringRequired = proto.String("string")
	pb.F_BytesRequired = []byte("bytes")
	pb.F_Sint32Required = proto.Int32(-32)
	pb.F_Sint64Required = proto.Int64(-64)
	pb.F_Sfixed32Required = proto.Int32(-32)
	pb.F_Sfixed64Required = proto.Int64(-64)
	pb.Requiredgroup = initGoTest_RequiredGroup()

	return pb
}

func overify(t *testing.T, pb *pb2.GoTest, want []byte) {
	bb := new(proto.Buffer)
	err := bb.Marshal(pb)
	got := bb.Bytes()
	if err != nil {
		t.Logf("overify marshal-1 err = %v", err)
	}
	if !bytes.Equal(got, want) {
		t.Fatalf("got %q\nwant  %q", got, want)
	}

	// Now test Unmarshal by recreating the original buffer.
	pbd := new(pb2.GoTest)
	err = bb.Unmarshal(pbd)
	if err != nil {
		t.Fatalf("overify unmarshal err = %v", err)
	}
	bb.Reset()
	err = bb.Marshal(pbd)
	got = bb.Bytes()
	if err != nil {
		t.Fatalf("overify marshal-2 err = %v", err)
	}
	if !bytes.Equal(got, want) {
		t.Fatalf("got %q\nwant  %q", got, want)
	}
}

// When hooks are enabled, RequiredNotSetError is typed alias to internal/proto
// package. Binary serialization has not been wrapped yet and hence produces
// requiredNotSetError instead. This function is a work-around to identify both
// aliased and non-aliased types.
func isRequiredNotSetError(err error) bool {
	e, ok := err.(interface{ RequiredNotSet() bool })
	return ok && e.RequiredNotSet()
}

// Simple tests for numeric encode/decode primitives (varint, etc.)
func TestNumericPrimitives(t *testing.T) {
	for i := uint64(0); i < 1e6; i += 111 {
		o := new(proto.Buffer)
		if o.EncodeVarint(i) != nil {
			t.Error("EncodeVarint")
			break
		}
		x, e := o.DecodeVarint()
		if e != nil {
			t.Fatal("DecodeVarint")
		}
		if x != i {
			t.Fatal("varint decode fail:", i, x)
		}

		o.Reset()
		if o.EncodeFixed32(i) != nil {
			t.Fatal("encFixed32")
		}
		x, e = o.DecodeFixed32()
		if e != nil {
			t.Fatal("decFixed32")
		}
		if x != i {
			t.Fatal("fixed32 decode fail:", i, x)
		}

		o.Reset()
		if o.EncodeFixed64(i*1234567) != nil {
			t.Error("encFixed64")
			break
		}
		x, e = o.DecodeFixed64()
		if e != nil {
			t.Error("decFixed64")
			break
		}
		if x != i*1234567 {
			t.Error("fixed64 decode fail:", i*1234567, x)
			break
		}

		o.Reset()
		i32 := int32(i - 12345)
		if o.EncodeZigzag32(uint64(i32)) != nil {
			t.Fatal("EncodeZigzag32")
		}
		x, e = o.DecodeZigzag32()
		if e != nil {
			t.Fatal("DecodeZigzag32")
		}
		if x != uint64(uint32(i32)) {
			t.Fatal("zigzag32 decode fail:", i32, x)
		}

		o.Reset()
		i64 := int64(i - 12345)
		if o.EncodeZigzag64(uint64(i64)) != nil {
			t.Fatal("EncodeZigzag64")
		}
		x, e = o.DecodeZigzag64()
		if e != nil {
			t.Fatal("DecodeZigzag64")
		}
		if x != uint64(i64) {
			t.Fatal("zigzag64 decode fail:", i64, x)
		}
	}
}

// fakeMarshaler is a simple struct implementing Marshaler and Message interfaces.
type fakeMarshaler struct {
	b   []byte
	err error
}

func (f *fakeMarshaler) Marshal() ([]byte, error) { return f.b, f.err }
func (f *fakeMarshaler) String() string           { return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err) }
func (f *fakeMarshaler) ProtoMessage()            {}
func (f *fakeMarshaler) Reset()                   {}

type msgWithFakeMarshaler struct {
	M *fakeMarshaler `protobuf:"bytes,1,opt,name=fake"`
}

func (m *msgWithFakeMarshaler) String() string { return proto.CompactTextString(m) }
func (m *msgWithFakeMarshaler) ProtoMessage()  {}
func (m *msgWithFakeMarshaler) Reset()         {}

// Simple tests for proto messages that implement the Marshaler interface.
func TestMarshalerEncoding(t *testing.T) {
	tests := []struct {
		name    string
		m       proto.Message
		want    []byte
		errType reflect.Type
	}{
		{
			name: "Marshaler that fails",
			m: &fakeMarshaler{
				err: errors.New("some marshal err"),
				b:   []byte{5, 6, 7},
			},
			errType: reflect.TypeOf(errors.New("some marshal err")),
		},
		{
			name: "Marshaler that fails with RequiredNotSetError",
			m: &msgWithFakeMarshaler{
				M: &fakeMarshaler{
					err: &proto.RequiredNotSetError{},
					b:   []byte{5, 6, 7},
				},
			},
			errType: reflect.TypeOf(&proto.RequiredNotSetError{}),
		},
		{
			name: "Marshaler that succeeds",
			m: &fakeMarshaler{
				b: []byte{0, 1, 2, 3, 4, 127, 255},
			},
			want: []byte{0, 1, 2, 3, 4, 127, 255},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			b := proto.NewBuffer(nil)
			err := b.Marshal(test.m)
			if reflect.TypeOf(err) != test.errType {
				t.Errorf("got err %T(%v) wanted %T", err, err, test.errType)
			}
			if err != nil {
				return // skip comparing output when marshal fails.
			}
			if !reflect.DeepEqual(test.want, b.Bytes()) {
				t.Errorf("got bytes %v wanted %v", b.Bytes(), test.want)
			}
			if size := proto.Size(test.m); size != len(b.Bytes()) {
				t.Errorf("Size(_) = %v, but marshaled to %v bytes", size, len(b.Bytes()))
			}

			m, mErr := proto.Marshal(test.m)
			if !bytes.Equal(b.Bytes(), m) {
				t.Errorf("Marshal returned %v, but (*Buffer).Marshal wrote %v", m, b.Bytes())
			}
			if !reflect.DeepEqual(err, mErr) {
				t.Errorf("Marshal err = %v, but (*Buffer).Marshal returned %v", mErr, err)
			}
		})
	}
}

// Ensure that Buffer.Marshal uses O(N) memory for N messages
func TestBufferMarshalAllocs(t *testing.T) {
	value := &pb2.OtherMessage{Key: proto.Int64(1)}
	msg := &pb2.MyMessage{Count: proto.Int32(1), Others: []*pb2.OtherMessage{value}}

	for _, prealloc := range []int{0, 100, 10000} {
		const count = 1000
		var b proto.Buffer
		s := make([]byte, 0, proto.Size(msg))
		marshalAllocs := testing.AllocsPerRun(count, func() {
			b.SetBuf(s)
			err := b.Marshal(msg)
			if err != nil {
				t.Errorf("Marshal err = %q", err)
			}
		})

		b.SetBuf(make([]byte, 0, prealloc))
		bufferAllocs := testing.AllocsPerRun(count, func() {
			err := b.Marshal(msg)
			if err != nil {
				t.Errorf("Marshal err = %q", err)
			}
		})

		if marshalAllocs != bufferAllocs {
			t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs)
			t.Errorf("%v allocs/op when repeatedly appending to a buffer", bufferAllocs)
			t.Errorf("expect amortized allocs/op to be identical")
		}
	}
}

// Simple tests for bytes
func TestBytesPrimitives(t *testing.T) {
	bb := new(proto.Buffer)
	want := []byte("now is the time")
	if err := bb.EncodeRawBytes(want); err != nil {
		t.Errorf("EncodeRawBytes error: %v", err)
	}
	got, err := bb.DecodeRawBytes(false)
	if err != nil {
		t.Errorf("DecodeRawBytes error: %v", err)
	}
	if !bytes.Equal(got, want) {
		t.Errorf("got %q\nwant  %q", got, want)
	}
}

// Simple tests for strings
func TestStringPrimitives(t *testing.T) {
	bb := new(proto.Buffer)
	want := "now is the time"
	if err := bb.EncodeStringBytes(want); err != nil {
		t.Errorf("EncodeStringBytes error: %v", err)
	}
	got, err := bb.DecodeStringBytes()
	if err != nil {
		t.Errorf("DecodeStringBytes error: %v", err)
	}
	if got != want {
		t.Errorf("got %q\nwant  %q", got, want)
	}
}

// Do we catch the "required bit not set" case?
func TestRequiredBit(t *testing.T) {
	o := new(proto.Buffer)
	pb := new(pb2.GoTest)
	err := o.Marshal(pb)
	if err == nil {
		t.Error("did not catch missing required fields")
	} else if !strings.Contains(err.Error(), "Kind") {
		t.Error("wrong error type:", err)
	}
}

// Check that all fields are nil.
// Clearly silly, and a residue from a more interesting test with an earlier,
// different initialization property, but it once caught a compiler bug so
// it lives.
func checkInitialized(pb *pb2.GoTest, t *testing.T) {
	switch {
	case pb.F_BoolDefaulted != nil:
		t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
	case pb.F_Int32Defaulted != nil:
		t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
	case pb.F_Int64Defaulted != nil:
		t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
	case pb.F_Fixed32Defaulted != nil:
		t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
	case pb.F_Fixed64Defaulted != nil:
		t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
	case pb.F_Uint32Defaulted != nil:
		t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
	case pb.F_Uint64Defaulted != nil:
		t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
	case pb.F_FloatDefaulted != nil:
		t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
	case pb.F_DoubleDefaulted != nil:
		t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
	case pb.F_StringDefaulted != nil:
		t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
	case pb.F_BytesDefaulted != nil:
		t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
	case pb.F_Sint32Defaulted != nil:
		t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
	case pb.F_Sint64Defaulted != nil:
		t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
	}
}

// Does Reset() reset?
func TestReset(t *testing.T) {
	pb := initGoTest(true)
	// muck with some values
	pb.F_BoolDefaulted = proto.Bool(false)
	pb.F_Int32Defaulted = proto.Int32(237)
	pb.F_Int64Defaulted = proto.Int64(12346)
	pb.F_Fixed32Defaulted = proto.Uint32(32000)
	pb.F_Fixed64Defaulted = proto.Uint64(666)
	pb.F_Uint32Defaulted = proto.Uint32(323232)
	pb.F_Uint64Defaulted = nil
	pb.F_FloatDefaulted = nil
	pb.F_DoubleDefaulted = proto.Float64(0)
	pb.F_StringDefaulted = proto.String("gotcha")
	pb.F_BytesDefaulted = []byte("asdfasdf")
	pb.F_Sint32Defaulted = proto.Int32(123)
	pb.F_Sint64Defaulted = proto.Int64(789)
	pb.Reset()
	checkInitialized(pb, t)
}

// All required fields set, no defaults provided.
func TestEncodeDecode1(t *testing.T) {
	pb := initGoTest(false)
	overify(t, pb,
		protopack.Message{
			protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
			protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
			protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
			protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
			protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
			protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
			protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
			protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
			protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
			protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
			protopack.Tag{70, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
			},
			protopack.Tag{70, protopack.EndGroupType},
			protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
			protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
		}.Marshal())
}

// All required fields set, defaults provided.
func TestEncodeDecode2(t *testing.T) {
	pb := initGoTest(true)
	overify(t, pb,
		protopack.Message{
			protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
			protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
			protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
			protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
			protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
			protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
			protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
			protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
			protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
			protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
			protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
			protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
			protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
			protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
			protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
			protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
			protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
			protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
			protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
			protopack.Tag{70, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
			},
			protopack.Tag{70, protopack.EndGroupType},
			protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
			protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
			protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
			protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
		}.Marshal())
}

// All default fields set to their default value by hand
func TestEncodeDecode3(t *testing.T) {
	pb := initGoTest(false)
	pb.F_BoolDefaulted = proto.Bool(true)
	pb.F_Int32Defaulted = proto.Int32(32)
	pb.F_Int64Defaulted = proto.Int64(64)
	pb.F_Fixed32Defaulted = proto.Uint32(320)
	pb.F_Fixed64Defaulted = proto.Uint64(640)
	pb.F_Uint32Defaulted = proto.Uint32(3200)
	pb.F_Uint64Defaulted = proto.Uint64(6400)
	pb.F_FloatDefaulted = proto.Float32(314159)
	pb.F_DoubleDefaulted = proto.Float64(271828)
	pb.F_StringDefaulted = proto.String("hello, \"world!\"\n")
	pb.F_BytesDefaulted = []byte("Bignose")
	pb.F_Sint32Defaulted = proto.Int32(-32)
	pb.F_Sint64Defaulted = proto.Int64(-64)
	pb.F_Sfixed32Defaulted = proto.Int32(-32)
	pb.F_Sfixed64Defaulted = proto.Int64(-64)

	overify(t, pb,
		protopack.Message{
			protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
			protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
			protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
			protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
			protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
			protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
			protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
			protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
			protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
			protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
			protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
			protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
			protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
			protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
			protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
			protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
			protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
			protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
			protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
			protopack.Tag{70, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
			},
			protopack.Tag{70, protopack.EndGroupType},
			protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
			protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
			protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
			protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
		}.Marshal())
}

// All required fields set, defaults provided, all non-defaulted optional fields have values.
func TestEncodeDecode4(t *testing.T) {
	pb := initGoTest(true)
	pb.Table = proto.String("hello")
	pb.Param = proto.Int32(7)
	pb.OptionalField = initGoTestField()
	pb.F_BoolOptional = proto.Bool(true)
	pb.F_Int32Optional = proto.Int32(32)
	pb.F_Int64Optional = proto.Int64(64)
	pb.F_Fixed32Optional = proto.Uint32(3232)
	pb.F_Fixed64Optional = proto.Uint64(6464)
	pb.F_Uint32Optional = proto.Uint32(323232)
	pb.F_Uint64Optional = proto.Uint64(646464)
	pb.F_FloatOptional = proto.Float32(32.)
	pb.F_DoubleOptional = proto.Float64(64.)
	pb.F_StringOptional = proto.String("hello")
	pb.F_BytesOptional = []byte("Bignose")
	pb.F_Sint32Optional = proto.Int32(-32)
	pb.F_Sint64Optional = proto.Int64(-64)
	pb.F_Sfixed32Optional = proto.Int32(-32)
	pb.F_Sfixed64Optional = proto.Int64(-64)
	pb.Optionalgroup = initGoTest_OptionalGroup()

	overify(t, pb,
		protopack.Message{
			protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
			protopack.Tag{2, protopack.BytesType}, protopack.String("hello"),
			protopack.Tag{3, protopack.VarintType}, protopack.Varint(7),
			protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{6, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
			protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
			protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
			protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
			protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
			protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
			protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
			protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
			protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
			protopack.Tag{30, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{31, protopack.VarintType}, protopack.Varint(32),
			protopack.Tag{32, protopack.VarintType}, protopack.Varint(64),
			protopack.Tag{33, protopack.Fixed32Type}, protopack.Uint32(3232),
			protopack.Tag{34, protopack.Fixed64Type}, protopack.Uint64(6464),
			protopack.Tag{35, protopack.VarintType}, protopack.Uvarint(323232),
			protopack.Tag{36, protopack.VarintType}, protopack.Uvarint(646464),
			protopack.Tag{37, protopack.Fixed32Type}, protopack.Float32(32),
			protopack.Tag{38, protopack.Fixed64Type}, protopack.Float64(64),
			protopack.Tag{39, protopack.BytesType}, protopack.String("hello"),
			protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
			protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
			protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
			protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
			protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
			protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
			protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
			protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
			protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
			protopack.Tag{70, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
			},
			protopack.Tag{70, protopack.EndGroupType},
			protopack.Tag{90, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{91, protopack.BytesType}, protopack.String("optional"),
			},
			protopack.Tag{90, protopack.EndGroupType},
			protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
			protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
			protopack.Tag{301, protopack.BytesType}, protopack.Bytes("Bignose"),
			protopack.Tag{302, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{303, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{304, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{305, protopack.Fixed64Type}, protopack.Int64(-64),
			protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
			protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
		}.Marshal())
}

// All required fields set, defaults provided, all repeated fields given two values.
func TestEncodeDecode5(t *testing.T) {
	pb := initGoTest(true)
	pb.RepeatedField = []*pb2.GoTestField{initGoTestField(), initGoTestField()}
	pb.F_BoolRepeated = []bool{false, true}
	pb.F_Int32Repeated = []int32{32, 33}
	pb.F_Int64Repeated = []int64{64, 65}
	pb.F_Fixed32Repeated = []uint32{3232, 3333}
	pb.F_Fixed64Repeated = []uint64{6464, 6565}
	pb.F_Uint32Repeated = []uint32{323232, 333333}
	pb.F_Uint64Repeated = []uint64{646464, 656565}
	pb.F_FloatRepeated = []float32{32., 33.}
	pb.F_DoubleRepeated = []float64{64., 65.}
	pb.F_StringRepeated = []string{"hello", "sailor"}
	pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
	pb.F_Sint32Repeated = []int32{32, -32}
	pb.F_Sint64Repeated = []int64{64, -64}
	pb.F_Sfixed32Repeated = []int32{32, -32}
	pb.F_Sfixed64Repeated = []int64{64, -64}
	pb.Repeatedgroup = []*pb2.GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}

	overify(t, pb,
		protopack.Message{
			protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
			protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{5, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{5, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
			protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
			protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
			protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
			protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
			protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
			protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
			protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
			protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
			protopack.Tag{20, protopack.VarintType}, protopack.Bool(false),
			protopack.Tag{20, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{21, protopack.VarintType}, protopack.Varint(32),
			protopack.Tag{21, protopack.VarintType}, protopack.Varint(33),
			protopack.Tag{22, protopack.VarintType}, protopack.Varint(64),
			protopack.Tag{22, protopack.VarintType}, protopack.Varint(65),
			protopack.Tag{23, protopack.Fixed32Type}, protopack.Uint32(3232),
			protopack.Tag{23, protopack.Fixed32Type}, protopack.Uint32(3333),
			protopack.Tag{24, protopack.Fixed64Type}, protopack.Uint64(6464),
			protopack.Tag{24, protopack.Fixed64Type}, protopack.Uint64(6565),
			protopack.Tag{25, protopack.VarintType}, protopack.Uvarint(323232),
			protopack.Tag{25, protopack.VarintType}, protopack.Uvarint(333333),
			protopack.Tag{26, protopack.VarintType}, protopack.Uvarint(646464),
			protopack.Tag{26, protopack.VarintType}, protopack.Uvarint(656565),
			protopack.Tag{27, protopack.Fixed32Type}, protopack.Float32(32),
			protopack.Tag{27, protopack.Fixed32Type}, protopack.Float32(33),
			protopack.Tag{28, protopack.Fixed64Type}, protopack.Float64(64),
			protopack.Tag{28, protopack.Fixed64Type}, protopack.Float64(65),
			protopack.Tag{29, protopack.BytesType}, protopack.String("hello"),
			protopack.Tag{29, protopack.BytesType}, protopack.String("sailor"),
			protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
			protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
			protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
			protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
			protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
			protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
			protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
			protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
			protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
			protopack.Tag{70, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
			},
			protopack.Tag{70, protopack.EndGroupType},
			protopack.Tag{80, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{81, protopack.BytesType}, protopack.String("repeated"),
			},
			protopack.Tag{80, protopack.EndGroupType},
			protopack.Tag{80, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{81, protopack.BytesType}, protopack.String("repeated"),
			},
			protopack.Tag{80, protopack.EndGroupType},
			protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
			protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
			protopack.Tag{201, protopack.BytesType}, protopack.Bytes("big"),
			protopack.Tag{201, protopack.BytesType}, protopack.Bytes("nose"),
			protopack.Tag{202, protopack.VarintType}, protopack.Svarint(32),
			protopack.Tag{202, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{203, protopack.VarintType}, protopack.Svarint(64),
			protopack.Tag{203, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{204, protopack.Fixed32Type}, protopack.Int32(32),
			protopack.Tag{204, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{205, protopack.Fixed64Type}, protopack.Int64(64),
			protopack.Tag{205, protopack.Fixed64Type}, protopack.Int64(-64),
			protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
			protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
		}.Marshal())
}

// All required fields set, all packed repeated fields given two values.
func TestEncodeDecode6(t *testing.T) {
	pb := initGoTest(false)
	pb.F_BoolRepeatedPacked = []bool{false, true}
	pb.F_Int32RepeatedPacked = []int32{32, 33}
	pb.F_Int64RepeatedPacked = []int64{64, 65}
	pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
	pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
	pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
	pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
	pb.F_FloatRepeatedPacked = []float32{32., 33.}
	pb.F_DoubleRepeatedPacked = []float64{64., 65.}
	pb.F_Sint32RepeatedPacked = []int32{32, -32}
	pb.F_Sint64RepeatedPacked = []int64{64, -64}
	pb.F_Sfixed32RepeatedPacked = []int32{32, -32}
	pb.F_Sfixed64RepeatedPacked = []int64{64, -64}

	overify(t, pb,
		protopack.Message{
			protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
			protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
				protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
			}),
			protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
			protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
			protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
			protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
			protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
			protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
			protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
			protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
			protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
			protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
			protopack.Tag{50, protopack.BytesType}, protopack.LengthPrefix{protopack.Bool(false), protopack.Bool(true)},
			protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{protopack.Varint(32), protopack.Varint(33)},
			protopack.Tag{52, protopack.BytesType}, protopack.LengthPrefix{protopack.Varint(64), protopack.Varint(65)},
			protopack.Tag{53, protopack.BytesType}, protopack.LengthPrefix{protopack.Uint32(3232), protopack.Uint32(3333)},
			protopack.Tag{54, protopack.BytesType}, protopack.LengthPrefix{protopack.Uint64(6464), protopack.Uint64(6565)},
			protopack.Tag{55, protopack.BytesType}, protopack.LengthPrefix{protopack.Uvarint(323232), protopack.Uvarint(333333)},
			protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix{protopack.Uvarint(646464), protopack.Uvarint(656565)},
			protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix{protopack.Float32(32), protopack.Float32(33)},
			protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix{protopack.Float64(64), protopack.Float64(65)},
			protopack.Tag{70, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
			},
			protopack.Tag{70, protopack.EndGroupType},
			protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
			protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
			protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
			protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
			protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
			protopack.Tag{502, protopack.BytesType}, protopack.LengthPrefix{protopack.Svarint(32), protopack.Svarint(-32)},
			protopack.Tag{503, protopack.BytesType}, protopack.LengthPrefix{protopack.Svarint(64), protopack.Svarint(-64)},
			protopack.Tag{504, protopack.BytesType}, protopack.LengthPrefix{protopack.Int32(32), protopack.Int32(-32)},
			protopack.Tag{505, protopack.BytesType}, protopack.LengthPrefix{protopack.Int64(64), protopack.Int64(-64)},
		}.Marshal())
}

// Test that we can encode empty bytes fields.
func TestEncodeDecodeBytes1(t *testing.T) {
	pb := initGoTest(false)

	// Create our bytes
	pb.F_BytesRequired = []byte{}
	pb.F_BytesRepeated = [][]byte{{}}
	pb.F_BytesOptional = []byte{}

	d, err := proto.Marshal(pb)
	if err != nil {
		t.Error(err)
	}

	pbd := new(pb2.GoTest)
	if err := proto.Unmarshal(d, pbd); err != nil {
		t.Error(err)
	}

	if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
		t.Error("required empty bytes field is incorrect")
	}
	if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
		t.Error("repeated empty bytes field is incorrect")
	}
	if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
		t.Error("optional empty bytes field is incorrect")
	}
}

// Test that we encode nil-valued fields of a repeated bytes field correctly.
// Since entries in a repeated field cannot be nil, nil must mean empty value.
func TestEncodeDecodeBytes2(t *testing.T) {
	pb := initGoTest(false)

	// Create our bytes
	pb.F_BytesRepeated = [][]byte{nil}

	d, err := proto.Marshal(pb)
	if err != nil {
		t.Error(err)
	}

	pbd := new(pb2.GoTest)
	if err := proto.Unmarshal(d, pbd); err != nil {
		t.Error(err)
	}

	if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
		t.Error("Unexpected value for repeated bytes field")
	}
}

// All required fields set, defaults provided, all repeated fields given two values.
func TestSkippingUnrecognizedFields(t *testing.T) {
	o := new(proto.Buffer)
	pb := initGoTestField()

	// Marshal it normally.
	o.Marshal(pb)

	// Now new a GoSkipTest record.
	skip := &pb2.GoSkipTest{
		SkipInt32:   proto.Int32(32),
		SkipFixed32: proto.Uint32(3232),
		SkipFixed64: proto.Uint64(6464),
		SkipString:  proto.String("skipper"),
		Skipgroup: &pb2.GoSkipTest_SkipGroup{
			GroupInt32:  proto.Int32(75),
			GroupString: proto.String("wxyz"),
		},
	}

	// Marshal it into same buffer.
	o.Marshal(skip)

	pbd := new(pb2.GoTestField)
	o.Unmarshal(pbd)

	// The __unrecognized field should be a marshaling of GoSkipTest
	skipd := new(pb2.GoSkipTest)

	o.SetBuf(pbd.XXX_unrecognized)
	o.Unmarshal(skipd)

	switch {
	case *skipd.SkipInt32 != *skip.SkipInt32:
		t.Error("skip int32", skipd.SkipInt32)
	case *skipd.SkipFixed32 != *skip.SkipFixed32:
		t.Error("skip fixed32", skipd.SkipFixed32)
	case *skipd.SkipFixed64 != *skip.SkipFixed64:
		t.Error("skip fixed64", skipd.SkipFixed64)
	case *skipd.SkipString != *skip.SkipString:
		t.Error("skip string", *skipd.SkipString)
	case *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32:
		t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
	case *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString:
		t.Error("skip group string", *skipd.Skipgroup.GroupString)
	}
}

// Check that unrecognized fields of a submessage are preserved.
func TestSubmessageUnrecognizedFields(t *testing.T) {
	nm := &pb2.NewMessage{
		Nested: &pb2.NewMessage_Nested{
			Name:      proto.String("Nigel"),
			FoodGroup: proto.String("carbs"),
		},
	}
	b, err := proto.Marshal(nm)
	if err != nil {
		t.Fatalf("Marshal of NewMessage: %v", err)
	}

	// Unmarshal into an OldMessage.
	om := new(pb2.OldMessage)
	if err := proto.Unmarshal(b, om); err != nil {
		t.Fatalf("Unmarshal to OldMessage: %v", err)
	}
	exp := &pb2.OldMessage{
		Nested: &pb2.OldMessage_Nested{
			Name: proto.String("Nigel"),
			// normal protocol buffer users should not do this
			XXX_unrecognized: []byte("\x12\x05carbs"),
		},
	}
	if !proto.Equal(om, exp) {
		t.Errorf("om = %v, want %v", om, exp)
	}

	// Clone the OldMessage.
	om = proto.Clone(om).(*pb2.OldMessage)
	if !proto.Equal(om, exp) {
		t.Errorf("Clone(om) = %v, want %v", om, exp)
	}

	// Marshal the OldMessage, then unmarshal it into an empty NewMessage.
	if b, err = proto.Marshal(om); err != nil {
		t.Fatalf("Marshal of OldMessage: %v", err)
	}
	t.Logf("Marshal(%v) -> %q", om, b)
	nm2 := new(pb2.NewMessage)
	if err := proto.Unmarshal(b, nm2); err != nil {
		t.Fatalf("Unmarshal to NewMessage: %v", err)
	}
	if !proto.Equal(nm, nm2) {
		t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
	}
}

// Check that an int32 field can be upgraded to an int64 field.
func TestNegativeInt32(t *testing.T) {
	om := &pb2.OldMessage{
		Num: proto.Int32(-1),
	}
	b, err := proto.Marshal(om)
	if err != nil {
		t.Fatalf("Marshal of OldMessage: %v", err)
	}

	// Check the size. It should be 11 bytes;
	// 1 for the field/wire type, and 10 for the negative number.
	if len(b) != 11 {
		t.Errorf("%v marshaled as %q, wanted 11 bytes", om, b)
	}

	// Unmarshal into a NewMessage.
	nm := new(pb2.NewMessage)
	if err := proto.Unmarshal(b, nm); err != nil {
		t.Fatalf("Unmarshal to NewMessage: %v", err)
	}
	want := &pb2.NewMessage{
		Num: proto.Int64(-1),
	}
	if !proto.Equal(nm, want) {
		t.Errorf("nm = %v, want %v", nm, want)
	}
}

// Check that we can grow an array (repeated field) to have many elements.
// This test doesn't depend only on our encoding; for variety, it makes sure
// we create, encode, and decode the correct contents explicitly.  It's therefore
// a bit messier.
// This test also uses (and hence tests) the Marshal/Unmarshal functions
// instead of the methods.
func TestBigRepeated(t *testing.T) {
	pb := initGoTest(true)

	// Create the arrays
	const N = 50 // Internally the library starts much smaller.
	pb.Repeatedgroup = make([]*pb2.GoTest_RepeatedGroup, N)
	pb.F_Sint64Repeated = make([]int64, N)
	pb.F_Sint32Repeated = make([]int32, N)
	pb.F_BytesRepeated = make([][]byte, N)
	pb.F_StringRepeated = make([]string, N)
	pb.F_DoubleRepeated = make([]float64, N)
	pb.F_FloatRepeated = make([]float32, N)
	pb.F_Uint64Repeated = make([]uint64, N)
	pb.F_Uint32Repeated = make([]uint32, N)
	pb.F_Fixed64Repeated = make([]uint64, N)
	pb.F_Fixed32Repeated = make([]uint32, N)
	pb.F_Int64Repeated = make([]int64, N)
	pb.F_Int32Repeated = make([]int32, N)
	pb.F_BoolRepeated = make([]bool, N)
	pb.RepeatedField = make([]*pb2.GoTestField, N)

	// Fill in the arrays with checkable values.
	igtf := initGoTestField()
	igtrg := initGoTest_RepeatedGroup()
	for i := 0; i < N; i++ {
		pb.Repeatedgroup[i] = igtrg
		pb.F_Sint64Repeated[i] = int64(i)
		pb.F_Sint32Repeated[i] = int32(i)
		s := fmt.Sprint(i)
		pb.F_BytesRepeated[i] = []byte(s)
		pb.F_StringRepeated[i] = s
		pb.F_DoubleRepeated[i] = float64(i)
		pb.F_FloatRepeated[i] = float32(i)
		pb.F_Uint64Repeated[i] = uint64(i)
		pb.F_Uint32Repeated[i] = uint32(i)
		pb.F_Fixed64Repeated[i] = uint64(i)
		pb.F_Fixed32Repeated[i] = uint32(i)
		pb.F_Int64Repeated[i] = int64(i)
		pb.F_Int32Repeated[i] = int32(i)
		pb.F_BoolRepeated[i] = i%2 == 0
		pb.RepeatedField[i] = igtf
	}

	// Marshal.
	buf, _ := proto.Marshal(pb)

	// Now test Unmarshal by recreating the original buffer.
	pbd := new(pb2.GoTest)
	proto.Unmarshal(buf, pbd)

	// Check the checkable values
	for i := uint64(0); i < N; i++ {
		switch {
		case pbd.Repeatedgroup[i] == nil:
			t.Error("pbd.Repeatedgroup bad")
		case uint64(pbd.F_Sint64Repeated[i]) != i:
			t.Error("pbd.F_Sint64Repeated bad", uint64(pbd.F_Sint64Repeated[i]), i)
		case uint64(pbd.F_Sint32Repeated[i]) != i:
			t.Error("pbd.F_Sint32Repeated bad", uint64(pbd.F_Sint32Repeated[i]), i)
		case !bytes.Equal(pbd.F_BytesRepeated[i], []byte(fmt.Sprint(i))):
			t.Error("pbd.F_BytesRepeated bad", pbd.F_BytesRepeated[i], i)
		case pbd.F_StringRepeated[i] != string(fmt.Sprint(i)):
			t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
		case uint64(pbd.F_DoubleRepeated[i]) != i:
			t.Error("pbd.F_DoubleRepeated bad", uint64(pbd.F_DoubleRepeated[i]), i)
		case uint64(pbd.F_FloatRepeated[i]) != i:
			t.Error("pbd.F_FloatRepeated bad", uint64(pbd.F_FloatRepeated[i]), i)
		case pbd.F_Uint64Repeated[i] != i:
			t.Error("pbd.F_Uint64Repeated bad", pbd.F_Uint64Repeated[i], i)
		case uint64(pbd.F_Uint32Repeated[i]) != i:
			t.Error("pbd.F_Uint32Repeated bad", uint64(pbd.F_Uint32Repeated[i]), i)
		case pbd.F_Fixed64Repeated[i] != i:
			t.Error("pbd.F_Fixed64Repeated bad", pbd.F_Fixed64Repeated[i], i)
		case uint64(pbd.F_Fixed32Repeated[i]) != i:
			t.Error("pbd.F_Fixed32Repeated bad", uint64(pbd.F_Fixed32Repeated[i]), i)
		case uint64(pbd.F_Int64Repeated[i]) != i:
			t.Error("pbd.F_Int64Repeated bad", uint64(pbd.F_Int64Repeated[i]), i)
		case uint64(pbd.F_Int32Repeated[i]) != i:
			t.Error("pbd.F_Int32Repeated bad", uint64(pbd.F_Int32Repeated[i]), i)
		case pbd.F_BoolRepeated[i] != (i%2 == 0):
			t.Error("pbd.F_BoolRepeated bad", pbd.F_BoolRepeated[i], i)
		case pbd.RepeatedField[i] == nil:
			t.Error("pbd.RepeatedField bad")
		}
	}
}

func TestBadWireTypeUnknown(t *testing.T) {
	b := protopack.Message{
		protopack.Tag{1, protopack.BytesType}, protopack.Bytes("x"),
		protopack.Tag{1, protopack.Fixed32Type}, protopack.Uint32(0),
		protopack.Tag{1, protopack.VarintType}, protopack.Varint(11),
		protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(22),
		protopack.Tag{2, protopack.BytesType}, protopack.String("aaa"),
		protopack.Tag{2, protopack.Fixed32Type}, protopack.Uint32(33),
		protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(44),
		protopack.Tag{4, protopack.BytesType}, protopack.String("bbb"),
		protopack.Tag{4, protopack.Fixed32Type}, protopack.Uint32(55),
		protopack.Tag{4, protopack.BytesType}, protopack.String("ccc"),
		protopack.Tag{4, protopack.Fixed64Type}, protopack.Uint64(66),
		protopack.Tag{11, protopack.VarintType}, protopack.Uvarint(77),
		protopack.Tag{11, protopack.BytesType}, protopack.Bytes("ddd"),
		protopack.Tag{11, protopack.Fixed64Type}, protopack.Float64(88),
		protopack.Tag{11, protopack.Fixed32Type}, protopack.Uint32(99),
	}.Marshal()

	m := new(pb2.MyMessage)
	if err := proto.Unmarshal(b, m); err != nil {
		t.Errorf("unexpected Unmarshal error: %v", err)
	}

	unknown := protopack.Message{
		protopack.Tag{1, protopack.BytesType}, protopack.Bytes("x"),
		protopack.Tag{1, protopack.Fixed32Type}, protopack.Uint32(0),
		protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(22),
		protopack.Tag{2, protopack.Fixed32Type}, protopack.Uint32(33),
		protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(44),
		protopack.Tag{4, protopack.Fixed32Type}, protopack.Uint32(55),
		protopack.Tag{4, protopack.Fixed64Type}, protopack.Uint64(66),
		protopack.Tag{11, protopack.VarintType}, protopack.Uvarint(77),
		protopack.Tag{11, protopack.BytesType}, protopack.Bytes("ddd"),
		protopack.Tag{11, protopack.Fixed32Type}, protopack.Uint32(99),
	}.Marshal()
	if !bytes.Equal(m.XXX_unrecognized, unknown) {
		t.Errorf("unknown bytes mismatch:\ngot  %x\nwant %x", m.XXX_unrecognized, unknown)
	}
	proto.DiscardUnknown(m)

	want := &pb2.MyMessage{Count: proto.Int32(11), Name: proto.String("aaa"), Pet: []string{"bbb", "ccc"}, Bigfloat: proto.Float64(88)}
	if !proto.Equal(m, want) {
		t.Errorf("message mismatch:\ngot  %v\nwant %v", m, want)
	}
}

func encodeDecode(t *testing.T, in, out proto.Message, msg string) {
	buf, err := proto.Marshal(in)
	if err != nil {
		t.Fatalf("failed marshaling %v: %v", msg, err)
	}
	if err := proto.Unmarshal(buf, out); err != nil {
		t.Fatalf("failed unmarshaling %v: %v", msg, err)
	}
}

func TestPackedNonPackedDecoderSwitching(t *testing.T) {
	np, p := new(pb2.NonPackedTest), new(pb2.PackedTest)

	// non-packed -> packed
	np.A = []int32{0, 1, 1, 2, 3, 5}
	encodeDecode(t, np, p, "non-packed -> packed")
	if !reflect.DeepEqual(np.A, p.B) {
		t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
	}

	// packed -> non-packed
	np.Reset()
	p.B = []int32{3, 1, 4, 1, 5, 9}
	encodeDecode(t, p, np, "packed -> non-packed")
	if !reflect.DeepEqual(p.B, np.A) {
		t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
	}
}

func TestProto1RepeatedGroup(t *testing.T) {
	pb := &pb2.MessageList{
		Message: []*pb2.MessageList_Message{
			{
				Name:  proto.String("blah"),
				Count: proto.Int32(7),
			},
			// NOTE: pb.Message[1] is a nil
			nil,
		},
	}

	o := new(proto.Buffer)
	err := o.Marshal(pb)
	if err == nil {
		t.Fatalf("expected error when marshaling repeted nil MessageList.Message")
	}
	if _, ok := err.(*proto.RequiredNotSetError); !ok {
		t.Fatalf("unexpected error when marshaling: %v", err)
	}
}

// Test that enums work.  Checks for a bug introduced by making enums
// named types instead of int32: newInt32FromUint64 would crash with
// a type mismatch in reflect.PointTo.
func TestEnum(t *testing.T) {
	pb := new(pb2.GoEnum)
	pb.Foo = pb2.FOO_FOO1.Enum()
	o := new(proto.Buffer)
	if err := o.Marshal(pb); err != nil {
		t.Fatal("error encoding enum:", err)
	}
	pb1 := new(pb2.GoEnum)
	if err := o.Unmarshal(pb1); err != nil {
		t.Fatal("error decoding enum:", err)
	}
	if *pb1.Foo != pb2.FOO_FOO1 {
		t.Error("expected 7 but got ", *pb1.Foo)
	}
}

// Enum types have String methods. Check that enum fields can be printed.
// We don't care what the value actually is, just as long as it doesn't crash.
func TestPrintingNilEnumFields(t *testing.T) {
	pb := new(pb2.GoEnum)
	_ = fmt.Sprintf("%+v", pb)
}

// Verify that absent required fields cause Marshal/Unmarshal to return errors.
func TestRequiredFieldEnforcement(t *testing.T) {
	pb := new(pb2.GoTestField)
	_, err := proto.Marshal(pb)
	if err == nil {
		t.Error("marshal: expected error, got nil")
	} else if !isRequiredNotSetError(err) {
		t.Errorf("marshal: bad error type: %v", err)
	}

	// A slightly sneaky, yet valid, proto. It encodes the same required field twice,
	// so simply counting the required fields is insufficient.
	// field 1, encoding 2, value "hi"
	buf := []byte("\x0A\x02hi\x0A\x02hi")
	err = proto.Unmarshal(buf, pb)
	if err == nil {
		t.Error("unmarshal: expected error, got nil")
	} else if !isRequiredNotSetError(err) {
		t.Errorf("unmarshal: bad error type: %v", err)
	}
}

// Verify that absent required fields in groups cause Marshal/Unmarshal to return errors.
func TestRequiredFieldEnforcementGroups(t *testing.T) {
	pb := &pb2.GoTestRequiredGroupField{Group: &pb2.GoTestRequiredGroupField_Group{}}
	if _, err := proto.Marshal(pb); err == nil {
		t.Error("marshal: expected error, got nil")
	} else if !isRequiredNotSetError(err) {
		t.Errorf("marshal: bad error type: %v", err)
	}

	buf := []byte{11, 12}
	if err := proto.Unmarshal(buf, pb); err == nil {
		t.Error("unmarshal: expected error, got nil")
	} else if !isRequiredNotSetError(err) {
		t.Errorf("unmarshal: bad error type: %v", err)
	}
}

func TestTypedNilMarshal(t *testing.T) {
	// A typed nil should return ErrNil and not crash.
	var m *pb2.GoEnum
	if _, err := proto.Marshal(m); err != proto.ErrNil {
		t.Errorf("Marshal(%#v): got %v, want ErrNil", m, err)
	}
}

func TestTypedNilMarshalInOneof(t *testing.T) {
	// It should not panic.
	m := &pb2.Communique{Union: &pb2.Communique_Msg{nil}}
	if _, err := proto.Marshal(m); err == proto.ErrNil {
		t.Errorf("Marshal(%#v): got %v, want nil or errOneofHasNil", m, err)
	}
}

// A type that implements the Marshaler interface, but is not nillable.
type nonNillableInt uint64

func (nni nonNillableInt) Marshal() ([]byte, error) {
	return proto.EncodeVarint(uint64(nni)), nil
}

type NNIMessage struct {
	nni nonNillableInt
}

func (*NNIMessage) Reset()         {}
func (*NNIMessage) String() string { return "" }
func (*NNIMessage) ProtoMessage()  {}

type NMMessage struct{}

func (*NMMessage) Reset()         {}
func (*NMMessage) String() string { return "" }
func (*NMMessage) ProtoMessage()  {}

// Verify a type that uses the Marshaler interface, but has a nil pointer.
func TestNilMarshaler(t *testing.T) {
	// Try a struct with a Marshaler field that is nil.
	// It should be directly marshable.
	nmm := new(NMMessage)
	if _, err := proto.Marshal(nmm); err != nil {
		t.Error("unexpected error marshaling nmm: ", err)
	}

	// Try a struct with a Marshaler field that is not nillable.
	nnim := new(NNIMessage)
	nnim.nni = 7
	var _ proto.Marshaler = nnim.nni // verify it is truly a Marshaler
	if _, err := proto.Marshal(nnim); err != nil {
		t.Error("unexpected error marshaling nnim: ", err)
	}
}

func TestAllSetDefaults(t *testing.T) {
	// Exercise SetDefaults with all scalar field types.
	got := &pb2.Defaults{
		// NaN != NaN, so override that here.
		F_Nan: proto.Float32(1.7),
	}
	want := &pb2.Defaults{
		F_Bool:    proto.Bool(true),
		F_Int32:   proto.Int32(32),
		F_Int64:   proto.Int64(64),
		F_Fixed32: proto.Uint32(320),
		F_Fixed64: proto.Uint64(640),
		F_Uint32:  proto.Uint32(3200),
		F_Uint64:  proto.Uint64(6400),
		F_Float:   proto.Float32(314159),
		F_Double:  proto.Float64(271828),
		F_String:  proto.String(`hello, "world!"` + "\n"),
		F_Bytes:   []byte("Bignose"),
		F_Sint32:  proto.Int32(-32),
		F_Sint64:  proto.Int64(-64),
		F_Enum:    pb2.Defaults_GREEN.Enum(),
		F_Pinf:    proto.Float32(float32(math.Inf(1))),
		F_Ninf:    proto.Float32(float32(math.Inf(-1))),
		F_Nan:     proto.Float32(1.7),
		StrZero:   proto.String(""),
	}
	proto.SetDefaults(got)
	if !proto.Equal(got, want) {
		t.Errorf("SetDefaults failed\n got %v\nwant %v", got, want)
	}
}

func TestSetDefaultsWithSetField(t *testing.T) {
	// Check that a set value is not overridden.
	m := &pb2.Defaults{
		F_Int32: proto.Int32(12),
	}
	proto.SetDefaults(m)
	if v := m.GetF_Int32(); v != 12 {
		t.Errorf("m.FInt32 = %v, want 12", v)
	}
}

func TestSetDefaultsWithSubMessage(t *testing.T) {
	got := &pb2.OtherMessage{
		Key: proto.Int64(123),
		Inner: &pb2.InnerMessage{
			Host: proto.String("gopher"),
		},
	}
	want := &pb2.OtherMessage{
		Key: proto.Int64(123),
		Inner: &pb2.InnerMessage{
			Host: proto.String("gopher"),
			Port: proto.Int32(4000),
		},
	}
	proto.SetDefaults(got)
	if !proto.Equal(got, want) {
		t.Errorf("\n got %v\nwant %v", got, want)
	}
}

func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
	got := &pb2.MyMessage{
		RepInner: []*pb2.InnerMessage{{}},
	}
	want := &pb2.MyMessage{
		RepInner: []*pb2.InnerMessage{{
			Port: proto.Int32(4000),
		}},
	}
	proto.SetDefaults(got)
	if !proto.Equal(got, want) {
		t.Errorf("\n got %v\nwant %v", got, want)
	}
}

func TestSetDefaultWithRepeatedNonMessage(t *testing.T) {
	got := &pb2.MyMessage{
		Pet: []string{"turtle", "wombat"},
	}
	want := proto.Clone(got)
	proto.SetDefaults(got)
	if !proto.Equal(got, want) {
		t.Errorf("\n got %v\nwant %v", got, want)
	}
}

func TestMaximumTagNumber(t *testing.T) {
	m := &pb2.MaxTag{
		LastField: proto.String("natural goat essence"),
	}
	buf, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("proto.Marshal failed: %v", err)
	}
	m2 := new(pb2.MaxTag)
	if err := proto.Unmarshal(buf, m2); err != nil {
		t.Fatalf("proto.Unmarshal failed: %v", err)
	}
	if got, want := m2.GetLastField(), *m.LastField; got != want {
		t.Errorf("got %q, want %q", got, want)
	}
}

func TestJSON(t *testing.T) {
	m := &pb2.MyMessage{
		Count: proto.Int32(4),
		Pet:   []string{"bunny", "kitty"},
		Inner: &pb2.InnerMessage{
			Host: proto.String("cauchy"),
		},
		Bikeshed: pb2.MyMessage_GREEN.Enum(),
	}
	const want = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`

	b, err := json.Marshal(m)
	if err != nil {
		t.Fatalf("json.Marshal failed: %v", err)
	}
	s := string(b)
	if s != want {
		t.Errorf("got  %s\nwant %s", s, want)
	}

	received := new(pb2.MyMessage)
	if err := json.Unmarshal(b, received); err != nil {
		t.Fatalf("json.Unmarshal failed: %v", err)
	}
	if !proto.Equal(received, m) {
		t.Fatalf("got %s, want %s", received, m)
	}

	// Test unmarshaling of JSON with symbolic enum name.
	const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
	received.Reset()
	if err := json.Unmarshal([]byte(old), received); err != nil {
		t.Fatalf("json.Unmarshal failed: %v", err)
	}
	if !proto.Equal(received, m) {
		t.Fatalf("got %s, want %s", received, m)
	}
}

func TestBadWireType(t *testing.T) {
	b := []byte{7<<3 | 6} // field 7, wire type 6
	pb := new(pb2.OtherMessage)
	if err := proto.Unmarshal(b, pb); err == nil {
		t.Errorf("Unmarshal did not fail")
	}
}

func TestBytesWithInvalidLength(t *testing.T) {
	// If a byte sequence has an invalid (negative) length, Unmarshal should not panic.
	b := protopack.Message{
		protopack.Tag{2, protopack.BytesType}, protopack.Denormalized{+1, protopack.Uvarint(34359738367)},
	}.Marshal()
	proto.Unmarshal(b, new(pb2.MyMessage))
}

func TestLengthOverflow(t *testing.T) {
	// Overflowing a length should not panic.
	b := protopack.Message{
		protopack.Tag{2, protopack.BytesType}, protopack.String("\x01"),
		protopack.Tag{3, protopack.BytesType}, protopack.Uvarint(9223372036854775807),
		protopack.Raw("\x01"),
	}.Marshal()
	proto.Unmarshal(b, new(pb2.MyMessage))
}

func TestVarintOverflow(t *testing.T) {
	// Overflowing a 64-bit length should not be allowed.
	b := protopack.Message{
		protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
		protopack.Tag{3, protopack.BytesType},
		protopack.Raw("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01"),
	}.Marshal()
	if err := proto.Unmarshal(b, new(pb2.MyMessage)); err == nil {
		t.Fatalf("Overflowed uint64 length without error")
	}
}

func TestBytesWithInvalidLengthInGroup(t *testing.T) {
	// Overflowing a 64-bit length should not be allowed.
	b := protopack.Message{
		protopack.Tag{775, protopack.StartGroupType},
		protopack.Message{
			protopack.Tag{774, protopack.BytesType}, protopack.Uvarint(13654841034505509168),
			protopack.Raw(""),
		},
	}.Marshal()
	if err := proto.Unmarshal(b, new(pb2.MyMessage)); err == nil {
		t.Fatalf("Overflowed uint64 length without error")
	}
}

func TestUnmarshalFuzz(t *testing.T) {
	const N = 1000
	seed := time.Now().UnixNano()
	t.Logf("RNG seed is %d", seed)
	rng := rand.New(rand.NewSource(seed))
	buf := make([]byte, 20)
	for i := 0; i < N; i++ {
		for j := range buf {
			buf[j] = byte(rng.Intn(256))
		}
		fuzzUnmarshal(t, buf)
	}
}

func TestMergeMessages(t *testing.T) {
	pb := &pb2.MessageList{Message: []*pb2.MessageList_Message{{Name: proto.String("x"), Count: proto.Int32(1)}}}
	data, err := proto.Marshal(pb)
	if err != nil {
		t.Fatalf("Marshal: %v", err)
	}

	pb1 := new(pb2.MessageList)
	if err := proto.Unmarshal(data, pb1); err != nil {
		t.Fatalf("first Unmarshal: %v", err)
	}
	if err := proto.Unmarshal(data, pb1); err != nil {
		t.Fatalf("second Unmarshal: %v", err)
	}
	if len(pb1.Message) != 1 {
		t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
	}

	pb2 := new(pb2.MessageList)
	if err := proto.UnmarshalMerge(data, pb2); err != nil {
		t.Fatalf("first UnmarshalMerge: %v", err)
	}
	if err := proto.UnmarshalMerge(data, pb2); err != nil {
		t.Fatalf("second UnmarshalMerge: %v", err)
	}
	if len(pb2.Message) != 2 {
		t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
	}
}

func TestExtensionMarshalOrder(t *testing.T) {
	m := &pb2.MyMessage{Count: proto.Int(123)}
	if err := proto.SetExtension(m, pb2.E_Ext_More, &pb2.Ext{Data: proto.String("alpha")}); err != nil {
		t.Fatalf("SetExtension: %v", err)
	}
	if err := proto.SetExtension(m, pb2.E_Ext_Text, proto.String("aleph")); err != nil {
		t.Fatalf("SetExtension: %v", err)
	}
	if err := proto.SetExtension(m, pb2.E_Ext_Number, proto.Int32(1)); err != nil {
		t.Fatalf("SetExtension: %v", err)
	}

	// Serialize m several times, and check we get the same bytes each time.
	var orig []byte
	for i := 0; i < 100; i++ {
		b, err := proto.Marshal(m)
		if err != nil {
			t.Fatalf("Marshal: %v", err)
		}
		if i == 0 {
			orig = b
			continue
		}
		if !bytes.Equal(b, orig) {
			t.Errorf("Bytes differ on attempt #%d", i)
		}
	}
}

func TestExtensionMapFieldMarshalDeterministic(t *testing.T) {
	m := &pb2.MyMessage{Count: proto.Int(123)}
	if err := proto.SetExtension(m, pb2.E_Ext_More, &pb2.Ext{MapField: map[int32]int32{1: 1, 2: 2, 3: 3, 4: 4}}); err != nil {
		t.Fatalf("SetExtension: %v", err)
	}
	marshal := func(m proto.Message) []byte {
		var b proto.Buffer
		b.SetDeterministic(true)
		if err := b.Marshal(m); err != nil {
			t.Fatalf("Marshal failed: %v", err)
		}
		return b.Bytes()
	}

	want := marshal(m)
	for i := 0; i < 100; i++ {
		if got := marshal(m); !bytes.Equal(got, want) {
			t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
		}
	}
}

func TestUnmarshalMergesMessages(t *testing.T) {
	// If a nested message occurs twice in the input,
	// the fields should be merged when decoding.
	a := &pb2.OtherMessage{
		Key: proto.Int64(123),
		Inner: &pb2.InnerMessage{
			Host: proto.String("polhode"),
			Port: proto.Int32(1234),
		},
	}
	aData, err := proto.Marshal(a)
	if err != nil {
		t.Fatalf("Marshal(a): %v", err)
	}
	b := &pb2.OtherMessage{
		Weight: proto.Float32(1.2),
		Inner: &pb2.InnerMessage{
			Host:      proto.String("herpolhode"),
			Connected: proto.Bool(true),
		},
	}
	bData, err := proto.Marshal(b)
	if err != nil {
		t.Fatalf("Marshal(b): %v", err)
	}
	want := &pb2.OtherMessage{
		Key:    proto.Int64(123),
		Weight: proto.Float32(1.2),
		Inner: &pb2.InnerMessage{
			Host:      proto.String("herpolhode"),
			Port:      proto.Int32(1234),
			Connected: proto.Bool(true),
		},
	}
	got := new(pb2.OtherMessage)
	if err := proto.Unmarshal(append(aData, bData...), got); err != nil {
		t.Fatalf("Unmarshal: %v", err)
	}
	if !proto.Equal(got, want) {
		t.Errorf("\n got %v\nwant %v", got, want)
	}
}

func TestUnmarshalMergesGroups(t *testing.T) {
	// If a nested group occurs twice in the input,
	// the fields should be merged when decoding.
	a := &pb2.GroupNew{
		G: &pb2.GroupNew_G{
			X: proto.Int32(7),
			Y: proto.Int32(8),
		},
	}
	aData, err := proto.Marshal(a)
	if err != nil {
		t.Fatalf("Marshal(a): %v", err)
	}
	b := &pb2.GroupNew{
		G: &pb2.GroupNew_G{
			X: proto.Int32(9),
		},
	}
	bData, err := proto.Marshal(b)
	if err != nil {
		t.Fatalf("Marshal(b): %v", err)
	}
	want := &pb2.GroupNew{
		G: &pb2.GroupNew_G{
			X: proto.Int32(9),
			Y: proto.Int32(8),
		},
	}
	got := new(pb2.GroupNew)
	if err := proto.Unmarshal(append(aData, bData...), got); err != nil {
		t.Fatalf("Unmarshal: %v", err)
	}
	if !proto.Equal(got, want) {
		t.Errorf("\n got %v\nwant %v", got, want)
	}
}

func TestEncodingSizes(t *testing.T) {
	tests := []struct {
		m proto.Message
		n int
	}{
		{&pb2.Defaults{F_Int32: proto.Int32(math.MaxInt32)}, 6},
		{&pb2.Defaults{F_Int32: proto.Int32(math.MinInt32)}, 11},
		{&pb2.Defaults{F_Uint32: proto.Uint32(uint32(math.MaxInt32) + 1)}, 6},
		{&pb2.Defaults{F_Uint32: proto.Uint32(math.MaxUint32)}, 6},
	}
	for _, test := range tests {
		b, err := proto.Marshal(test.m)
		if err != nil {
			t.Errorf("Marshal(%v): %v", test.m, err)
			continue
		}
		if len(b) != test.n {
			t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
		}
	}
}

func TestRequiredNotSetError(t *testing.T) {
	pb := initGoTest(false)
	pb.RequiredField.Label = nil
	pb.F_Int32Required = nil
	pb.F_Int64Required = nil

	want := protopack.Message{
		protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
		protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
			protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
		}),
		protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
		protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
		protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
		protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
		protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
		protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
		protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
		protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
		protopack.Tag{70, protopack.StartGroupType},
		protopack.Message{
			protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
		},
		protopack.Tag{70, protopack.EndGroupType},
		protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
		protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
		protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
		protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
		protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
	}.Marshal()

	got, err := proto.Marshal(pb)
	if !isRequiredNotSetError(err) {
		t.Logf("marshal-1 err = %v, want *RequiredNotSetError", err)
		t.Fatalf("got %q\nwant  %q", got, want)
	}
	if !bytes.Equal(got, want) {
		t.Fatalf("got %q\nwant  %q", got, want)
	}

	// Now test Unmarshal by recreating the original buffer.
	pbd := new(pb2.GoTest)
	err = proto.Unmarshal(got, pbd)
	if !isRequiredNotSetError(err) {
		t.Errorf("unmarshal err = %v, want *RequiredNotSetError", err)
		t.Fatalf("got %q\nwant  %q", got, want)
	}
	got, err = proto.Marshal(pbd)
	if !isRequiredNotSetError(err) {
		t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err)
		t.Fatalf("got %q\nwant  %q", got, want)
	}
	if !bytes.Equal(got, want) {
		t.Fatalf("got %q\nwant  %q", got, want)
	}
}

func TestRequiredNotSetErrorWithBadWireTypes(t *testing.T) {
	// Required field expects a varint, and properly found a varint.
	if err := proto.Unmarshal([]byte{0x08, 0x00}, new(pb2.GoEnum)); err != nil {
		t.Errorf("Unmarshal = %v, want nil", err)
	}
	// Required field expects a varint, but found a fixed32 instead.
	if err := proto.Unmarshal([]byte{0x0d, 0x00, 0x00, 0x00, 0x00}, new(pb2.GoEnum)); err == nil {
		t.Errorf("Unmarshal = nil, want RequiredNotSetError")
	}
	// Required field expects a varint, and found both a varint and fixed32 (ignored).
	m := new(pb2.GoEnum)
	if err := proto.Unmarshal([]byte{0x08, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00}, m); err != nil {
		t.Errorf("Unmarshal = %v, want nil", err)
	}
	if !bytes.Equal(m.XXX_unrecognized, []byte{0x0d, 0x00, 0x00, 0x00, 0x00}) {
		t.Errorf("expected fixed32 to appear as unknown bytes: %x", m.XXX_unrecognized)
	}
}

func fuzzUnmarshal(t *testing.T, data []byte) {
	defer func() {
		if e := recover(); e != nil {
			t.Errorf("These bytes caused a panic: %+v", data)
			t.Logf("Stack:\n%s", debug.Stack())
			t.FailNow()
		}
	}()

	pb := new(pb2.MyMessage)
	proto.Unmarshal(data, pb)
}

func TestMapFieldMarshal(t *testing.T) {
	m := &pb2.MessageWithMap{
		NameMapping: map[int32]string{
			1: "Rob",
			4: "Ian",
			8: "Dave",
		},
	}
	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal: %v", err)
	}

	// b should be the concatenation of these three byte sequences in some order.
	parts := []string{
		"\n\a\b\x01\x12\x03Rob",
		"\n\a\b\x04\x12\x03Ian",
		"\n\b\b\x08\x12\x04Dave",
	}
	ok := false
	for i := range parts {
		for j := range parts {
			if j == i {
				continue
			}
			for k := range parts {
				if k == i || k == j {
					continue
				}
				try := parts[i] + parts[j] + parts[k]
				if bytes.Equal(b, []byte(try)) {
					ok = true
					break
				}
			}
		}
	}
	if !ok {
		t.Fatalf("Incorrect Marshal output.\n got %q\nwant %q (or a permutation of that)", b, parts[0]+parts[1]+parts[2])
	}
	t.Logf("FYI b: %q", b)
}

func TestMapFieldDeterministicMarshal(t *testing.T) {
	m := &pb2.MessageWithMap{
		NameMapping: map[int32]string{
			1: "Rob",
			4: "Ian",
			8: "Dave",
		},
	}

	marshal := func(m proto.Message) []byte {
		var b proto.Buffer
		b.SetDeterministic(true)
		if err := b.Marshal(m); err != nil {
			t.Fatalf("Marshal failed: %v", err)
		}
		return b.Bytes()
	}

	want := marshal(m)
	for i := 0; i < 10; i++ {
		if got := marshal(m); !bytes.Equal(got, want) {
			t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
		}
	}
}

func TestMapFieldRoundTrips(t *testing.T) {
	m := &pb2.MessageWithMap{
		NameMapping: map[int32]string{
			1: "Rob",
			4: "Ian",
			8: "Dave",
		},
		MsgMapping: map[int64]*pb2.FloatingPoint{
			0x7001: {F: proto.Float64(2.0)},
		},
		ByteMapping: map[bool][]byte{
			false: []byte("that's not right!"),
			true:  []byte("aye, 'tis true!"),
		},
	}
	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal: %v", err)
	}
	t.Logf("FYI b: %q", b)
	m2 := new(pb2.MessageWithMap)
	if err := proto.Unmarshal(b, m2); err != nil {
		t.Fatalf("Unmarshal: %v", err)
	}
	if !proto.Equal(m, m2) {
		t.Errorf("Map did not survive a round trip.\ninitial: %v\n  final: %v", m, m2)
	}
}

func TestMapFieldWithNil(t *testing.T) {
	m1 := &pb2.MessageWithMap{
		MsgMapping: map[int64]*pb2.FloatingPoint{
			1: nil,
		},
	}
	b, err := proto.Marshal(m1)
	if _, ok := err.(*proto.RequiredNotSetError); !ok {
		t.Fatalf("Marshal(%v): err=%v, want RequiredNotSet", m1, err)
	}
	m2 := new(pb2.MessageWithMap)
	err = proto.Unmarshal(b, m2)
	if _, ok := err.(*proto.RequiredNotSetError); !ok {
		t.Fatalf("Unmarshal(%v): err=%v, want RequiredNotSet", m1, err)
	}
	if !proto.Equal(m1, m2) {
		t.Fatalf("roundtrip marshal/unmarshal changed message; got:\n%v\nwant:\n%v", m2, m1)
	}
}

func TestMapFieldWithNilBytes(t *testing.T) {
	m1 := &pb2.MessageWithMap{
		ByteMapping: map[bool][]byte{
			false: {},
			true:  nil,
		},
	}
	n := proto.Size(m1)
	b, err := proto.Marshal(m1)
	if err != nil {
		t.Fatalf("Marshal: %v", err)
	}
	if n != len(b) {
		t.Errorf("Size(m1) = %d; want len(Marshal(m1)) = %d", n, len(b))
	}
	m2 := new(pb2.MessageWithMap)
	if err := proto.Unmarshal(b, m2); err != nil {
		t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b)
	}
	if v, ok := m2.ByteMapping[false]; !ok {
		t.Error("byte_mapping[false] not present")
	} else if len(v) != 0 {
		t.Errorf("byte_mapping[false] not empty: %#v", v)
	}
	if v, ok := m2.ByteMapping[true]; !ok {
		t.Error("byte_mapping[true] not present")
	} else if len(v) != 0 {
		t.Errorf("byte_mapping[true] not empty: %#v", v)
	}
}

func TestDecodeMapFieldMissingKey(t *testing.T) {
	b := []byte{
		0x0A, 0x03, // message, tag 1 (name_mapping), of length 3 bytes
		// no key
		0x12, 0x01, 0x6D, // string value of length 1 byte, value "m"
	}
	got := &pb2.MessageWithMap{}
	err := proto.Unmarshal(b, got)
	if err != nil {
		t.Fatalf("failed to marshal map with missing key: %v", err)
	}
	want := &pb2.MessageWithMap{NameMapping: map[int32]string{0: "m"}}
	if !proto.Equal(got, want) {
		t.Errorf("Unmarshaled map with no key was not as expected. got: %v, want %v", got, want)
	}
}

func TestDecodeMapFieldMissingValue(t *testing.T) {
	b := protopack.Message{
		protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
			protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(1),
		}),
	}.Marshal()
	got := &pb2.MessageWithMap{}
	err := proto.Unmarshal(b, got)
	if err != nil {
		t.Fatalf("failed to marshal map with missing value: %v", err)
	}
	want := &pb2.MessageWithMap{NameMapping: map[int32]string{1: ""}}
	if !proto.Equal(got, want) {
		t.Errorf("Unmarshaled map with no value was not as expected. got: %v, want %v", got, want)
	}
}

func TestOneof(t *testing.T) {
	m := &pb2.Communique{}
	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal of empty message with oneof: %v", err)
	}
	if len(b) != 0 {
		t.Errorf("Marshal of empty message yielded too many bytes: %v", b)
	}

	m = &pb2.Communique{
		Union: &pb2.Communique_Name{"Barry"},
	}

	// Round-trip.
	b, err = proto.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal of message with oneof: %v", err)
	}
	if len(b) != 7 { // name tag/wire (1) + name len (1) + name (5)
		t.Errorf("Incorrect marshal of message with oneof: %v", b)
	}
	m.Reset()
	if err := proto.Unmarshal(b, m); err != nil {
		t.Fatalf("Unmarshal of message with oneof: %v", err)
	}
	if x, ok := m.Union.(*pb2.Communique_Name); !ok || x.Name != "Barry" {
		t.Errorf("After round trip, Union = %+v", m.Union)
	}
	if name := m.GetName(); name != "Barry" {
		t.Errorf("After round trip, GetName = %q, want %q", name, "Barry")
	}

	// Let's try with a message in the oneof.
	m.Union = &pb2.Communique_Msg{&pb2.Strings{StringField: proto.String("deep deep string")}}
	b, err = proto.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal of message with oneof set to message: %v", err)
	}
	if len(b) != 20 { // msg tag/wire (1) + msg len (1) + msg (1 + 1 + 16)
		t.Errorf("Incorrect marshal of message with oneof set to message: %v", b)
	}
	m.Reset()
	if err := proto.Unmarshal(b, m); err != nil {
		t.Fatalf("Unmarshal of message with oneof set to message: %v", err)
	}
	ss, ok := m.Union.(*pb2.Communique_Msg)
	if !ok || ss.Msg.GetStringField() != "deep deep string" {
		t.Errorf("After round trip with oneof set to message, Union = %+v", m.Union)
	}
}

func TestOneofNilBytes(t *testing.T) {
	// A oneof with nil byte slice should marshal to tag + 0 (size), with no error.
	m := &pb2.Communique{Union: &pb2.Communique_Data{Data: nil}}
	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("Marshal failed: %v", err)
	}
	want := protopack.Message{
		protopack.Tag{7, protopack.BytesType}, protopack.Bytes(""),
	}.Marshal()
	if !bytes.Equal(b, want) {
		t.Errorf("Wrong result of Marshal: got %x, want %x", b, want)
	}
}

func TestInefficientPackedBool(t *testing.T) {
	// https://github.com/golang/protobuf/issues/76
	inp := protopack.Message{
		protopack.Tag{2, protopack.BytesType}, protopack.Bytes("\xb90"),
	}.Marshal()
	if err := proto.Unmarshal(inp, new(pb2.MoreRepeated)); err != nil {
		t.Error(err)
	}
}

// Make sure pure-reflect-based implementation handles
// []int32-[]enum conversion correctly.
func TestRepeatedEnum2(t *testing.T) {
	pb := &pb2.RepeatedEnum{
		Color: []pb2.RepeatedEnum_Color{pb2.RepeatedEnum_RED},
	}
	b, err := proto.Marshal(pb)
	if err != nil {
		t.Fatalf("Marshal failed: %v", err)
	}
	x := new(pb2.RepeatedEnum)
	err = proto.Unmarshal(b, x)
	if err != nil {
		t.Fatalf("Unmarshal failed: %v", err)
	}
	if !proto.Equal(pb, x) {
		t.Errorf("Incorrect result: want: %v got: %v", pb, x)
	}
}

// TestConcurrentMarshal makes sure that it is safe to marshal
// same message in multiple goroutines concurrently.
func TestConcurrentMarshal(t *testing.T) {
	pb := initGoTest(true)
	const N = 100
	b := make([][]byte, N)

	var wg sync.WaitGroup
	for i := 0; i < N; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			var err error
			b[i], err = proto.Marshal(pb)
			if err != nil {
				t.Errorf("marshal error: %v", err)
			}
		}(i)
	}

	wg.Wait()
	for i := 1; i < N; i++ {
		if !bytes.Equal(b[0], b[i]) {
			t.Errorf("concurrent marshal result not same: b[0] = %v, b[%d] = %v", b[0], i, b[i])
		}
	}
}

func TestInvalidUTF8(t *testing.T) {
	const invalidUTF8 = "\xde\xad\xbe\xef\x80\x00\xff"
	tests := []struct {
		label  string
		proto2 proto.Message
		proto3 proto.Message
		want   []byte
	}{{
		label:  "Scalar",
		proto2: &pb2.TestUTF8{Scalar: proto.String(invalidUTF8)},
		proto3: &pb3.TestUTF8{Scalar: invalidUTF8},
		want:   []byte{0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
	}, {
		label:  "Vector",
		proto2: &pb2.TestUTF8{Vector: []string{invalidUTF8}},
		proto3: &pb3.TestUTF8{Vector: []string{invalidUTF8}},
		want:   []byte{0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
	}, {
		label:  "Oneof",
		proto2: &pb2.TestUTF8{Oneof: &pb2.TestUTF8_Field{invalidUTF8}},
		proto3: &pb3.TestUTF8{Oneof: &pb3.TestUTF8_Field{invalidUTF8}},
		want:   []byte{0x1a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
	}, {
		label:  "MapKey",
		proto2: &pb2.TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
		proto3: &pb3.TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
		want:   []byte{0x22, 0x0b, 0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff, 0x10, 0x00},
	}, {
		label:  "MapValue",
		proto2: &pb2.TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
		proto3: &pb3.TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
		want:   []byte{0x2a, 0x0b, 0x08, 0x00, 0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
	}}

	for _, tt := range tests {
		// Proto2 should not validate UTF-8.
		b, err := proto.Marshal(tt.proto2)
		if err != nil {
			t.Errorf("Marshal(proto2.%s) = %v, want nil", tt.label, err)
		}
		if !bytes.Equal(b, tt.want) {
			t.Errorf("Marshal(proto2.%s) = %x, want %x", tt.label, b, tt.want)
		}

		m := proto.Clone(tt.proto2)
		m.Reset()
		if err = proto.Unmarshal(tt.want, m); err != nil {
			t.Errorf("Unmarshal(proto2.%s) = %v, want nil", tt.label, err)
		}
		if !proto.Equal(m, tt.proto2) {
			t.Errorf("proto2.%s: output mismatch:\ngot  %v\nwant %v", tt.label, m, tt.proto2)
		}

		// Proto3 should validate UTF-8.
		if _, err := proto.Marshal(tt.proto3); err == nil {
			t.Errorf("Marshal(proto3.%s) = %v, want non-nil", tt.label, err)
		}

		m = proto.Clone(tt.proto3)
		m.Reset()
		if err := proto.Unmarshal(tt.want, m); err == nil {
			t.Errorf("Unmarshal(proto3.%s) = %v, want non-nil", tt.label, err)
		}
	}
}

func TestRequired(t *testing.T) {
	// The F_BoolRequired field appears after all of the required fields.
	// It should still be handled even after multiple required field violations.
	m := &pb2.GoTest{F_BoolRequired: proto.Bool(true)}
	got, err := proto.Marshal(m)
	if !isRequiredNotSetError(err) {
		t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
	}
	if want := []byte{0x50, 0x01}; !bytes.Equal(got, want) {
		t.Errorf("Marshal() = %x, want %x", got, want)
	}

	m = new(pb2.GoTest)
	err = proto.Unmarshal(got, m)
	if !isRequiredNotSetError(err) {
		t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
	}
	if !m.GetF_BoolRequired() {
		t.Error("m.F_BoolRequired = false, want true")
	}
}

func TestUnknownV2(t *testing.T) {
	m := new(tspb.Timestamp)
	m.ProtoReflect().SetUnknown([]byte("\x92\x4d\x12unknown field 1234"))
	got := proto.CompactTextString(m)
	if !strings.Contains(got, "unknown field 1234") {
		t.Errorf("got %q, want contains %q", got, "unknown field 1234")
	}
}

func testMsg() *pb2.GoTest {
	pb := initGoTest(true)
	const N = 1000 // Internally the library starts much smaller.
	pb.F_Int32Repeated = make([]int32, N)
	pb.F_DoubleRepeated = make([]float64, N)
	for i := 0; i < N; i++ {
		pb.F_Int32Repeated[i] = int32(i)
		pb.F_DoubleRepeated[i] = float64(i)
	}
	return pb
}

func bytesMsg() *pb2.GoTest {
	pb := initGoTest(true)
	buf := make([]byte, 4000)
	for i := range buf {
		buf[i] = byte(i)
	}
	pb.F_BytesDefaulted = buf
	return pb
}

func benchmarkMarshal(b *testing.B, pb proto.Message, marshal func(proto.Message) ([]byte, error)) {
	d, _ := marshal(pb)
	b.SetBytes(int64(len(d)))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		marshal(pb)
	}
}

func benchmarkBufferMarshal(b *testing.B, pb proto.Message) {
	p := proto.NewBuffer(nil)
	benchmarkMarshal(b, pb, func(pb0 proto.Message) ([]byte, error) {
		p.Reset()
		err := p.Marshal(pb0)
		return p.Bytes(), err
	})
}

func benchmarkSize(b *testing.B, pb proto.Message) {
	benchmarkMarshal(b, pb, func(pb0 proto.Message) ([]byte, error) {
		proto.Size(pb)
		return nil, nil
	})
}

func TestProto3ZeroValues(t *testing.T) {
	tests := []struct {
		desc string
		m    proto.Message
	}{
		{"zero message", &pb3.Message{}},
		{"empty bytes field", &pb3.Message{Data: []byte{}}},
	}
	for _, test := range tests {
		b, err := proto.Marshal(test.m)
		if err != nil {
			t.Errorf("%s: proto.Marshal: %v", test.desc, err)
			continue
		}
		if len(b) > 0 {
			t.Errorf("%s: Encoding is non-empty: %q", test.desc, b)
		}
	}
}

func TestRoundTripProto3(t *testing.T) {
	m := &pb3.Message{
		Name:         "David",          // (2 | 1<<3): 0x0a 0x05 "David"
		Hilarity:     pb3.Message_PUNS, // (0 | 2<<3): 0x10 0x01
		HeightInCm:   178,              // (0 | 3<<3): 0x18 0xb2 0x01
		Data:         []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto"
		ResultCount:  47,               // (0 | 7<<3): 0x38 0x2f
		TrueScotsman: true,             // (0 | 8<<3): 0x40 0x01
		Score:        8.1,              // (5 | 9<<3): 0x4d <8.1>

		Key: []uint64{1, 0xdeadbeef},
		Nested: &pb3.Nested{
			Bunny: "Monty",
		},
	}
	t.Logf(" m: %v", m)

	b, err := proto.Marshal(m)
	if err != nil {
		t.Fatalf("proto.Marshal: %v", err)
	}
	t.Logf(" b: %q", b)

	m2 := new(pb3.Message)
	if err := proto.Unmarshal(b, m2); err != nil {
		t.Fatalf("proto.Unmarshal: %v", err)
	}
	t.Logf("m2: %v", m2)

	if !proto.Equal(m, m2) {
		t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
	}
}

func TestGettersForBasicTypesExist(t *testing.T) {
	var m pb3.Message
	if got := m.GetNested().GetBunny(); got != "" {
		t.Errorf("m.GetNested().GetBunny() = %q, want empty string", got)
	}
	if got := m.GetNested().GetCute(); got {
		t.Errorf("m.GetNested().GetCute() = %t, want false", got)
	}
}

func TestProto3SetDefaults(t *testing.T) {
	in := &pb3.Message{
		Terrain: map[string]*pb3.Nested{
			"meadow": new(pb3.Nested),
		},
		Proto2Field: new(pb2.SubDefaults),
		Proto2Value: map[string]*pb2.SubDefaults{
			"badlands": new(pb2.SubDefaults),
		},
	}

	got := proto.Clone(in).(*pb3.Message)
	proto.SetDefaults(got)

	// There are no defaults in proto3.  Everything should be the zero value, but
	// we need to remember to set defaults for nested proto2 messages.
	want := &pb3.Message{
		Terrain: map[string]*pb3.Nested{
			"meadow": new(pb3.Nested),
		},
		Proto2Field: &pb2.SubDefaults{N: proto.Int64(7)},
		Proto2Value: map[string]*pb2.SubDefaults{
			"badlands": &pb2.SubDefaults{N: proto.Int64(7)},
		},
	}

	if !proto.Equal(got, want) {
		t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
	}
}

func TestUnknownFieldPreservation(t *testing.T) {
	b1 := "\x0a\x05David"      // Known tag 1
	b2 := "\xc2\x0c\x06Google" // Unknown tag 200
	b := []byte(b1 + b2)

	m := new(pb3.Message)
	if err := proto.Unmarshal(b, m); err != nil {
		t.Fatalf("proto.Unmarshal: %v", err)
	}

	if !bytes.Equal(m.XXX_unrecognized, []byte(b2)) {
		t.Fatalf("mismatching unknown fields:\ngot  %q\nwant %q", m.XXX_unrecognized, b2)
	}
}

func TestMap(t *testing.T) {
	b := protopack.Message{
		protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.String("Key1"),
			protopack.Tag{2, protopack.BytesType}, protopack.String("Val1"),
		}),
		protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.String("Key2"),
			protopack.Tag{2, protopack.BytesType}, protopack.String("Val2a"),
			protopack.Tag{2, protopack.BytesType}, protopack.String("Val2"),
		}),
		protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.String("Key3"),
			protopack.Tag{1, protopack.Fixed32Type}, protopack.Uint32(5),
			protopack.Tag{2, protopack.BytesType}, protopack.String("Val3b"),
			protopack.Tag{3, protopack.BytesType}, protopack.Bytes("Val3a"),
			protopack.Tag{2, protopack.BytesType}, protopack.String("Val3"),
			protopack.Tag{2, protopack.Fixed32Type}, protopack.Uint32(5),
		}),
		protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix{},
		protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.String("Key4"),
			protopack.Tag{2, protopack.StartGroupType},
			protopack.Message{
				protopack.Tag{1, protopack.BytesType}, protopack.Bytes("SomeURL"),
				protopack.Tag{2, protopack.BytesType}, protopack.Bytes("SomeTitle"),
				protopack.Tag{3, protopack.BytesType}, protopack.Bytes("Snippet1"),
			},
			protopack.Tag{2, protopack.EndGroupType},
		}),
	}.Marshal()

	var m pb3.Message
	if err := proto.Unmarshal(b, &m); err != nil {
		t.Fatalf("proto.Unmarshal error: %v", err)
	}

	got := m.StringMap
	want := map[string]string{
		"":     "",
		"Key1": "Val1",
		"Key2": "Val2",
		"Key3": "Val3",
		"Key4": "",
	}

	if !reflect.DeepEqual(got, want) {
		t.Errorf("maps differ:\ngot  %#v\nwant %#v", got, want)
	}
}

func marshalled() []byte {
	m := &pb3.IntMaps{}
	for i := 0; i < 1000; i++ {
		m.Maps = append(m.Maps, &pb3.IntMap{
			Rtt: map[int32]int32{1: 2},
		})
	}
	b, err := proto.Marshal(m)
	if err != nil {
		panic(fmt.Sprintf("Can't marshal %+v: %v", m, err))
	}
	return b
}

var messageWithExtension1 = &pb2.MyMessage{Count: proto.Int32(7)}

// messageWithExtension2 is in equal_test.go.
var messageWithExtension3 = &pb2.MyMessage{Count: proto.Int32(8)}

func init() {
	if err := proto.SetExtension(messageWithExtension1, pb2.E_Ext_More, &pb2.Ext{Data: proto.String("Abbott")}); err != nil {
		log.Panicf("proto.SetExtension: %v", err)
	}
	if err := proto.SetExtension(messageWithExtension3, pb2.E_Ext_More, &pb2.Ext{Data: proto.String("Costello")}); err != nil {
		log.Panicf("proto.SetExtension: %v", err)
	}

	// Force messageWithExtension3 to have the extension encoded.
	proto.Marshal(messageWithExtension3)

}

// non-pointer custom message
type nonptrMessage struct{}

func (m nonptrMessage) ProtoMessage()  {}
func (m nonptrMessage) Reset()         {}
func (m nonptrMessage) String() string { return "" }

func (m nonptrMessage) Marshal() ([]byte, error) {
	return []byte{42}, nil
}

var SizeTests = []struct {
	desc string
	pb   proto.Message
}{
	{"empty", &pb2.OtherMessage{}},
	// Basic types.
	{"bool", &pb2.Defaults{F_Bool: proto.Bool(true)}},
	{"int32", &pb2.Defaults{F_Int32: proto.Int32(12)}},
	{"negative int32", &pb2.Defaults{F_Int32: proto.Int32(-1)}},
	{"small int64", &pb2.Defaults{F_Int64: proto.Int64(1)}},
	{"big int64", &pb2.Defaults{F_Int64: proto.Int64(1 << 20)}},
	{"negative int64", &pb2.Defaults{F_Int64: proto.Int64(-1)}},
	{"fixed32", &pb2.Defaults{F_Fixed32: proto.Uint32(71)}},
	{"fixed64", &pb2.Defaults{F_Fixed64: proto.Uint64(72)}},
	{"uint32", &pb2.Defaults{F_Uint32: proto.Uint32(123)}},
	{"uint64", &pb2.Defaults{F_Uint64: proto.Uint64(124)}},
	{"float", &pb2.Defaults{F_Float: proto.Float32(12.6)}},
	{"double", &pb2.Defaults{F_Double: proto.Float64(13.9)}},
	{"string", &pb2.Defaults{F_String: proto.String("niles")}},
	{"bytes", &pb2.Defaults{F_Bytes: []byte("wowsa")}},
	{"bytes, empty", &pb2.Defaults{F_Bytes: []byte{}}},
	{"sint32", &pb2.Defaults{F_Sint32: proto.Int32(65)}},
	{"sint64", &pb2.Defaults{F_Sint64: proto.Int64(67)}},
	{"enum", &pb2.Defaults{F_Enum: pb2.Defaults_BLUE.Enum()}},
	// Repeated.
	{"empty repeated bool", &pb2.MoreRepeated{Bools: []bool{}}},
	{"repeated bool", &pb2.MoreRepeated{Bools: []bool{false, true, true, false}}},
	{"packed repeated bool", &pb2.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
	{"repeated int32", &pb2.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}},
	{"repeated int32 packed", &pb2.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
	{"repeated int64 packed", &pb2.MoreRepeated{Int64SPacked: []int64{
		// Need enough large numbers to verify that the header is counting the number of bytes
		// for the field, not the number of elements.
		1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
		1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
	}}},
	{"repeated string", &pb2.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
	{"repeated fixed", &pb2.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
	// Nested.
	{"nested", &pb2.OldMessage{Nested: &pb2.OldMessage_Nested{Name: proto.String("whatever")}}},
	{"group", &pb2.GroupOld{G: &pb2.GroupOld_G{X: proto.Int32(12345)}}},
	// Other things.
	{"unrecognized", &pb2.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
	{"extension (unencoded)", messageWithExtension1},
	{"extension (encoded)", messageWithExtension3},
	// proto3 message
	{"proto3 empty", &pb3.Message{}},
	{"proto3 bool", &pb3.Message{TrueScotsman: true}},
	{"proto3 int64", &pb3.Message{ResultCount: 1}},
	{"proto3 uint32", &pb3.Message{HeightInCm: 123}},
	{"proto3 float", &pb3.Message{Score: 12.6}},
	{"proto3 string", &pb3.Message{Name: "Snezana"}},
	{"proto3 bytes", &pb3.Message{Data: []byte("wowsa")}},
	{"proto3 bytes, empty", &pb3.Message{Data: []byte{}}},
	{"proto3 enum", &pb3.Message{Hilarity: pb3.Message_PUNS}},
	{"proto3 map field with empty bytes", &pb3.MessageWithMap{ByteMapping: map[bool][]byte{false: []byte{}}}},

	{"map field", &pb2.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}},
	{"map field with message", &pb2.MessageWithMap{MsgMapping: map[int64]*pb2.FloatingPoint{0x7001: &pb2.FloatingPoint{F: proto.Float64(2.0)}}}},
	{"map field with bytes", &pb2.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}},
	{"map field with empty bytes", &pb2.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte{}}}},

	{"map field with big entry", &pb2.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}},
	{"map field with big key and val", &pb2.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}},
	{"map field with big numeric key", &pb2.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}},

	{"oneof not set", &pb2.Oneof{}},
	{"oneof bool", &pb2.Oneof{Union: &pb2.Oneof_F_Bool{true}}},
	{"oneof zero int32", &pb2.Oneof{Union: &pb2.Oneof_F_Int32{0}}},
	{"oneof big int32", &pb2.Oneof{Union: &pb2.Oneof_F_Int32{1 << 20}}},
	{"oneof int64", &pb2.Oneof{Union: &pb2.Oneof_F_Int64{42}}},
	{"oneof fixed32", &pb2.Oneof{Union: &pb2.Oneof_F_Fixed32{43}}},
	{"oneof fixed64", &pb2.Oneof{Union: &pb2.Oneof_F_Fixed64{44}}},
	{"oneof uint32", &pb2.Oneof{Union: &pb2.Oneof_F_Uint32{45}}},
	{"oneof uint64", &pb2.Oneof{Union: &pb2.Oneof_F_Uint64{46}}},
	{"oneof float", &pb2.Oneof{Union: &pb2.Oneof_F_Float{47.1}}},
	{"oneof double", &pb2.Oneof{Union: &pb2.Oneof_F_Double{48.9}}},
	{"oneof string", &pb2.Oneof{Union: &pb2.Oneof_F_String{"Rhythmic Fman"}}},
	{"oneof bytes", &pb2.Oneof{Union: &pb2.Oneof_F_Bytes{[]byte("let go")}}},
	{"oneof sint32", &pb2.Oneof{Union: &pb2.Oneof_F_Sint32{50}}},
	{"oneof sint64", &pb2.Oneof{Union: &pb2.Oneof_F_Sint64{51}}},
	{"oneof enum", &pb2.Oneof{Union: &pb2.Oneof_F_Enum{pb2.MyMessage_BLUE}}},
	{"message for oneof", &pb2.GoTestField{Label: proto.String("k"), Type: proto.String("v")}},
	{"oneof message", &pb2.Oneof{Union: &pb2.Oneof_F_Message{&pb2.GoTestField{Label: proto.String("k"), Type: proto.String("v")}}}},
	{"oneof group", &pb2.Oneof{Union: &pb2.Oneof_FGroup{&pb2.Oneof_F_Group{X: proto.Int32(52)}}}},
	{"oneof largest tag", &pb2.Oneof{Union: &pb2.Oneof_F_Largest_Tag{1}}},
	{"multiple oneofs", &pb2.Oneof{Union: &pb2.Oneof_F_Int32{1}, Tormato: &pb2.Oneof_Value{2}}},

	{"non-pointer message", nonptrMessage{}},
}

func TestSize(t *testing.T) {
	for _, tc := range SizeTests {
		t.Run(tc.desc, func(t *testing.T) {
			size := proto.Size(tc.pb)
			b, err := proto.Marshal(tc.pb)
			if err != nil {
				t.Errorf("%v: Marshal failed: %v", tc.desc, err)
				return
			}
			if size != len(b) {
				t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
				t.Logf("%v: bytes: %#v", tc.desc, b)
			}
		})
	}
}

func TestVarintSize(t *testing.T) {
	// Check the edge cases carefully.
	testCases := []struct {
		n    uint64
		size int
	}{
		{0, 1},
		{1, 1},
		{127, 1},
		{128, 2},
		{16383, 2},
		{16384, 3},
		{math.MaxInt64, 9},
		{math.MaxInt64 + 1, 10},
	}
	for _, tc := range testCases {
		size := proto.SizeVarint(tc.n)
		if size != tc.size {
			t.Errorf("sizeVarint(%d) = %d, want %d", tc.n, size, tc.size)
		}
	}
}

Youez - 2016 - github.com/yon3zu
LinuXploit