main.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2017, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. var s = ""
  18. s += "/*\n"
  19. s += " *\n"
  20. s += " * Copyright 2017, Google Inc.\n"
  21. s += " * All rights reserved.\n"
  22. s += " *\n"
  23. s += " * Redistribution and use in source and binary forms, with or without\n"
  24. s += " * modification, are permitted provided that the following conditions are\n"
  25. s += " * met:\n"
  26. s += " *\n"
  27. s += " * * Redistributions of source code must retain the above copyright\n"
  28. s += " * notice, this list of conditions and the following disclaimer.\n"
  29. s += " * * Redistributions in binary form must reproduce the above\n"
  30. s += " * copyright notice, this list of conditions and the following disclaimer\n"
  31. s += " * in the documentation and/or other materials provided with the\n"
  32. s += " * distribution.\n"
  33. s += " * * Neither the name of Google Inc. nor the names of its\n"
  34. s += " * contributors may be used to endorse or promote products derived from\n"
  35. s += " * this software without specific prior written permission.\n"
  36. s += " *\n"
  37. s += " * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  38. s += " * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  39. s += " * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  40. s += " * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
  41. s += " * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  42. s += " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  43. s += " * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  44. s += " * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  45. s += " * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  46. s += " * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  47. s += " * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  48. s += " *\n"
  49. s += " */\n"
  50. s += "// GENERATED: DO NOT EDIT\n"
  51. if CommandLine.arguments.contains("test") {
  52. s += "//\n"
  53. s += "// This file contain a function that returns the contents of certificates and keys in Tests/.\n"
  54. s += "//\n"
  55. s += "import Foundation\n"
  56. s += "\n"
  57. // server.crt
  58. s += "let serverCertificate = "
  59. let certificateURL = URL(fileURLWithPath: "Tests/server.crt")
  60. let certificateData = try Data(contentsOf: certificateURL)
  61. let encodedCertificate = certificateData.base64EncodedString()
  62. s += "Data(base64Encoded: \"\(encodedCertificate)\", options:[])!\n"
  63. // server.pem
  64. s += "let serverKey = "
  65. let keyURL = URL(fileURLWithPath: "Tests/server.pem")
  66. let keyData = try Data(contentsOf: keyURL)
  67. let encodedKey = keyData.base64EncodedString()
  68. s += "Data(base64Encoded: \"\(encodedKey)\", options:[])!\n"
  69. // ca.crt
  70. s += "let trustCollectionCertificate = "
  71. let caURL = URL(fileURLWithPath: "Tests/ca.crt")
  72. let caData = try Data(contentsOf: caURL)
  73. let caEncoded = caData.base64EncodedString()
  74. s += "Data(base64Encoded: \"\(caEncoded)\", options:[])!\n"
  75. // client.crt
  76. s += "let clientCertificate = "
  77. let clientCertURL = URL(fileURLWithPath: "Tests/client.crt")
  78. let clientCertData = try Data(contentsOf: clientCertURL)
  79. let clientCertEncoded = clientCertData.base64EncodedString()
  80. s += "Data(base64Encoded: \"\(clientCertEncoded)\", options:[])!\n"
  81. // client.pem
  82. s += "let clientKey = "
  83. let clientKeyURL = URL(fileURLWithPath: "Tests/client.pem")
  84. let clientKeyData = try Data(contentsOf: clientKeyURL)
  85. let clientKeyEncoded = clientKeyData.base64EncodedString()
  86. s += "Data(base64Encoded: \"\(clientKeyEncoded)\", options:[])!"
  87. } else {
  88. s += "//\n"
  89. s += "// This file contain a function that returns the default roots.pem.\n"
  90. s += "//\n"
  91. s += "import Foundation\n"
  92. s += "\n"
  93. s += "public func roots_pem() -> String {\n"
  94. let fileURL = URL(fileURLWithPath: "Assets/roots.pem")
  95. let filedata = try Data(contentsOf: fileURL)
  96. let encoding = filedata.base64EncodedString()
  97. s += " let roots = \n"
  98. s += " \"" + encoding + "\"\n"
  99. s += " return String(data: Data(base64Encoded: roots, options: [])!, encoding: .utf8)!\n"
  100. s += "}\n"
  101. }
  102. print(s)