Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: net/quic/test_tools/crypto_test_utils.cc

Issue 2678973002: Replace va_arg version of crypto_test_utils::Message with a more modern vector argument version. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/test_tools/crypto_test_utils.h ('k') | net/quic/test_tools/crypto_test_utils_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/quic/test_tools/crypto_test_utils.h" 5 #include "net/quic/test_tools/crypto_test_utils.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "crypto/openssl_util.h" 9 #include "crypto/openssl_util.h"
10 #include "crypto/secure_hash.h" 10 #include "crypto/secure_hash.h"
(...skipping 823 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 for (size_t i = 0; i < 4; i++) { 834 for (size_t i = 0; i < 4; i++) {
835 tag >>= 8; 835 tag >>= 8;
836 if (i < len) { 836 if (i < len) {
837 tag |= static_cast<uint32_t>(tagstr[i]) << 24; 837 tag |= static_cast<uint32_t>(tagstr[i]) << 24;
838 } 838 }
839 } 839 }
840 840
841 return tag; 841 return tag;
842 } 842 }
843 843
844 CryptoHandshakeMessage Message(const char* message_tag, ...) { 844 CryptoHandshakeMessage CreateCHLO(
845 va_list ap; 845 std::vector<std::pair<string, string>> tags_and_values) {
846 va_start(ap, message_tag); 846 return CreateCHLO(tags_and_values, -1);
847 }
847 848
849 CryptoHandshakeMessage CreateCHLO(
850 std::vector<std::pair<string, string>> tags_and_values,
851 int minimum_size_bytes) {
848 CryptoHandshakeMessage msg; 852 CryptoHandshakeMessage msg;
849 msg.set_tag(ParseTag(message_tag)); 853 msg.set_tag(MakeQuicTag('C', 'H', 'L', 'O'));
850 854
851 for (;;) { 855 if (minimum_size_bytes > 0) {
852 const char* tagstr = va_arg(ap, const char*); 856 msg.set_minimum_size(minimum_size_bytes);
853 if (tagstr == nullptr) { 857 }
854 break;
855 }
856 858
857 if (tagstr[0] == '$') { 859 for (const auto& tag_and_value : tags_and_values) {
858 // Special value. 860 const string& tag = tag_and_value.first;
859 const char* const special = tagstr + 1; 861 const string& value = tag_and_value.second;
860 if (strcmp(special, "padding") == 0) {
861 const int min_bytes = va_arg(ap, int);
862 msg.set_minimum_size(min_bytes);
863 } else {
864 CHECK(false) << "Unknown special value: " << special;
865 }
866 862
863 const QuicTag quic_tag = ParseTag(tag.c_str());
864
865 size_t value_len = value.length();
866 if (value_len > 0 && value[0] == '#') {
867 // This is ascii encoded hex.
868 string hex_value = QuicTextUtils::HexDecode(StringPiece(&value[1]));
869 msg.SetStringPiece(quic_tag, hex_value);
867 continue; 870 continue;
868 } 871 }
869 872 msg.SetStringPiece(quic_tag, value);
870 const QuicTag tag = ParseTag(tagstr);
871 const char* valuestr = va_arg(ap, const char*);
872
873 size_t len = strlen(valuestr);
874 if (len > 0 && valuestr[0] == '#') {
875 valuestr++;
876 len--;
877
878 CHECK_EQ(0u, len % 2);
879 std::unique_ptr<uint8_t[]> buf(new uint8_t[len / 2]);
880
881 for (size_t i = 0; i < len / 2; i++) {
882 uint8_t v = 0;
883 CHECK(HexChar(valuestr[i * 2], &v));
884 buf[i] = v << 4;
885 CHECK(HexChar(valuestr[i * 2 + 1], &v));
886 buf[i] |= v;
887 }
888
889 msg.SetStringPiece(
890 tag, StringPiece(reinterpret_cast<char*>(buf.get()), len / 2));
891 continue;
892 }
893
894 msg.SetStringPiece(tag, valuestr);
895 } 873 }
896 874
897 // The CryptoHandshakeMessage needs to be serialized and parsed to ensure 875 // The CryptoHandshakeMessage needs to be serialized and parsed to ensure
898 // that any padding is included. 876 // that any padding is included.
899 std::unique_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); 877 std::unique_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg));
900 std::unique_ptr<CryptoHandshakeMessage> parsed( 878 std::unique_ptr<CryptoHandshakeMessage> parsed(
901 CryptoFramer::ParseMessage(bytes->AsStringPiece())); 879 CryptoFramer::ParseMessage(bytes->AsStringPiece()));
902 CHECK(parsed.get()); 880 CHECK(parsed.get());
903 881
904 va_end(ap);
905 return *parsed; 882 return *parsed;
906 } 883 }
907 884
908 ChannelIDSource* ChannelIDSourceForTesting() { 885 ChannelIDSource* ChannelIDSourceForTesting() {
909 return new TestChannelIDSource(); 886 return new TestChannelIDSource();
910 } 887 }
911 888
912 void MovePackets(PacketSavingConnection* source_conn, 889 void MovePackets(PacketSavingConnection* source_conn,
913 size_t* inout_packet_index, 890 size_t* inout_packet_index,
914 QuicCryptoStream* dest_stream, 891 QuicCryptoStream* dest_stream,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 dest_stream->OnHandshakeMessage(message); 930 dest_stream->OnHandshakeMessage(message);
954 } 931 }
955 QuicConnectionPeer::SetCurrentPacket(dest_conn, StringPiece(nullptr, 0)); 932 QuicConnectionPeer::SetCurrentPacket(dest_conn, StringPiece(nullptr, 0));
956 } 933 }
957 934
958 CryptoHandshakeMessage GenerateDefaultInchoateCHLO( 935 CryptoHandshakeMessage GenerateDefaultInchoateCHLO(
959 const QuicClock* clock, 936 const QuicClock* clock,
960 QuicVersion version, 937 QuicVersion version,
961 QuicCryptoServerConfig* crypto_config) { 938 QuicCryptoServerConfig* crypto_config) {
962 // clang-format off 939 // clang-format off
963 return Message( 940 return CreateCHLO(
964 "CHLO", 941 {{"PDMD", "X509"},
965 "PDMD", "X509", 942 {"AEAD", "AESG"},
966 "AEAD", "AESG", 943 {"KEXS", "C255"},
967 "KEXS", "C255", 944 {"PUBS", GenerateClientPublicValuesHex().c_str()},
968 "PUBS", GenerateClientPublicValuesHex().c_str(), 945 {"NONC", GenerateClientNonceHex(clock, crypto_config).c_str()},
969 "NONC", GenerateClientNonceHex(clock, 946 {"VER\0", QuicTagToString(QuicVersionToQuicTag(version)).c_str()}},
970 crypto_config).c_str(), 947 kClientHelloMinimumSize);
971 "VER\0", QuicTagToString(
972 QuicVersionToQuicTag(version)).c_str(),
973 "$padding", static_cast<int>(kClientHelloMinimumSize),
974 nullptr);
975 // clang-format on 948 // clang-format on
976 } 949 }
977 950
978 string GenerateClientNonceHex(const QuicClock* clock, 951 string GenerateClientNonceHex(const QuicClock* clock,
979 QuicCryptoServerConfig* crypto_config) { 952 QuicCryptoServerConfig* crypto_config) {
980 net::QuicCryptoServerConfig::ConfigOptions old_config_options; 953 net::QuicCryptoServerConfig::ConfigOptions old_config_options;
981 net::QuicCryptoServerConfig::ConfigOptions new_config_options; 954 net::QuicCryptoServerConfig::ConfigOptions new_config_options;
982 old_config_options.id = "old-config-id"; 955 old_config_options.id = "old-config-id";
983 delete crypto_config->AddDefaultConfig(net::QuicRandom::GetInstance(), clock, 956 delete crypto_config->AddDefaultConfig(net::QuicRandom::GetInstance(), clock,
984 old_config_options); 957 old_config_options);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 FullChloGenerator generator(crypto_config, server_addr, client_addr, clock, 991 FullChloGenerator generator(crypto_config, server_addr, client_addr, clock,
1019 proof, compressed_certs_cache, out); 992 proof, compressed_certs_cache, out);
1020 crypto_config->ValidateClientHello( 993 crypto_config->ValidateClientHello(
1021 inchoate_chlo, client_addr.host(), server_addr, version, clock, proof, 994 inchoate_chlo, client_addr.host(), server_addr, version, clock, proof,
1022 generator.GetValidateClientHelloCallback()); 995 generator.GetValidateClientHelloCallback());
1023 } 996 }
1024 997
1025 } // namespace crypto_test_utils 998 } // namespace crypto_test_utils
1026 } // namespace test 999 } // namespace test
1027 } // namespace net 1000 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/test_tools/crypto_test_utils.h ('k') | net/quic/test_tools/crypto_test_utils_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698